001/*
002 * Copyright (c) 2009-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.gui;
031
032import java.awt.BorderLayout;
033
034import javax.swing.JPanel;
035import javax.swing.JScrollPane;
036import javax.swing.JTree;
037import javax.swing.tree.TreePath;
038
039import ptolemy.kernel.util.IllegalActionException;
040import ptolemy.vergil.tree.PTree;
041
042/**
043 * LibrarySearchResultPane provides an interface that should be used by all
044 * classes that can display the Kepler search results panel.
045 * 
046 *@author Chad Berkley
047 *@since February 17, 2005
048 */
049public abstract class LibrarySearchResultPane extends JPanel {
050        /**
051         * the library for the result pane
052         */
053        protected PTree library;
054
055        /**
056         * the search results to display in the pane
057         */
058        protected LibrarySearchResults results;
059
060        /**
061         * constructor. this initializes the library and results variables as well
062         * as sets the layout for the panel. update is automatically called from
063         * here as well.
064         * 
065         *@param library
066         *            the library to search
067         *@param results
068         *            the results of a search
069         *@exception IllegalActionException
070         *                Description of the Exception
071         *@throws IllegalActionException
072         *             if the search fails
073         */
074        public LibrarySearchResultPane(PTree library, LibrarySearchResults results)
075                        throws IllegalActionException {
076                this.library = library;
077                this.results = results;
078                this.setLayout(new BorderLayout());
079                this.add(new JScrollPane(library), BorderLayout.CENTER);
080                update(results);
081        }
082
083        /**
084         * this method allows the search results to be updated in the panel
085         * 
086         *@param results
087         *            the results to update to
088         *@throws IllegalActionException
089         *             if the update fails
090         */
091        public abstract void update(LibrarySearchResults results)
092                        throws IllegalActionException;
093
094        /**
095         * collapses a given library completely
096         * 
097         *@param library
098         *            the JTree to collapse
099         */
100        public static void collapseAll(JTree library) {
101                int count = library.getRowCount();
102                for (int i = count - 1; i >= 0; i--) {
103                        // collapse in reverse order or else the outer nodes get collapsed
104                        // before the inner ones.
105                        if (!library.isCollapsed(i)) {
106                                library.collapseRow(i);
107                        }
108                }
109        }
110
111        /**
112         * expand a given tree
113         * 
114         *@param library
115         *            the JTree to expand
116         */
117        public static void expandAll(JTree library) {
118                int rowCount = library.getRowCount();
119                boolean doneFlag = false;
120                int previousRowCount = 0;
121
122                while (true) {
123                        for (int i = rowCount - 1; i >= 0; i--) {
124                                // expand all the currently visible rows
125                                library.expandRow(i);
126                        }
127
128                        previousRowCount = rowCount;
129                        rowCount = library.getRowCount();
130                        // update the new rowcount
131
132                        if (previousRowCount == rowCount) {
133                                // HACK because the tree doesn't expand (and thus returns true
134                                // on it's
135                                // isCollapsed() call) on CompositeEntity nodes
136                                // with no children. this code should just run the following for
137                                // loop until it makes it to the end, signalling that entire
138                                // tree
139                                // is collapsed.
140                                break;
141                        }
142
143                        for (int i = rowCount - 1; i >= 0; i--) {
144                                // check to see if all of the visible rows are expanded
145                                if (library.isCollapsed(i)) {
146                                        break;
147                                }
148                        }
149                }
150        }
151
152        /**
153         * collapses the default library tree completely
154         */
155        protected void collapseAll() {
156                collapseAll(this.library);
157        }
158
159        /**
160         * expand the entire default library tree
161         */
162        protected void expandAll() {
163                expandAll(this.library);
164        }
165
166        /**
167         * create an array of treepaths from the results
168         * 
169         *@return Description of the Return Value
170         */
171        protected TreePath[] createTreePathArray() {
172                TreePath[] tm = new TreePath[results.size()];
173                for (int i = 0; i < results.size(); i++) {
174                        tm[i] = results.getTreePath(i);
175                }
176                return tm;
177        }
178}