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.beaninfo.editors;
043:
044: import java.awt.BorderLayout;
045: import java.awt.Color;
046: import java.awt.Dimension;
047: import java.io.ByteArrayInputStream;
048: import java.io.ByteArrayOutputStream;
049: import java.io.IOException;
050: import java.io.UnsupportedEncodingException;
051: import java.util.Properties;
052: import java.util.regex.Pattern;
053: import javax.swing.JEditorPane;
054: import javax.swing.JPanel;
055: import javax.swing.JScrollPane;
056: import javax.swing.JTextField;
057: import javax.swing.UIManager;
058: import javax.swing.event.DocumentEvent;
059: import javax.swing.event.DocumentListener;
060: import org.openide.util.HelpCtx;
061: import org.openide.util.NbBundle;
062:
063: /**
064: * A custom editor for Properties.
065: */
066: public class PropertiesCustomEditor extends JPanel implements
067: DocumentListener {
068:
069: private PropertiesEditor editor;
070: private JEditorPane editorPane;
071: private JTextField warnings;
072:
073: public PropertiesCustomEditor(PropertiesEditor ed) {
074: editor = ed;
075: initComponents();
076: Properties props = (Properties) editor.getValue();
077: if (props == null)
078: props = new Properties();
079: ByteArrayOutputStream baos = new ByteArrayOutputStream();
080: try {
081: props.store(baos, ""); // NOI18N
082: } catch (IOException e) {
083: throw new AssertionError(e);
084: }
085: try {
086: // Remove all comments from text.
087: editorPane.setText(baos.toString("ISO-8859-1").replaceAll(
088: "(?m)^#.*" + System.getProperty("line.separator"),
089: "")); // NOI18N
090: } catch (UnsupportedEncodingException x) {
091: throw new AssertionError(x);
092: }
093: HelpCtx.setHelpIDString(this , PropertiesCustomEditor.class
094: .getName());
095:
096: editorPane.getAccessibleContext().setAccessibleName(
097: NbBundle.getBundle(PropertiesCustomEditor.class)
098: .getString("ACS_PropertiesEditorPane"));
099: editorPane.getAccessibleContext().setAccessibleDescription(
100: NbBundle.getBundle(PropertiesCustomEditor.class)
101: .getString("ACSD_PropertiesEditorPane"));
102: editorPane.getDocument().addDocumentListener(this );
103: getAccessibleContext().setAccessibleDescription(
104: NbBundle.getBundle(PropertiesCustomEditor.class)
105: .getString("ACSD_CustomPropertiesEditor"));
106: }
107:
108: public void insertUpdate(DocumentEvent e) {
109: change();
110: }
111:
112: public void removeUpdate(DocumentEvent e) {
113: change();
114: }
115:
116: public void changedUpdate(DocumentEvent e) {
117: }
118:
119: private void change() {
120: Properties v = new Properties();
121: boolean loaded = false;
122: try {
123: v.load(new ByteArrayInputStream(editorPane.getText()
124: .getBytes("ISO-8859-1")));
125: loaded = true;
126: } catch (Exception x) { // IOException, IllegalArgumentException, maybe others
127: Color c = UIManager.getColor("nb.errorForeground"); // NOI18N
128: if (c != null) {
129: warnings.setForeground(c);
130: }
131: warnings.setText(x.toString());
132: }
133: if (loaded) {
134: editor.setValue(v);
135: if (Pattern.compile("^#", Pattern.MULTILINE).matcher(
136: editorPane.getText()).find()) { // #20996
137: Color c = UIManager.getColor("nb.warningForeground"); // NOI18N
138: if (c != null) {
139: warnings.setForeground(c);
140: }
141: warnings.setText(NbBundle.getMessage(
142: PropertiesCustomEditor.class,
143: "WARN_PropertiesComments"));
144: } else {
145: warnings.setText(null);
146: }
147: }
148: }
149:
150: public @Override
151: Dimension getPreferredSize() {
152: return new Dimension(600, 400);
153: }
154:
155: /** This method is called from within the constructor to
156: * initialize the form.
157: */
158: private void initComponents() {
159: setLayout(new BorderLayout());
160:
161: editorPane = new JEditorPane();
162: editorPane.setContentType("text/x-properties"); // NOI18N
163: // XXX pretty arbitrary! No way to set by rows & columns??
164: editorPane.setPreferredSize(new Dimension(200, 100));
165: add(new JScrollPane(editorPane), BorderLayout.CENTER);
166:
167: warnings = new JTextField(30);
168: warnings.setEditable(false);
169: add(warnings, BorderLayout.SOUTH);
170: }
171: }
|