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.editor;
043:
044: import java.awt.BorderLayout;
045: import java.beans.PropertyChangeListener;
046: import java.util.ArrayList;
047: import java.util.Collection;
048: import javax.swing.JComponent;
049: import javax.swing.JLabel;
050: import javax.swing.JPanel;
051: import javax.swing.JTabbedPane;
052: import javax.swing.border.EmptyBorder;
053: import org.netbeans.spi.options.OptionsPanelController;
054: import org.openide.awt.Mnemonics;
055: import org.openide.util.HelpCtx;
056: import org.openide.util.Lookup;
057: import org.openide.util.LookupEvent;
058: import org.openide.util.LookupListener;
059: import org.openide.util.WeakListeners;
060: import org.openide.util.lookup.Lookups;
061:
062: /**
063: * Implementation of one panel in Options Dialog.
064: *
065: * @author Jan Jancura
066: */
067: public class FolderBasedController extends OptionsPanelController {
068:
069: private final Lookup.Result<? extends OptionsPanelController> lookupResult;
070: private final LookupListener lookupListener = new LookupListener() {
071: public void resultChanged(LookupEvent ev) {
072: rebuild();
073: }
074: };
075: private final HelpCtx helpCtx;
076:
077: private Collection<? extends OptionsPanelController> delegates;
078: private JComponent component;
079:
080: public FolderBasedController(String path, HelpCtx helpCtx) {
081: this .helpCtx = helpCtx;
082:
083: Lookup lookup = Lookups.forPath(path);
084: lookupResult = lookup
085: .lookupResult(OptionsPanelController.class);
086: lookupResult.addLookupListener(WeakListeners.create(
087: LookupListener.class, lookupListener, lookupResult));
088: rebuild();
089: }
090:
091: public final void update() {
092: Collection<? extends OptionsPanelController> controllers = delegates;
093: for (OptionsPanelController c : controllers) {
094: c.update();
095: }
096: }
097:
098: public final void applyChanges() {
099: Collection<? extends OptionsPanelController> controllers = delegates;
100: for (OptionsPanelController c : controllers) {
101: c.applyChanges();
102: }
103: }
104:
105: public final void cancel() {
106: Collection<? extends OptionsPanelController> controllers = delegates;
107: for (OptionsPanelController c : controllers) {
108: c.cancel();
109: }
110: }
111:
112: public final boolean isValid() {
113: Collection<? extends OptionsPanelController> controllers = delegates;
114: for (OptionsPanelController c : controllers) {
115: if (!c.isValid()) {
116: return false;
117: }
118: }
119: return true;
120: }
121:
122: public final boolean isChanged() {
123: Collection<? extends OptionsPanelController> controllers = delegates;
124: for (OptionsPanelController c : controllers) {
125: if (c.isChanged()) {
126: return true;
127: }
128: }
129: return false;
130: }
131:
132: public final HelpCtx getHelpCtx() {
133: return helpCtx;
134: }
135:
136: public final JComponent getComponent(Lookup masterLookup) {
137: if (component == null) {
138: Collection<JComponent> panels = new ArrayList<JComponent>();
139: Collection<? extends OptionsPanelController> controllers = delegates;
140:
141: for (OptionsPanelController c : controllers) {
142: panels.add(c.getComponent(masterLookup));
143: }
144:
145: component = createComponent(panels);
146: }
147:
148: return component;
149: }
150:
151: protected JComponent createComponent(
152: Collection<? extends JComponent> panels) {
153: return new TabbedPanel(panels);
154: }
155:
156: public final void addPropertyChangeListener(PropertyChangeListener l) {
157: Collection<? extends OptionsPanelController> controllers = delegates;
158: for (OptionsPanelController c : controllers) {
159: c.addPropertyChangeListener(l);
160: }
161: }
162:
163: public final void removePropertyChangeListener(
164: PropertyChangeListener l) {
165: Collection<? extends OptionsPanelController> controllers = delegates;
166: for (OptionsPanelController c : controllers) {
167: c.removePropertyChangeListener(l);
168: }
169: }
170:
171: private void rebuild() {
172: this .delegates = lookupResult.allInstances();
173: this .component = null;
174: }
175:
176: private static final class TabbedPanel extends JPanel {
177:
178: private JTabbedPane tabbedPane = new JTabbedPane();
179:
180: public TabbedPanel(Collection<? extends JComponent> panels) {
181: JLabel label = new JLabel(); // Only for setting tab names
182:
183: for (JComponent p : panels) {
184: p.setBorder(new EmptyBorder(8, 8, 8, 8));
185:
186: String tabName = p.getName();
187: Mnemonics.setLocalizedText(label, tabName);
188: tabbedPane.addTab(label.getText(), p);
189:
190: int idx = Mnemonics.findMnemonicAmpersand(tabName);
191: if (idx != -1 && idx + 1 < tabName.length()) {
192: tabbedPane.setMnemonicAt(
193: tabbedPane.getTabCount() - 1, Character
194: .toUpperCase(tabName
195: .charAt(idx + 1)));
196: }
197: }
198:
199: setLayout(new BorderLayout());
200: add(tabbedPane, BorderLayout.CENTER);
201: }
202: } // End of TabbedPane class
203: }
|