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: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.ui.components;
042:
043: import java.awt.Component;
044: import java.awt.event.ItemEvent;
045: import javax.swing.ComboBoxModel;
046: import javax.swing.DefaultComboBoxModel;
047: import javax.swing.DefaultListCellRenderer;
048: import javax.swing.JComboBox;
049: import javax.swing.JFrame;
050: import javax.swing.JList;
051: import javax.swing.JSeparator;
052: import javax.swing.SwingUtilities;
053: import javax.swing.UIManager;
054:
055: /**
056: * JComboBox which supports JSeparator inside its popup menu.
057: *
058: * @author Jiri Sedlacek
059: */
060: public class JExtendedComboBox extends JComboBox {
061: //~ Inner Classes ------------------------------------------------------------------------------------------------------------
062:
063: private class ExtendedComboListRenderer extends
064: DefaultListCellRenderer {
065: //~ Methods --------------------------------------------------------------------------------------------------------------
066:
067: public Component getListCellRendererComponent(JList list,
068: Object value, int index, boolean isSelected,
069: boolean cellHasFocus) {
070: if ((value != null) && value instanceof JSeparator) {
071: return (JSeparator) value;
072: } else {
073: return super .getListCellRendererComponent(list, value,
074: index, isSelected, cellHasFocus);
075: }
076: }
077: }
078:
079: //~ Instance fields ----------------------------------------------------------------------------------------------------------
080:
081: private DefaultComboBoxModel model = new DefaultComboBoxModel();
082: private boolean closingWithSeparator = false;
083: private int lastSelectedIndex = 0;
084:
085: //~ Constructors -------------------------------------------------------------------------------------------------------------
086:
087: public JExtendedComboBox() {
088: setModel(model);
089: setRenderer(new ExtendedComboListRenderer());
090: }
091:
092: //~ Methods ------------------------------------------------------------------------------------------------------------------
093:
094: public void setModel(ComboBoxModel aModel) {
095: if (!(aModel instanceof DefaultComboBoxModel)) {
096: throw new RuntimeException(
097: "Only DefaultComboBoxModel is supported for this component"); //NOI18N
098: }
099:
100: model = (DefaultComboBoxModel) aModel;
101: super .setModel(model);
102: }
103:
104: public void firePopupMenuWillBecomeInvisible() {
105: if (getSelectedItem() instanceof JSeparator) {
106: closingWithSeparator = true;
107: }
108:
109: super .firePopupMenuWillBecomeInvisible();
110: }
111:
112: protected void fireItemStateChanged(ItemEvent e) {
113: switch (e.getStateChange()) {
114: case ItemEvent.SELECTED:
115:
116: if (e.getItem() instanceof JSeparator) {
117: SwingUtilities.invokeLater(new Runnable() {
118: public void run() {
119: selectNextItem();
120: }
121: });
122:
123: }
124:
125: break;
126: case ItemEvent.DESELECTED:
127:
128: if (!(e.getItem() instanceof JSeparator)) {
129: lastSelectedIndex = model.getIndexOf(e.getItem());
130: }
131:
132: break;
133: }
134:
135: super .fireItemStateChanged(e);
136: }
137:
138: private void selectNextItem() {
139: int currentSelectedIndex = getSelectedIndex();
140:
141: if (closingWithSeparator) {
142: setSelectedIndex(lastSelectedIndex);
143: closingWithSeparator = false;
144: } else if (currentSelectedIndex > lastSelectedIndex) {
145: setSelectedIndex(currentSelectedIndex + 1);
146: } else {
147: setSelectedIndex(currentSelectedIndex - 1);
148: }
149: }
150: }
|