1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyEditor ; 25 import org.openide.explorer.propertysheet.PropertyEnv; 26 import org.openide.util.NbBundle; 27 import javax.swing.*; 28 import javax.swing.border.*; 29 import javax.swing.text.JTextComponent ; 30 import java.awt.BorderLayout ; 31 36 public class StringCustomEditor extends javax.swing.JPanel implements PropertyChangeListener { 37 38 static final long serialVersionUID =7348579663907322425L; 39 40 boolean oneline=false; 41 String instructions = null; 42 43 private PropertyEnv env; 44 45 private PropertyEditor editor; 46 47 48 55 StringCustomEditor (String value, boolean editable, boolean oneline, String instructions, PropertyEditor editor, PropertyEnv env) { 56 this.oneline = oneline; 57 this.instructions = instructions; 58 this.env = env; 59 this.editor = editor; 60 61 this.env.setState(PropertyEnv.STATE_NEEDS_VALIDATION); 62 this.env.addPropertyChangeListener(this); 63 64 init (value, editable); 65 } 66 67 69 public StringCustomEditor(String s, boolean editable) { 70 init (s, editable); 71 } 72 73 private void init (String s, boolean editable) { 74 setLayout (new java.awt.BorderLayout ()); 75 if (oneline) { 76 textArea = new javax.swing.JTextField (); 77 add (textArea, BorderLayout.CENTER); 78 } else { 79 textAreaScroll = new javax.swing.JScrollPane (); 80 textArea = new javax.swing.JTextArea (); 81 textAreaScroll.setViewportView (textArea); 82 add (textAreaScroll, BorderLayout.CENTER); 83 } 84 textArea.setEditable(editable); 86 87 int from = 0; 88 int to = 0; 89 int ctn = 1; 90 while ((to = s.indexOf ("\n", from)) > 0) { 91 ctn ++; 92 from = to + 1; 93 } 94 95 textArea.setText (s); 96 if (textArea instanceof JTextArea && s.length () < 1024) { 97 ((JTextArea) textArea).setWrapStyleWord( true ); 98 ((JTextArea)textArea).setLineWrap( true ); 99 setPreferredSize (new java.awt.Dimension (500, 300)); 100 } else { 101 textArea.setMinimumSize (new java.awt.Dimension (100, 20)); 102 if (textArea instanceof JTextArea) { 103 setPreferredSize (new java.awt.Dimension (500, 300)); 107 } 108 } 109 110 if ( !editable ) { 111 JTextField hack = new JTextField(); 114 hack.setEditable( false ); 115 textArea.setBackground( hack.getBackground() ); 116 textArea.setForeground( hack.getForeground() ); 117 } 118 119 setBorder (BorderFactory.createEmptyBorder(12,12,0,11)); 120 121 textArea.getAccessibleContext().setAccessibleName(NbBundle.getBundle(StringCustomEditor.class).getString("ACS_TextArea")); if (instructions == null) { 123 textArea.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(StringCustomEditor.class).getString("ACSD_TextArea")); } else { 125 textArea.getAccessibleContext().setAccessibleDescription(instructions); 126 } 127 getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(StringCustomEditor.class).getString("ACSD_CustomStringEditor")); int prefHeight; 131 132 if (s.length () < 1024) { 135 prefHeight = textArea.getPreferredSize().height + 8; 136 } else { 137 prefHeight = ctn * 8; 138 } 139 140 if (instructions != null) { 141 final JTextArea jta = new JTextArea(instructions); 142 jta.setEditable (false); 143 java.awt.Color c = UIManager.getColor("control"); if (c != null) { 145 jta.setBackground (c); 146 } else { 147 jta.setBackground (getBackground()); 148 } 149 jta.setLineWrap(true); 150 jta.setWrapStyleWord(true); 151 jta.setFont (getFont()); 152 add (jta, BorderLayout.NORTH, 0); 153 jta.getAccessibleContext().setAccessibleName( 154 NbBundle.getMessage(StringCustomEditor.class, 155 "ACS_Instructions")); jta.getAccessibleContext().setAccessibleDescription( 157 NbBundle.getMessage(StringCustomEditor.class, 158 "ACSD_Instructions")); prefHeight += jta.getPreferredSize().height; 160 jta.addFocusListener(new java.awt.event.FocusListener () { 162 public void focusGained(java.awt.event.FocusEvent e) { 163 jta.setSelectionStart(0); 164 jta.setSelectionEnd(jta.getText().length()); 165 } 166 public void focusLost(java.awt.event.FocusEvent e) { 167 jta.setSelectionStart(0); 168 jta.setSelectionEnd(0); 169 } 170 }); 171 } 172 if (textArea instanceof JTextField) { 173 setPreferredSize (new java.awt.Dimension (300, 174 prefHeight)); 175 } 176 } 177 178 public void addNotify () { 179 super.addNotify(); 180 if (isEnabled() && isFocusable()) { 182 textArea.requestFocus(); 183 } 184 } 185 186 191 private Object getPropertyValue () throws IllegalStateException { 192 return textArea.getText (); 193 } 194 195 196 197 public void propertyChange(PropertyChangeEvent evt) { 198 if (PropertyEnv.PROP_STATE.equals(evt.getPropertyName()) && evt.getNewValue() == PropertyEnv.STATE_VALID) { 199 editor.setValue(getPropertyValue()); 200 } 201 } 202 203 private javax.swing.JScrollPane textAreaScroll; 204 private JTextComponent textArea; 205 } 206 | Popular Tags |