KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.BorderLayout JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.Insets JavaDoc;
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.ByteArrayOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.UnsupportedEncodingException JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.regex.Pattern JavaDoc;
32 import javax.swing.JEditorPane JavaDoc;
33 import javax.swing.JPanel JavaDoc;
34 import javax.swing.JScrollPane JavaDoc;
35 import javax.swing.JTextField JavaDoc;
36 import javax.swing.UIManager JavaDoc;
37 import javax.swing.border.EmptyBorder JavaDoc;
38 import javax.swing.event.DocumentEvent JavaDoc;
39 import javax.swing.event.DocumentListener JavaDoc;
40 import org.openide.util.HelpCtx;
41 import org.openide.util.NbBundle;
42
43 /**
44  * A custom editor for Properties.
45  */

46 public class PropertiesCustomEditor extends JPanel JavaDoc implements DocumentListener JavaDoc {
47
48     private PropertiesEditor editor;
49     private JEditorPane JavaDoc editorPane;
50     private JTextField JavaDoc warnings;
51     
52     public PropertiesCustomEditor(PropertiesEditor ed) {
53         editor = ed;
54         initComponents ();
55         Properties JavaDoc props = (Properties JavaDoc) editor.getValue ();
56         if (props == null) props = new Properties JavaDoc ();
57         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc ();
58         try {
59             props.store (baos, ""); // NOI18N
60
} catch (IOException JavaDoc e) {
61             throw new AssertionError JavaDoc(e);
62         }
63         try {
64             // Remove all comments from text.
65
editorPane.setText(baos.toString("ISO-8859-1").replaceAll("(?m)^#.*" + System.getProperty("line.separator"), "")); // NOI18N
66
} catch (UnsupportedEncodingException JavaDoc x) {
67             throw new AssertionError JavaDoc(x);
68         }
69         setBorder (new EmptyBorder JavaDoc (new Insets JavaDoc(12, 12, 0, 11)));
70         HelpCtx.setHelpIDString (this, PropertiesCustomEditor.class.getName ());
71         
72         editorPane.getAccessibleContext().setAccessibleName(NbBundle.getBundle(PropertiesCustomEditor.class).getString("ACS_PropertiesEditorPane"));
73         editorPane.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(PropertiesCustomEditor.class).getString("ACSD_PropertiesEditorPane"));
74         editorPane.getDocument().addDocumentListener(this);
75         getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(PropertiesCustomEditor.class).getString("ACSD_CustomPropertiesEditor"));
76     }
77     
78     public void insertUpdate(DocumentEvent JavaDoc e) {
79         change();
80     }
81
82     public void removeUpdate(DocumentEvent JavaDoc e) {
83         change();
84     }
85
86     public void changedUpdate(DocumentEvent JavaDoc e) {}
87
88     private void change() {
89         Properties JavaDoc v = new Properties JavaDoc();
90         boolean loaded = false;
91         try {
92             v.load(new ByteArrayInputStream JavaDoc(editorPane.getText().getBytes("ISO-8859-1")));
93             loaded = true;
94         } catch (Exception JavaDoc x) { // IOException, IllegalArgumentException, maybe others
95
Color JavaDoc c = UIManager.getColor("nb.errorForeground"); // NOI18N
96
if (c != null) {
97                 warnings.setForeground(c);
98             }
99             warnings.setText(x.toString());
100         }
101         if (loaded) {
102             editor.setValue(v);
103             if (Pattern.compile("^#", Pattern.MULTILINE).matcher(editorPane.getText()).find()) { // #20996
104
Color JavaDoc c = UIManager.getColor("nb.warningForeground"); // NOI18N
105
if (c != null) {
106                     warnings.setForeground(c);
107                 }
108                 warnings.setText(NbBundle.getMessage(PropertiesCustomEditor.class, "WARN_PropertiesComments"));
109             } else {
110                 warnings.setText(null);
111             }
112         }
113     }
114
115     public Dimension JavaDoc getPreferredSize() {
116         return new Dimension JavaDoc(600, 400);
117     }
118
119     /** This method is called from within the constructor to
120      * initialize the form.
121      */

122     private void initComponents() {
123         setLayout(new BorderLayout JavaDoc());
124         
125         editorPane = new JEditorPane JavaDoc();
126         editorPane.setContentType("text/x-properties"); // NOI18N
127
add(new JScrollPane JavaDoc(editorPane), BorderLayout.CENTER);
128
129         warnings = new JTextField JavaDoc(30);
130         warnings.setEditable(false);
131         add(warnings, BorderLayout.SOUTH);
132     }
133 }
134
Popular Tags