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.options.advanced;
043:
044: import java.beans.PropertyChangeListener;
045: import java.text.Collator;
046: import java.util.ArrayList;
047: import java.util.Collections;
048: import java.util.HashMap;
049: import java.util.Iterator;
050: import java.util.List;
051: import java.util.Map;
052: import javax.swing.JComponent;
053: import javax.swing.border.Border;
054: import javax.swing.border.CompoundBorder;
055: import javax.swing.border.EmptyBorder;
056: import org.netbeans.modules.options.ui.TabbedPanelModel;
057: import org.netbeans.spi.options.AdvancedOption;
058: import org.netbeans.spi.options.OptionsPanelController;
059: import org.openide.util.HelpCtx;
060: import org.openide.util.Lookup;
061: import org.openide.util.Lookup.Result;
062: import org.openide.util.LookupListener;
063: import org.openide.util.lookup.Lookups;
064: import org.openide.util.lookup.ProxyLookup;
065:
066: /**
067: *
068: * @author Jan Jancura
069: */
070: public final class Model extends TabbedPanelModel {
071:
072: private Map<String, AdvancedOption> categoryToOption = new HashMap<String, AdvancedOption>();
073: private Map<String, JComponent> categoryToPanel = new HashMap<String, JComponent>();
074: private Map<String, OptionsPanelController> categoryToController = new HashMap<String, OptionsPanelController>();
075: private Lookup masterLookup;
076: private LookupListener lkpListener;
077: private Result<AdvancedOption> lkpResult;
078:
079: public Model(LookupListener listener) {
080: this .lkpListener = listener;
081: }
082:
083: public List getCategories() {
084: init();
085: List<String> l = new ArrayList<String>(categoryToOption
086: .keySet());
087: Collections.sort(l, Collator.getInstance());
088: return l;
089: }
090:
091: public String getToolTip(String category) {
092: AdvancedOption option = categoryToOption.get(category);
093: return option.getTooltip();
094: }
095:
096: public JComponent getPanel(String category) {
097: init();
098: JComponent panel = categoryToPanel.get(category);
099: if (panel != null)
100: return panel;
101: AdvancedOption option = categoryToOption.get(category);
102: OptionsPanelController controller = new DelegatingController(
103: option.create());
104: categoryToController.put(category, controller);
105: panel = controller.getComponent(masterLookup);
106: categoryToPanel.put(category, panel);
107: Border b = panel.getBorder();
108: if (b != null)
109: b = new CompoundBorder(new EmptyBorder(6, 16, 6, 6), b);
110: else
111: b = new EmptyBorder(6, 16, 6, 6);
112: panel.setBorder(b);
113: //panel.setBackground (Color.white);
114: panel.setMaximumSize(panel.getPreferredSize());
115: return panel;
116: }
117:
118: // implementation ..........................................................
119: void update(String category) {
120: OptionsPanelController controller = categoryToController
121: .get(category);
122: if (controller != null) {
123: controller.update();
124: }
125: }
126:
127: void applyChanges() {
128: Iterator it = categoryToController.values().iterator();
129: while (it.hasNext())
130: ((OptionsPanelController) it.next()).applyChanges();
131: }
132:
133: void cancel() {
134: Iterator it = categoryToController.values().iterator();
135: while (it.hasNext())
136: ((OptionsPanelController) it.next()).cancel();
137: }
138:
139: boolean isValid() {
140: Iterator it = categoryToController.values().iterator();
141: while (it.hasNext())
142: if (!((OptionsPanelController) it.next()).isValid())
143: return false;
144: return true;
145: }
146:
147: boolean isChanged() {
148: Iterator it = categoryToController.values().iterator();
149: while (it.hasNext())
150: if (((OptionsPanelController) it.next()).isChanged())
151: return true;
152: return false;
153: }
154:
155: Lookup getLookup() {
156: List<Lookup> lookups = new ArrayList<Lookup>();
157: Iterator<OptionsPanelController> it = categoryToController
158: .values().iterator();
159: while (it.hasNext())
160: lookups.add(it.next().getLookup());
161: return new ProxyLookup(lookups.toArray(new Lookup[lookups
162: .size()]));
163: }
164:
165: HelpCtx getHelpCtx(JComponent panel) {
166: Iterator it = categoryToPanel.keySet().iterator();
167: while (it.hasNext()) {
168: String category = (String) it.next();
169: if (panel == null || panel == categoryToPanel.get(category)) {
170: OptionsPanelController controller = categoryToController
171: .get(category);
172: if (controller != null) {
173: return controller.getHelpCtx();
174: }
175: }
176: }
177: return new HelpCtx("netbeans.optionsDialog.advanced");
178: }
179:
180: private boolean initialized = false;
181:
182: private void init() {
183: if (initialized)
184: return;
185: initialized = true;
186:
187: Lookup lookup = Lookups.forPath("OptionsDialog/Advanced"); // NOI18N
188: lkpResult = lookup.lookup(new Lookup.Template<AdvancedOption>(
189: AdvancedOption.class));
190: lkpResult.addLookupListener(lkpListener);
191: lkpListener = null;
192: Iterator<? extends AdvancedOption> it = lkpResult
193: .allInstances().iterator();
194: while (it.hasNext()) {
195: AdvancedOption option = it.next();
196: categoryToOption.put(option.getDisplayName(), option);
197: }
198: }
199:
200: void setLoookup(Lookup masterLookup) {
201: this .masterLookup = masterLookup;
202: }
203:
204: private static final class DelegatingController extends
205: OptionsPanelController {
206: private OptionsPanelController delegate;
207: private boolean isUpdated;
208:
209: private DelegatingController(OptionsPanelController delegate) {
210: this .delegate = delegate;
211: }
212:
213: public void update() {
214: if (!isUpdated) {
215: isUpdated = true;
216: delegate.update();
217: }
218: }
219:
220: public void applyChanges() {
221: isUpdated = false;
222: delegate.applyChanges();
223: }
224:
225: public void cancel() {
226: isUpdated = false;
227: delegate.cancel();
228: }
229:
230: public boolean isValid() {
231: return delegate.isValid();
232: }
233:
234: public boolean isChanged() {
235: return delegate.isChanged();
236: }
237:
238: public JComponent getComponent(Lookup masterLookup) {
239: return delegate.getComponent(masterLookup);
240: }
241:
242: public HelpCtx getHelpCtx() {
243: return delegate.getHelpCtx();
244: }
245:
246: public void addPropertyChangeListener(PropertyChangeListener l) {
247: delegate.addPropertyChangeListener(l);
248: }
249:
250: public void removePropertyChangeListener(
251: PropertyChangeListener l) {
252: delegate.removePropertyChangeListener(l);
253: }
254: }
255: }
|