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: package org.netbeans.modules.java.ui;
042:
043: import java.awt.Component;
044: import java.beans.PropertyChangeListener;
045: import java.beans.PropertyChangeSupport;
046: import java.util.Collection;
047: import java.util.HashMap;
048: import java.util.LinkedHashMap;
049: import java.util.LinkedList;
050: import java.util.List;
051: import java.util.Map;
052: import javax.swing.JComponent;
053: import javax.swing.JTabbedPane;
054: import org.netbeans.spi.options.AdvancedOption;
055: import org.netbeans.spi.options.OptionsPanelController;
056: import org.openide.filesystems.FileObject;
057: import org.openide.filesystems.FileSystem;
058: import org.openide.filesystems.Repository;
059: import org.openide.loaders.DataFolder;
060: import org.openide.loaders.FolderLookup;
061: import org.openide.util.HelpCtx;
062: import org.openide.util.Lookup;
063:
064: final class JavaOptionsPanelController extends OptionsPanelController {
065:
066: private static final String TAB_FOLDER = "org.netbeans.modules.java.source/options/";
067:
068: private final PropertyChangeSupport pcs = new PropertyChangeSupport(
069: this );
070:
071: private Map<JComponent, OptionsPanelController> component2Option;
072: private List<AdvancedOption> options;
073:
074: private JTabbedPane pane;
075:
076: public JavaOptionsPanelController() {
077: readPanels();
078: }
079:
080: public void update() {
081: for (OptionsPanelController c : getControllers()) {
082: c.update();
083: }
084: }
085:
086: public void applyChanges() {
087: for (OptionsPanelController c : getControllers()) {
088: c.applyChanges();
089: }
090: }
091:
092: public void cancel() {
093: for (OptionsPanelController c : getControllers()) {
094: c.cancel();
095: }
096: }
097:
098: public boolean isValid() {
099: for (OptionsPanelController c : getControllers()) {
100: if (!c.isValid()) {
101: return false;
102: }
103: }
104: return true;
105: }
106:
107: public boolean isChanged() {
108: for (OptionsPanelController c : getControllers()) {
109: if (c.isChanged()) {
110: return true;
111: }
112: }
113: return false;
114: }
115:
116: public HelpCtx getHelpCtx() {
117: return pane != null ? getHelpCtx(pane.getSelectedComponent())
118: : null;
119: }
120:
121: private HelpCtx getHelpCtx(Component c) {
122: OptionsPanelController o = component2Option.get(c);
123:
124: if (o != null) {
125: return o.getHelpCtx();
126: }
127:
128: return new HelpCtx("netbeans.optionsDialog.java");
129: }
130:
131: public synchronized JComponent getComponent(Lookup masterLookup) {
132: if (pane == null) {
133: pane = new JTabbedPane();
134: component2Option = new HashMap<JComponent, OptionsPanelController>();
135: for (OptionsPanelController c : getControllers()) {
136: JComponent comp = c.getComponent(c.getLookup());
137: pane.add(controllers2Options.get(c).getDisplayName(),
138: comp);
139: component2Option.put(comp, c);
140: }
141: }
142: return pane;
143: }
144:
145: public void addPropertyChangeListener(PropertyChangeListener l) {
146: pcs.addPropertyChangeListener(l);
147: }
148:
149: public void removePropertyChangeListener(PropertyChangeListener l) {
150: pcs.removePropertyChangeListener(l);
151: }
152:
153: // Private methods ---------------------------------------------------------
154:
155: private Map<OptionsPanelController, AdvancedOption> controllers2Options;
156: private List<OptionsPanelController> controllers;
157:
158: private synchronized Collection<OptionsPanelController> getControllers() {
159: if (controllers == null) {
160: controllers2Options = new LinkedHashMap<OptionsPanelController, AdvancedOption>();
161: controllers = new LinkedList<OptionsPanelController>();
162: for (AdvancedOption o : options) {
163: OptionsPanelController c = o.create();
164: controllers2Options.put(c, o);
165: controllers.add(c);
166: }
167: }
168:
169: return controllers;
170: }
171:
172: private void readPanels() {
173: FileSystem fs = Repository.getDefault().getDefaultFileSystem();
174: FileObject fo = fs.getRoot().getFileObject(TAB_FOLDER);
175:
176: DataFolder dataFolder = DataFolder.findFolder(fo);
177: FolderLookup fl = new FolderLookup(dataFolder);
178:
179: Lookup lookup = fl.getLookup();
180:
181: Lookup.Result<AdvancedOption> result = lookup
182: .lookup(new Lookup.Template<AdvancedOption>(
183: AdvancedOption.class));
184:
185: options = new LinkedList<AdvancedOption>();
186:
187: for (AdvancedOption advancedOption : result.allInstances()) {
188: options.add(advancedOption);
189: }
190:
191: }
192: }
|