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.Component;
033
034import javax.swing.BorderFactory;
035import javax.swing.Box;
036import javax.swing.JCheckBox;
037import javax.swing.JLabel;
038import javax.swing.JScrollPane;
039import javax.swing.JTextArea;
040import javax.swing.JTextField;
041import javax.swing.SwingConstants;
042import javax.swing.border.Border;
043
044import org.apache.commons.logging.Log;
045import org.apache.commons.logging.LogFactory;
046import org.kepler.moml.NamedObjId;
047import org.kepler.util.StaticResources;
048
049import ptolemy.actor.gui.TableauFrame;
050import ptolemy.kernel.util.IllegalActionException;
051import ptolemy.kernel.util.NameDuplicationException;
052import ptolemy.kernel.util.NamedObj;
053import ptolemy.util.CancelException;
054import ptolemy.util.MessageHandler;
055
056/**
057 * A JTabbedPane tab to be added to a TabbedDialog object. This particular pane
058 * shows the "General" settings for the object being configured
059 * 
060 * @author Matthew Brooke
061 * @since 27 February 2006
062 */
063public class DialogGeneralTab extends AbstractDialogTab {
064
065        public DialogGeneralTab(NamedObj target, String targetType,
066                        TableauFrame frame) {
067                super(target, targetType, frame);
068        }
069
070        /**
071         * check the user input for errors/omissions. Return true if everything is
072         * OK and we can proceed with a save(). Return false if there are problems
073         * that need to be corrected, and preferably request focus for the "problem"
074         * UI component
075         * 
076         * @return boolean true if everything is OK and we can proceed with a
077         *         save(). Return false if there are problems that need to be
078         *         corrected, and preferably request focus for the "problem" UI
079         *         component
080         */
081        public boolean validateInput() {
082                if (nameTxtFld.getText() == null
083                                || nameTxtFld.getText().trim().length() < 1) {
084                        nameTxtFld.requestFocus();
085                        return false;
086                }
087                return true;
088        }
089
090        /**
091         * Save the user-editable values associated with this tab. The container
092         * should probably call validateInput() on each tab before saving
093         */
094        public void save() {
095
096                if (_target == null) {
097                        log.warn("Cannot save - target is NULL");
098                        return;
099                }
100                try {
101                        _target.setName(nameTxtFld.getText());
102                } catch (NameDuplicationException ex) {
103                        try {
104                                MessageHandler.warning(StaticResources.getDisplayString(
105                                                "general.errors.NameDuplication", "Error"), ex);
106                        } catch (CancelException exception) {
107                                // Ignore the cancel.
108                        }
109                } catch (IllegalActionException ex) {
110                        try {
111                                MessageHandler.warning(StaticResources.getDisplayString(
112                                                "general.errors.UnknownCannotDo", "Error"), ex);
113                        } catch (CancelException exception) {
114                                // Ignore the cancel.
115                        }
116                }
117                /** @todo - FIXME - need to save these values - how? */
118                noteTxtArea.getText();
119
120                /** @todo - FIXME - need to save these values - how? */
121                cbName.isSelected();
122                cbNotes.isSelected();
123                cbPorts.isSelected();
124        }
125
126        /**
127         * getTopPanel
128         * 
129         * @return Component
130         */
131        protected Component getTopPanel() {
132
133                Box topPanel = Box.createHorizontalBox();
134                JLabel nameLbl = WidgetFactory.makeJLabel(StaticResources
135                                .getDisplayString("dialogs." + _targetType + ".general.name",
136                                                ""), TabbedDialog.jLabelDims);
137                topPanel.add(nameLbl);
138
139                nameTxtFld = WidgetFactory.makeJTextField((_target != null ? _target
140                                .getName() : ""), TabbedDialog.textFieldDims);
141                topPanel.add(nameTxtFld);
142
143                JLabel idLbl = WidgetFactory.makeJLabel(
144                                StaticResources.getDisplayString("dialogs." + _targetType
145                                                + ".general.id", ""), TabbedDialog.idLabelDims);
146                idLbl.setHorizontalAlignment(SwingConstants.RIGHT);
147                topPanel.add(idLbl);
148
149                topPanel.add(WidgetFactory.getDefaultSpacer());
150
151                JLabel idFieldLbl = WidgetFactory.makeJLabel(getLSIDString(_target),
152                                TabbedDialog.idValueDims);
153                topPanel.add(idFieldLbl);
154
155                topPanel.add(Box.createHorizontalGlue());
156
157                return topPanel;
158        }
159
160        /**
161         * getCenterPanel
162         * 
163         * @return Component
164         */
165        protected Component getCenterPanel() {
166                Box centerPanel = Box.createHorizontalBox();
167
168                final Border middlePanelPaddingBorder = BorderFactory
169                                .createEmptyBorder( // top, left, bottom, right
170                                                StaticResources.getSize(
171                                                                "dialogs.tabPanels.padding.top", 0), 0, 0, 0);
172
173                centerPanel.setBorder(middlePanelPaddingBorder);
174
175                JLabel noteLbl = WidgetFactory.makeJLabel(StaticResources
176                                .getDisplayString("dialogs." + _targetType + ".general.note",
177                                                "Note"), TabbedDialog.jLabelDims);
178                centerPanel.add(noteLbl);
179
180                noteTxtArea = WidgetFactory.makeJTextArea(_target != null ? _target
181                                .getName() : "");
182                JScrollPane scrollPane = new JScrollPane(noteTxtArea);
183                scrollPane
184                                .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
185                scrollPane
186                                .setVerticalScrollBarPolicy(scrollPane.VERTICAL_SCROLLBAR_ALWAYS);
187                scrollPane.setWheelScrollingEnabled(true);
188                centerPanel.add(scrollPane);
189
190                return centerPanel;
191        }
192
193        /**
194         * getBottomPanel
195         * 
196         * @return Component
197         */
198        protected Component getBottomPanel() {
199                final Box bottomPanel = Box.createVerticalBox();
200                final Border bottomPanelTitledBorder = BorderFactory
201                                .createTitledBorder(StaticResources.getDisplayString("dialogs."
202                                                + _targetType + ".general.NamesNotesBorderTitle", ""));
203
204                bottomPanel.setBorder(bottomPanelTitledBorder);
205
206                cbName = new JCheckBox(StaticResources.getDisplayString("dialogs."
207                                + _targetType + ".general.showNameCheckbox", ""));
208                cbNotes = new JCheckBox(StaticResources.getDisplayString("dialogs."
209                                + _targetType + ".general.showNoteCheckbox", ""));
210                cbPorts = new JCheckBox(StaticResources.getDisplayString("dialogs."
211                                + _targetType + ".general.showPortNamesCheckbox", ""));
212
213                bottomPanel.add(cbName);
214                bottomPanel.add(cbNotes);
215                bottomPanel.add(cbPorts);
216                return bottomPanel;
217        }
218
219        private String getLSIDString(NamedObj target) {
220
221                // never knowingly returns null...
222                if (target == null) {
223                        return "";
224                }
225                NamedObjId lsid = (NamedObjId) target
226                                .getAttribute(NamedObjId.NAME);
227                String lsidStr = "";
228                if (lsid != null) {
229                        lsidStr = lsid.getExpression();
230                        if (isDebugging) {
231                                log.debug("\n\n*** FOUND LSID (" + lsidStr + ") for: "
232                                                + target.getClassName());
233                        }
234                }
235                return lsidStr;
236        }
237
238        private JCheckBox cbName;
239        private JCheckBox cbNotes;
240        private JCheckBox cbPorts;
241        private JTextArea noteTxtArea;
242        private JTextField nameTxtFld;
243
244        private static final Log log = LogFactory.getLog(
245                        DialogGeneralTab.class.getName());
246        private static final boolean isDebugging = log.isDebugEnabled();
247}