001/*
002 * Copyright (c) 2004-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;
033import java.awt.Component;
034import java.awt.Dimension;
035import java.awt.Font;
036import java.awt.event.ActionEvent;
037import java.awt.event.ActionListener;
038
039import javax.swing.BorderFactory;
040import javax.swing.Box;
041import javax.swing.JButton;
042import javax.swing.JCheckBox;
043import javax.swing.JPanel;
044import javax.swing.UIManager;
045import javax.swing.border.Border;
046import javax.swing.plaf.InsetsUIResource;
047
048import org.kepler.util.StaticResources;
049
050import ptolemy.actor.gui.Configurer;
051import ptolemy.actor.gui.TableauFrame;
052import ptolemy.gui.ComponentDialog;
053import ptolemy.gui.Query;
054import ptolemy.kernel.util.NamedObj;
055import ptolemy.moml.MoMLChangeRequest;
056import ptolemy.util.StringUtilities;
057
058/**
059 * A JTabbedPane tab to be added to a TabbedDialog object. This particular pane
060 * shows the "Parameters" settings for the object being configured
061 * 
062 * @author Matthew Brooke
063 * @since 27 February 2006
064 */
065public class DialogParametersTab extends AbstractDialogTab {
066
067        public DialogParametersTab(NamedObj target, String targetType,
068                        TableauFrame frame) {
069                super(target, targetType, frame);
070        }
071
072        /**
073         * check the user input for errors/omissions. Return true if everything is
074         * OK and we can proceed with a save(). Return false if there are problems
075         * that need to be corrected, and preferably request focus for the "problem"
076         * UI component
077         * 
078         * @return boolean true if everything is OK and we can proceed with a
079         *         save(). Return false if there are problems that need to be
080         *         corrected, and preferably request focus for the "problem" UI
081         *         component
082         */
083        public boolean validateInput() {
084                /** @todo - FIXME - needs to be implemented */
085                return true;
086        }
087
088        /**
089         * Save the user-editable values associated with this tab. The container
090         * should probably call validateInput() on each tab before saving
091         */
092        public void save() {
093                /** @todo - FIXME - needs to be implemented */
094        }
095
096        /**
097         * getTopPanel
098         * 
099         * @return Component
100         */
101        protected Component getTopPanel() {
102                Box topPanel = Box.createHorizontalBox();
103                cbExpert = new JCheckBox(StaticResources.getDisplayString(
104                                "dialogs.actor.parameters.expertCheckbox", ""));
105                topPanel.add(cbExpert);
106                topPanel.add(WidgetFactory.getDefaultSpacer());
107                return topPanel;
108        }
109
110        /**
111         * getCenterPanel
112         * 
113         * @return Component
114         */
115        protected Component getCenterPanel() {
116                JPanel centerPanel = new JPanel(new BorderLayout());
117
118                final Box paramsBox = Box.createVerticalBox();
119                final Border titledBorder = BorderFactory.createTitledBorder("");
120
121                paramsBox.setBorder(titledBorder);
122                centerPanel.add(paramsBox, BorderLayout.CENTER);
123
124                Box buttonBox = Box.createVerticalBox();
125
126                initButtons();
127                buttonBox.add(addButton);
128                buttonBox.add(WidgetFactory.getDefaultSpacer());
129                buttonBox.add(delButton);
130                buttonBox.add(WidgetFactory.getDefaultSpacer());
131                buttonBox.add(fmtButton);
132                buttonBox.add(WidgetFactory.getDefaultSpacer());
133                buttonBox.add(rstButton);
134                buttonBox.add(Box.createVerticalGlue());
135                centerPanel.add(buttonBox, BorderLayout.EAST);
136
137                paramsBox.add(new Configurer(_target));
138
139                return centerPanel;
140        }
141
142        /**
143         * getBottomPanel
144         * 
145         * @return Component
146         */
147        protected Component getBottomPanel() {
148
149                Dimension dim = new Dimension(StaticResources.getSize(
150                                "dialogs.tabPanels.padding.top", 0), 20); // y-component is
151                                                                                                                        // ignored
152                return Box.createRigidArea(dim);
153        }
154
155        private void initButtons() {
156
157                // remember default button margins
158                final InsetsUIResource defaultUIMgrButtonMargin = (InsetsUIResource) UIManager
159                                .get("Button.margin");
160                // now set our custom ones
161                UIManager.put("Button.margin", BUTTON_INSIDE_PADDING);
162
163                final int BUTTON_FONT_SIZE = StaticResources.getSize(
164                                "button.limitedSpace.maxFontSize", 11);
165                // remember default button font
166                final Font defaultUIMgrButtonFont = (Font) UIManager.get("Button.font");
167
168                // now set our custom size, provided it's smaller than the default:
169                int buttonFontSize = (defaultUIMgrButtonFont.getSize() < BUTTON_FONT_SIZE) ? defaultUIMgrButtonFont
170                                .getSize()
171                                : BUTTON_FONT_SIZE;
172
173                addButton = new JButton();
174                delButton = new JButton();
175                fmtButton = new JButton();
176                rstButton = new JButton();
177
178                // text
179                addButton.setText(StaticResources.getDisplayString(
180                                "dialogs.actor.parameters.addButton", ""));
181                delButton.setText(StaticResources.getDisplayString(
182                                "dialogs.actor.parameters.deleteButton", ""));
183                fmtButton.setText(StaticResources.getDisplayString(
184                                "dialogs.actor.parameters.formatButton", ""));
185                rstButton.setText(StaticResources.getDisplayString(
186                                "dialogs.actor.parameters.resetButton", ""));
187                // Dims
188                WidgetFactory.setPrefMinMaxSizes(addButton, BUTTON_DIMS);
189                WidgetFactory.setPrefMinMaxSizes(delButton, BUTTON_DIMS);
190                WidgetFactory.setPrefMinMaxSizes(fmtButton, BUTTON_DIMS);
191                WidgetFactory.setPrefMinMaxSizes(rstButton, BUTTON_DIMS);
192
193                // actionListeners
194                addButton.addActionListener(new ActionListener() {
195                        public void actionPerformed(ActionEvent e) {
196                                _addAction();
197                        }
198                });
199                delButton.addActionListener(new ActionListener() {
200                        public void actionPerformed(ActionEvent e) {
201                                _deleteAction();
202                        }
203                });
204                fmtButton.addActionListener(new ActionListener() {
205                        public void actionPerformed(ActionEvent e) {
206                                _formatAction();
207                        }
208                });
209                rstButton.addActionListener(new ActionListener() {
210                        public void actionPerformed(ActionEvent e) {
211                                _resetAction();
212                        }
213                });
214
215                // restore default button margins
216                if (defaultUIMgrButtonMargin != null) {
217                        UIManager.put("Button.margin", defaultUIMgrButtonMargin);
218                }
219                // restore default button font
220                if (defaultUIMgrButtonFont != null) {
221                        UIManager.put("Button.font", defaultUIMgrButtonFont);
222                }
223
224        }
225
226        /**
227         * _addAction
228         */
229        private void _addAction() {
230
231                // * * * SEE ptolemy.actor.gui.EditParametersDialog for how to do this *
232                // * *
233
234                /**
235                 * @todo - note this changes params immediately on the target object - so
236                 *       if user hits cancel, we need to roll them back
237                 *       (configurer.restore()???)
238                 */
239
240                // Create a new dialog to add a parameter, then open a new
241                // EditParametersDialog.
242                Query _query = new Query();
243
244                _query.addLine("name", "Name", "");
245                _query.addLine("default", "Default value", "");
246                _query.addLine("class", "Class", "ptolemy.data.expr.Parameter");
247
248                ComponentDialog dialog = new ComponentDialog(null,
249                                "Add a new parameter to " + _target.getFullName(), _query, null);
250
251                // If the OK button was pressed, then queue a mutation
252                // to create the parameter.
253                // A blank property name is interpreted as a cancel.
254                String newName = _query.getStringValue("name");
255
256                // Need to escape quotes in default value.
257                String newDefValue = StringUtilities.escapeForXML(_query
258                                .getStringValue("default"));
259
260                if (dialog.buttonPressed().equals("OK") && !newName.equals("")) {
261                        String moml = "<property name=\"" + newName + "\" value=\""
262                                        + newDefValue + "\" class=\""
263                                        + _query.getStringValue("class") + "\"/>";
264                        // _target.addChangeListener(this);
265
266                        MoMLChangeRequest request = new MoMLChangeRequest(this, _target,
267                                        moml);
268                        request.setUndoable(true);
269                        _target.requestChange(request);
270                }
271                dialog.setVisible(true);
272        }
273
274        /**
275         * _addAction
276         */
277        private void _deleteAction() {
278                /** @todo - FIXME - implement this */
279                // * * * SEE ptolemy.actor.gui.EditParametersDialog for how to do this *
280                // * *
281        }
282
283        /**
284         * _addAction
285         */
286        private void _formatAction() {
287                /** @todo - FIXME - implement this. What does it do??? */
288        }
289
290        /**
291         * _addAction
292         */
293        private void _resetAction() {
294                /** @todo - FIXME - implement this */
295                // * * * SEE ptolemy.actor.gui.EditParametersDialog for how to do this *
296                // * *
297                // maybe use configurer.restore()??
298                //
299                // ALSO - should global cancel call this method to reset params to
300                // original??
301        }
302
303        private JButton addButton;
304        private JButton delButton;
305        private JButton fmtButton;
306        private JButton rstButton;
307        private final InsetsUIResource BUTTON_INSIDE_PADDING = new InsetsUIResource(
308                        2, 0, 2, 0); // (top, left, bottom, right)
309        private JCheckBox cbExpert;
310        private static final Dimension BUTTON_DIMS = StaticGUIResources.getDimension(
311                        "dialogs.params.buttons.width", "dialogs.params.buttons.height",
312                        94, 22);
313}