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.awt.BorderLayout;
045: import java.util.Iterator;
046: import java.util.List;
047: import javax.swing.JComponent;
048: import javax.swing.JLabel;
049: import javax.swing.JPanel;
050: import javax.swing.JTabbedPane;
051: import javax.swing.border.EmptyBorder;
052: import javax.swing.event.ChangeEvent;
053: import javax.swing.event.ChangeListener;
054: import org.netbeans.spi.options.OptionsPanelController;
055: import org.openide.util.HelpCtx;
056: import org.openide.util.Lookup;
057: import org.netbeans.modules.options.*;
058: import org.openide.util.LookupEvent;
059: import org.openide.util.LookupListener;
060:
061: /**
062: * Implementation of one panel in Options Dialog.
063: *
064: * @author Jan Jancura
065: */
066: public final class AdvancedPanel extends JPanel {
067: JTabbedPane tabbedPanel;
068: private LookupListener listener = new LookupListenerImpl();
069: private Model model = new Model(listener);
070: private ChangeListener changeListener = new ChangeListener() {
071: public void stateChanged(ChangeEvent e) {
072: handleTabSwitched();
073: }
074: };
075:
076: AdvancedPanel() {
077: }
078:
079: public void update() {
080: int idx = tabbedPanel.getSelectedIndex();
081: if (idx != -1) {
082: String category = tabbedPanel.getTitleAt(idx);
083: model.update(category);
084: }
085: }
086:
087: public void applyChanges() {
088: model.applyChanges();
089: }
090:
091: public void cancel() {
092: model.cancel();
093: }
094:
095: public HelpCtx getHelpCtx() {
096: return model
097: .getHelpCtx((tabbedPanel != null) ? ((JComponent) tabbedPanel
098: .getSelectedComponent())
099: : null);
100: }
101:
102: public boolean dataValid() {
103: return model.isValid();
104: }
105:
106: public boolean isChanged() {
107: return model.isChanged();
108: }
109:
110: public Lookup getLookup() {
111: return model.getLookup();
112: }
113:
114: void init(Lookup masterLookup) {
115: // init components
116: tabbedPanel = new JTabbedPane();
117:
118: // define layout
119: setLayout(new BorderLayout());
120: add(tabbedPanel, BorderLayout.CENTER);
121: initTabbedPane(masterLookup);
122: }
123:
124: private void initTabbedPane(Lookup masterLookup) {
125: tabbedPanel.removeChangeListener(changeListener);
126: tabbedPanel.removeAll();
127: List categories = model.getCategories();
128: tabbedPanel.setVisible(categories.size() > 0);
129: Iterator it = categories.iterator();
130: while (it.hasNext()) {
131: String category = (String) it.next();
132: tabbedPanel.addTab(category, new JLabel(category));
133: }
134: tabbedPanel.addChangeListener(changeListener);
135: handleTabSwitched();
136: }
137:
138: private void handleTabSwitched() {
139: final int selectedIndex = tabbedPanel.getSelectedIndex() >= 0 ? tabbedPanel
140: .getSelectedIndex()
141: : -1;
142: if (selectedIndex != -1) {
143: String category = tabbedPanel.getTitleAt(selectedIndex);
144: if (tabbedPanel.getSelectedComponent() instanceof JLabel) {
145: tabbedPanel.setComponentAt(tabbedPanel
146: .getSelectedIndex(), model.getPanel(category));
147: ((JComponent) tabbedPanel.getSelectedComponent())
148: .setBorder(new EmptyBorder(11, 11, 11, 11));
149: }
150: model.update(category);
151: firePropertyChange(OptionsPanelController.PROP_HELP_CTX,
152: null, null);
153: }
154: }
155:
156: private class LookupListenerImpl implements LookupListener {
157: public void resultChanged(LookupEvent ev) {
158: Lookup masterLookup = model.getLookup();
159: model = new Model(listener);
160: initTabbedPane(masterLookup);
161: }
162: }
163: }
|