1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Color ; 24 import java.awt.Dimension ; 25 import java.awt.Insets ; 26 import java.io.ByteArrayInputStream ; 27 import java.io.ByteArrayOutputStream ; 28 import java.io.IOException ; 29 import java.io.UnsupportedEncodingException ; 30 import java.util.Properties ; 31 import java.util.regex.Pattern ; 32 import javax.swing.JEditorPane ; 33 import javax.swing.JPanel ; 34 import javax.swing.JScrollPane ; 35 import javax.swing.JTextField ; 36 import javax.swing.UIManager ; 37 import javax.swing.border.EmptyBorder ; 38 import javax.swing.event.DocumentEvent ; 39 import javax.swing.event.DocumentListener ; 40 import org.openide.util.HelpCtx; 41 import org.openide.util.NbBundle; 42 43 46 public class PropertiesCustomEditor extends JPanel implements DocumentListener { 47 48 private PropertiesEditor editor; 49 private JEditorPane editorPane; 50 private JTextField warnings; 51 52 public PropertiesCustomEditor(PropertiesEditor ed) { 53 editor = ed; 54 initComponents (); 55 Properties props = (Properties ) editor.getValue (); 56 if (props == null) props = new Properties (); 57 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 58 try { 59 props.store (baos, ""); } catch (IOException e) { 61 throw new AssertionError (e); 62 } 63 try { 64 editorPane.setText(baos.toString("ISO-8859-1").replaceAll("(?m)^#.*" + System.getProperty("line.separator"), "")); } catch (UnsupportedEncodingException x) { 67 throw new AssertionError (x); 68 } 69 setBorder (new EmptyBorder (new Insets (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 e) { 79 change(); 80 } 81 82 public void removeUpdate(DocumentEvent e) { 83 change(); 84 } 85 86 public void changedUpdate(DocumentEvent e) {} 87 88 private void change() { 89 Properties v = new Properties (); 90 boolean loaded = false; 91 try { 92 v.load(new ByteArrayInputStream (editorPane.getText().getBytes("ISO-8859-1"))); 93 loaded = true; 94 } catch (Exception x) { Color c = UIManager.getColor("nb.errorForeground"); 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()) { Color c = UIManager.getColor("nb.warningForeground"); 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 getPreferredSize() { 116 return new Dimension (600, 400); 117 } 118 119 122 private void initComponents() { 123 setLayout(new BorderLayout ()); 124 125 editorPane = new JEditorPane (); 126 editorPane.setContentType("text/x-properties"); add(new JScrollPane (editorPane), BorderLayout.CENTER); 128 129 warnings = new JTextField (30); 130 warnings.setEditable(false); 131 add(warnings, BorderLayout.SOUTH); 132 } 133 } 134 | Popular Tags |