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.GridBagConstraints;
044: import java.beans.PropertyChangeListener;
045: import java.beans.PropertyChangeSupport;
046: import java.text.DateFormat;
047: import java.text.SimpleDateFormat;
048: import java.util.Date;
049: import javax.swing.JLabel;
050: import javax.swing.JTextField;
051: import javax.swing.SwingConstants;
052: import javax.swing.event.DocumentEvent;
053: import com.sun.rave.designtime.DesignProperty;
054:
055: /**
056: * @author eric
057: *
058: * TODO To change the template for this generated type comment go to
059: * Window - Preferences - Java - Code Generation - Code and Comments
060: */
061: public class DateTimePatternPanel extends AbstractPropertyJPanel {
062:
063: private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
064: this );
065: protected JTextField nowOutputField;
066: protected JTextField patternInputField;
067: protected JTextField sampleInputField;
068: protected JTextField sampleOutputField;
069:
070: /**
071: *
072: */
073: public DateTimePatternPanel(
074: DateTimePatternPropertyEditor propertyEditor,
075: DesignProperty liveProperty) {
076:
077: super (propertyEditor, liveProperty);
078: updateNowOutputField();
079: }
080:
081: /**
082: * Add a PropertyChangeListener to this panel's list of listeners. The
083: * listener will be notified of all property changes. The property editor
084: * that creates this panel <strong>must</strong> register itself as a
085: * property change listener, so that actions taken in the panel are
086: * communicated back to the IDE.
087: *
088: * @param listener The listener to add.
089: */
090: public void addPropertyChangeListener(
091: PropertyChangeListener listener) {
092: propertyChangeSupport.addPropertyChangeListener(listener);
093: }
094:
095: /**
096: * Remove listener from this panel's list of listeners.
097: *
098: * @param listener The listener to remove.
099: */
100: public void removePropertyChangeListener(
101: PropertyChangeListener listener) {
102: propertyChangeSupport.removePropertyChangeListener(listener);
103: }
104:
105: public void documentEvent(DocumentEvent event) {
106:
107: if (initializing) {
108: return;
109: }
110: if (event.getDocument() == patternInputField.getDocument()) {
111: handlePatternInputDocumentEvent(event);
112: }
113: if (event.getDocument() == sampleInputField.getDocument()) {
114: handleSampleInputDocumentEvent(event);
115: }
116: }
117:
118: public DateTimePatternPropertyEditor getDateTimePatternPropertyEditor() {
119:
120: return (DateTimePatternPropertyEditor) getPropertyEditor();
121: }
122:
123: protected String getPattern() {
124: String result;
125:
126: result = patternInputField.getText();
127: if (result == null) {
128: result = ""; //NOI18N
129: }
130: return result;
131: }
132:
133: protected String getSample() {
134: String result;
135:
136: result = sampleInputField.getText();
137: if (result == null) {
138: result = ""; //NOI18N
139: }
140: return result;
141: }
142:
143: String previousPattern;
144:
145: public void handlePatternInputDocumentEvent(DocumentEvent event) {
146: updateNowOutputField();
147: String property = this .getDateTimePatternPropertyEditor()
148: .getDesignProperty().getPropertyDescriptor().getName();
149: this .propertyChangeSupport.firePropertyChange(property,
150: previousPattern, this .getPattern());
151: previousPattern = this .getPattern();
152: }
153:
154: public void handleSampleInputDocumentEvent(DocumentEvent event) {
155: updateSampleOutputField();
156: }
157:
158: public void initializeComponents() {
159: GridBagConstraints gridBagConstraints;
160: JLabel label;
161:
162: setLayout(new java.awt.GridBagLayout());
163:
164: /*
165: * Screen looks something like this
166: * Pattern: [ ]
167: * Sample
168: * In [ ]
169: * Out < >
170: * Now < >
171: */
172:
173: label = new javax.swing.JLabel();
174: label.setHorizontalAlignment(SwingConstants.LEFT);
175: label.setText(BundleHolder.bundle.getMessage("pattern")); //NOI18N
176: gridBagConstraints = new java.awt.GridBagConstraints();
177: gridBagConstraints.gridx = 0;
178: gridBagConstraints.gridy = 0;
179: gridBagConstraints.ipadx = 0;
180: gridBagConstraints.ipady = 0;
181: gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
182: gridBagConstraints.fill = GridBagConstraints.BOTH;
183: add(label, gridBagConstraints);
184:
185: patternInputField = new JTextField();
186: patternInputField.getDocument().addDocumentListener(this );
187: patternInputField.setText(getPropertyEditor().getAsText());
188: patternInputField.getAccessibleContext().setAccessibleName(
189: java.util.ResourceBundle.getBundle(
190: "com/sun/jsfcl/std/property/Bundle").getString(
191: "pattern"));
192: patternInputField.getAccessibleContext()
193: .setAccessibleDescription(
194: java.util.ResourceBundle.getBundle(
195: "com/sun/jsfcl/std/property/Bundle")
196: .getString("input_pattern"));
197: label.setLabelFor(patternInputField);
198: gridBagConstraints = new java.awt.GridBagConstraints();
199: gridBagConstraints.gridx = 1;
200: gridBagConstraints.gridy = 0;
201: gridBagConstraints.gridwidth = 3;
202: gridBagConstraints.ipadx = 280;
203: gridBagConstraints.ipady = 0;
204: gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
205: gridBagConstraints.fill = GridBagConstraints.BOTH;
206: add(patternInputField, gridBagConstraints);
207:
208: /*
209: label = new javax.swing.JLabel();
210: label.setHorizontalAlignment(SwingConstants.RIGHT);
211: label.setText("Now");
212: gridBagConstraints = new java.awt.GridBagConstraints();
213: gridBagConstraints.gridx = 1;
214: gridBagConstraints.gridy = 4;
215: gridBagConstraints.ipadx = 0;
216: gridBagConstraints.ipady = 0;
217: gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
218: gridBagConstraints.fill = GridBagConstraints.BOTH;
219: add(label, gridBagConstraints);
220: */
221: nowOutputField = new JTextField();
222: nowOutputField.setText(""); //NOI18N
223: nowOutputField.setEditable(false);
224: nowOutputField.getAccessibleContext().setAccessibleName(
225: java.util.ResourceBundle.getBundle(
226: "com/sun/jsfcl/std/property/Bundle").getString(
227: "pattern"));
228: nowOutputField.getAccessibleContext().setAccessibleDescription(
229: java.util.ResourceBundle.getBundle(
230: "com/sun/jsfcl/std/property/Bundle").getString(
231: "output_pattern"));
232: gridBagConstraints = new java.awt.GridBagConstraints();
233: gridBagConstraints.gridx = 1;
234: gridBagConstraints.gridy = 1;
235: gridBagConstraints.gridwidth = 3;
236: gridBagConstraints.ipadx = 0;
237: gridBagConstraints.ipady = 0;
238: gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
239: gridBagConstraints.fill = GridBagConstraints.BOTH;
240: add(nowOutputField, gridBagConstraints);
241: /*
242: label = new javax.swing.JLabel();
243: label.setHorizontalAlignment(SwingConstants.LEFT);
244: label.setText("Sample");
245: gridBagConstraints = new java.awt.GridBagConstraints();
246: gridBagConstraints.gridx = 0;
247: gridBagConstraints.gridy = 1;
248: gridBagConstraints.gridwidth = 3;
249: gridBagConstraints.ipadx = 0;
250: gridBagConstraints.ipady = 0;
251: gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
252: gridBagConstraints.fill = GridBagConstraints.BOTH;
253: add(label, gridBagConstraints);
254: */
255: label = new javax.swing.JLabel();
256: label.setHorizontalAlignment(SwingConstants.RIGHT);
257: label.setText(BundleHolder.bundle.getMessage("test")); //NOI18N
258: gridBagConstraints = new java.awt.GridBagConstraints();
259: gridBagConstraints.gridx = 0;
260: gridBagConstraints.gridy = 2;
261: gridBagConstraints.ipadx = 0;
262: gridBagConstraints.ipady = 0;
263: gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
264: gridBagConstraints.fill = GridBagConstraints.BOTH;
265: add(label, gridBagConstraints);
266:
267: sampleInputField = new JTextField();
268: sampleInputField.getDocument().addDocumentListener(this );
269: sampleInputField.setText(""); //NOI18N
270: sampleInputField.getAccessibleContext().setAccessibleName(
271: java.util.ResourceBundle.getBundle(
272: "com/sun/jsfcl/std/property/Bundle").getString(
273: "test"));
274: sampleInputField.getAccessibleContext()
275: .setAccessibleDescription(
276: java.util.ResourceBundle.getBundle(
277: "com/sun/jsfcl/std/property/Bundle")
278: .getString("input_test"));
279: label.setLabelFor(sampleInputField);
280: gridBagConstraints = new java.awt.GridBagConstraints();
281: gridBagConstraints.gridx = 1;
282: gridBagConstraints.gridy = 2;
283: gridBagConstraints.gridwidth = 3;
284: gridBagConstraints.ipadx = 200;
285: gridBagConstraints.ipady = 0;
286: gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
287: gridBagConstraints.fill = GridBagConstraints.BOTH;
288: add(sampleInputField, gridBagConstraints);
289: /*
290: label = new javax.swing.JLabel();
291: label.setHorizontalAlignment(SwingConstants.RIGHT);
292: label.setText("Out");
293: gridBagConstraints = new java.awt.GridBagConstraints();
294: gridBagConstraints.gridx = 1;
295: gridBagConstraints.gridy = 3;
296: gridBagConstraints.ipadx = 0;
297: gridBagConstraints.ipady = 0;
298: gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
299: gridBagConstraints.fill = GridBagConstraints.BOTH;
300: add(label, gridBagConstraints);
301: */
302: sampleOutputField = new JTextField();
303: sampleOutputField.setText(""); //NOI18N
304: sampleOutputField.setEditable(false);
305: sampleOutputField.getAccessibleContext().setAccessibleName(
306: java.util.ResourceBundle.getBundle(
307: "com/sun/jsfcl/std/property/Bundle").getString(
308: "test"));
309: sampleOutputField.getAccessibleContext()
310: .setAccessibleDescription(
311: java.util.ResourceBundle.getBundle(
312: "com/sun/jsfcl/std/property/Bundle")
313: .getString("output_test"));
314: gridBagConstraints = new java.awt.GridBagConstraints();
315: gridBagConstraints.gridx = 1;
316: gridBagConstraints.gridy = 3;
317: gridBagConstraints.gridwidth = 3;
318: gridBagConstraints.ipadx = 0;
319: gridBagConstraints.ipady = 0;
320: gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
321: gridBagConstraints.fill = GridBagConstraints.BOTH;
322: add(sampleOutputField, gridBagConstraints);
323:
324: }
325:
326: protected void updateNowOutputField() {
327: String output;
328:
329: try {
330: String pattern;
331: SimpleDateFormat format;
332: Date date;
333:
334: date = new Date();
335: pattern = getPattern();
336: format = new SimpleDateFormat(pattern);
337: output = format.format(date);
338: } catch (Throwable t) {
339: output = "** ERROR: " + t.getMessage(); //NOI18N
340: }
341: nowOutputField.setText(output);
342: }
343:
344: protected void updateSampleOutputField() {
345: String output;
346:
347: try {
348: String pattern, sample;
349: SimpleDateFormat inputFormat;
350: DateFormat outputFormat;
351: Date date;
352:
353: pattern = getPattern();
354: inputFormat = new SimpleDateFormat(pattern);
355: sample = getSample();
356: date = inputFormat.parse(sample);
357: outputFormat = DateFormat.getDateTimeInstance(
358: DateFormat.FULL, DateFormat.FULL);
359: output = outputFormat.format(date);
360: } catch (Throwable t) {
361: output = "** ERROR: " + t.getMessage(); //NOI18N
362: }
363: sampleOutputField.setText(output);
364: }
365:
366: }
|