KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.ByteArrayInputStream JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.beans.PropertyEditorSupport JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Properties JavaDoc;
30 import org.netbeans.core.UIExceptions;
31 import org.openide.util.NbBundle;
32
33
34 /** A property editor for Properties class.
35 * @author Ian Formanek
36 */

37 public class PropertiesEditor extends PropertyEditorSupport JavaDoc {
38
39     /** Overrides superclass method. */
40     public String JavaDoc getAsText() {
41         Object JavaDoc value = getValue();
42         
43         if(value instanceof Properties JavaDoc) {
44             Properties JavaDoc prop = (Properties JavaDoc)value;
45
46             StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
47             
48             for(Enumeration JavaDoc e = prop.keys(); e.hasMoreElements(); ) {
49                 if(buff.length() > 0) {
50                     buff.append("; "); // NOI18N
51
}
52                 
53                 Object JavaDoc key = e.nextElement();
54                 
55                 buff.append(key).append('=').append(prop.get(key)); // NOI18N
56
}
57             
58             return buff.toString();
59         }
60         
61         return String.valueOf(value); // NOI18N
62
}
63
64     /** Overrides superclass method.
65      * @exception IllegalArgumentException if <code>null</code> value
66      * is passes in or some io problem by converting occured */

67     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
68         try {
69             if(text == null) {
70                 throw new IllegalArgumentException JavaDoc("Inserted value can't be null."); // NOI18N
71
}
72             Properties JavaDoc prop = new Properties JavaDoc();
73             InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(
74                 text.replace(';', '\n').getBytes("ISO8859_1") // NOI18N
75
);
76             prop.load(is);
77             setValue(prop);
78         } catch(IOException JavaDoc ioe) {
79             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc (ioe.getMessage());
80             String JavaDoc msg = ioe.getLocalizedMessage();
81             if (msg == null) {
82                 msg = MessageFormat.format(
83                 NbBundle.getMessage(
84                     PropertiesEditor.class, "FMT_EXC_GENERIC_BAD_VALUE"), new Object JavaDoc[] {text}); //NOI18N
85
}
86             UIExceptions.annotateUser(iae, iae.getMessage(), msg, ioe, new Date JavaDoc());
87             throw iae;
88         }
89     }
90
91     public String JavaDoc getJavaInitializationString () {
92         return null; // does not generate any code
93
}
94
95     public boolean supportsCustomEditor () {
96         return true;
97     }
98
99     public java.awt.Component JavaDoc getCustomEditor () {
100         return new PropertiesCustomEditor (this);
101     }
102
103 }
104
Popular Tags