001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visualweb.propertyeditors;
042:
043: import com.sun.rave.propertyeditors.domains.EditableDomain;
044: import com.sun.rave.propertyeditors.domains.Element;
045: import java.awt.Dialog;
046: import java.text.MessageFormat;
047: import java.util.ResourceBundle;
048: import javax.swing.JDialog;
049: import javax.swing.JOptionPane;
050: import javax.swing.JPanel;
051: import javax.swing.event.DocumentEvent;
052: import javax.swing.event.DocumentListener;
053:
054: /**
055: * A panel for gathering the display label and value of a new domain element.
056: *
057: * @author gjmurphy
058: */
059:
060: // TODO Allow value types other than String to be edited
061: // TODO Check whether new element is already in domain
062: class EditElementDialog extends JPanel {
063:
064: static ResourceBundle bundle = ResourceBundle
065: .getBundle(EditElementDialog.class.getPackage().getName()
066: + ".Bundle"); //NOI18N
067:
068: private Element element;
069: private EditableDomain domain;
070:
071: EditElementDialog(EditableDomain domain) {
072: this (null, domain);
073: }
074:
075: EditElementDialog(Element element, EditableDomain domain) {
076: this .element = element;
077: this .domain = domain;
078: initComponents();
079: }
080:
081: /** This method is called from within the constructor to initialize the form.
082: * WARNING: Do NOT modify this code. The content of this method is
083: * always regenerated by the Form Editor.
084: */
085: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
086: private void initComponents() {
087: java.awt.GridBagConstraints gridBagConstraints;
088:
089: labelLabel = new javax.swing.JLabel();
090: valueLabel = new javax.swing.JLabel();
091: labelTextField = new javax.swing.JTextField();
092: if (this .element != null)
093: this .labelTextField.setText(this .element.getLabel());
094: this .labelTextField.getDocument().addDocumentListener(
095: new DocumentListener() {
096: public void removeUpdate(DocumentEvent e) {
097: labelTextFieldUpdated();
098: }
099:
100: public void insertUpdate(DocumentEvent e) {
101: labelTextFieldUpdated();
102: }
103:
104: public void changedUpdate(DocumentEvent e) {
105: labelTextFieldUpdated();
106: }
107: });
108: valueTextField = new javax.swing.JTextField();
109: if (this .element != null)
110: this .valueTextField.setText(this .element.getValue()
111: .toString());
112: valueCheckBox = new javax.swing.JCheckBox();
113: jPanel1 = new javax.swing.JPanel();
114: okButton = new javax.swing.JButton();
115: cancelButton = new javax.swing.JButton();
116:
117: setLayout(new java.awt.GridBagLayout());
118:
119: labelLabel
120: .setText(java.util.ResourceBundle
121: .getBundle(
122: "org.netbeans.modules.visualweb.propertyeditors.Bundle")
123: .getString("EditElementDialog.value"));
124: gridBagConstraints = new java.awt.GridBagConstraints();
125: gridBagConstraints.gridx = 0;
126: gridBagConstraints.gridy = 1;
127: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
128: gridBagConstraints.insets = new java.awt.Insets(0, 6, 0, 0);
129: add(labelLabel, gridBagConstraints);
130:
131: valueLabel
132: .setText(java.util.ResourceBundle
133: .getBundle(
134: "org.netbeans.modules.visualweb.propertyeditors.Bundle")
135: .getString("EditElementDialog.label"));
136: gridBagConstraints = new java.awt.GridBagConstraints();
137: gridBagConstraints.gridx = 0;
138: gridBagConstraints.gridy = 0;
139: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
140: gridBagConstraints.insets = new java.awt.Insets(6, 6, 6, 0);
141: add(valueLabel, gridBagConstraints);
142:
143: labelTextField.setColumns(24);
144: gridBagConstraints = new java.awt.GridBagConstraints();
145: gridBagConstraints.gridx = 1;
146: gridBagConstraints.gridy = 0;
147: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
148: gridBagConstraints.weightx = 1.0;
149: gridBagConstraints.insets = new java.awt.Insets(6, 6, 6, 0);
150: add(labelTextField, gridBagConstraints);
151:
152: valueTextField.setColumns(24);
153: gridBagConstraints = new java.awt.GridBagConstraints();
154: gridBagConstraints.gridx = 1;
155: gridBagConstraints.gridy = 1;
156: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
157: gridBagConstraints.weightx = 1.0;
158: gridBagConstraints.insets = new java.awt.Insets(0, 6, 0, 0);
159: add(valueTextField, gridBagConstraints);
160:
161: valueCheckBox
162: .setText(java.util.ResourceBundle
163: .getBundle(
164: "org.netbeans.modules.visualweb.propertyeditors.Bundle")
165: .getString("EditElementDialog.same"));
166: valueCheckBox.setBorder(javax.swing.BorderFactory
167: .createEmptyBorder(0, 0, 0, 0));
168: valueCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
169: valueCheckBox
170: .addActionListener(new java.awt.event.ActionListener() {
171: public void actionPerformed(
172: java.awt.event.ActionEvent evt) {
173: valueCheckBoxActionPerformed(evt);
174: }
175: });
176:
177: gridBagConstraints = new java.awt.GridBagConstraints();
178: gridBagConstraints.gridx = 2;
179: gridBagConstraints.gridy = 1;
180: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
181: gridBagConstraints.insets = new java.awt.Insets(0, 6, 0, 6);
182: add(valueCheckBox, gridBagConstraints);
183:
184: jPanel1.setLayout(new java.awt.FlowLayout(
185: java.awt.FlowLayout.RIGHT, 6, 6));
186:
187: okButton
188: .setText(java.util.ResourceBundle
189: .getBundle(
190: "org.netbeans.modules.visualweb.propertyeditors.Bundle")
191: .getString("EditElementDialog.ok"));
192: okButton.setActionCommand("ok");
193: okButton.addActionListener(new java.awt.event.ActionListener() {
194: public void actionPerformed(java.awt.event.ActionEvent evt) {
195: okButtonActionPerformed(evt);
196: }
197: });
198:
199: jPanel1.add(okButton);
200:
201: cancelButton
202: .setText(java.util.ResourceBundle
203: .getBundle(
204: "org.netbeans.modules.visualweb.propertyeditors.Bundle")
205: .getString("EditElementDialog.cancel"));
206: cancelButton.setActionCommand("cancel");
207: cancelButton
208: .addActionListener(new java.awt.event.ActionListener() {
209: public void actionPerformed(
210: java.awt.event.ActionEvent evt) {
211: cancelButtonActionPerformed(evt);
212: }
213: });
214:
215: jPanel1.add(cancelButton);
216:
217: gridBagConstraints = new java.awt.GridBagConstraints();
218: gridBagConstraints.gridx = 1;
219: gridBagConstraints.gridy = 2;
220: gridBagConstraints.gridwidth = 2;
221: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
222: gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
223: add(jPanel1, gridBagConstraints);
224:
225: }// </editor-fold>//GEN-END:initComponents
226:
227: private void valueCheckBoxActionPerformed(
228: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_valueCheckBoxActionPerformed
229: if (evt.getSource() == this .valueCheckBox) {
230: if (this .valueCheckBox.getModel().isSelected()) {
231: this .valueTextField.setEnabled(false);
232: this .valueTextField.setText(this .labelTextField
233: .getText());
234: } else {
235: this .valueTextField.setEnabled(true);
236: }
237: }
238: }//GEN-LAST:event_valueCheckBoxActionPerformed
239:
240: private void cancelButtonActionPerformed(
241: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
242: this .element = null;
243: dialog.setVisible(false);
244: }//GEN-LAST:event_cancelButtonActionPerformed
245:
246: private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
247: String label = this .labelTextField.getText().trim();
248: String value = this .valueTextField.getText().trim();
249: if (value.length() == 0) {
250: this .element = null;
251: String message = bundle
252: .getString("EditElementDialog.emptyValueError");
253: JOptionPane.showMessageDialog(dialog, message);
254: } else {
255: this .element = new Element(value, label);
256: dialog.setVisible(false);
257: }
258: }//GEN-LAST:event_okButtonActionPerformed
259:
260: private void labelTextFieldUpdated() {
261: if (this .valueCheckBox.getModel().isSelected())
262: this .valueTextField.setText(this .labelTextField.getText());
263: }
264:
265: // Variables declaration - do not modify//GEN-BEGIN:variables
266: private javax.swing.JButton cancelButton;
267: private javax.swing.JPanel jPanel1;
268: private javax.swing.JLabel labelLabel;
269: private javax.swing.JTextField labelTextField;
270: private javax.swing.JButton okButton;
271: private javax.swing.JCheckBox valueCheckBox;
272: private javax.swing.JLabel valueLabel;
273: private javax.swing.JTextField valueTextField;
274: // End of variables declaration//GEN-END:variables
275:
276: private JDialog dialog;
277:
278: public Element showDialog(JPanel parent) {
279: String domainDisplayName = this .domain.getDisplayName();
280: String dialogTitle;
281: if (domainDisplayName == null)
282: dialogTitle = bundle
283: .getString("EditElementDialog.title.default");
284: else
285: dialogTitle = MessageFormat.format(bundle
286: .getString("EditElementDialog.title"),
287: new String[] { domainDisplayName });
288: this .dialog = new JDialog((Dialog) parent.getRootPane()
289: .getParent(), dialogTitle, true);
290: this .valueCheckBox.getModel().setSelected(true);
291: this .valueTextField.setEnabled(false);
292: if (element != null) {
293: String label = element.getLabel();
294: String value = element.getValue().toString();
295: this .labelTextField.setText(label);
296: this .valueTextField.setText(value);
297: if (!label.equals(value)) {
298: this .valueCheckBox.getModel().setSelected(false);
299: this .valueTextField.setEnabled(true);
300: }
301: }
302: this.dialog.setContentPane(this);
303: this.dialog.getRootPane().setDefaultButton(this.okButton);
304: this.dialog.pack();
305: this.dialog.setLocationRelativeTo(dialog);
306: this.labelTextField.requestFocusInWindow();
307: this.dialog.show();
308: this.dialog.dispose();
309: this.dialog = null;
310: return this.element;
311: }
312: }
|