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;
031
032import java.awt.BorderLayout;
033import java.awt.Dimension;
034import java.awt.Font;
035import java.awt.HeadlessException;
036import java.awt.Insets;
037import java.util.Vector;
038
039import javax.swing.Box;
040import javax.swing.BoxLayout;
041import javax.swing.JButton;
042import javax.swing.JCheckBox;
043import javax.swing.JLabel;
044import javax.swing.JPanel;
045import javax.swing.UIManager;
046
047import org.apache.commons.logging.Log;
048import org.apache.commons.logging.LogFactory;
049import org.kepler.gui.PreferencesTab;
050import org.kepler.gui.PreferencesTabFactory;
051import org.kepler.util.StaticResources;
052
053import ptolemy.actor.gui.TableauFrame;
054import ptolemy.kernel.util.IllegalActionException;
055import ptolemy.kernel.util.NameDuplicationException;
056import ptolemy.kernel.util.NamedObj;
057
058/**
059 * Class reprents a frame which will display a service panel
060 * 
061 *@author not attributable
062 *@created February 17, 2005
063 */
064
065public class EcogridPreferencesTab extends JPanel implements PreferencesTab {
066
067        private String _tabName;
068
069        private JPanel mainPanel = new JPanel();
070        private JPanel textPanel = new JPanel();
071        private JLabel textLabel = new JLabel();
072        private JPanel buttonPanel = new JPanel();
073        private ServicesDisplayPanel servicesDisplayPanel = null;
074        private String displayText = "";
075        private Vector<String> selectedServiceList = null;
076
077        private static int BUTTON_FONT_SIZE = StaticResources.getSize(
078                        "button.limitedSpace.maxFontSize", 11);
079        private JButton refreshButton = null;
080        private JCheckBox keepExistingCheckbox = null;
081        private JPanel newButtonPanel = null;
082
083        /**
084         * Description of the Field
085         */
086        protected EcoGridServicesController controller = null;
087        private Vector<SelectableEcoGridService> originalServiceList = new Vector<SelectableEcoGridService>();
088
089        protected TableauFrame parentFrame;
090
091        /**
092         * Description of the Field
093         */
094        public final static int MARGINGSIZE = 20;
095        /**
096         * Description of the Field
097         */
098        public final static int GAP = 30;
099        /**
100         * Description of the Field
101         */
102        public final static int HEIGHT = 400;
103        /**
104         * Description of the Field
105         */
106        public final static Dimension BUTTONDIMENSION = new Dimension(83, 25);
107        private final static int ROWNUMBER = 5;
108
109        protected final static Log log;
110        static {
111                log = LogFactory
112                                .getLog("org.ecoinformatics.seek.ecogrid.ServicesDisplayFrame");
113        }
114
115        /**
116         * Constructor of the frame
117         * 
118         *@param frameTitle
119         *            Description of the Parameter
120         *@param selectedServiceList
121         *            Description of the Parameter
122         *@param controller
123         *            Description of the Parameter
124         *@param location
125         *            Description of the Parameter
126         *@exception HeadlessException
127         *                Description of the Exception
128         *@throws HeadlessException
129         */
130        public EcogridPreferencesTab() throws HeadlessException {
131                super();
132        }
133
134        /*
135         * (non-Javadoc)
136         * 
137         * @see org.kepler.gui.PreferencesTab#getTabName()
138         */
139        public String getTabName() {
140                return _tabName;
141        }
142
143        public void setTabName(String tabName) {
144                _tabName = tabName;
145        }
146
147        public void initializeTab() throws Exception {
148                this.controller = EcoGridServicesController.getInstance();
149                this.selectedServiceList = this.controller.getQueryServicesList();
150                initOriginalServiceList();
151                initMainPanel();
152                initButtonPanel();
153                this.setLayout(new BorderLayout());
154
155                // GridBagLayout gridbag = new GridBagLayout();
156
157                /*
158                 * int gridx, int gridy, int gridwidth, int gridheight, double weightx,
159                 * double weighty, int anchor, int fill, Insets insets, int ipadx, int
160                 * ipady
161                 */
162                // this.setLayout(gridbag);
163                this.add(mainPanel, BorderLayout.CENTER);
164                initPanelSize();
165        }
166
167        /*
168         * Method to initial frame size
169         */
170        /**
171         * Description of the Method
172         */
173        private void initPanelSize() {
174                Insets insets = getInsets();
175                int width = 2 * MARGINGSIZE + insets.left + insets.right
176                                + ServicesDisplayPanel.HEADNAME.length
177                                * ServicesDisplayPanel.CELLPREFERREDWIDTH;
178                int height = HEIGHT;
179                this.setMinimumSize(new Dimension(width, height));
180                this.setPreferredSize(new Dimension(width, height));
181        }
182
183        /*
184         * (non-Javadoc)
185         * 
186         * @see
187         * org.kepler.gui.PreferencesTab#setParent(ptolemy.actor.gui.TableauFrame)
188         */
189        public void setParent(TableauFrame frame) {
190                this.parentFrame = frame;
191        }
192
193        /*
194         * This method will init button panel
195         */
196        private void initButtonPanel() {
197
198                // remember default button font
199                final Font defaultUIMgrButtonFont = (Font) UIManager.get("Button.font");
200
201                // now set our custom size, provided it's smaller than the default:
202                int buttonFontSize = (defaultUIMgrButtonFont.getSize() < BUTTON_FONT_SIZE) ? defaultUIMgrButtonFont
203                                .getSize(): BUTTON_FONT_SIZE;
204
205                final Font BUTTON_FONT = new Font(defaultUIMgrButtonFont.getFontName(),
206                                defaultUIMgrButtonFont.getStyle(), buttonFontSize);
207
208                UIManager.put("Button.font", BUTTON_FONT);
209
210                refreshButton = new JButton(
211                                new ServicesRefreshAction(
212                                                StaticResources.getDisplayString(
213                                                                "preferences.data.refresh", 
214                                                                "Refresh"), 
215                                                this));
216                refreshButton.setMinimumSize(EcogridPreferencesTab.BUTTONDIMENSION);
217                refreshButton.setPreferredSize(EcogridPreferencesTab.BUTTONDIMENSION);
218                refreshButton.setSize(EcogridPreferencesTab.BUTTONDIMENSION);
219                // setMaximumSize was truncating label on osX. I don't know why. 
220                // It seems about the same as how SearchUIJPanel does things. -derik
221                //refreshButton.setMaximumSize(EcogridPreferencesTab.BUTTONDIMENSION);
222
223                keepExistingCheckbox = new JCheckBox(
224                                StaticResources.getDisplayString(
225                                                "preferences.data.keepExistingSources",
226                                                "Keep existing sources"));
227                keepExistingCheckbox.setSelected(false);
228
229                JPanel bottomPanel = new JPanel();
230
231                bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));
232                bottomPanel.add(refreshButton);
233                bottomPanel.add(keepExistingCheckbox);
234
235
236                newButtonPanel = new JPanel();
237                newButtonPanel.setLayout(new BorderLayout());
238                newButtonPanel.add(Box
239                                .createVerticalStrut(EcogridPreferencesTab.MARGINGSIZE),
240                                BorderLayout.NORTH);
241                newButtonPanel.add(bottomPanel, BorderLayout.SOUTH);
242                setButtonPanel(newButtonPanel);
243                
244                // restore default button font
245                if (defaultUIMgrButtonFont != null) {
246                        UIManager.put("Button.font", defaultUIMgrButtonFont);
247                }
248        }
249
250        /**
251         * This method will re-add buttons to frame
252         */
253        public void updateButtonPanel() {
254                this.remove(newButtonPanel);
255                initButtonPanel();
256        }
257
258        public boolean isKeepExisting() {
259                return this.keepExistingCheckbox.isSelected();
260        }
261
262        // ServicesDisplayFrame
263
264        /*
265         * Method to initPanles (box layout)
266         */
267        /**
268         * Description of the Method
269         */
270        private void initMainPanel() {
271                mainPanel.setLayout(new BorderLayout());
272
273                // init text panel and add it to main panel
274                initTextPanel();
275                mainPanel.add(textPanel, BorderLayout.NORTH);
276                // add gap between text and servicepanel
277
278                // init service dispaly panel and add it to main panel
279                initServiceDisplayPanel();
280                mainPanel.add(servicesDisplayPanel, BorderLayout.CENTER);
281                mainPanel.add(buttonPanel, BorderLayout.SOUTH);
282
283        }
284
285        // initMainPanel
286
287        /*
288         * Init text panel (border Layout and box layout)
289         */
290        /**
291         * Description of the Method
292         */
293        private void initTextPanel() {
294                // text part of text panel
295                JPanel textTopPanel = new JPanel();
296                textTopPanel.setLayout(new BoxLayout(textTopPanel, BoxLayout.X_AXIS));
297                textTopPanel.add(textLabel);
298                textTopPanel.add(Box.createHorizontalGlue());
299                // y box layout
300                textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.Y_AXIS));
301                textPanel.add(textTopPanel);
302                textPanel.add(Box.createVerticalStrut(GAP));
303        }
304
305        // initTextPanel
306
307        /*
308         * Init service display panel(border layout)
309         */
310        /**
311         * Description of the Method
312         */
313        private void initServiceDisplayPanel() {
314                servicesDisplayPanel = new ServicesDisplayPanel(selectedServiceList);
315        }
316
317        // initServiceDisplayPanel
318
319        /*
320         * Method to initialize the orginal service, it will copy the service list
321         * in controller to another vector(it will create a new EcoGridService
322         * object, not a pointer)
323         */
324        /**
325         * Description of the Method
326         */
327        private void initOriginalServiceList() {
328                Vector serviceList = controller.getServicesList();
329                if (serviceList != null) {
330
331                        int size = serviceList.size();
332                        for (int i = 0; i < size; i++) {
333                                SelectableEcoGridService service = (SelectableEcoGridService) serviceList
334                                                .elementAt(i);
335                                try {
336                                        SelectableEcoGridService newServcie = SelectableEcoGridService
337                                                        .copySelectableEcoGridService(service);
338                                        originalServiceList.add(newServcie);
339
340                                } catch (Exception e) {
341                                        log
342                                                        .debug(
343                                                                        "Error for copy a servie in initOrignialServiceList ",
344                                                                        e);
345                                }
346
347                        }
348                }
349        }
350
351        //
352
353        /**
354         * Method to get the main panel.
355         * 
356         *@return JPanel
357         */
358        public JPanel getMainPanel() {
359                return this.mainPanel;
360        }
361
362        // getMainPanel
363
364        /**
365         * Method to get service display panel
366         * 
367         *@return JPanel
368         */
369        public ServicesDisplayPanel getServicesDisplayPanel() {
370                return this.servicesDisplayPanel;
371        }
372
373        // getServiceDisplayPanel()
374
375        /**
376         * Method to set up a new service dipaly panel panel
377         * 
378         *@param servicesDisplayPanel
379         *            ServicesDisplayPanel
380         */
381        public void setServiceDisplayPanel(ServicesDisplayPanel servicesDisplayPanel) {
382                mainPanel.remove(this.servicesDisplayPanel);
383                this.servicesDisplayPanel = servicesDisplayPanel;
384                mainPanel.add(this.servicesDisplayPanel, BorderLayout.CENTER);
385                mainPanel.validate();
386        }
387
388        // setServiceDisplayPanel
389
390        /**
391         * Method to get button panel
392         * 
393         *@return JPanel
394         */
395        public JPanel getButtonPanel() {
396                return this.buttonPanel;
397        }
398
399        // get button panel
400
401        /**
402         * Method to reset buttion panel
403         * 
404         *@param buttonPanel
405         *            JPanel
406         */
407        public void setButtonPanel(JPanel buttonPanel) {
408                mainPanel.remove(this.buttonPanel);
409                this.buttonPanel = buttonPanel;
410                mainPanel.add(this.buttonPanel, BorderLayout.SOUTH);
411                mainPanel.validate();
412        }
413
414        // setButtonPanel
415
416        /**
417         * Method to get display text
418         * 
419         *@return String
420         */
421        public String getDisplayText() {
422                return this.displayText;
423        }
424
425        // getDisplayText
426
427        /**
428         * Method to set the display string
429         * 
430         *@param displayText
431         *            String
432         */
433        public void setDisplayText(String displayText) {
434                this.displayText = displayText;
435                textLabel.setText(this.displayText);
436        }
437
438        // setDislayText
439
440        /**
441         * Method to get controller
442         * 
443         *@return EcoGridServicesController
444         */
445        public EcoGridServicesController getEcoGridServicesController() {
446                return this.controller;
447        }
448
449        // getecogridcontroller
450
451        /**
452         * Method to set controller
453         * 
454         *@param controller
455         *            EcoGridServicesController
456         */
457        public void setEcoGridServicesController(
458                        EcoGridServicesController controller) {
459
460                this.controller = controller;
461        }
462
463        // setEcoGridServicesController
464
465        /**
466         * Method to get original service list(this list is for cancel button)
467         * 
468         *@return Vector
469         */
470        public Vector getOriginalServiceList() {
471                return this.originalServiceList;
472        }
473
474        // getOriginalServiceList
475
476        /**
477         * Set original service list
478         * 
479         *@param originalServiceList
480         *            Vector
481         */
482        public void setOriginalServiceList(Vector originalServiceList) {
483                this.originalServiceList = originalServiceList;
484        }
485
486        // setOriginalServiceList
487
488        /**
489         * Method to update service in controller base on the user selection in the
490         * panel
491         */
492        public void updateController() {
493                // currently we just set new service into controller(memory), later will
494                // need to
495                // save to jar configure file
496                if (servicesDisplayPanel != null && controller != null) {
497                        // removed the all unselected serivce
498                        Vector allUnSelectedList = servicesDisplayPanel
499                                        .getAllUnSelectedServicesList();
500                        if (allUnSelectedList != null) {
501                                int size = allUnSelectedList.size();
502                                for (int i = 0; i < size; i++) {
503                                        SelectableEcoGridService service = (SelectableEcoGridService) allUnSelectedList
504                                                        .elementAt(i);
505                                        controller.removeService(service);
506                                }
507                        }
508                        // updated partial selected services
509                        // this vector is a service list with the selected document type
510                        Vector partialSelectedList = servicesDisplayPanel
511                                        .getPartialSelectedServicesList();
512                        if (partialSelectedList != null) {
513                                int length = partialSelectedList.size();
514                                for (int j = 0; j < length; j++) {
515                                        SelectableEcoGridService service = (SelectableEcoGridService) partialSelectedList
516                                                        .elementAt(j);
517                                        try {
518                                                controller.updateService(service);
519                                        } catch (Exception ee) {
520                                                log.debug("Could not update a service "
521                                                                + service.getServiceName(), ee);
522                                        }
523                                }// for
524                        }// if
525                }
526        }
527
528        /**
529         * A factory that creates the ServicesListModification panel for the
530         * PreferencesFrame.
531         * 
532         *@author Aaron Schultz
533         */
534        public static class Factory extends PreferencesTabFactory {
535                /**
536                 * Create a factory with the given name and container.
537                 * 
538                 *@param container
539                 *            The container.
540                 *@param name
541                 *            The name of the entity.
542                 *@exception IllegalActionException
543                 *                If the container is incompatible with this attribute.
544                 *@exception NameDuplicationException
545                 *                If the name coincides with an attribute already in the
546                 *                container.
547                 */
548                public Factory(NamedObj container, String name)
549                                throws IllegalActionException, NameDuplicationException {
550                        super(container, name);
551                }
552
553                /**
554                 * Create a PreferencesTab that displays the selected Ecogrid Services.
555                 * 
556                 * @return A new LibraryPaneTab that displays the library
557                 */
558                public PreferencesTab createPreferencesTab() {
559                        EcogridPreferencesTab ept = new EcogridPreferencesTab();
560                        ept.setTabName(this.getName());
561                        return ept;
562                }
563        }
564
565        public void onClose() {
566                EcoGridServicesController esc = EcoGridServicesController.getInstance();
567                try {
568                        esc.writeServices();
569                } catch (Exception e) {
570                        esc.deleteServicesFile();
571                }
572        }
573
574        /*
575         * (non-Javadoc)
576         * 
577         * @see org.kepler.gui.PreferencesTab#onCancel()
578         */
579        public void onCancel() {
580                controller.setServicesList(originalServiceList);
581        }
582
583}