KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > advanced > Model


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.advanced;
21
22 import java.awt.Color JavaDoc;
23 import java.text.Collator JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import javax.swing.JComponent JavaDoc;
31 import javax.swing.border.Border JavaDoc;
32 import javax.swing.border.CompoundBorder JavaDoc;
33 import javax.swing.border.EmptyBorder JavaDoc;
34 import org.netbeans.modules.options.ui.TabbedPanelModel;
35 import org.netbeans.spi.options.AdvancedOption;
36 import org.netbeans.spi.options.OptionsPanelController;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.Repository;
39 import org.openide.loaders.DataFolder;
40 import org.openide.loaders.FolderLookup;
41 import org.openide.util.HelpCtx;
42 import org.openide.util.Lookup;
43 import org.openide.util.lookup.ProxyLookup;
44
45 /**
46  *
47  * @author Jan Jancura
48  */

49 public final class Model extends TabbedPanelModel {
50     
51     private Map JavaDoc<String JavaDoc,AdvancedOption> categoryToOption = new HashMap JavaDoc<String JavaDoc,AdvancedOption>();
52     private Map JavaDoc<String JavaDoc, JComponent JavaDoc> categoryToPanel = new HashMap JavaDoc<String JavaDoc, JComponent JavaDoc> ();
53     private Map JavaDoc<String JavaDoc, OptionsPanelController> categoryToController = new HashMap JavaDoc<String JavaDoc, OptionsPanelController>();
54     private Lookup masterLookup;
55
56     
57     public List JavaDoc getCategories () {
58         init ();
59         List JavaDoc<String JavaDoc> l = new ArrayList JavaDoc<String JavaDoc>(categoryToOption.keySet ());
60         Collections.sort(l, Collator.getInstance());
61         return l;
62     }
63     
64     public String JavaDoc getToolTip (String JavaDoc category) {
65         AdvancedOption option = (AdvancedOption) categoryToOption.get (category);
66         return option.getTooltip ();
67     }
68     
69     public JComponent JavaDoc getPanel (String JavaDoc category) {
70         init ();
71         JComponent JavaDoc panel = (JComponent JavaDoc) categoryToPanel.get (category);
72         if (panel != null) return panel;
73         AdvancedOption option = (AdvancedOption) categoryToOption.get (category);
74         OptionsPanelController controller = option.create ();
75         categoryToController.put (category, controller);
76         panel = controller.getComponent (masterLookup);
77         categoryToPanel.put (category, panel);
78         Border JavaDoc b = panel.getBorder ();
79         if (b != null)
80             b = new CompoundBorder JavaDoc (
81                 new EmptyBorder JavaDoc (6, 16, 6, 6),
82                 b
83             );
84         else
85             b = new EmptyBorder JavaDoc (6, 16, 6, 6);
86         panel.setBorder (b);
87         panel.setBackground (Color.white);
88         panel.setMaximumSize (panel.getPreferredSize ());
89         return panel;
90     }
91     
92     
93     // implementation ..........................................................
94

95     void update () {
96         Iterator JavaDoc it = categoryToController.values ().iterator ();
97         while (it.hasNext ())
98             ((OptionsPanelController) it.next ()).update ();
99     }
100     
101     void applyChanges () {
102         Iterator JavaDoc it = categoryToController.values ().iterator ();
103         while (it.hasNext ())
104             ((OptionsPanelController) it.next ()).applyChanges ();
105     }
106     
107     void cancel () {
108         Iterator JavaDoc it = categoryToController.values ().iterator ();
109         while (it.hasNext ())
110             ((OptionsPanelController) it.next ()).cancel ();
111     }
112     
113     boolean isValid () {
114         Iterator JavaDoc it = categoryToController.values ().iterator ();
115         while (it.hasNext ())
116             if (!((OptionsPanelController) it.next ()).isValid ())
117                 return false;
118         return true;
119     }
120     
121     boolean isChanged () {
122         Iterator JavaDoc it = categoryToController.values ().iterator ();
123         while (it.hasNext ())
124             if (((OptionsPanelController) it.next ()).isChanged ())
125                 return true;
126         return false;
127     }
128     
129     Lookup getLookup () {
130         List JavaDoc<Lookup> lookups = new ArrayList JavaDoc<Lookup> ();
131         Iterator JavaDoc<OptionsPanelController> it = categoryToController.values ().iterator ();
132         while (it.hasNext ())
133             lookups.add (it.next ().getLookup ());
134         return new ProxyLookup
135             ((Lookup[]) lookups.toArray (new Lookup [lookups.size ()]));
136     }
137     
138     HelpCtx getHelpCtx (JComponent JavaDoc panel) {
139         Iterator JavaDoc it = categoryToPanel.keySet ().iterator ();
140         while (it.hasNext ()) {
141             String JavaDoc category = (String JavaDoc) it.next ();
142             if (panel == categoryToPanel.get (category)) {
143                 OptionsPanelController controller = (OptionsPanelController)
144                     categoryToController.get (category);
145                 return controller.getHelpCtx ();
146             }
147         }
148         return new HelpCtx ("netbeans.optionsDialog.advanced");
149     }
150     
151     private boolean initialized = false;
152     
153     private void init () {
154         if (initialized) return;
155         initialized = true;
156         FileObject fo = Repository.getDefault ().getDefaultFileSystem ().
157             findResource ("OptionsDialog/Advanced");
158         if (fo == null) return;
159         Lookup lookup = new FolderLookup (DataFolder.findFolder (fo)).getLookup ();
160         Iterator JavaDoc<? extends AdvancedOption> it = lookup.lookup (new Lookup.Template<AdvancedOption> (AdvancedOption.class)).
161                 allInstances ().iterator ();
162         while (it.hasNext ()) {
163             AdvancedOption option = it.next ();
164             categoryToOption.put (option.getDisplayName (), option);
165         }
166     }
167     
168     void setLoookup (Lookup masterLookup) {
169         this.masterLookup = masterLookup;
170     }
171 }
172
173
174
Popular Tags