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