KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > editors > DateEditor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 // HACK - read the comment at the beginning of the Task class
21

22 // Also see core/src/org/netbeans/beaninfo/editors and
23
// openide/src/org/openide/explorer/propertysheet/editors
24

25 package org.netbeans.modules.tasklist.usertasks.editors;
26
27 import java.util.Date JavaDoc;
28 import java.text.ParseException JavaDoc;
29 import java.text.SimpleDateFormat JavaDoc;
30 import java.awt.Component JavaDoc;
31 import java.beans.FeatureDescriptor JavaDoc;
32 import java.beans.PropertyEditorSupport JavaDoc;
33 import org.netbeans.modules.tasklist.usertasks.DateSelectionPanel;
34 import org.openide.explorer.propertysheet.ExPropertyEditor;
35 import org.openide.explorer.propertysheet.PropertyEnv;
36
37 // bugfix# 9219 for attachEnv() method
38
import org.openide.nodes.Node;
39 import org.openide.util.Exceptions;
40 import org.openide.util.NbBundle;
41  
42
43 /**
44  * A property editor for the Date class.
45  *
46  * @author Tor Norbye
47  * @author Trond Norbye
48  */

49 public class DateEditor extends PropertyEditorSupport JavaDoc
50 implements ExPropertyEditor {
51     private static SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc();
52     
53     // bugfix# 9219 added editable field and isEditable() "getter" to be used
54
// in StringCustomEditor
55
private boolean editable = true;
56
57     /**
58      * gets information if the text in editor should be editable or not
59      *
60      * @return true = editable
61      */

62     public boolean isEditable(){
63         return editable;
64     }
65                 
66     public void setAsText(String JavaDoc s) throws java.lang.IllegalArgumentException JavaDoc {
67         if (s.trim().length() == 0) {
68             setValue(null);
69             return;
70         }
71         try {
72             setValue(format.parse(s));
73         } catch (ParseException JavaDoc e) {
74             String JavaDoc msg = NbBundle.getMessage(DateEditor.class,
75                 "IllegalDateValue", new Object JavaDoc[] {s}); //NOI18N
76
RuntimeException JavaDoc iae = new IllegalArgumentException JavaDoc(msg);
77             Exceptions.attachLocalizedMessage(iae, msg);
78             throw iae;
79         }
80     }
81
82     public String JavaDoc getAsText() {
83         Object JavaDoc val = getValue();
84         if (val instanceof Date JavaDoc) {
85             return format.format((Date JavaDoc) val);
86         } else if (val instanceof Long JavaDoc) {
87             long v = ((Long JavaDoc) val).longValue();
88             if (v == 0)
89                 return ""; // NOI18N
90
else
91                 return format.format(new Date JavaDoc(v));
92         } else {
93             return ""; // NOI18N
94
}
95     }
96
97     public boolean supportsCustomEditor () {
98         return true;
99     }
100
101     public Component JavaDoc getCustomEditor() {
102         Date JavaDoc d;
103         if (getValue() instanceof Date JavaDoc) {
104             d = (Date JavaDoc) getValue();
105         } else if (getValue() instanceof Long JavaDoc) {
106             d = new Date JavaDoc(((Long JavaDoc) getValue()).longValue());
107         } else {
108             d = new Date JavaDoc();
109             setValue(d);
110         }
111     return new DateSelectionPanel(d);
112     }
113
114     public void attachEnv(PropertyEnv env) {
115         FeatureDescriptor JavaDoc desc = env.getFeatureDescriptor();
116         if (desc instanceof Node.Property){
117             Node.Property prop = (Node.Property)desc;
118             editable = prop.canWrite();
119         }
120     }
121 }
122
Popular Tags