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: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.java.ui;
043:
044: import java.awt.Component;
045: import java.awt.Point;
046: import java.awt.Rectangle;
047: import java.awt.event.KeyEvent;
048: import java.awt.event.KeyListener;
049: import java.awt.event.MouseEvent;
050: import java.awt.event.MouseListener;
051: import java.util.Enumeration;
052: import java.util.LinkedList;
053: import java.util.List;
054: import java.util.prefs.Preferences;
055: import javax.swing.JCheckBox;
056: import javax.swing.JPanel;
057: import javax.swing.JTree;
058: import javax.swing.tree.DefaultMutableTreeNode;
059: import javax.swing.tree.DefaultTreeCellRenderer;
060: import javax.swing.tree.DefaultTreeModel;
061: import javax.swing.tree.TreeCellRenderer;
062: import javax.swing.tree.TreePath;
063: import org.openide.util.NbBundle;
064: import static org.netbeans.modules.java.ui.FmtOptions.*;
065:
066: /**
067: *
068: * @author phrebejk
069: */
070: public class FmtSpaces extends JPanel implements TreeCellRenderer,
071: MouseListener, KeyListener {
072:
073: private static Controller controller;
074:
075: private DefaultTreeModel model;
076:
077: private DefaultTreeCellRenderer dr = new DefaultTreeCellRenderer();
078: private JCheckBox renderer = new JCheckBox();
079:
080: /** Creates new form FmtSpaces */
081: public FmtSpaces() {
082: initComponents();
083: model = createModel();
084: cfgTree.setModel(model);
085: cfgTree.setRootVisible(false);
086: cfgTree.setShowsRootHandles(true);
087: cfgTree.setCellRenderer(this );
088: cfgTree.setEditable(false);
089: cfgTree.addMouseListener(this );
090: cfgTree.addKeyListener(this );
091:
092: dr.setIcon(null);
093: dr.setOpenIcon(null);
094: dr.setClosedIcon(null);
095:
096: DefaultMutableTreeNode root = (DefaultMutableTreeNode) model
097: .getRoot();
098: for (int i = root.getChildCount(); i >= 0; i--) {
099: cfgTree.expandRow(i);
100: }
101: }
102:
103: public static FormatingOptionsPanel.Category getController() {
104: if (controller == null) {
105: controller = new Controller();
106: }
107: return controller;
108: }
109:
110: /** This method is called from within the constructor to
111: * initialize the form.
112: * WARNING: Do NOT modify this code. The content of this method is
113: * always regenerated by the Form Editor.
114: */
115: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
116: private void initComponents() {
117: java.awt.GridBagConstraints gridBagConstraints;
118:
119: jScrollPane1 = new javax.swing.JScrollPane();
120: cfgTree = new javax.swing.JTree();
121:
122: setOpaque(false);
123: setLayout(new java.awt.GridBagLayout());
124:
125: cfgTree.setRootVisible(false);
126: jScrollPane1.setViewportView(cfgTree);
127:
128: gridBagConstraints = new java.awt.GridBagConstraints();
129: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
130: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
131: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
132: gridBagConstraints.weightx = 1.0;
133: gridBagConstraints.weighty = 1.0;
134: add(jScrollPane1, gridBagConstraints);
135: }// </editor-fold>//GEN-END:initComponents
136:
137: // Variables declaration - do not modify//GEN-BEGIN:variables
138: private javax.swing.JTree cfgTree;
139: private javax.swing.JScrollPane jScrollPane1;
140:
141: // End of variables declaration//GEN-END:variables
142:
143: // TreeCellRenderer implemetation ------------------------------------------
144:
145: public Component getTreeCellRendererComponent(JTree tree,
146: Object value, boolean selected, boolean expanded,
147: boolean leaf, int row, boolean hasFocus) {
148:
149: renderer.setBackground(selected ? dr
150: .getBackgroundSelectionColor() : dr
151: .getBackgroundNonSelectionColor());
152: renderer.setForeground(selected ? dr.getTextSelectionColor()
153: : dr.getTextNonSelectionColor());
154: renderer.setEnabled(true);
155:
156: Object data = ((DefaultMutableTreeNode) value).getUserObject();
157:
158: if (data instanceof Item) {
159: Item item = ((Item) data);
160:
161: if (((DefaultMutableTreeNode) value).getAllowsChildren()) {
162: Component c = dr.getTreeCellRendererComponent(tree,
163: value, leaf, expanded, leaf, row, hasFocus);
164: return c;
165: } else {
166: renderer.setText(item.displayName);
167: renderer.setSelected(item.value);
168: }
169: } else {
170: Component c = dr.getTreeCellRendererComponent(tree, value,
171: leaf, expanded, leaf, row, hasFocus);
172: return c;
173: }
174:
175: return renderer;
176: }
177:
178: // MouseListener implementation --------------------------------------------
179:
180: public void mouseClicked(MouseEvent e) {
181: Point p = e.getPoint();
182: TreePath path = cfgTree.getPathForLocation(e.getPoint().x, e
183: .getPoint().y);
184: if (path != null) {
185: Rectangle r = cfgTree.getPathBounds(path);
186: if (r != null) {
187: r.width = r.height;
188: if (r.contains(p)) {
189: toggle(path);
190: }
191: }
192: }
193: }
194:
195: public void mouseEntered(MouseEvent e) {
196: }
197:
198: public void mouseExited(MouseEvent e) {
199: }
200:
201: public void mousePressed(MouseEvent e) {
202: }
203:
204: public void mouseReleased(MouseEvent e) {
205: }
206:
207: // KeyListener implementation ----------------------------------------------
208:
209: public void keyTyped(KeyEvent e) {
210: }
211:
212: public void keyReleased(KeyEvent e) {
213: }
214:
215: public void keyPressed(KeyEvent e) {
216: if (e.getKeyCode() == KeyEvent.VK_SPACE
217: || e.getKeyCode() == KeyEvent.VK_ENTER) {
218:
219: if (e.getSource() instanceof JTree) {
220: JTree tree = (JTree) e.getSource();
221: TreePath path = tree.getSelectionPath();
222:
223: if (toggle(path)) {
224: e.consume();
225: }
226: }
227: }
228: }
229:
230: // Private methods ---------------------------------------------------------
231:
232: private DefaultTreeModel createModel() {
233:
234: Item[] categories = new Item[] {
235: new Item("BeforeKeywords", // NOI18N
236: new Item(spaceBeforeWhile), new Item(
237: spaceBeforeElse), new Item(
238: spaceBeforeCatch), new Item(
239: spaceBeforeFinally)),
240:
241: new Item(
242: "BeforeParentheses", // NOI18N
243: new Item(spaceBeforeMethodDeclParen), new Item(
244: spaceBeforeMethodCallParen), new Item(
245: spaceBeforeIfParen), new Item(
246: spaceBeforeForParen), new Item(
247: spaceBeforeWhileParen), new Item(
248: spaceBeforeCatchParen), new Item(
249: spaceBeforeSwitchParen), new Item(
250: spaceBeforeSynchronizedParen),
251: new Item(spaceBeforeAnnotationParen)),
252:
253: new Item("AroundOperators", // NOI18N
254: new Item(spaceAroundUnaryOps), new Item(
255: spaceAroundBinaryOps), new Item(
256: spaceAroundTernaryOps), new Item(
257: spaceAroundAssignOps)),
258:
259: new Item(
260: "BeforeLeftBraces", // NOI18N
261: new Item(spaceBeforeClassDeclLeftBrace),
262: new Item(spaceBeforeMethodDeclLeftBrace),
263: new Item(spaceBeforeIfLeftBrace), new Item(
264: spaceBeforeElseLeftBrace), new Item(
265: spaceBeforeWhileLeftBrace), new Item(
266: spaceBeforeForLeftBrace), new Item(
267: spaceBeforeDoLeftBrace), new Item(
268: spaceBeforeSwitchLeftBrace), new Item(
269: spaceBeforeTryLeftBrace), new Item(
270: spaceBeforeCatchLeftBrace), new Item(
271: spaceBeforeFinallyLeftBrace), new Item(
272: spaceBeforeSynchronizedLeftBrace),
273: new Item(spaceBeforeStaticInitLeftBrace),
274: new Item(spaceBeforeArrayInitLeftBrace)),
275:
276: new Item(
277: "WithinParentheses", // NOI18N
278: new Item(spaceWithinParens), new Item(
279: spaceWithinMethodDeclParens), new Item(
280: spaceWithinMethodCallParens), new Item(
281: spaceWithinIfParens), new Item(
282: spaceWithinForParens), new Item(
283: spaceWithinWhileParens), new Item(
284: spaceWithinSwitchParens), new Item(
285: spaceWithinCatchParens), new Item(
286: spaceWithinSynchronizedParens),
287: new Item(spaceWithinTypeCastParens), new Item(
288: spaceWithinAnnotationParens), new Item(
289: spaceWithinBraces), new Item(
290: spaceWithinArrayInitBrackets)),
291:
292: new Item("Other", // NOI18N
293: new Item(spaceBeforeComma), new Item(
294: spaceAfterComma), new Item(
295: spaceBeforeSemi), new Item(
296: spaceAfterSemi), new Item(
297: spaceBeforeColon), new Item(
298: spaceAfterColon), new Item(
299: spaceAfterTypeCast))
300:
301: };
302:
303: DefaultMutableTreeNode root = new DefaultMutableTreeNode(
304: "root", true); // NOI18N
305: DefaultTreeModel model = new DefaultTreeModel(root);
306:
307: for (Item item : categories) {
308: DefaultMutableTreeNode cn = new DefaultMutableTreeNode(
309: item, true);
310: root.add(cn);
311: for (Item si : item.items) {
312: DefaultMutableTreeNode in = new DefaultMutableTreeNode(
313: si, false);
314: cn.add(in);
315: }
316: }
317:
318: return model;
319: }
320:
321: private boolean toggle(TreePath treePath) {
322:
323: if (treePath == null) {
324: return false;
325: }
326:
327: Object o = ((DefaultMutableTreeNode) treePath
328: .getLastPathComponent()).getUserObject();
329:
330: DefaultTreeModel model = (DefaultTreeModel) cfgTree.getModel();
331: DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath
332: .getLastPathComponent();
333:
334: if (o instanceof Item) {
335: Item item = (Item) o;
336:
337: if (node.getAllowsChildren()) {
338: return false;
339: }
340:
341: item.value = !item.value;
342: model.nodeChanged(node);
343: model.nodeChanged(node.getParent());
344: controller.changed();
345: }
346:
347: return false;
348: }
349:
350: // Innerclasses ------------------------------------------------------------
351:
352: private static class Item {
353:
354: String id;
355: String displayName;
356: boolean value;
357: Item[] items;
358:
359: public Item(String id, Item... items) {
360: this .id = id;
361: this .items = items;
362: this .displayName = NbBundle.getMessage(FmtSpaces.class,
363: "LBL_" + id); // NOI18N
364: }
365:
366: @Override
367: public String toString() {
368: return displayName;
369: }
370:
371: }
372:
373: private static class Controller extends FmtOptions.CategorySupport {
374:
375: FmtSpaces panel;
376:
377: public Controller() {
378: super ("LBL_Spaces", // NOI18N
379: new FmtSpaces(), NbBundle.getMessage(
380: FmtSpaces.class, "SAMPLE_Spaces"), // NOI18N
381: new String[] { FmtOptions.placeCatchOnNewLine,
382: Boolean.FALSE.toString() }, new String[] {
383: FmtOptions.placeElseOnNewLine,
384: Boolean.FALSE.toString() }, new String[] {
385: FmtOptions.placeWhileOnNewLine,
386: Boolean.FALSE.toString() }, new String[] {
387: FmtOptions.placeFinallyOnNewLine,
388: Boolean.FALSE.toString() });
389: this .panel = (FmtSpaces) getComponent(null);
390: update();
391: }
392:
393: @Override
394: protected void addListeners() {
395: // Should not do anything
396: }
397:
398: @Override
399: public void update() {
400:
401: DefaultMutableTreeNode root = (DefaultMutableTreeNode) panel.model
402: .getRoot();
403: List<Item> items = getAllItems();
404:
405: Preferences node = getPreferences(getCurrentProfileId());
406:
407: for (Item item : items) {
408: boolean df = FmtOptions.getDefaultAsBoolean(item.id);
409: item.value = node.getBoolean(item.id, df);
410: }
411:
412: }
413:
414: @Override
415: public void applyChanges() {
416: storeTo(getPreferences(getCurrentProfileId()));
417: }
418:
419: @Override
420: public void storeTo(Preferences preferences) {
421: DefaultMutableTreeNode root = (DefaultMutableTreeNode) panel.model
422: .getRoot();
423: List<Item> items = getAllItems();
424:
425: for (Item item : items) {
426: preferences.putBoolean(item.id, item.value);
427: }
428: }
429:
430: private List<Item> getAllItems() {
431: List<Item> result = new LinkedList<FmtSpaces.Item>();
432:
433: DefaultMutableTreeNode root = (DefaultMutableTreeNode) panel.model
434: .getRoot();
435:
436: Enumeration children = root.depthFirstEnumeration();
437:
438: while (children.hasMoreElements()) {
439: DefaultMutableTreeNode node = (DefaultMutableTreeNode) children
440: .nextElement();
441: Object o = node.getUserObject();
442: if (o instanceof Item) {
443: Item item = (Item) o;
444: if (item.items == null || item.items.length == 0) {
445: result.add(item);
446: }
447: }
448: }
449:
450: return result;
451: }
452: }
453:
454: }
|