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-2007 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.uml.codegen.ui.customizer;
043:
044: import java.awt.Color;
045: import java.awt.Cursor;
046: import java.awt.Font;
047: import java.awt.GridBagConstraints;
048: import java.awt.GridBagLayout;
049: import java.awt.SystemColor;
050: import java.awt.event.ActionEvent;
051: import java.awt.event.ActionListener;
052: import java.awt.event.KeyEvent;
053: import java.awt.event.KeyListener;
054: import java.awt.event.MouseEvent;
055: import java.awt.event.MouseListener;
056: import java.util.HashSet;
057: import java.util.Iterator;
058: import java.util.List;
059: import java.util.Set;
060: import java.util.Vector;
061: import javax.swing.AbstractButton;
062: import javax.swing.ImageIcon;
063: import javax.swing.JButton;
064: import javax.swing.JComponent;
065: import javax.swing.JLabel;
066: import javax.swing.JPanel;
067: import javax.swing.SwingUtilities;
068: import javax.swing.border.EmptyBorder;
069: import javax.swing.border.LineBorder;
070:
071: /**
072: * @author Jan Jancura
073: */
074: public class TabbedPanel extends JPanel {
075: public static int EXPAND_ONE = 1;
076: public static int EXPAND_SOME = 2;
077: public static int EXPAND_ALL = 3;
078:
079: public ImageIcon expanded;
080: public ImageIcon collapsed;
081:
082: private static Color bColor = new Color(Math.max(
083: (int) (SystemColor.control.getRed() * 0.9), 0), Math.max(
084: (int) (SystemColor.control.getGreen() * 0.9), 0), Math.max(
085: (int) (SystemColor.control.getBlue() * 0.9), 0));
086:
087: private TabbedPanelModel model;
088: private int selectedIndex = -1;
089: private Set<Integer> selectedIndexes = new HashSet<Integer>();
090: private int expansionPolicy;
091: private boolean fill;
092: private JComponent selectedComponent;
093: private JComponent[] titles;
094: private Vector<ActionListener> listeners = new Vector<ActionListener>();
095:
096: public TabbedPanel(TabbedPanelModel model, int expansionPolicy,
097: boolean fill) {
098: this .model = model;
099: this .expansionPolicy = expansionPolicy;
100: this .fill = fill;
101: addKeyListener(listener);
102: refreshPanels();
103: setBorder(new LineBorder(new Color(127, 157, 185)));
104:
105: ClassLoader loader = getClass().getClassLoader();
106: expanded = new ImageIcon(
107: loader
108: .getResource("org/netbeans/modules/uml/codegen/resources/expanded.gif")); // NOI18N
109: collapsed = new ImageIcon(
110: loader
111: .getResource("org/netbeans/modules/uml/codegen/resources/collapsed.gif")); // NOI18N
112: }
113:
114: public int getExpansionPolicy() {
115: return expansionPolicy;
116: }
117:
118: public int getSelectedIndex() {
119: return selectedIndex;
120: }
121:
122: public boolean isExpanded(int index) {
123: if (index == selectedIndex)
124: return true;
125:
126: return selectedIndexes.contains(new Integer(index));
127: }
128:
129: public void setSelectedIndex(final int index) {
130: final int ii = index >= 0 ? index : selectedIndex;
131:
132: if (expansionPolicy == EXPAND_ALL) {
133: Integer i = new Integer(index);
134: if (!selectedIndexes.remove(i))
135: selectedIndexes.add(i);
136: }
137:
138: else
139: selectedIndex = index;
140:
141: refreshPanels();
142:
143: SwingUtilities.invokeLater(new Runnable() {
144: public void run() {
145: if (ii >= 0)
146: titles[ii].requestFocus();
147: }
148: });
149: }
150:
151: public JComponent getSelectedComponent() {
152: return selectedComponent;
153: }
154:
155: public String getSelectedCategory() {
156: int i = getSelectedIndex();
157:
158: if (i < 0)
159: return null;
160:
161: return (String) model.getCategories().get(i);
162: }
163:
164: public void addActionListener(ActionListener listener) {
165: listeners.add(listener);
166: }
167:
168: public void removeActionListener(ActionListener listener) {
169: listeners.remove(listener);
170: }
171:
172: protected void fireActionPerformed(ActionEvent event) {
173: Vector vector = (Vector) listeners.clone();
174: Iterator it = vector.iterator();
175:
176: while (it.hasNext())
177: ((ActionListener) it.next()).actionPerformed(event);
178: }
179:
180: public void refreshPanels() {
181: removeAll();
182: selectedComponent = null;
183:
184: if (expansionPolicy == EXPAND_ONE && selectedIndex < 0)
185: selectedIndex = 0;
186:
187: setLayout(new GridBagLayout());
188: GridBagConstraints constraints = new GridBagConstraints();
189: constraints.gridwidth = GridBagConstraints.REMAINDER;
190: constraints.weightx = 1.0;
191: constraints.fill = GridBagConstraints.BOTH;
192: List<String> categories = model.getCategories();
193: int i, k = categories.size(), j = 1;
194: titles = new JComponent[k];
195:
196: for (i = 0; i < k; i++) {
197: String category = categories.get(i);
198:
199: JComponent comp = createTitleComponent(category, model
200: .getToolTip(category), i);
201:
202: add(comp, constraints);
203: titles[i] = comp;
204:
205: if ((expansionPolicy == EXPAND_ALL && selectedIndexes
206: .contains(new Integer(i)))
207: || i == selectedIndex) {
208: selectedComponent = model.getPanel(categories.get(i));
209: add(selectedComponent, constraints);
210: }
211: }
212:
213: constraints.weighty = 1.0;
214: JPanel fooPanel = new JPanel();
215: fooPanel.setOpaque(false);
216: add(fooPanel, constraints);
217: revalidate();
218: repaint();
219: fireActionPerformed(new ActionEvent(this , 0, "selectedIndex")); // NOI18N
220: }
221:
222: protected JComponent createTitleComponent(String name,
223: String toolTip, int index) {
224: JLabel label = new JLabel(name,
225: index == selectedIndex ? expanded : collapsed,
226: JButton.LEFT);
227:
228: label.setFont(label.getFont().deriveFont(Font.BOLD));
229: label.setBackground(bColor);
230: label.setOpaque(true);
231: label.putClientProperty("index", new Integer(index)); // NOI18N
232: label.addMouseListener(listener);
233: label.setBorder(new EmptyBorder(1, 1, 1, 1));
234: label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
235: return label;
236: }
237:
238: private Listener listener = new Listener();
239:
240: private class Listener implements ActionListener, KeyListener,
241: MouseListener {
242: public void actionPerformed(ActionEvent e) {
243: AbstractButton b = (AbstractButton) e.getSource();
244: int i = ((Integer) b.getClientProperty("index")).intValue(); // NOI18N
245:
246: if (i == selectedIndex)
247: setSelectedIndex(-1);
248:
249: else
250: setSelectedIndex(i);
251: }
252:
253: public void keyTyped(KeyEvent e) {
254: }
255:
256: public void keyPressed(KeyEvent e) {
257: }
258:
259: public void keyReleased(KeyEvent e) {
260: }
261:
262: public void mouseClicked(MouseEvent e) {
263: if (!(e.getSource() instanceof JLabel))
264: return;
265:
266: JLabel l = (JLabel) e.getSource();
267: int i = ((Integer) l.getClientProperty("index")).intValue(); // NOI18N
268:
269: if (i == selectedIndex) {
270: if (expansionPolicy != EXPAND_ONE)
271: setSelectedIndex(-1);
272: } else
273: setSelectedIndex(i);
274: }
275:
276: public void mousePressed(MouseEvent e) {
277: }
278:
279: public void mouseReleased(MouseEvent e) {
280: }
281:
282: public void mouseEntered(MouseEvent e) {
283: if (!(e.getSource() instanceof JLabel))
284: return;
285: JLabel l = (JLabel) e.getSource();
286: l.setBackground(SystemColor.control);
287: revalidate();
288: repaint();
289: }
290:
291: public void mouseExited(MouseEvent e) {
292: if (!(e.getSource() instanceof JLabel))
293: return;
294: JLabel l = (JLabel) e.getSource();
295: l.setBackground(bColor);
296: revalidate();
297: repaint();
298: }
299: };
300: }
|