KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > beaninfo > editors > DataFolderEditor


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 package org.netbeans.beaninfo.editors;
21
22 import java.beans.*;
23 import java.io.File JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import org.netbeans.core.UIExceptions;
26 import org.openide.explorer.propertysheet.ExPropertyEditor;
27 import org.openide.explorer.propertysheet.PropertyEnv;
28
29 import org.openide.loaders.DataFolder;
30 import org.openide.util.NbBundle;
31
32 /**
33  *
34  * @author David Strupl
35  * @version
36  */

37 public class DataFolderEditor extends PropertyEditorSupport implements ExPropertyEditor {
38
39     /** Creates new DataFolderEditor */
40     public DataFolderEditor() {
41     }
42
43     private DataFolderPanel dfPanel;
44
45     PropertyEnv env;
46
47     /**
48     * @return The property value as a human editable string.
49     * <p> Returns null if the value can't be expressed as an editable string.
50     * <p> If a non-null value is returned, then the PropertyEditor should
51     * be prepared to parse that string back in setAsText().
52     */

53     public String JavaDoc getAsText() {
54         DataFolder df = (DataFolder)getValue ();
55         String JavaDoc result;
56         if (df == null) {
57             result = getString ("LAB_DefaultDataFolder"); //NOI18N
58
} else {
59             result = df.getName();
60         }
61         if (result.length() ==0) {
62             result = File.pathSeparator;
63         }
64         return result;
65     }
66
67     /** Set the property value by parsing a given String. May raise
68     * java.lang.IllegalArgumentException if either the String is
69     * badly formatted or if this kind of property can't be expressed
70     * as text.
71     * @param text The string to be parsed.
72     */

73     public void setAsText(String JavaDoc text) {
74         if (text==null ||
75             "".equals(text) ||
76             text.equals(getString ("LAB_DefaultDataFolder")) ||
77             File.pathSeparator.equals(text)) {
78             //XXX Mysterious why a real implementation of setAsText is not here
79
setValue(null);
80         } else {
81             //Reasonable to assume any exceptions from core/jdk editors are legit
82
IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc ();
83             String JavaDoc msg = MessageFormat.format(
84                 NbBundle.getMessage(
85                     DataFolderEditor.class, "FMT_DF_UNKNOWN"), new Object JavaDoc[] {text}); //NOI18N
86
UIExceptions.annotateUser(iae, iae.getMessage(), msg, null,
87                                      new java.util.Date JavaDoc());
88             throw iae;
89         }
90     }
91
92     public boolean supportsCustomEditor () {
93         return true;
94     }
95
96     public java.awt.Component JavaDoc getCustomEditor () {
97         dfPanel = getDFPanel();
98         Object JavaDoc val = getValue();
99         if (val instanceof DataFolder) {
100             dfPanel.setTargetFolder((DataFolder)val);
101         }
102         return dfPanel;
103     }
104
105     /** Calls super.setValue(newValue) and then updates
106      * the look of the associated DataFolderPanel by
107      * providing appropriate node to display.
108      */

109     public void setValue(Object JavaDoc newValue) {
110         Object JavaDoc oldValue = getValue();
111         super.setValue(newValue);
112         DataFolderPanel dfp = getDFPanel();
113         if ((newValue != oldValue)&&(dfp != null) && (newValue instanceof DataFolder)){
114             dfp.setTargetFolder((DataFolder)newValue);
115         }
116
117     }
118
119     /** This method is called from DataFolderPanel, so it is similar to
120      * setValue but does not call DataFolderPanel.setTargetFolder()
121      */

122     void setDataFolder(DataFolder newDf) {
123         super.setValue(newDf);
124     }
125     
126     public DataFolderPanel getDFPanel() {
127         if (dfPanel == null) {
128             dfPanel = new DataFolderPanel(this);
129         }
130         return dfPanel;
131     }
132
133     private static String JavaDoc getString (String JavaDoc s) {
134         return org.openide.util.NbBundle.getBundle (DataFolderEditor.class).getString (s);
135     }
136
137     public void attachEnv(PropertyEnv env) {
138         this.env = env;
139     }
140 }
141
Popular Tags