1 19 20 package org.netbeans.modules.options.indentation; 21 22 import java.awt.Component ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.io.BufferedReader ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.InputStreamReader ; 29 import javax.swing.AbstractButton ; 30 import javax.swing.JLabel ; 31 import javax.swing.JPanel ; 32 import javax.swing.SpinnerNumberModel ; 33 import javax.swing.SwingUtilities ; 34 import javax.swing.border.EtchedBorder ; 35 import javax.swing.event.ChangeEvent ; 36 import javax.swing.event.ChangeListener ; 37 import javax.swing.text.BadLocationException ; 38 import javax.swing.text.Document ; 39 import org.netbeans.editor.BaseDocument; 40 import org.netbeans.spi.options.OptionsPanelController; 41 import org.openide.awt.Mnemonics; 42 import org.openide.util.NbBundle; 43 44 45 50 public class IndentationPanel extends JPanel implements ChangeListener , 51 ActionListener { 52 53 private IndentationModel model; 54 private String originalText; 55 private boolean listen = false; 56 private boolean changed = false; 57 58 59 62 public IndentationPanel () { 63 initComponents (); 64 65 loc (lStatementContinuationIndent, "Statement_Indent"); 67 loc (lNumberOfSpacesPerIndent, "Indent"); 68 loc (lPreview, "Preview"); 69 loc (cbExpandTabsToSpaces, "Expand_Tabs"); 70 loc (cbAddLeadingStarInComments, "Add_Leading_Star"); 71 loc (cbAddNewLineBeforeBrace, "Add_New_Line"); 72 loc (cbAddSpaceBeforeParenthesis, "Add_Space"); 73 epPreview.getAccessibleContext ().setAccessibleName (loc ("AN_Preview")); 74 epPreview.getAccessibleContext ().setAccessibleDescription (loc ("AD_Preview")); 75 76 epPreview.setBorder (new EtchedBorder ()); 78 cbAddNewLineBeforeBrace.addActionListener (this); 79 cbAddLeadingStarInComments.addActionListener (this); 80 cbExpandTabsToSpaces.addActionListener (this); 81 cbAddSpaceBeforeParenthesis.addActionListener (this); 82 sStatementContinuationIndent.setModel (new SpinnerNumberModel (8, 1, 50, 1)); 83 sStatementContinuationIndent.addChangeListener (this); 84 sNumberOfSpacesPerIndent.setModel (new SpinnerNumberModel (4, 1, 50, 1)); 85 sNumberOfSpacesPerIndent.addChangeListener (this); 86 epPreview.setEnabled (false); 87 } 88 89 94 private void initComponents() { 96 lStatementContinuationIndent = new javax.swing.JLabel (); 97 sStatementContinuationIndent = new javax.swing.JSpinner (); 98 lNumberOfSpacesPerIndent = new javax.swing.JLabel (); 99 sNumberOfSpacesPerIndent = new javax.swing.JSpinner (); 100 cbExpandTabsToSpaces = new javax.swing.JCheckBox (); 101 cbAddLeadingStarInComments = new javax.swing.JCheckBox (); 102 cbAddNewLineBeforeBrace = new javax.swing.JCheckBox (); 103 cbAddSpaceBeforeParenthesis = new javax.swing.JCheckBox (); 104 lPreview = new javax.swing.JLabel (); 105 jScrollPane1 = new javax.swing.JScrollPane (); 106 epPreview = new javax.swing.JEditorPane (); 107 108 lStatementContinuationIndent.setLabelFor(sStatementContinuationIndent); 109 lStatementContinuationIndent.setText("Statement Continuation Indent:"); 110 111 lNumberOfSpacesPerIndent.setLabelFor(sNumberOfSpacesPerIndent); 112 lNumberOfSpacesPerIndent.setText("Number of Spaces per Indent:"); 113 114 cbExpandTabsToSpaces.setText("Expand Tabs to Spaces"); 115 cbExpandTabsToSpaces.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 116 cbExpandTabsToSpaces.setMargin(new java.awt.Insets (0, 0, 0, 0)); 117 118 cbAddLeadingStarInComments.setText("Add Leading Star in Comments"); 119 cbAddLeadingStarInComments.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 120 cbAddLeadingStarInComments.setMargin(new java.awt.Insets (0, 0, 0, 0)); 121 122 cbAddNewLineBeforeBrace.setText("Add New Line Before Brace"); 123 cbAddNewLineBeforeBrace.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 124 cbAddNewLineBeforeBrace.setMargin(new java.awt.Insets (0, 0, 0, 0)); 125 126 cbAddSpaceBeforeParenthesis.setText("Add Space Before Parenthesis"); 127 cbAddSpaceBeforeParenthesis.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 128 cbAddSpaceBeforeParenthesis.setMargin(new java.awt.Insets (0, 0, 0, 0)); 129 130 lPreview.setText("Preview:"); 131 132 jScrollPane1.setViewportView(epPreview); 133 134 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 135 this.setLayout(layout); 136 layout.setHorizontalGroup( 137 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 138 .add(layout.createSequentialGroup() 139 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 140 .add(lPreview) 141 .add(lNumberOfSpacesPerIndent) 142 .add(lStatementContinuationIndent)) 143 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 144 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 145 .add(sStatementContinuationIndent, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 146 .add(sNumberOfSpacesPerIndent, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 147 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 148 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 149 .add(cbExpandTabsToSpaces) 150 .add(cbAddLeadingStarInComments) 151 .add(cbAddNewLineBeforeBrace) 152 .add(cbAddSpaceBeforeParenthesis)) 153 .addContainerGap(49, Short.MAX_VALUE)) 154 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE) 155 ); 156 layout.setVerticalGroup( 157 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 158 .add(layout.createSequentialGroup() 159 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 160 .add(lStatementContinuationIndent) 161 .add(sStatementContinuationIndent, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 162 .add(cbExpandTabsToSpaces)) 163 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 164 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 165 .add(lNumberOfSpacesPerIndent) 166 .add(sNumberOfSpacesPerIndent, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 167 .add(cbAddLeadingStarInComments)) 168 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 169 .add(cbAddNewLineBeforeBrace) 170 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 171 .add(cbAddSpaceBeforeParenthesis) 172 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 173 .add(lPreview) 174 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 175 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)) 176 ); 177 } 179 180 private javax.swing.JCheckBox cbAddLeadingStarInComments; 182 private javax.swing.JCheckBox cbAddNewLineBeforeBrace; 183 private javax.swing.JCheckBox cbAddSpaceBeforeParenthesis; 184 private javax.swing.JCheckBox cbExpandTabsToSpaces; 185 private javax.swing.JEditorPane epPreview; 186 private javax.swing.JScrollPane jScrollPane1; 187 private javax.swing.JLabel lNumberOfSpacesPerIndent; 188 private javax.swing.JLabel lPreview; 189 private javax.swing.JLabel lStatementContinuationIndent; 190 private javax.swing.JSpinner sNumberOfSpacesPerIndent; 191 private javax.swing.JSpinner sStatementContinuationIndent; 192 194 195 private static String loc (String key) { 196 return NbBundle.getMessage (IndentationPanel.class, key); 197 } 198 199 private static void loc (Component c, String key) { 200 if (!(c instanceof JLabel )) { 201 c.getAccessibleContext ().setAccessibleName (loc ("AN_" + key)); 202 c.getAccessibleContext ().setAccessibleDescription (loc ("AD_" + key)); 203 } 204 if (c instanceof AbstractButton ) { 205 Mnemonics.setLocalizedText ( 206 (AbstractButton ) c, 207 loc ("CTL_" + key) 208 ); 209 } else { 210 Mnemonics.setLocalizedText ( 211 (JLabel ) c, 212 loc ("CTL_" + key) 213 ); 214 } 215 } 216 217 private void updatePreview () { 218 model.setJavaFormatLeadingStarInComment (cbAddLeadingStarInComments.isSelected ()); 219 model.setJavaFormatNewlineBeforeBrace (cbAddNewLineBeforeBrace.isSelected ()); 220 model.setJavaFormatSpaceBeforeParenthesis (cbAddSpaceBeforeParenthesis.isSelected ()); 221 model.setExpandTabs (cbExpandTabsToSpaces.isSelected ()); 222 model.setJavaFormatStatementContinuationIndent ( 223 (Integer ) sStatementContinuationIndent.getValue () 224 ); 225 model.setSpacesPerTab ( 226 (Integer ) sNumberOfSpacesPerIndent.getValue () 227 ); 228 229 SwingUtilities.invokeLater (new Runnable () { 231 public void run () { 232 epPreview.setText (originalText); 233 Document doc = epPreview.getDocument (); 234 if (doc instanceof BaseDocument) 235 try { 236 ((BaseDocument) doc).getFormatter ().reformat ( 237 (BaseDocument) doc, 238 0, 239 ((BaseDocument) doc).getEndPosition ().getOffset () - 1 240 ); 241 } catch (BadLocationException ex) { 242 ex.printStackTrace (); 243 } 244 } 245 }); 246 } 247 248 249 251 public void stateChanged (ChangeEvent e) { 252 if (!listen) return; 253 updatePreview (); 254 if (changed != model.isChanged ()) 255 firePropertyChange ( 256 OptionsPanelController.PROP_CHANGED, 257 Boolean.valueOf (changed), 258 Boolean.valueOf (model.isChanged ()) 259 ); 260 changed = model.isChanged (); 261 } 262 263 public void actionPerformed (ActionEvent e) { 264 if (!listen) return; 265 updatePreview (); 266 if (changed != model.isChanged ()) 267 firePropertyChange ( 268 OptionsPanelController.PROP_CHANGED, 269 Boolean.valueOf (changed), 270 Boolean.valueOf (model.isChanged ()) 271 ); 272 changed = model.isChanged (); 273 } 274 275 public void update () { 276 model = new IndentationModel (); 277 278 if (originalText == null) { 279 InputStream is = getClass ().getResourceAsStream 281 ("/org/netbeans/modules/options/indentation/indentationExample"); 282 BufferedReader r = new BufferedReader (new InputStreamReader (is)); 283 StringBuffer sb = new StringBuffer (); 284 try { 285 String line = r.readLine (); 286 while (line != null) { 287 sb.append (line).append ('\n'); 288 line = r.readLine (); 289 } 290 originalText = new String (sb); 291 } catch (IOException ex) { 292 ex.printStackTrace (); 293 } 294 } 295 296 listen = false; 298 SwingUtilities.invokeLater (new Runnable () { 299 public void run () { 300 epPreview.setContentType ("text/x-java"); 301 cbExpandTabsToSpaces.setSelected (model.isExpandTabs ()); 302 cbAddLeadingStarInComments.setSelected 303 (model.getJavaFormatLeadingStarInComment ()); 304 cbAddNewLineBeforeBrace.setSelected 305 (model.getJavaFormatNewlineBeforeBrace ()); 306 cbAddSpaceBeforeParenthesis.setSelected 307 (model.getJavaFormatSpaceBeforeParenthesis ()); 308 sNumberOfSpacesPerIndent.setValue (model.getSpacesPerTab ()); 309 sStatementContinuationIndent.setValue 310 (model.getJavaFormatStatementContinuationIndent ()); 311 listen = true; 312 313 updatePreview (); 315 } 316 }); 317 } 318 319 public void applyChanges () { 320 if (model != null) 321 model.applyChanges (); 322 } 323 324 public void cancel () { 325 if (model != null) 326 model.revertChanges (); 327 } 328 329 public boolean dataValid () { 330 return true; 331 } 332 333 public boolean isChanged () { 334 if (model == null) return false; 335 return model.isChanged (); 336 } 337 } 338 | Popular Tags |