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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.cnd.editor.options;
041:
042: import java.awt.GridBagConstraints;
043: import java.awt.Rectangle;
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import java.beans.PropertyChangeEvent;
047: import java.beans.PropertyChangeListener;
048: import java.io.IOException;
049: import java.io.InputStreamReader;
050: import java.util.HashMap;
051: import java.util.Map;
052: import java.util.TreeMap;
053: import java.util.prefs.BackingStoreException;
054: import java.util.prefs.PreferenceChangeEvent;
055: import java.util.prefs.PreferenceChangeListener;
056: import java.util.prefs.Preferences;
057: import javax.swing.DefaultComboBoxModel;
058: import javax.swing.JEditorPane;
059: import javax.swing.SwingUtilities;
060: import javax.swing.UIManager;
061: import javax.swing.text.BadLocationException;
062: import org.netbeans.editor.BaseDocument;
063: import org.netbeans.editor.Formatter;
064: import org.netbeans.modules.cnd.editor.api.CodeStyle;
065: import org.netbeans.modules.cnd.editor.reformat.Reformatter;
066: import org.openide.explorer.propertysheet.PropertySheet;
067: import org.openide.filesystems.FileObject;
068: import org.openide.filesystems.FileSystem;
069: import org.openide.filesystems.Repository;
070: import org.openide.nodes.AbstractNode;
071: import org.openide.nodes.Children;
072: import org.openide.nodes.Sheet;
073: import org.openide.util.Exceptions;
074: import org.openide.util.NbBundle;
075:
076: /**
077: *
078: * @author as204739
079: */
080: public class EditorPropertySheet extends javax.swing.JPanel implements
081: ActionListener, PropertyChangeListener,
082: PreferenceChangeListener {
083:
084: private static final boolean USE_NEW_FORMATTER = false;
085:
086: private EditorOptionsPanelController topControler;
087: private boolean loaded = false;
088: private CodeStyle.Language currentLanguage;
089: private String lastChangedproperty;
090: private Map<CodeStyle.Language, String> defaultStyles = new HashMap<CodeStyle.Language, String>();
091: private Map<CodeStyle.Language, Map<String, PreviewPreferences>> allPreferences = new HashMap<CodeStyle.Language, Map<String, PreviewPreferences>>();
092: private PropertySheet holder = new PropertySheet();
093:
094: EditorPropertySheet(EditorOptionsPanelController topControler) {
095: this .topControler = topControler;
096: initComponents();
097: setName("Tab_Name"); // NOI18N (used as a bundle key)
098: if ("Windows".equals(UIManager.getLookAndFeel().getID())) { //NOI18N
099: setOpaque(false);
100: }
101: previewPane.setContentType("text/x-c++"); // NOI18N
102: // Don't highlight caret row
103: previewPane
104: .putClientProperty(
105: "HighlightsLayerExcludes", // NOI18N
106: "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.CaretRowHighlighting$" // NOI18N
107: );
108: previewPane.setText("1234567890123456789012345678901234567890"); // NOI18N
109: previewPane.setDoubleBuffered(true);
110: initLanguages();
111: initLanguageCategory();
112: }
113:
114: private void initLanguageStylePreferences(
115: CodeStyle.Language language, String styleId) {
116: Map<String, PreviewPreferences> map = allPreferences
117: .get(language);
118: if (map == null) {
119: map = new TreeMap<String, PreviewPreferences>();
120: allPreferences.put(language, map);
121: }
122: PreviewPreferences clone = new PreviewPreferences(EditorOptions
123: .getPreferences(language, styleId), language, styleId);
124: clone.addPreferenceChangeListener(this );
125: map.put(styleId, clone);
126: }
127:
128: private void initLanguageMap() {
129: for (String style : EditorOptions.PREDEFINED_STYLES) {
130: initLanguageStylePreferences(CodeStyle.Language.C, style);
131: }
132: defaultStyles.put(CodeStyle.Language.C, EditorOptions
133: .getCurrentProfileId(CodeStyle.Language.C));
134:
135: for (String style : EditorOptions.PREDEFINED_STYLES) {
136: initLanguageStylePreferences(CodeStyle.Language.CPP, style);
137: }
138: defaultStyles.put(CodeStyle.Language.CPP, EditorOptions
139: .getCurrentProfileId(CodeStyle.Language.CPP));
140: }
141:
142: private void initLanguages() {
143: DefaultComboBoxModel model = new DefaultComboBoxModel();
144: model.addElement(CodeStyle.Language.C);
145: model.addElement(CodeStyle.Language.CPP);
146: initLanguageMap();
147: languagesComboBox.setModel(model);
148: currentLanguage = CodeStyle.Language.C;
149: languagesComboBox.setSelectedIndex(0);
150: languagesComboBox.addActionListener(this );
151: }
152:
153: private void initLanguageCategory() {
154: styleComboBox.removeActionListener(this );
155: DefaultComboBoxModel model = new DefaultComboBoxModel();
156: Map<String, PreviewPreferences> map = allPreferences
157: .get(currentLanguage);
158: String currentProfile = defaultStyles.get(currentLanguage);
159: int index = 0;
160: int i = 0;
161: for (Map.Entry<String, PreviewPreferences> entry : map
162: .entrySet()) {
163: if (entry.getKey().equals(currentProfile)) {
164: index = i;
165: }
166: model.addElement(new EntryWrapper(entry));
167: i++;
168: }
169: styleComboBox.setModel(model);
170: styleComboBox.setSelectedIndex(index);
171: EntryWrapper entry = (EntryWrapper) styleComboBox
172: .getSelectedItem();
173: initSheets(entry.preferences);
174: styleComboBox.addActionListener(this );
175: //actionPerformed(new ActionEvent(styleComboBox, 0, null));
176: }
177:
178: private void initSheets(PreviewPreferences preferences) {
179: Sheet sheet = new Sheet();
180: Sheet.Set set = new Sheet.Set();
181: set.setName("Indents"); // NOI18N
182: set.setDisplayName(getString("LBL_TabsAndIndents")); // NOI18N
183: set.setShortDescription(getString("HINT_TabsAndIndents")); // NOI18N
184: set.put(new IntNodeProp(currentLanguage, preferences,
185: EditorOptions.statementContinuationIndent));
186: set
187: .put(new PreprocessorIndentProperty(currentLanguage,
188: preferences,
189: EditorOptions.indentPreprocessorDirectives));
190: set.put(new BooleanNodeProp(currentLanguage, preferences,
191: EditorOptions.sharpAtStartLine));
192: set.put(new BooleanNodeProp(currentLanguage, preferences,
193: EditorOptions.indentCasesFromSwitch));
194: sheet.put(set);
195:
196: set = new Sheet.Set();
197: set.setName("BracesPlacement"); // NOI18N
198: set.setDisplayName(getString("LBL_BracesPlacement")); // NOI18N
199: set.setShortDescription(getString("HINT_BracesPlacement")); // NOI18N
200: set
201: .put(new BracePlacementProperty(currentLanguage,
202: preferences,
203: EditorOptions.newLineBeforeBraceNamespace));
204: set.put(new BracePlacementProperty(currentLanguage,
205: preferences, EditorOptions.newLineBeforeBraceClass));
206: set.put(new BracePlacementProperty(currentLanguage,
207: preferences,
208: EditorOptions.newLineBeforeBraceDeclaration));
209: set.put(new BracePlacementProperty(currentLanguage,
210: preferences, EditorOptions.newLineBeforeBrace));
211: sheet.put(set);
212:
213: set = new Sheet.Set();
214: set.setName("MultilineAlignment"); // NOI18N
215: set.setDisplayName(getString("LBL_MultilineAlignment")); // NOI18N
216: set.setShortDescription(getString("HINT_MultilineAlignment")); // NOI18N
217: set.put(new BooleanNodeProp(currentLanguage, preferences,
218: EditorOptions.alignMultilineMethodParams));
219: set.put(new BooleanNodeProp(currentLanguage, preferences,
220: EditorOptions.alignMultilineCallArgs));
221: set.put(new BooleanNodeProp(currentLanguage, preferences,
222: EditorOptions.alignMultilineArrayInit));
223: sheet.put(set);
224:
225: set = new Sheet.Set();
226: set.setName("NewLine"); // NOI18N
227: set.setDisplayName(getString("LBL_NewLine")); // NOI18N
228: set.setShortDescription(getString("HINT_NewLine")); // NOI18N
229: set.put(new BooleanNodeProp(currentLanguage, preferences,
230: EditorOptions.newLineCatch));
231: set.put(new BooleanNodeProp(currentLanguage, preferences,
232: EditorOptions.newLineElse));
233: set.put(new BooleanNodeProp(currentLanguage, preferences,
234: EditorOptions.newLineWhile));
235: sheet.put(set);
236:
237: set = new Sheet.Set();
238: set.setName("SpacesBeforeKeywords"); // NOI18N
239: set.setDisplayName(getString("LBL_BeforeKeywords")); // NOI18N
240: set.setShortDescription(getString("HINT_BeforeKeywords")); // NOI18N
241: set.put(new BooleanNodeProp(currentLanguage, preferences,
242: EditorOptions.spaceBeforeCatch));
243: set.put(new BooleanNodeProp(currentLanguage, preferences,
244: EditorOptions.spaceBeforeElse));
245: set.put(new BooleanNodeProp(currentLanguage, preferences,
246: EditorOptions.spaceBeforeWhile));
247: sheet.put(set);
248:
249: set = new Sheet.Set();
250: set.setName("SpacesBeforeParentheses"); // NOI18N
251: set.setDisplayName(getString("LBL_BeforeParentheses")); // NOI18N
252: set.setShortDescription(getString("HINT_BeforeParentheses")); // NOI18N
253: set.put(new BooleanNodeProp(currentLanguage, preferences,
254: EditorOptions.spaceBeforeMethodDeclParen));
255: set.put(new BooleanNodeProp(currentLanguage, preferences,
256: EditorOptions.spaceBeforeMethodCallParen));
257: set.put(new BooleanNodeProp(currentLanguage, preferences,
258: EditorOptions.spaceBeforeCatchParen));
259: set.put(new BooleanNodeProp(currentLanguage, preferences,
260: EditorOptions.spaceBeforeForParen));
261: set.put(new BooleanNodeProp(currentLanguage, preferences,
262: EditorOptions.spaceBeforeIfParen));
263: set.put(new BooleanNodeProp(currentLanguage, preferences,
264: EditorOptions.spaceBeforeSwitchParen));
265: set.put(new BooleanNodeProp(currentLanguage, preferences,
266: EditorOptions.spaceBeforeWhileParen));
267: sheet.put(set);
268:
269: set = new Sheet.Set();
270: set.setName("SpacesAroundOperators"); // NOI18N
271: set.setDisplayName(getString("LBL_AroundOperators")); // NOI18N
272: set.setShortDescription(getString("HINT_AroundOperators")); // NOI18N
273: set.put(new BooleanNodeProp(currentLanguage, preferences,
274: EditorOptions.spaceAroundAssignOps));
275: set.put(new BooleanNodeProp(currentLanguage, preferences,
276: EditorOptions.spaceAroundBinaryOps));
277: set.put(new BooleanNodeProp(currentLanguage, preferences,
278: EditorOptions.spaceAroundTernaryOps));
279: set.put(new BooleanNodeProp(currentLanguage, preferences,
280: EditorOptions.spaceAroundUnaryOps));
281: sheet.put(set);
282:
283: set = new Sheet.Set();
284: set.setName("SpacesBeforeLeftBracess"); // NOI18N
285: set.setDisplayName(getString("LBL_BeforeLeftBraces")); // NOI18N
286: set.setShortDescription(getString("HINT_BeforeLeftBraces")); // NOI18N
287: set.put(new BooleanNodeProp(currentLanguage, preferences,
288: EditorOptions.spaceBeforeClassDeclLeftBrace));
289: set.put(new BooleanNodeProp(currentLanguage, preferences,
290: EditorOptions.spaceBeforeMethodDeclLeftBrace));
291: set.put(new BooleanNodeProp(currentLanguage, preferences,
292: EditorOptions.spaceBeforeArrayInitLeftBrace));
293: set.put(new BooleanNodeProp(currentLanguage, preferences,
294: EditorOptions.spaceBeforeCatchLeftBrace));
295: set.put(new BooleanNodeProp(currentLanguage, preferences,
296: EditorOptions.spaceBeforeDoLeftBrace));
297: set.put(new BooleanNodeProp(currentLanguage, preferences,
298: EditorOptions.spaceBeforeElseLeftBrace));
299: set.put(new BooleanNodeProp(currentLanguage, preferences,
300: EditorOptions.spaceBeforeForLeftBrace));
301: set.put(new BooleanNodeProp(currentLanguage, preferences,
302: EditorOptions.spaceBeforeIfLeftBrace));
303: set.put(new BooleanNodeProp(currentLanguage, preferences,
304: EditorOptions.spaceBeforeSwitchLeftBrace));
305: set.put(new BooleanNodeProp(currentLanguage, preferences,
306: EditorOptions.spaceBeforeTryLeftBrace));
307: set.put(new BooleanNodeProp(currentLanguage, preferences,
308: EditorOptions.spaceBeforeWhileLeftBrace));
309: sheet.put(set);
310:
311: set = new Sheet.Set();
312: set.setName("SpacesWithinParentheses"); // NOI18N
313: set.setDisplayName(getString("LBL_WithinParentheses")); // NOI18N
314: set.setShortDescription(getString("HINT_WithinParentheses")); // NOI18N
315: set.put(new BooleanNodeProp(currentLanguage, preferences,
316: EditorOptions.spaceWithinMethodDeclParens));
317: set.put(new BooleanNodeProp(currentLanguage, preferences,
318: EditorOptions.spaceWithinMethodCallParens));
319: set.put(new BooleanNodeProp(currentLanguage, preferences,
320: EditorOptions.spaceWithinArrayInitBrackets));
321: set.put(new BooleanNodeProp(currentLanguage, preferences,
322: EditorOptions.spaceWithinBraces));
323: set.put(new BooleanNodeProp(currentLanguage, preferences,
324: EditorOptions.spaceWithinParens));
325: set.put(new BooleanNodeProp(currentLanguage, preferences,
326: EditorOptions.spaceWithinCatchParens));
327: set.put(new BooleanNodeProp(currentLanguage, preferences,
328: EditorOptions.spaceWithinForParens));
329: set.put(new BooleanNodeProp(currentLanguage, preferences,
330: EditorOptions.spaceWithinIfParens));
331: set.put(new BooleanNodeProp(currentLanguage, preferences,
332: EditorOptions.spaceWithinSwitchParens));
333: set.put(new BooleanNodeProp(currentLanguage, preferences,
334: EditorOptions.spaceWithinTypeCastParens));
335: set.put(new BooleanNodeProp(currentLanguage, preferences,
336: EditorOptions.spaceWithinWhileParens));
337: sheet.put(set);
338:
339: set = new Sheet.Set();
340: set.setName("SpacesOther"); // NOI18N
341: set.setDisplayName(getString("LBL_Other_Spaces")); // NOI18N
342: set.setShortDescription(getString("HINT_Other_Spaces")); // NOI18N
343: set.put(new BooleanNodeProp(currentLanguage, preferences,
344: EditorOptions.spaceBeforeComma));
345: set.put(new BooleanNodeProp(currentLanguage, preferences,
346: EditorOptions.spaceAfterComma));
347: set.put(new BooleanNodeProp(currentLanguage, preferences,
348: EditorOptions.spaceBeforeSemi));
349: set.put(new BooleanNodeProp(currentLanguage, preferences,
350: EditorOptions.spaceAfterSemi));
351: set.put(new BooleanNodeProp(currentLanguage, preferences,
352: EditorOptions.spaceBeforeColon));
353: set.put(new BooleanNodeProp(currentLanguage, preferences,
354: EditorOptions.spaceAfterColon));
355: set.put(new BooleanNodeProp(currentLanguage, preferences,
356: EditorOptions.spaceAfterTypeCast));
357: sheet.put(set);
358:
359: set = new Sheet.Set();
360: set.setName("BlankLines"); // NOI18N
361: set.setDisplayName(getString("LBL_BlankLines")); // NOI18N
362: set.setShortDescription(getString("HINT_BlankLines")); // NOI18N
363: set.put(new IntNodeProp(currentLanguage, preferences,
364: EditorOptions.blankLinesBeforeClass));
365: set.put(new IntNodeProp(currentLanguage, preferences,
366: EditorOptions.blankLinesAfterClass));
367: set.put(new IntNodeProp(currentLanguage, preferences,
368: EditorOptions.blankLinesAfterClassHeader));
369: set.put(new IntNodeProp(currentLanguage, preferences,
370: EditorOptions.blankLinesBeforeFields));
371: set.put(new IntNodeProp(currentLanguage, preferences,
372: EditorOptions.blankLinesAfterFields));
373: set.put(new IntNodeProp(currentLanguage, preferences,
374: EditorOptions.blankLinesBeforeMethods));
375: set.put(new IntNodeProp(currentLanguage, preferences,
376: EditorOptions.blankLinesAfterMethods));
377: sheet.put(set);
378:
379: set = new Sheet.Set();
380: set.setName("Other"); // NOI18N
381: set.setDisplayName(getString("LBL_Other")); // NOI18N
382: set.setShortDescription(getString("HINT_Other")); // NOI18N
383: set.put(new BooleanNodeProp(currentLanguage, preferences,
384: EditorOptions.addLeadingStarInComment));
385: sheet.put(set);
386:
387: categoryPanel.setVisible(false);
388: categoryPanel.removeAll();
389: holder.removeNotify();
390: DummyNode[] dummyNodes = new DummyNode[1];
391: dummyNodes[0] = new DummyNode(sheet, "Sheet"); // NOI18N
392: holder.setNodes(dummyNodes);
393: GridBagConstraints fillConstraints = new GridBagConstraints();
394: fillConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
395: fillConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
396: fillConstraints.fill = java.awt.GridBagConstraints.BOTH;
397: fillConstraints.weightx = 1.0;
398: fillConstraints.weighty = 1.0;
399: categoryPanel.add(holder, fillConstraints);
400: categoryPanel.validate();
401: categoryPanel.repaint();
402: categoryPanel.setVisible(true);
403: }
404:
405: void load() {
406: loaded = false;
407: initLanguageMap();
408: initLanguageCategory();
409: loaded = true;
410: repaintPreview();
411: }
412:
413: void store() {
414: for (Map.Entry<CodeStyle.Language, Map<String, PreviewPreferences>> entry : allPreferences
415: .entrySet()) {
416: CodeStyle.Language language = entry.getKey();
417: Map<String, PreviewPreferences> map = entry.getValue();
418: EditorOptions.setCurrentProfileId(language, defaultStyles
419: .get(language));
420: for (Map.Entry<String, PreviewPreferences> prefEntry : map
421: .entrySet()) {
422: String style = prefEntry.getKey();
423: PreviewPreferences preferences = prefEntry.getValue();
424: Preferences toSave = EditorOptions.getPreferences(
425: language, style);
426: try {
427: for (String key : preferences.keys()) {
428: Object def = EditorOptions.getDefault(language,
429: key, defaultStyles.get(language));
430: if (def instanceof Boolean) {
431: toSave.putBoolean(key, preferences
432: .getBoolean(key, (Boolean) def));
433: } else if (def instanceof Integer) {
434: toSave.putInt(key, preferences.getInt(key,
435: (Integer) def));
436: } else {
437: toSave.put(key, preferences.get(key,
438: (String) def));
439: }
440: }
441: } catch (BackingStoreException ex) {
442: Exceptions.printStackTrace(ex);
443: }
444: }
445: }
446: defaultStyles.clear();
447: allPreferences.clear();
448: }
449:
450: void cancel() {
451: defaultStyles.clear();
452: allPreferences.clear();
453: }
454:
455: // Change in the combo
456: public void actionPerformed(ActionEvent e) {
457: lastChangedproperty = null;
458: if (styleComboBox.equals(e.getSource())) {
459: EntryWrapper category = (EntryWrapper) styleComboBox
460: .getSelectedItem();
461: if (category != null) {
462: defaultStyles.put(currentLanguage, category.name);
463: initSheets(category.preferences);
464: if (CodeStyle.Language.C.equals(currentLanguage)) {
465: previewPane.setContentType("text/x-c"); // NOI18N
466: } else {
467: previewPane.setContentType("text/x-c++"); // NOI18N
468: }
469: if (loaded) {
470: repaintPreview();
471: }
472: }
473: } else if (languagesComboBox.equals(e.getSource())) {
474: currentLanguage = (CodeStyle.Language) languagesComboBox
475: .getSelectedItem();
476: initLanguageCategory();
477: }
478: }
479:
480: public void preferenceChange(PreferenceChangeEvent evt) {
481: lastChangedproperty = evt.getKey();
482: change();
483: }
484:
485: // Change in some of the subpanels
486: public void propertyChange(PropertyChangeEvent evt) {
487: change();
488: }
489:
490: private void change() {
491: if (!loaded) {
492: return;
493: }
494: Runnable run = new Runnable() {
495: public void run() {
496: // Notify the main controler that the page has changed
497: topControler.changed();
498: // Repaint the preview
499: repaintPreview();
500: }
501: };
502: if (SwingUtilities.isEventDispatchThread()) {
503: run.run();
504: } else {
505: SwingUtilities.invokeLater(run);
506: }
507: }
508:
509: private void repaintPreview() {
510: EntryWrapper category = (EntryWrapper) styleComboBox
511: .getSelectedItem();
512: if (category != null) {
513: Preferences p = new PreviewPreferences(
514: category.preferences, category.preferences
515: .getLanguage(), category.preferences
516: .getStyleId());
517: jScrollPane1.setIgnoreRepaint(true);
518: refreshPreview(previewPane, p);
519: previewPane.setIgnoreRepaint(false);
520: previewPane
521: .scrollRectToVisible(new Rectangle(0, 0, 10, 10));
522: previewPane.repaint(100);
523: }
524: }
525:
526: private String getPreviwText() {
527: String suffix;
528: if (CodeStyle.Language.C.equals(currentLanguage)) {
529: suffix = ".c";
530: } else {
531: suffix = ".cpp";
532: }
533: if (lastChangedproperty != null) {
534: if (lastChangedproperty.startsWith("space")) { // NOI18N
535: return loadPreviewExample("SAMPLE_Spaces" + suffix); // NOI18N
536: } else if (lastChangedproperty.startsWith("blank")) { // NOI18N
537: return loadPreviewExample("SAMPLE_BlankLines" + suffix); // NOI18N
538: } else if (lastChangedproperty.startsWith("align") || // NOI18N
539: lastChangedproperty.startsWith("new")) { // NOI18N
540: return loadPreviewExample("SAMPLE_AlignBraces" + suffix); // NOI18N
541: }
542: }
543: return loadPreviewExample("SAMPLE_TabsIndents" + suffix); // NOI18N
544: }
545:
546: private String loadPreviewExample(String example) {
547: //return getString(example);
548: FileSystem fs = Repository.getDefault().getDefaultFileSystem();
549: FileObject exampleFile = fs
550: .findResource("OptionsDialog/CPlusPlus/FormatterPreviewExamples/"
551: + example); //NOI18N
552: if (exampleFile != null) {
553: StringBuilder sb = new StringBuilder((int) exampleFile
554: .getSize());
555: try {
556: InputStreamReader is = new InputStreamReader(
557: exampleFile.getInputStream());
558: char[] buffer = new char[1024];
559: int size;
560: try {
561: while (0 < (size = is
562: .read(buffer, 0, buffer.length))) {
563: sb.append(buffer, 0, size);
564: }
565: } finally {
566: is.close();
567: }
568: } catch (IOException ioe) {
569: ioe.printStackTrace();
570: }
571: return sb.toString();
572: } else {
573: return ""; //NOI18N
574: }
575: }
576:
577: public void refreshPreview(JEditorPane pane, Preferences p) {
578: pane.setText(getPreviwText());
579: BaseDocument bd = (BaseDocument) pane.getDocument();
580: if (USE_NEW_FORMATTER) {
581: CodeStyle codeStyle = EditorOptions.createCodeStyle(
582: currentLanguage, p);
583: try {
584: new Reformatter(bd, codeStyle).reformat();
585: } catch (BadLocationException ex) {
586: Exceptions.printStackTrace(ex);
587: }
588: } else {
589: CodeStyle codeStyle = CodeStyle.getDefault(currentLanguage);
590: Preferences oldPreferences = EditorOptions
591: .getPreferences(codeStyle);
592: EditorOptions.setPreferences(codeStyle, p);
593: Formatter f = bd.getFormatter();
594: try {
595: f.reformatLock();
596: f.reformat(bd, 0, bd.getLength());
597: String x = bd.getText(0, bd.getLength());
598: pane.setText(x);
599: } catch (BadLocationException ex) {
600: Exceptions.printStackTrace(ex);
601: } finally {
602: EditorOptions.setPreferences(codeStyle, oldPreferences);
603: f.reformatUnlock();
604: }
605: }
606: }
607:
608: private static String getString(String key) {
609: return NbBundle.getMessage(EditorPropertySheet.class, key);
610: }
611:
612: /** This method is called from within the constructor to
613: * initialize the form.
614: * WARNING: Do NOT modify this code. The content of this method is
615: * always regenerated by the Form Editor.
616: */
617: @SuppressWarnings("unchecked")
618: // NOI18N
619: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
620: private void initComponents() {
621: java.awt.GridBagConstraints gridBagConstraints;
622:
623: jLabel2 = new javax.swing.JLabel();
624: languagesComboBox = new javax.swing.JComboBox();
625: jSplitPane1 = new javax.swing.JSplitPane();
626: oprionsPanel = new javax.swing.JPanel();
627: jLabel1 = new javax.swing.JLabel();
628: styleComboBox = new javax.swing.JComboBox();
629: categoryPanel = new javax.swing.JPanel();
630: manageStyles = new javax.swing.JButton();
631: previewPanel = new javax.swing.JPanel();
632: jScrollPane1 = new javax.swing.JScrollPane();
633: previewPane = new javax.swing.JEditorPane();
634: jSeparator1 = new javax.swing.JSeparator();
635:
636: setLayout(new java.awt.GridBagLayout());
637:
638: jLabel2.setLabelFor(languagesComboBox);
639: org.openide.awt.Mnemonics.setLocalizedText(jLabel2,
640: org.openide.util.NbBundle.getMessage(
641: EditorPropertySheet.class,
642: "EditorPropertySheet.jLabel2.text")); // NOI18N
643: gridBagConstraints = new java.awt.GridBagConstraints();
644: gridBagConstraints.gridx = 0;
645: gridBagConstraints.gridy = 0;
646: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
647: gridBagConstraints.insets = new java.awt.Insets(0, 6, 6, 6);
648: add(jLabel2, gridBagConstraints);
649:
650: gridBagConstraints = new java.awt.GridBagConstraints();
651: gridBagConstraints.gridx = 1;
652: gridBagConstraints.gridy = 0;
653: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
654: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
655: add(languagesComboBox, gridBagConstraints);
656:
657: jSplitPane1.setBorder(null);
658: jSplitPane1.setDividerLocation(300);
659:
660: oprionsPanel.setBorder(javax.swing.BorderFactory
661: .createEmptyBorder(1, 1, 1, 1));
662: oprionsPanel.setLayout(new java.awt.GridBagLayout());
663:
664: jLabel1.setLabelFor(styleComboBox);
665: org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
666: org.openide.util.NbBundle.getMessage(
667: EditorPropertySheet.class, "LBL_Style_Name")); // NOI18N
668: gridBagConstraints = new java.awt.GridBagConstraints();
669: gridBagConstraints.gridx = 0;
670: gridBagConstraints.gridy = 1;
671: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
672: gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
673: oprionsPanel.add(jLabel1, gridBagConstraints);
674:
675: gridBagConstraints = new java.awt.GridBagConstraints();
676: gridBagConstraints.gridx = 1;
677: gridBagConstraints.gridy = 1;
678: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
679: gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
680: oprionsPanel.add(styleComboBox, gridBagConstraints);
681:
682: categoryPanel.setBorder(javax.swing.BorderFactory
683: .createEtchedBorder());
684: categoryPanel.setLayout(new java.awt.GridBagLayout());
685: gridBagConstraints = new java.awt.GridBagConstraints();
686: gridBagConstraints.gridx = 0;
687: gridBagConstraints.gridy = 2;
688: gridBagConstraints.gridwidth = 3;
689: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
690: gridBagConstraints.weightx = 1.0;
691: gridBagConstraints.weighty = 1.0;
692: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6);
693: oprionsPanel.add(categoryPanel, gridBagConstraints);
694:
695: org.openide.awt.Mnemonics.setLocalizedText(manageStyles,
696: org.openide.util.NbBundle.getMessage(
697: EditorPropertySheet.class,
698: "EditorPropertySheet.manageStyles.text")); // NOI18N
699: gridBagConstraints = new java.awt.GridBagConstraints();
700: gridBagConstraints.gridx = 2;
701: gridBagConstraints.gridy = 1;
702: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
703: gridBagConstraints.insets = new java.awt.Insets(0, 6, 12, 6);
704: oprionsPanel.add(manageStyles, gridBagConstraints);
705:
706: jSplitPane1.setLeftComponent(oprionsPanel);
707:
708: previewPanel.setBorder(javax.swing.BorderFactory
709: .createEmptyBorder(1, 1, 1, 1));
710: previewPanel.setLayout(new java.awt.GridBagLayout());
711:
712: jScrollPane1.setViewportView(previewPane);
713:
714: gridBagConstraints = new java.awt.GridBagConstraints();
715: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
716: gridBagConstraints.weightx = 1.0;
717: gridBagConstraints.weighty = 1.0;
718: previewPanel.add(jScrollPane1, gridBagConstraints);
719:
720: jSplitPane1.setRightComponent(previewPanel);
721:
722: gridBagConstraints = new java.awt.GridBagConstraints();
723: gridBagConstraints.gridx = 0;
724: gridBagConstraints.gridy = 2;
725: gridBagConstraints.gridwidth = 2;
726: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
727: gridBagConstraints.weightx = 1.0;
728: gridBagConstraints.weighty = 1.0;
729: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
730: add(jSplitPane1, gridBagConstraints);
731:
732: jSeparator1.setForeground(javax.swing.UIManager.getDefaults()
733: .getColor("Button.disabledText"));
734: gridBagConstraints = new java.awt.GridBagConstraints();
735: gridBagConstraints.gridx = 0;
736: gridBagConstraints.gridy = 1;
737: gridBagConstraints.gridwidth = 2;
738: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
739: gridBagConstraints.insets = new java.awt.Insets(0, 6, 6, 6);
740: add(jSeparator1, gridBagConstraints);
741: }// </editor-fold>//GEN-END:initComponents
742:
743: // Variables declaration - do not modify//GEN-BEGIN:variables
744: private javax.swing.JPanel categoryPanel;
745: private javax.swing.JLabel jLabel1;
746: private javax.swing.JLabel jLabel2;
747: private javax.swing.JScrollPane jScrollPane1;
748: private javax.swing.JSeparator jSeparator1;
749: private javax.swing.JSplitPane jSplitPane1;
750: private javax.swing.JComboBox languagesComboBox;
751: private javax.swing.JButton manageStyles;
752: private javax.swing.JPanel oprionsPanel;
753: private javax.swing.JEditorPane previewPane;
754: private javax.swing.JPanel previewPanel;
755: private javax.swing.JComboBox styleComboBox;
756:
757: // End of variables declaration//GEN-END:variables
758:
759: private static class EntryWrapper {
760: private final String name;
761: private final PreviewPreferences preferences;
762:
763: private EntryWrapper(Map.Entry<String, PreviewPreferences> enrty) {
764: this .name = enrty.getKey();
765: this .preferences = enrty.getValue();
766: }
767:
768: @Override
769: public String toString() {
770: return name;
771: }
772: }
773:
774: private class DummyNode extends AbstractNode {
775: public DummyNode(Sheet sheet, String name) {
776: super(Children.LEAF);
777: if (sheet != null) {
778: setSheet(sheet);
779: }
780: setName(name);
781: }
782: }
783: }
|