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 com.sun.jsfcl.std.property;
042:
043: import java.awt.Dialog;
044: import java.awt.Dimension;
045: import java.awt.GridBagConstraints;
046: import java.awt.event.ActionEvent;
047: import java.awt.event.ActionListener;
048: import javax.swing.Box;
049: import javax.swing.BoxLayout;
050: import javax.swing.JButton;
051: import javax.swing.JCheckBox;
052: import javax.swing.JDialog;
053: import javax.swing.JLabel;
054: import javax.swing.JOptionPane;
055: import javax.swing.JPanel;
056: import javax.swing.JTextField;
057: import javax.swing.event.DocumentEvent;
058: import javax.swing.event.DocumentListener;
059: import javax.swing.event.UndoableEditEvent;
060: import javax.swing.event.UndoableEditListener;
061: import com.sun.jsfcl.std.reference.CompositeReferenceData;
062: import com.sun.jsfcl.std.reference.ReferenceDataItem;
063:
064: /**
065: * @author eric
066: *
067: * TODO To change the template for this generated type comment go to
068: * Window - Preferences - Java - Code Style - Code Templates
069: */
070: public class NewReferenceDataItemDialog extends JPanel implements
071: DocumentListener, UndoableEditListener, ActionListener {
072:
073: protected JButton cancelButton;
074: protected JDialog dialog;
075: protected JTextField nameTextField;
076: protected JButton okButton;
077: protected CompositeReferenceData referenceData;
078: protected JCheckBox sameCheckBox;
079: protected JTextField valueTextField;
080: protected ReferenceDataItem newItem;
081:
082: public NewReferenceDataItemDialog(
083: CompositeReferenceData referenceData) {
084:
085: this .referenceData = referenceData;
086: initializeComponents();
087: }
088:
089: public void actionPerformed(ActionEvent event) {
090:
091: Object source = event.getSource();
092: if (source == sameCheckBox) {
093: if (isValueSameAsName()) {
094: valueTextField.setEditable(false);
095: } else {
096: valueTextField.setEditable(true);
097: valueTextField.selectAll();
098: valueTextField.requestFocusInWindow();
099: }
100: handleNameTextChanged();
101: } else if (source == okButton) {
102: handleOkButton();
103: } else if (source == cancelButton) {
104: if (dialog != null) {
105: dialog.setVisible(false);
106: }
107: }
108: }
109:
110: public void changedUpdate(DocumentEvent e) {
111:
112: if (e.getDocument() == nameTextField.getDocument()) {
113: handleNameTextChanged();
114: } else if (e.getDocument() == valueTextField.getDocument()) {
115: handleValueTextChanged();
116: }
117: }
118:
119: protected void addDialogButtons() {
120:
121: JPanel buttonPanel = new JPanel();
122: buttonPanel.setLayout(new BoxLayout(buttonPanel,
123: BoxLayout.LINE_AXIS));
124: okButton = new JButton(BundleHolder.bundle.getMessage("OK")); //NOI18N
125: okButton.addActionListener(this );
126: buttonPanel.add(okButton);
127: buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
128: cancelButton = new JButton(BundleHolder.bundle
129: .getMessage("Cancel")); //NOI18N
130: cancelButton.addActionListener(this );
131: buttonPanel.add(cancelButton);
132: GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
133: gridBagConstraints.gridx = 1;
134: gridBagConstraints.gridy = 2;
135: gridBagConstraints.gridwidth = 2;
136: gridBagConstraints.anchor = GridBagConstraints.LINE_END;
137: gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
138: add(buttonPanel, gridBagConstraints);
139: }
140:
141: public void handleNameTextChanged() {
142:
143: if (isValueSameAsName()) {
144: valueTextField.setText(nameTextField.getText());
145: }
146: }
147:
148: public void handleValueTextChanged() {
149:
150: }
151:
152: protected void handleOkButton() {
153:
154: String name = nameTextField.getText().trim();
155: String value = valueTextField.getText().trim();
156: newItem = referenceData.getDefiner().newItem(name, value, null,
157: false, true);
158: newItem.setIsRemovable(true);
159: String message;
160: if (value.length() == 0) {
161: message = BundleHolder.bundle
162: .getMessage("newReferenceDataItemEmptyValueError"); //NOI18N
163: } else if (referenceData.getItems().indexOf(newItem) >= 0) {
164: message = BundleHolder.bundle.getMessage(
165: "newReferenceDataItemAlreadyExistError", value); //NOI18N
166: } else {
167: message = null;
168: }
169: if (message == null) {
170: referenceData.add(newItem);
171: dialog.setVisible(false);
172: } else {
173: newItem = null;
174: JOptionPane.showMessageDialog(dialog, message);
175: }
176: }
177:
178: protected void initializeComponents() {
179: GridBagConstraints gridBagConstraints;
180:
181: setLayout(new java.awt.GridBagLayout());
182:
183: JLabel label = new JLabel();
184: label.setText(BundleHolder.bundle
185: .getMessage("newReferenceDataItemName")); //NOI18N
186: gridBagConstraints = new java.awt.GridBagConstraints();
187: gridBagConstraints.gridx = 0;
188: gridBagConstraints.gridy = 0;
189: gridBagConstraints.gridwidth = 1;
190: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
191: gridBagConstraints.anchor = GridBagConstraints.PAGE_START;
192: gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 5);
193: add(label, gridBagConstraints);
194:
195: nameTextField = new JTextField();
196: nameTextField.setColumns(20);
197: nameTextField.getDocument().addDocumentListener(this );
198: nameTextField.getDocument().addUndoableEditListener(this );
199: gridBagConstraints = new java.awt.GridBagConstraints();
200: gridBagConstraints.gridx = 1;
201: gridBagConstraints.gridy = 0;
202: gridBagConstraints.gridwidth = 1;
203: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
204: gridBagConstraints.anchor = GridBagConstraints.CENTER;
205: gridBagConstraints.weightx = 1.0;
206: gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
207: add(nameTextField, gridBagConstraints);
208:
209: label = new JLabel();
210: label.setText(BundleHolder.bundle
211: .getMessage("newReferenceDataItemValue")); //NOI18N
212: gridBagConstraints = new java.awt.GridBagConstraints();
213: gridBagConstraints.gridx = 0;
214: gridBagConstraints.gridy = 1;
215: gridBagConstraints.gridwidth = 1;
216: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
217: gridBagConstraints.anchor = GridBagConstraints.PAGE_START;
218: gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 5);
219: add(label, gridBagConstraints);
220:
221: valueTextField = new JTextField();
222: valueTextField.setEditable(false);
223: valueTextField.getDocument().addDocumentListener(this );
224: valueTextField.getDocument().addUndoableEditListener(this );
225: gridBagConstraints = new java.awt.GridBagConstraints();
226: gridBagConstraints.gridx = 1;
227: gridBagConstraints.gridy = 1;
228: gridBagConstraints.gridwidth = 1;
229: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
230: gridBagConstraints.anchor = GridBagConstraints.CENTER;
231: gridBagConstraints.weightx = 1.0;
232: gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
233: add(valueTextField, gridBagConstraints);
234:
235: sameCheckBox = new JCheckBox(BundleHolder.bundle
236: .getMessage("newReferenceDataItemSameCheckbox"), true);
237: sameCheckBox.addActionListener(this );
238: gridBagConstraints = new java.awt.GridBagConstraints();
239: gridBagConstraints.gridx = 2;
240: gridBagConstraints.gridy = 1;
241: gridBagConstraints.gridwidth = 1;
242: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
243: gridBagConstraints.anchor = GridBagConstraints.CENTER;
244: gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 10);
245: add(sameCheckBox, gridBagConstraints);
246:
247: }
248:
249: protected boolean isValueSameAsName() {
250:
251: return sameCheckBox.getModel().isSelected();
252: }
253:
254: public void insertUpdate(DocumentEvent e) {
255:
256: if (e.getDocument() == nameTextField.getDocument()) {
257: handleNameTextChanged();
258: } else if (e.getDocument() == valueTextField.getDocument()) {
259: handleValueTextChanged();
260: }
261: }
262:
263: public void removeUpdate(DocumentEvent e) {
264:
265: if (e.getDocument() == nameTextField.getDocument()) {
266: handleNameTextChanged();
267: } else if (e.getDocument() == valueTextField.getDocument()) {
268: handleValueTextChanged();
269: }
270: }
271:
272: public ReferenceDataItem showDialog(JPanel parent) {
273:
274: addDialogButtons();
275: Dialog parentDialog = (Dialog) parent.getRootPane().getParent();
276: dialog = new JDialog(parentDialog, BundleHolder.bundle
277: .getMessage("newReferenceDataItemPanelTitle",
278: referenceData.getDisplayName()), // NOI18N
279: true);
280: dialog.setContentPane(this );
281: dialog.pack();
282: dialog.setLocationRelativeTo(dialog);
283: nameTextField.requestFocusInWindow();
284: dialog.show();
285: dialog.dispose();
286: dialog = null;
287: okButton = cancelButton = null;
288: return newItem;
289: }
290:
291: public void undoableEditHappened(UndoableEditEvent e) {
292:
293: if (e.getSource() == nameTextField) {
294: handleNameTextChanged();
295: } else if (e.getSource() == valueTextField) {
296: handleValueTextChanged();
297: }
298: }
299:
300: }
|