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-2006 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:
042: package org.netbeans.modules.vmd.midp.propertyeditors.date;
043:
044: import java.awt.BorderLayout;
045: import java.awt.event.FocusEvent;
046: import java.awt.event.FocusListener;
047: import java.text.DateFormat;
048: import java.text.ParseException;
049: import java.text.SimpleDateFormat;
050: import java.util.Collections;
051: import java.util.Date;
052: import javax.swing.JComponent;
053: import javax.swing.JPanel;
054: import javax.swing.JRadioButton;
055: import javax.swing.JTextField;
056: import javax.swing.event.DocumentEvent;
057: import javax.swing.event.DocumentListener;
058: import org.netbeans.modules.vmd.api.model.DesignComponent;
059: import org.netbeans.modules.vmd.api.model.PropertyValue;
060: import org.netbeans.modules.vmd.api.properties.DesignPropertyEditor;
061: import org.netbeans.modules.vmd.midp.components.MidpTypes;
062: import org.netbeans.modules.vmd.midp.components.items.DateFieldCD;
063: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorElement;
064: import org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorUserCode;
065: import org.openide.awt.Mnemonics;
066: import org.openide.util.NbBundle;
067:
068: /**
069: *
070: * @author Anton Chechel
071: */
072: public final class PropertyEditorDate extends PropertyEditorUserCode
073: implements PropertyEditorElement {
074:
075: private static final DateFormat FORMAT_DATE_TIME = new SimpleDateFormat(
076: "dd.MM.yyyy HH:mm:ss"); // NOI18N
077: private static final DateFormat FORMAT_DATE = new SimpleDateFormat(
078: "dd.MM.yyyy"); // NOI18N
079: private static final DateFormat FORMAT_TIME = new SimpleDateFormat(
080: "HH:mm:ss"); // NOI18N
081:
082: private static final String NON_DATE_TIME_TEXT = NbBundle
083: .getMessage(PropertyEditorDate.class, "MSG_NON_DATE_TIME"); // NOI18N
084: private static final String NON_DATE_TEXT = NbBundle.getMessage(
085: PropertyEditorDate.class, "MSG_NON_DATE"); // NOI18N
086: private static final String NON_TIME_TEXT = NbBundle.getMessage(
087: PropertyEditorDate.class, "MSG_NON_TIME"); // NOI18N
088:
089: private CustomEditor customEditor;
090: private JRadioButton radioButton;
091:
092: private PropertyEditorDate() {
093: super (NbBundle.getMessage(PropertyEditorDate.class,
094: "LBL_DATE_UCLABEL")); // NOI18N
095: initComponents();
096:
097: initElements(Collections
098: .<PropertyEditorElement> singleton(this ));
099: }
100:
101: public static final DesignPropertyEditor createInstance() {
102: return new PropertyEditorDate();
103: }
104:
105: private void initComponents() {
106: radioButton = new JRadioButton();
107: Mnemonics.setLocalizedText(radioButton, NbBundle.getMessage(
108: PropertyEditorDate.class, "LBL_DATE_STR")); // NOI18N
109: customEditor = new CustomEditor();
110: }
111:
112: public JComponent getCustomEditorComponent() {
113: return customEditor;
114: }
115:
116: public JRadioButton getRadioButton() {
117: return radioButton;
118: }
119:
120: public boolean isInitiallySelected() {
121: return false;
122: }
123:
124: public boolean isVerticallyResizable() {
125: return false;
126: }
127:
128: @Override
129: public String getAsText() {
130: String super Text = super .getAsText();
131: if (super Text != null) {
132: return super Text;
133: }
134: return getValueAsText((PropertyValue) super .getValue());
135: }
136:
137: public void setTextForPropertyValue(String text) {
138: saveValue(text);
139: }
140:
141: public String getTextForPropertyValue() {
142: return null;
143: }
144:
145: public void updateState(PropertyValue value) {
146: if (isCurrentValueANull() || value == null) {
147: customEditor.setText(null);
148: } else {
149: customEditor.setText(getValueAsText(value));
150: }
151: radioButton.setSelected(!isCurrentValueAUserCodeType());
152: }
153:
154: private void saveValue(String text) {
155: int inputMode = getInputMode();
156: try {
157: Date date = getFormatter(inputMode).parse(text);
158: super .setValue(MidpTypes.createLongValue(date.getTime()));
159: } catch (ParseException ex) {
160: }
161: }
162:
163: @Override
164: public void customEditorOKButtonPressed() {
165: super .customEditorOKButtonPressed();
166: if (radioButton.isSelected()) {
167: saveValue(customEditor.getText());
168: }
169: }
170:
171: private String getValueAsText(PropertyValue value) {
172: Date date = new Date();
173: Object valueValue = value.getPrimitiveValue();
174: date.setTime((Long) valueValue);
175: int inputMode = getInputMode();
176: return getFormatter(inputMode).format(date);
177: }
178:
179: private DateFormat getFormatter(int inputMode) {
180: if (inputMode == DateFieldCD.VALUE_DATE) {
181: return FORMAT_DATE;
182: } else if (inputMode == DateFieldCD.VALUE_TIME) {
183: return FORMAT_TIME;
184: }
185: return FORMAT_DATE_TIME;
186: }
187:
188: private int getInputMode() {
189: final int[] inputMode = new int[] { DateFieldCD.VALUE_DATE_TIME };
190: if (component != null && component.get() != null) {
191: final DesignComponent _component = component.get();
192: _component.getDocument().getTransactionManager()
193: .readAccess(new Runnable() {
194:
195: public void run() {
196: PropertyValue pv = _component
197: .readProperty(DateFieldCD.PROP_INPUT_MODE);
198: if (pv.getKind() == PropertyValue.Kind.VALUE) {
199: inputMode[0] = MidpTypes.getInteger(pv);
200: }
201: }
202: });
203: }
204: return inputMode[0];
205: }
206:
207: private class CustomEditor extends JPanel implements
208: DocumentListener, FocusListener {
209:
210: private JTextField textField;
211:
212: public CustomEditor() {
213: radioButton.addFocusListener(this );
214: initComponents();
215: }
216:
217: private void initComponents() {
218: setLayout(new BorderLayout());
219: textField = new JTextField();
220: textField.getDocument().addDocumentListener(this );
221: textField.addFocusListener(this );
222: add(textField, BorderLayout.CENTER);
223: }
224:
225: public void setText(String text) {
226: textField.setText(text);
227: }
228:
229: public String getText() {
230: return textField.getText();
231: }
232:
233: public void checkDateStatus() {
234: int inputMode = getInputMode();
235: try {
236: getFormatter(inputMode).parse(textField.getText());
237: clearErrorStatus();
238: } catch (ParseException e) {
239: displayWarning(getMessage(inputMode));
240: }
241: }
242:
243: private String getMessage(int inputMode) {
244: if (inputMode == DateFieldCD.VALUE_DATE) {
245: return NON_DATE_TEXT;
246: } else if (inputMode == DateFieldCD.VALUE_TIME) {
247: return NON_TIME_TEXT;
248: }
249: return NON_DATE_TIME_TEXT;
250: }
251:
252: public void insertUpdate(DocumentEvent evt) {
253: if (textField.hasFocus()) {
254: radioButton.setSelected(true);
255: checkDateStatus();
256: }
257: }
258:
259: public void removeUpdate(DocumentEvent evt) {
260: if (textField.hasFocus()) {
261: radioButton.setSelected(true);
262: checkDateStatus();
263: }
264: }
265:
266: public void changedUpdate(DocumentEvent evt) {
267: }
268:
269: public void focusGained(FocusEvent e) {
270: if (e.getSource() == radioButton
271: || e.getSource() == textField) {
272: checkDateStatus();
273: }
274: }
275:
276: public void focusLost(FocusEvent e) {
277: clearErrorStatus();
278: }
279: }
280: }
|