KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > OptionsPanel


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;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Cursor JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Font JavaDoc;
27 import java.awt.Frame JavaDoc;
28 import java.awt.GridBagConstraints JavaDoc;
29 import java.awt.GridBagLayout JavaDoc;
30 import java.awt.BorderLayout JavaDoc;
31 import java.awt.SystemColor JavaDoc;
32 import java.awt.Window JavaDoc;
33 import java.awt.event.ActionEvent JavaDoc;
34 import java.awt.event.FocusEvent JavaDoc;
35 import java.awt.event.FocusListener JavaDoc;
36 import java.awt.event.KeyEvent JavaDoc;
37 import java.awt.event.MouseEvent JavaDoc;
38 import java.awt.event.MouseListener JavaDoc;
39 import java.beans.PropertyChangeEvent JavaDoc;
40 import java.beans.PropertyChangeListener JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.LinkedHashMap JavaDoc;
43 import java.util.Map JavaDoc;
44 import javax.swing.AbstractAction JavaDoc;
45 import javax.swing.BorderFactory JavaDoc;
46 import javax.swing.FocusManager JavaDoc;
47 import javax.swing.Icon JavaDoc;
48 import javax.swing.InputMap JavaDoc;
49 import javax.swing.JComponent JavaDoc;
50 import javax.swing.JLabel JavaDoc;
51 import javax.swing.JPanel JavaDoc;
52 import javax.swing.KeyStroke JavaDoc;
53 import javax.swing.SwingUtilities JavaDoc;
54 import javax.swing.UIManager JavaDoc;
55 import javax.swing.border.CompoundBorder JavaDoc;
56 import javax.swing.border.EmptyBorder JavaDoc;
57 import javax.swing.border.LineBorder JavaDoc;
58 import org.netbeans.modules.options.ui.LoweredBorder;
59 import org.netbeans.modules.options.ui.VariableBorder;
60 import org.netbeans.spi.options.OptionsPanelController;
61 import org.openide.awt.Mnemonics;
62 import org.openide.util.HelpCtx;
63 import org.openide.util.NbBundle;
64 import org.openide.util.NbPreferences;
65 import org.openide.util.RequestProcessor;
66
67 import org.openide.util.Utilities;
68 import org.openide.windows.WindowManager;
69
70 public class OptionsPanel extends JPanel JavaDoc {
71     private CategoryModel model = CategoryModel.getInstance();
72     private JPanel JavaDoc pCategories;
73     private JPanel JavaDoc pCategories2;
74     private JPanel JavaDoc pOptions;
75     private JLabel JavaDoc lTitle;
76     private JPanel JavaDoc pTitle;
77
78     private Map JavaDoc<String JavaDoc, CategoryButton> buttons = new LinkedHashMap JavaDoc<String JavaDoc, CategoryButton>();
79     private final boolean isMac = UIManager.getLookAndFeel ().getID ().equals ("Aqua");
80     private Color JavaDoc selected = isMac ? new Color JavaDoc(221, 221, 221) : new Color JavaDoc (193, 210, 238);
81     private Color JavaDoc selectedB = isMac ? new Color JavaDoc(183, 183, 183) : new Color JavaDoc (149, 106, 197);
82     private Color JavaDoc highlighted = isMac ? new Color JavaDoc(221, 221, 221) : new Color JavaDoc (224, 232, 246);
83     private Color JavaDoc highlightedB = new Color JavaDoc (152, 180, 226);
84     private Color JavaDoc iconViewBorder = new Color JavaDoc (127, 157, 185);
85     private ControllerListener coltrollerListener = new ControllerListener ();
86     
87     private final Color JavaDoc borderMac = new Color JavaDoc(141, 141, 141);
88     private final Font JavaDoc labelFontMac = new Font JavaDoc("Lucida Grande", 0, 10);
89     
90     private static String JavaDoc loc (String JavaDoc key) {
91         return NbBundle.getMessage (OptionsPanel.class, key);
92     }
93     
94     /** Creates new form OptionsPanel */
95     public OptionsPanel () {
96         this(null);
97     }
98     
99     public OptionsPanel (String JavaDoc categoryID) {
100         // init UI components, layout and actions, and add some default values
101
initUI(categoryID);
102     }
103     
104     private String JavaDoc getCategoryID(String JavaDoc categoryID) {
105         return categoryID == null ? model.getCurrentCategoryID() : categoryID;
106     }
107         
108     void initCurrentCategory (final String JavaDoc categoryID) {
109         //generalpanel should be moved to core/options and then could be implemented better
110
//generalpanel doesn't need lookup
111
boolean isGeneralPanel = "General".equals(getCategoryID(categoryID));//NOI18N
112
if (model.isLookupInitialized() || isGeneralPanel) {
113             setCurrentCategory(model.getCategory(getCategoryID(categoryID)), false);
114             initActions();
115         } else {
116             RequestProcessor.getDefault().post(new Runnable JavaDoc() {
117                 public void run() {
118                     SwingUtilities.invokeLater(new Runnable JavaDoc() {
119                         public void run() {
120                             // change cursor
121
final Frame JavaDoc frame = WindowManager.getDefault().getMainWindow();
122                             final Cursor JavaDoc cursor = frame.getCursor();
123                             frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
124                             setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
125                             setCurrentCategory(model.getCategory(getCategoryID(categoryID)), true);
126                             initActions();
127                             // reset cursor
128
frame.setCursor(cursor);
129                             setCursor(cursor);
130                         }
131                     });
132                 }
133             }, 500);
134         }
135     }
136     
137     private void setCurrentCategory (final CategoryModel.Category category, final boolean repaintAllowed) {
138         CategoryModel.Category oldCategory = model.getCurrent();
139         if (oldCategory != null) {
140             ((CategoryButton) buttons.get (oldCategory.getID())).setNormal ();
141         }
142         if (category != null) {
143             ((CategoryButton) buttons.get (category.getID())).setSelected ();
144         }
145         
146         model.setCurrent(category);
147         // refresh central panel
148
pOptions.removeAll ();
149         final Dimension JavaDoc size;
150         JComponent JavaDoc component = category.getComponent();
151         category.update(coltrollerListener, false);
152         size = component.getSize();
153         pOptions.add("Center",component);
154         // set title
155
Icon JavaDoc icon = category.getIcon ();
156         if (icon != null) {
157             lTitle.setIcon (icon);
158         }
159         lTitle.setText (category.getTitle ());
160         
161         // repaint
162
// repaint
163
SwingUtilities.invokeLater (new Runnable JavaDoc () {
164             public void run () {
165                 if (!checkSize (size) && repaintAllowed) {
166                     revalidate ();
167                     repaint ();
168                 }
169                 if (model.getCurrent() != null) {
170                     ((CategoryButton) buttons.get (model.getCurrentCategoryID())).requestFocus ();
171                 }
172             }
173         });
174         firePropertyChange ("buran" + OptionsPanelController.PROP_HELP_CTX, null, null);
175     }
176         
177     HelpCtx getHelpCtx () {
178         return model.getHelpCtx ();
179     }
180     
181     void update () {
182         model.update(coltrollerListener, true);
183     }
184     
185     void save () {
186         model.save();
187     }
188     
189     void cancel () {
190         model.cancel();
191     }
192     
193     boolean dataValid () {
194         return model.dataValid();
195     }
196     
197     boolean isChanged () {
198         return model.isChanged();
199     }
200     
201     boolean needsReinit() {
202         return model.needsReinit();
203     }
204     
205     // private methods .........................................................
206

207     private void initUI(String JavaDoc categoryName) {
208         // central panel
209
pOptions = new JPanel JavaDoc ();
210         pOptions.setLayout (new BorderLayout JavaDoc ());
211         pOptions.setPreferredSize (getUserSize());
212         JLabel JavaDoc label = new JLabel JavaDoc (loc ("CTL_Loading_Options"));
213         label.setHorizontalAlignment (label.CENTER);
214         pOptions.add ("Center", label);
215
216         // title bar
217
JPanel JavaDoc pTitle = new JPanel JavaDoc (new BorderLayout JavaDoc ());
218         lTitle = new JLabel JavaDoc ();
219         if (Utilities.isWindows ()) {
220             lTitle.setBackground (SystemColor.activeCaption);
221             lTitle.setForeground (SystemColor.activeCaptionText);
222         } else {
223             lTitle.setBackground (Color.white);
224             lTitle.setForeground (Color.black);
225         }
226         Font JavaDoc f = lTitle.getFont ();
227         lTitle.setFont (new Font JavaDoc (f.getName (), Font.BOLD, 16));
228         lTitle.setIconTextGap (8);
229         lTitle.setOpaque (true);
230         if (Utilities.isWindows ()) {
231             pTitle.setBorder (new CompoundBorder JavaDoc (
232                 new LoweredBorder (),
233                 new LineBorder JavaDoc (SystemColor.activeCaption, 1)
234             ));
235         } else {
236             pTitle.setBorder (new CompoundBorder JavaDoc (
237                 new LineBorder JavaDoc (iconViewBorder, 1),
238                 new LineBorder JavaDoc (Color.white, 2)
239             ));
240         }
241         
242         pTitle.add ("Center", lTitle);
243
244         // icon view
245
pCategories2 = new JPanel JavaDoc (new GridBagLayout JavaDoc());
246         pCategories2.setBackground (Color.white);
247         pCategories2.setBorder (null);
248         addCategoryButtons();
249
250         pCategories = new JPanel JavaDoc (new BorderLayout JavaDoc ());
251         if (isMac) {
252             pCategories.setBorder (new CompoundBorder JavaDoc (
253                 new VariableBorder (null, null, borderMac, null),
254                 BorderFactory.createEmptyBorder (0, 4, 0, 4)
255             ));
256         } else {
257             pCategories.setBorder (new LineBorder JavaDoc (iconViewBorder));
258         }
259         pCategories.setBackground (Color.white);
260         pCategories.add ("North", pCategories2);
261         
262         // layout
263
setLayout (new BorderLayout JavaDoc (10, 10));
264         if (isMac) {
265             pOptions.setBorder (new CompoundBorder JavaDoc (
266                 new VariableBorder (null, null, borderMac, null),
267                 BorderFactory.createEmptyBorder (0, 20, 5, 20)
268             ));
269             add (pCategories, BorderLayout.NORTH);
270             add (pOptions, BorderLayout.CENTER);
271         } else {
272             JPanel JavaDoc centralPanel = new JPanel JavaDoc (new BorderLayout JavaDoc (10, 10));
273             centralPanel.add (pTitle, BorderLayout.NORTH);
274             centralPanel.add (pOptions, BorderLayout.CENTER);
275             add (pCategories, BorderLayout.WEST);
276             add (centralPanel, BorderLayout.CENTER);
277             setBorder (new EmptyBorder JavaDoc (10, 10, 0, 10));
278         }
279      
280         categoryName = getCategoryID(categoryName);
281         if (categoryName != null) {
282             CategoryModel.Category c = model.getCategory(getCategoryID(categoryName));
283             Icon JavaDoc icon = c.getIcon();
284             if (icon != null) {
285                 lTitle.setIcon(icon);
286             }
287             lTitle.setText(c.getTitle());
288             
289             CategoryButton b = (CategoryButton) buttons.get(categoryName);
290             if (b != null) {
291                 b.setSelected();
292             }
293         }
294     }
295         
296     private void initActions () {
297         if (getActionMap ().get("UP") == null) {
298             InputMap JavaDoc inputMap = getInputMap
299                 (JComponent.WHEN_IN_FOCUSED_WINDOW);
300             inputMap.put (
301                 isMac ? KeyStroke.getKeyStroke (KeyEvent.VK_LEFT, 0) :
302                         KeyStroke.getKeyStroke (KeyEvent.VK_UP, 0),
303                 "UP"
304             );
305             getActionMap ().put ("UP", new UpAction ());
306             inputMap.put (
307                 KeyStroke.getKeyStroke (KeyEvent.VK_SPACE, 0),
308                 "SPACE"
309             );
310             getActionMap ().put ("SPACE", new SelectCurrentAction ());
311             inputMap.put (
312                 isMac ? KeyStroke.getKeyStroke (KeyEvent.VK_RIGHT, 0) :
313                         KeyStroke.getKeyStroke (KeyEvent.VK_DOWN, 0),
314                 "DOWN"
315             );
316             getActionMap ().put ("DOWN", new DownAction ());
317         }
318     }
319     
320     private void addCategoryButtons () {
321         // remove old buttons
322
Iterator JavaDoc it = buttons.values().iterator ();
323         while (it.hasNext ()) {
324             removeButton ((CategoryButton) it.next ());
325         }
326         pCategories2.removeAll ();
327         buttons = new LinkedHashMap JavaDoc<String JavaDoc, CategoryButton>();
328         
329         // add new buttons
330
String JavaDoc[] names = model.getCategoryIDs();
331         for (int i = 0; i < names.length; i++) {
332             CategoryModel.Category category = model.getCategory(names[i]);
333             addButton (category);
334         }
335         
336         addFakeButton ();
337     }
338                 
339     private void addButton (CategoryModel.Category category) {
340         int index = buttons.size ();
341         CategoryButton button = new CategoryButton (category);
342
343         // add shortcut
344
KeyStroke JavaDoc keyStroke = KeyStroke.getKeyStroke
345             (button.getDisplayedMnemonic (), KeyEvent.ALT_MASK);
346         getInputMap (JComponent.WHEN_IN_FOCUSED_WINDOW).put (keyStroke, button);
347         getActionMap ().put (button, new SelectAction (category));
348
349         if (isMac) {
350             GridBagConstraints JavaDoc gbc = new GridBagConstraints JavaDoc ();
351             gbc.anchor = GridBagConstraints.NORTHWEST;
352             gbc.fill = GridBagConstraints.VERTICAL;
353             gbc.weightx = 0.0;
354             gbc.weighty = 1.0;
355             gbc.gridx = index;
356             gbc.gridy = 0;
357             pCategories2.add (button, gbc);
358         } else {
359             GridBagConstraints JavaDoc gbc = new GridBagConstraints JavaDoc();
360             gbc.anchor = GridBagConstraints.NORTHWEST;
361             gbc.fill = GridBagConstraints.HORIZONTAL;
362             gbc.weightx = 1.0;
363             gbc.weighty = 0.0;
364             gbc.gridx = 0;
365             gbc.gridy = index;
366             pCategories2.add (button, gbc);
367         }
368         buttons.put (category.getID(), button);
369     }
370     
371     private void removeButton (CategoryButton button) {
372         KeyStroke JavaDoc keyStroke = KeyStroke.getKeyStroke
373             (button.getDisplayedMnemonic (), KeyEvent.ALT_MASK);
374         getInputMap (JComponent.WHEN_IN_FOCUSED_WINDOW).remove (keyStroke);
375         getActionMap ().remove (button);
376     }
377     
378     private void addFakeButton () {
379         /* i don't know a better workaround */
380         GridBagConstraints JavaDoc gbc = new GridBagConstraints JavaDoc();
381         gbc.fill = GridBagConstraints.BOTH;
382         gbc.weightx = 1.0;
383         gbc.weighty = 1.0;
384         if (isMac)
385             gbc.gridy = 0;
386         else
387             gbc.gridx = 0;
388         pCategories2.add (new javax.swing.JLabel JavaDoc (""), gbc);
389     }
390         
391     private Dimension JavaDoc getInitSize() {
392         //if necessary init size could be chosen for individual resolutions differently
393
//DisplayMode dm = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode();
394
return new Dimension JavaDoc(750, 500);
395     }
396     
397     Dimension JavaDoc getUserSize() {
398         int w = NbPreferences.forModule(OptionsPanel.class).getInt("OptionsWidth",getInitSize().width);//NOI18N
399
int h = NbPreferences.forModule(OptionsPanel.class).getInt("OptionsHeight",getInitSize().height);//NOI18N
400
return new Dimension JavaDoc (w, h);
401     }
402     
403     void storeUserSize() {
404         Dimension JavaDoc d = pOptions.getSize();
405         NbPreferences.forModule(OptionsPanel.class).putInt("OptionsWidth",d.width);//NOI18N
406
NbPreferences.forModule(OptionsPanel.class).putInt("OptionsHeight",d.height);//NOI18N
407
pOptions.setPreferredSize(d);
408     }
409     
410     private boolean checkSize(Dimension JavaDoc componentSize) {
411         boolean retval = false;
412         Dimension JavaDoc prefSize = pOptions.getPreferredSize();
413         Dimension JavaDoc userSize = getUserSize();
414         componentSize = new Dimension JavaDoc(Math.max(componentSize.width, userSize.width),Math.max(componentSize.height, userSize.height));
415         if (prefSize.width < componentSize.width || prefSize.height < componentSize.height) {
416             Dimension JavaDoc newSize = new Dimension JavaDoc(Math.max(prefSize.width, componentSize.width),Math.max(prefSize.height, componentSize.height));
417             pOptions.setPreferredSize(newSize);
418             Window JavaDoc w = (Window JavaDoc) SwingUtilities.getAncestorOfClass(Window JavaDoc.class, this);
419             invalidate();
420             if (w != null) w.pack();
421             retval = true;
422         }
423         return retval;
424     }
425
426     // innerclasses ............................................................
427

428     private class SelectAction extends AbstractAction JavaDoc {
429         private CategoryModel.Category category;
430         
431         SelectAction (CategoryModel.Category category) {
432             this.category = category;
433         }
434         public void actionPerformed (ActionEvent JavaDoc e) {
435             setCurrentCategory (category, true);
436         }
437     }
438     
439     private class SelectCurrentAction extends AbstractAction JavaDoc {
440         public void actionPerformed (ActionEvent JavaDoc e) {
441             Component JavaDoc c = FocusManager.getCurrentManager ().getFocusOwner ();
442             if (c instanceof CategoryButton) {
443                 setCurrentCategory (((CategoryButton) c).category, true);
444                 ((CategoryButton) c).setSelected ();
445             }
446         }
447     }
448     
449     private class UpAction extends AbstractAction JavaDoc {
450         public void actionPerformed (ActionEvent JavaDoc e) {
451             model.setPreviousCategoryAsCurrent();
452             setCurrentCategory (model.getCurrent(), true);
453         }
454     }
455     
456     private class DownAction extends AbstractAction JavaDoc {
457         public void actionPerformed (ActionEvent JavaDoc e) {
458             model.setNextCategoryAsCurrent();
459             setCurrentCategory (model.getCurrent(), true);
460         }
461     }
462     
463     class ControllerListener implements PropertyChangeListener JavaDoc {
464         public void propertyChange (PropertyChangeEvent JavaDoc evt) {
465             OptionsPanel.this.firePropertyChange
466                 ("buran" + evt.getPropertyName (), null, null);
467         }
468     }
469     
470     class CategoryButton extends JLabel JavaDoc implements MouseListener JavaDoc {
471         private final CategoryModel.Category category;
472         CategoryButton (final CategoryModel.Category category) {
473             super (category.getIcon());
474             this.category = category;
475             Mnemonics.setLocalizedText (this, category.getCategoryName());
476             setOpaque (true);
477             setVerticalTextPosition (BOTTOM);
478             setHorizontalTextPosition (CENTER);
479             setHorizontalAlignment (CENTER);
480             addMouseListener (this);
481             setFocusable (true);
482             setFocusTraversalKeysEnabled (true);
483             setForeground (Color.black);
484             
485             if (isMac) {
486                 setFont(labelFontMac);
487                 setIconTextGap(2);
488             }
489             
490             setNormal ();
491             addFocusListener(new FocusListener JavaDoc() {
492                 public void focusGained(FocusEvent JavaDoc e) {
493                     if (model.getCurrent() != null && !category.isCurrent()) {
494                         setHighlighted();
495                     }
496                 }
497                 public void focusLost(FocusEvent JavaDoc e) {
498                     if (model.getCurrent() != null && !category.isCurrent() && !isMac) {
499                         setNormal();
500                     }
501                 }
502             });
503         }
504         
505         void setNormal () {
506             if (isMac) {
507                 setBorder (new EmptyBorder JavaDoc (5, 6, 3, 6));
508             } else {
509                 setBorder (new EmptyBorder JavaDoc (2, 4, 2, 4));
510             }
511             setBackground (Color.white);
512         }
513         
514         void setSelected () {
515             if (isMac) {
516                 setBorder(new CompoundBorder JavaDoc (
517                         new VariableBorder(null, selectedB, null, selectedB),
518                         BorderFactory.createEmptyBorder(5, 5, 3, 5)
519                         ));
520             } else {
521                 setBorder (new CompoundBorder JavaDoc (
522                     new CompoundBorder JavaDoc (
523                         new LineBorder JavaDoc (Color.white),
524                         new LineBorder JavaDoc (selectedB)
525                     ),
526                     new EmptyBorder JavaDoc (0, 2, 0, 2)
527                 ));
528             }
529             setBackground (selected);
530         }
531         
532         void setHighlighted() {
533             if (!isMac) {
534                 setBorder(new CompoundBorder JavaDoc(
535                         new CompoundBorder JavaDoc(
536                         new LineBorder JavaDoc(Color.white),
537                         new LineBorder JavaDoc(highlightedB)
538                         ),
539                         new EmptyBorder JavaDoc(0, 2, 0, 2)
540                         ));
541                 setBackground(highlighted);
542             }
543             if (!category.isHighlited()) {
544                 if (model.getHighlitedCategoryID() != null) {
545                     CategoryButton b = (CategoryButton)buttons.get(model.getHighlitedCategoryID());
546                     if (b != null && !b.category.isCurrent()) {
547                         b.setNormal();
548                     }
549                 }
550                 model.setHighlited(category);
551             }
552         }
553         
554         public void mouseClicked (MouseEvent JavaDoc e) {
555         }
556
557         public void mousePressed (MouseEvent JavaDoc e) {
558             if (!isMac && model.getCurrent() != null) {
559                 setSelected ();
560             }
561         }
562
563         public void mouseReleased (MouseEvent JavaDoc e) {
564             if (!category.isCurrent() && category.isHighlited() && model.getCurrent() != null) {
565                 setCurrentCategory(category, true);
566             }
567         }
568
569         public void mouseEntered (MouseEvent JavaDoc e) {
570             if (!category.isCurrent() && model.getCurrent() != null) {
571                 setHighlighted ();
572             }
573         }
574
575         public void mouseExited (MouseEvent JavaDoc e) {
576             if (!category.isCurrent() && !isMac && model.getCurrent() != null) {
577                 setNormal ();
578             }
579         }
580     }
581 }
582
Popular Tags