KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > indentation > IndentationPanel


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.modules.options.indentation;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.io.BufferedReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.InputStreamReader JavaDoc;
29 import javax.swing.AbstractButton JavaDoc;
30 import javax.swing.JLabel JavaDoc;
31 import javax.swing.JPanel JavaDoc;
32 import javax.swing.SpinnerNumberModel JavaDoc;
33 import javax.swing.SwingUtilities JavaDoc;
34 import javax.swing.border.EtchedBorder JavaDoc;
35 import javax.swing.event.ChangeEvent JavaDoc;
36 import javax.swing.event.ChangeListener JavaDoc;
37 import javax.swing.text.BadLocationException JavaDoc;
38 import javax.swing.text.Document JavaDoc;
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 /**
46  * Implementation of one panel in Options Dialog.
47  *
48  * @author Jan Jancura
49  */

50 public class IndentationPanel extends JPanel JavaDoc implements ChangeListener JavaDoc,
51 ActionListener JavaDoc {
52     
53     private IndentationModel model;
54     private String JavaDoc originalText;
55     private boolean listen = false;
56     private boolean changed = false;
57
58     
59     /**
60      * Creates new form IndentationPanel.
61      */

62     public IndentationPanel () {
63         initComponents ();
64         
65         // localization
66
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         //listeners
77
epPreview.setBorder (new EtchedBorder JavaDoc ());
78         cbAddNewLineBeforeBrace.addActionListener (this);
79         cbAddLeadingStarInComments.addActionListener (this);
80         cbExpandTabsToSpaces.addActionListener (this);
81         cbAddSpaceBeforeParenthesis.addActionListener (this);
82         sStatementContinuationIndent.setModel (new SpinnerNumberModel JavaDoc (8, 1, 50, 1));
83         sStatementContinuationIndent.addChangeListener (this);
84         sNumberOfSpacesPerIndent.setModel (new SpinnerNumberModel JavaDoc (4, 1, 50, 1));
85         sNumberOfSpacesPerIndent.addChangeListener (this);
86         epPreview.setEnabled (false);
87     }
88     
89     /** This method is called from within the constructor to
90      * initialize the form.
91      * WARNING: Do NOT modify this code. The content of this method is
92      * always regenerated by the Form Editor.
93      */

94     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
95
private void initComponents() {
96         lStatementContinuationIndent = new javax.swing.JLabel JavaDoc();
97         sStatementContinuationIndent = new javax.swing.JSpinner JavaDoc();
98         lNumberOfSpacesPerIndent = new javax.swing.JLabel JavaDoc();
99         sNumberOfSpacesPerIndent = new javax.swing.JSpinner JavaDoc();
100         cbExpandTabsToSpaces = new javax.swing.JCheckBox JavaDoc();
101         cbAddLeadingStarInComments = new javax.swing.JCheckBox JavaDoc();
102         cbAddNewLineBeforeBrace = new javax.swing.JCheckBox JavaDoc();
103         cbAddSpaceBeforeParenthesis = new javax.swing.JCheckBox JavaDoc();
104         lPreview = new javax.swing.JLabel JavaDoc();
105         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
106         epPreview = new javax.swing.JEditorPane JavaDoc();
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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc(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     }// </editor-fold>//GEN-END:initComponents
178

179     
180     // Variables declaration - do not modify//GEN-BEGIN:variables
181
private javax.swing.JCheckBox JavaDoc cbAddLeadingStarInComments;
182     private javax.swing.JCheckBox JavaDoc cbAddNewLineBeforeBrace;
183     private javax.swing.JCheckBox JavaDoc cbAddSpaceBeforeParenthesis;
184     private javax.swing.JCheckBox JavaDoc cbExpandTabsToSpaces;
185     private javax.swing.JEditorPane JavaDoc epPreview;
186     private javax.swing.JScrollPane JavaDoc jScrollPane1;
187     private javax.swing.JLabel JavaDoc lNumberOfSpacesPerIndent;
188     private javax.swing.JLabel JavaDoc lPreview;
189     private javax.swing.JLabel JavaDoc lStatementContinuationIndent;
190     private javax.swing.JSpinner JavaDoc sNumberOfSpacesPerIndent;
191     private javax.swing.JSpinner JavaDoc sStatementContinuationIndent;
192     // End of variables declaration//GEN-END:variables
193

194     
195     private static String JavaDoc loc (String JavaDoc key) {
196         return NbBundle.getMessage (IndentationPanel.class, key);
197     }
198     
199     private static void loc (Component JavaDoc c, String JavaDoc key) {
200         if (!(c instanceof JLabel JavaDoc)) {
201             c.getAccessibleContext ().setAccessibleName (loc ("AN_" + key));
202             c.getAccessibleContext ().setAccessibleDescription (loc ("AD_" + key));
203         }
204         if (c instanceof AbstractButton JavaDoc) {
205             Mnemonics.setLocalizedText (
206                 (AbstractButton JavaDoc) c,
207                 loc ("CTL_" + key)
208             );
209         } else {
210             Mnemonics.setLocalizedText (
211                 (JLabel JavaDoc) 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 JavaDoc) sStatementContinuationIndent.getValue ()
224         );
225         model.setSpacesPerTab (
226             (Integer JavaDoc) sNumberOfSpacesPerIndent.getValue ()
227         );
228         
229         // start formatter
230
SwingUtilities.invokeLater (new Runnable JavaDoc () {
231             public void run () {
232                 epPreview.setText (originalText);
233                 Document JavaDoc 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 JavaDoc ex) {
242                     ex.printStackTrace ();
243                 }
244             }
245         });
246     }
247     
248     
249     // ActionListener ..........................................................
250

251     public void stateChanged (ChangeEvent JavaDoc 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 JavaDoc 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             // add text to preview
280
InputStream JavaDoc is = getClass ().getResourceAsStream
281                 ("/org/netbeans/modules/options/indentation/indentationExample");
282             BufferedReader JavaDoc r = new BufferedReader JavaDoc (new InputStreamReader JavaDoc (is));
283             StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
284             try {
285                 String JavaDoc line = r.readLine ();
286                 while (line != null) {
287                     sb.append (line).append ('\n');
288                     line = r.readLine ();
289                 }
290                 originalText = new String JavaDoc (sb);
291             } catch (IOException JavaDoc ex) {
292                 ex.printStackTrace ();
293             }
294         }
295         
296         // init components
297
listen = false;
298         SwingUtilities.invokeLater (new Runnable JavaDoc () {
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                 // update preview
314
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