001/*
002 * Copyright (c) 2009-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: barseghian $'
006 * '$Date: 2011-12-24 10:08:56 +0000 (Sat, 24 Dec 2011) $' 
007 * '$Revision: 29100 $'
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;
033import java.awt.Component;
034import java.awt.Cursor;
035import java.awt.Dimension;
036import java.awt.event.ActionEvent;
037import java.awt.event.ActionListener;
038
039import javax.swing.JButton;
040import javax.swing.JFrame;
041import javax.swing.JPanel;
042import javax.swing.JTabbedPane;
043
044import org.kepler.util.StaticResources;
045
046import ptolemy.actor.gui.TableauFrame;
047
048/**
049 * This frame allows a common place for modules to set preferences. By adding an
050 * entry to the preferencesTabFactory in gui/resources/configurations/configuration.xml 
051 * modules can include their own preferences.
052 * 
053 * @author Aaron Schultz
054 * 
055 */
056public class PreferencesFrame extends JFrame implements ActionListener {
057
058        private TableauFrame _frame;
059
060        private JTabbedPane _preferenceTabs;
061        private JPanel _controls;
062
063        private JButton _okButton;
064        private JButton _cancelButton;
065
066        private int _width = 700;
067        private int _height = 600;
068        
069        /**
070         * Constructor accepts a title for the frame and the parent of the frame.
071         * 
072         * @param title
073         *            the title to appear at the top of the preferences frame
074         * @param frame
075         *            the parent TableauFrame
076         */
077        public PreferencesFrame(String title, TableauFrame frame) {
078                this(title, frame, null);
079        }
080
081        /**
082         * Constructor accepts a title for the frame, the parent of the frame, and
083         * the tab to display after opening the preferencesFrame
084         * 
085         * @param title
086         *            the title to appear at the top of the preferences frame
087         * @param frame
088         *            the parent TableauFrame
089         * @param openTabName
090         *            the name of the tab to show by default
091         */
092        public PreferencesFrame(String title, TableauFrame frame, String openTabName) {
093                super(title);
094                _frame = frame;
095
096                setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
097                setSize(new Dimension(_width, _height));
098
099                JPanel layoutPanel = new JPanel();
100                layoutPanel.setLayout(new BorderLayout());
101
102                _preferenceTabs = new JTabbedPane();
103                initTabs();
104                layoutPanel.add(_preferenceTabs, BorderLayout.CENTER);
105
106                _controls = new JPanel();
107                initControls();
108                layoutPanel.add(_controls, BorderLayout.SOUTH);
109
110                getContentPane().add(layoutPanel);
111
112                if (openTabName != null && openTabName.length() > 0) {
113                        setSelectedTab(openTabName);
114                }
115        }
116
117        /**
118         * Initialize the preference tab extensions from configuration.
119         */
120        protected void initTabs() {
121    try
122    {
123      PreferencesTabFactory PTfactory = (PreferencesTabFactory)_frame.getConfiguration().getAttribute("PreferencesTabFactor");
124      if(PTfactory == null)
125      {
126        PTfactory = new PreferencesTabFactory(_frame.getConfiguration(), "PreferencesTabFactor");
127      }
128      if (PTfactory != null) {
129        boolean success = PTfactory.createPreferencesTabs(_preferenceTabs,
130            _frame);
131        if (!success) {
132          System.out
133              .println("error: preferenceTab is null.  "
134                  + "This "
135                  + "problem can be fixed by adding a librarySearchGUIPane "
136                  + "property in the configuration.xml file.");
137        }
138      } else {
139        System.out.println("error: PreferencesTabFactory is "
140            + "null.  This "
141            + "problem can be fixed by adding a LibraryPaneTabFactory "
142            + "property in the configuration.xml file.");
143      }
144    }
145    catch(Exception e)
146    {
147      System.out.println("Could not create tab preferences factory: " + e.getMessage());
148      e.printStackTrace();
149    }
150
151        }
152
153        /**
154         * Initialize the control buttons.
155         */
156        protected void initControls() {
157
158                _okButton = new JButton(
159                                StaticResources.getDisplayString("general.OK", "Ok"));
160                _okButton.addActionListener(this);
161                _controls.add(_okButton);
162
163                _cancelButton = new JButton(
164                                StaticResources.getDisplayString("general.CANCEL", "Cancel"));
165                _cancelButton.addActionListener(this);
166                _controls.add(_cancelButton);
167        }
168
169        /**
170         * Set the selected tab using the index of the tab.
171         * 
172         * @param tabIndex
173         */
174        public void setSelectedTab(int tabIndex) {
175                _preferenceTabs.setSelectedIndex(tabIndex);
176        }
177
178        /**
179         * Set the selected tab using the name of the tab.
180         * 
181         * @param tabName
182         */
183        public void setSelectedTab(String tabName) {
184                boolean tabFound = false;
185                for (int i = 0; i < _preferenceTabs.getTabCount(); i++) {
186                        Component c = _preferenceTabs.getComponentAt(i);
187                        if (c instanceof PreferencesTab) {
188                                if (((PreferencesTab) c).getTabName().equals(tabName)) {
189                                        _preferenceTabs.setSelectedIndex(i);
190                                        tabFound = true;
191                                        break;
192                                }
193                        }
194                }
195                if (!tabFound) {
196                        _preferenceTabs.setSelectedIndex(0);
197                }
198        }
199
200        /**
201         * As the PreferenceFrame is disposed the onClose method of each preference
202         * tab is called.
203         */
204        public void dispose() {
205                for (int i = 0; i < _preferenceTabs.getTabCount(); i++) {
206                        Component c = _preferenceTabs.getComponentAt(i);
207                        if (c instanceof PreferencesTab) {
208                                PreferencesTab pt = (PreferencesTab) c;
209                                pt.onClose();
210                                pt.setParent(null);
211                        }
212                }
213                PreferencesFrameTracker pft = PreferencesFrameTracker.getInstance();
214                pft.setClosed();
215                super.dispose();
216        }
217
218        /*
219         * (non-Javadoc)
220         * 
221         * @see
222         * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
223         */
224        public void actionPerformed(ActionEvent e) {
225                
226                setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
227
228                try {
229                        if (e.getSource() == _okButton) {
230
231                                dispose();
232
233                        } else if (e.getSource() == _cancelButton) {
234
235                                for (int i = 0; i < _preferenceTabs.getTabCount(); i++) {
236                                        Component c = _preferenceTabs.getComponentAt(i);
237                                        if (c instanceof PreferencesTab) {
238                                                ((PreferencesTab) c).onCancel();
239                                        }
240                                }
241                                PreferencesFrameTracker pft = PreferencesFrameTracker.getInstance();
242                                pft.setClosed();
243                                super.dispose();
244
245                        }
246                } catch (Exception e1) {
247                        e1.printStackTrace();
248                } finally {
249                        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
250                }
251
252        }
253
254}