001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: aschultz $'
006 * '$Date: 2010-12-23 19:01:04 +0000 (Thu, 23 Dec 2010) $' 
007 * '$Revision: 26600 $'
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.ecoinformatics.seek.ecogrid.quicksearch;
031
032import java.awt.BorderLayout;
033import java.awt.event.ActionEvent;
034
035import javax.swing.AbstractAction;
036import javax.swing.JLabel;
037import javax.swing.JPanel;
038import javax.swing.JProgressBar;
039
040import org.ecoinformatics.seek.ecogrid.EcoGridServicesController;
041import org.kepler.gui.PreferencesAction;
042import org.kepler.gui.SearchUIJPanel;
043import org.kepler.gui.TabPane;
044import org.kepler.gui.TabPaneFactory;
045import org.kepler.util.StaticResources;
046
047import ptolemy.actor.gui.TableauFrame;
048import ptolemy.kernel.CompositeEntity;
049import ptolemy.kernel.util.Attribute;
050import ptolemy.kernel.util.IllegalActionException;
051import ptolemy.kernel.util.NameDuplicationException;
052import ptolemy.kernel.util.NamedObj;
053import ptolemy.vergil.tree.EntityTreeModel;
054import ptolemy.vergil.tree.VisibleTreeModel;
055
056/**
057 * This class will display a panel for search and search result from ecogrid
058 * service.
059 * 
060 *@author berkley
061 *@created February 17, 2005
062 */
063public class DatasetPanel extends JPanel implements TabPane {
064
065        // declare controls
066        // search part panel (above)
067        private SearchUIJPanel _searchUIJPanel;
068        private QuickSearchAction quickSearchAction;
069        private QuickSearchCancelAction cancelQuickSearchAction;
070        private JProgressBar _progressBar;
071        private JLabel _progressLabel;
072
073        // result part panel
074        private CompositeEntity resultsRoot;
075        private ResultPanel resultPanel;
076        private EcoGridServicesController searchController = null;
077
078        private TableauFrame _frame;
079
080        private String name = "Data";
081
082        /**
083         * Set up the pane with preference size and configuration for search
084         * 
085         *@param controller
086         *            Description of the Parameter
087         *@exception Exception
088         *                Description of the Exception
089         */
090        public DatasetPanel(TableauFrame parent) {
091                super();
092                _frame = parent;
093                searchController = EcoGridServicesController.getInstance();
094                
095                quickSearchAction = new QuickSearchAction(searchController,
096                                SearchUIJPanel.SEARCH_BUTTON_CAPTION, this);
097        }
098
099        /**
100         * Implementation of TabPane getName()
101         */
102        public String getTabName() {
103                return name;
104        }
105
106        /**
107         * Implementation of TabPane setParentFrame(TableauFrame)
108         */
109        public void setParentFrame(TableauFrame parent) {
110                _frame = parent;
111        }
112
113        /**
114         * Implementation of TabPane getParentFrame()
115         */
116        public TableauFrame getParentFrame() {
117                return _frame;
118        }
119
120        /**
121         * Get the search text field value
122         * 
123         *@return The searchTextFieldValue value
124         */
125        public String getSearchTextFieldValue() {
126                return _searchUIJPanel.getSearchTerm();
127        }
128
129        /**
130         * Gets the resultRoot attribute of the DatasetPanel object
131         * 
132         *@return The resultRoot value
133         */
134        public CompositeEntity getResultRoot() {
135                return resultsRoot;
136        }
137
138        /**
139         * Initialize the tab panel
140         * 
141         *@throws Exception
142         */
143        public void initializeTab() throws Exception {
144
145                this.setLayout(new BorderLayout());
146
147                // init searchPartPanel
148                initSearchPartPanel();
149
150                // add searchPartPanel into this panel top(North)
151                this.add(_searchUIJPanel, BorderLayout.NORTH);
152
153                // add empty result scroll panel into this panel bottotm(South)
154                resultPanel = new ResultPanel(createResultModel());
155                this.add(resultPanel, BorderLayout.CENTER);
156
157                _progressBar = new JProgressBar();
158                _progressBar.setVisible(false);
159
160                _progressLabel = new JLabel();
161                _progressLabel.setVisible(true);
162
163                JPanel progressPanel = new JPanel(new BorderLayout());
164                progressPanel.add(_progressLabel, BorderLayout.NORTH);
165                progressPanel.add(_progressBar, BorderLayout.SOUTH);
166                this.add(progressPanel, BorderLayout.SOUTH);
167                
168
169        }
170
171        /**
172         * Initialize the search field and buttons
173         * 
174         * @throws Exception
175         */
176        private void initSearchPartPanel() throws Exception {
177
178                _searchUIJPanel = new SearchUIJPanel();
179
180                _searchUIJPanel.setBorderTitle(
181                                StaticResources.getDisplayString("data.search", "Search Data"));
182
183                _searchUIJPanel.setSearchAction(quickSearchAction);
184                
185                //_searchUIJPanel.setSearchAction(new QuickSearchAction(searchController,
186                //              SearchUIJPanel.SEARCH_BUTTON_CAPTION, this));
187
188                /*
189                _searchUIJPanel.setResetAction(new AbstractAction() {
190                        public void actionPerformed(ActionEvent e) {
191                                resetResultsPanel();
192                                _searchUIJPanel.setSearchTerm("");
193                        }
194                });
195                */
196
197                //_searchUIJPanel.setCancelAction(new QuickSearchCancelAction(
198                //              quickSearchAction, SearchUIJPanel.CANCEL_BUTTON_CAPTION));
199                _searchUIJPanel.setCancelAction(new AbstractAction() {
200                        public void actionPerformed(ActionEvent e) {
201                                resetResultsPanel();
202                                _searchUIJPanel.setSearchTerm("");
203                                quickSearchAction.stop(); //TODO get errors from CacheManager when doing this call. 
204                                                                                  //Check: can it work both during and post-search?
205                                _searchUIJPanel.setCancelButtonEnabled(false);
206                        }
207                });
208
209                _searchUIJPanel.setSourceAction(new PreferencesAction("Data"));
210
211                _searchUIJPanel.init();
212
213        }
214
215        /**
216         * Create the tree model used to contain the result set. This is displayed
217         * in the JTree on the left of the window.
218         * 
219         *@return the EntityTreeModel that can contain the results
220         */
221        private EntityTreeModel createResultModel() {
222                try {
223                        resultsRoot = new ResultTreeRoot("resultset");
224                        Attribute libraryMarker = new Attribute(resultsRoot,
225                                        "_libraryMarker");
226                } catch (IllegalActionException iae) {
227                        System.err.println("Could not create entity.");
228                } catch (NameDuplicationException nde) {
229                        System.err.println("An entity with that name already exists.");
230                }
231                EntityTreeModel resultTreeModel = new VisibleTreeModel(resultsRoot);
232                return resultTreeModel;
233        }
234
235        /**
236         * Method to get EcoGridServicesController
237         * 
238         *@return EcoGridServicesController
239         */
240        public EcoGridServicesController getSearchScope() {
241                return this.searchController;
242        }
243
244        /**
245         * Set searchController for this panel
246         * 
247         *@param newTreeData
248         *            Description of the Parameter
249         */
250        /*
251         * public void setEcoGridServiceController(EcoGridServicesController
252         * controller) { this.searchController = controller; if (quickSearchAction
253         * != null) { Vector servicesVector = controller.getServicesList();
254         * quickSearchAction.setSearchSerivcesVector(servicesVector); } }
255         */
256
257        /**
258         * Update the result panel after searching
259         * 
260         *@param newTreeData
261         *            CompositeEntity, the search result
262         */
263        public void update(EntityTreeModel newTreeData) {
264                resultPanel.setTreeModel(newTreeData);
265        }
266
267        /**
268         * Sets the progressLabel attribute of the DatasetPanel object
269         * 
270         *@param aMsg
271         *            The new progressLabel value
272         */
273        public void setProgressLabel(String aMsg) {
274                _progressLabel.setText(aMsg);
275        }
276
277        /**
278         * Method to starting search progress bar
279         * 
280         *@param all
281         *            If true, disable _all_ buttons (for auto-fetching data
282         *            sources)
283         */
284        public void startSearchProgressBar(boolean all) {
285
286                if (all) {
287                        _searchUIJPanel.setAllSearchEnabled(false);
288                } else {
289                        _searchUIJPanel.setSearchEnabled(false);
290                }
291                _progressLabel.setText("");
292                _progressBar.setIndeterminate(true);
293                _progressBar.setVisible(true);
294                _progressLabel.setVisible(false);
295        }
296
297        /**
298         * Reset the search panel - enabling/disabling buttons as appropriate and
299         * hiding progress bar
300         */
301        public void resetSearchPanel() {
302
303                _searchUIJPanel.setSearchEnabled(true);
304                _progressBar.setIndeterminate(false);
305                _progressBar.setVisible(false);
306                _progressLabel.setVisible(true);
307                _searchUIJPanel.setCancelButtonEnabled(true);
308
309        }
310
311        /**
312         * Reset the results panel to ist blank state
313         */
314        public void resetResultsPanel() {
315
316                this.getResultRoot().removeAllEntities();
317                this.update(new VisibleTreeModel(this.getResultRoot()));
318                
319        }
320
321        /**
322         * A factory that creates the library panel for the editors.
323         * 
324         *@author Aaron Schultz
325         */
326        public static class Factory extends TabPaneFactory {
327                /**
328                 * Create a factory with the given name and container.
329                 * 
330                 *@param container
331                 *            The container.
332                 *@param name
333                 *            The name of the entity.
334                 *@exception IllegalActionException
335                 *                If the container is incompatible with this attribute.
336                 *@exception NameDuplicationException
337                 *                If the name coincides with an attribute already in the
338                 *                container.
339                 */
340                public Factory(NamedObj container, String name)
341                                throws IllegalActionException, NameDuplicationException {
342                        super(container, name);
343                }
344
345                /**
346                 * Create a library pane that displays the given library of actors.
347                 * 
348                 * @return A new LibraryPaneTab that displays the library
349                 */
350                public TabPane createTabPane(TableauFrame parent) {
351                        DatasetPanel dsp = new DatasetPanel(parent);
352                        dsp.name = this.getName();
353                        return dsp;
354                }
355        }
356}