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.form;
043:
044: import java.awt.*;
045: import java.awt.event.*;
046: import javax.swing.*;
047: import javax.swing.border.EmptyBorder;
048: import javax.swing.event.PopupMenuEvent;
049: import javax.swing.event.PopupMenuListener;
050: import javax.swing.event.TreeSelectionEvent;
051: import javax.swing.event.TreeSelectionListener;
052: import javax.swing.tree.*;
053:
054: /**
055: * ComboBox with tree in popup.
056: *
057: * @author Jan Stola
058: */
059: public class ComboBoxWithTree extends JComboBox {
060: /** Window used as popup. */
061: private Window popup;
062: /** Tree in popup. */
063: private JTree tree;
064: /** Scroll pane enclosing the tree. */
065: private JScrollPane scrollPane;
066: /** Converter between tree path and its string representation. */
067: private Converter converter;
068:
069: /**
070: * Creates new <code>ComboBoxWithTree</code>.
071: *
072: * @param treeModel tree model.
073: * @param treeCellRenderer tree cell renderer.
074: * @param converter converter between tree path and its string representation.
075: */
076: public ComboBoxWithTree(TreeModel treeModel,
077: TreeCellRenderer treeCellRenderer, Converter converter) {
078: this .converter = converter;
079: initCombo();
080: initTree(treeModel, treeCellRenderer);
081: }
082:
083: /**
084: * Initializes the combo.
085: */
086: private void initCombo() {
087: setEditable(true);
088: addPopupMenuListener(new PopupMenuListener() {
089: public void popupMenuCanceled(PopupMenuEvent e) {
090: if (issue112997Hack) {
091: setPopupVisible(true);
092: return;
093: }
094: getPopup().setVisible(false);
095: }
096:
097: public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
098: if (issue112997Hack) {
099: issue112997Hack = false;
100: setPopupVisible(true);
101: return;
102: }
103: getPopup().setVisible(false);
104: }
105:
106: public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
107: updateTreeSelection();
108: Dimension dim = getSize();
109: Point p = getLocationOnScreen();
110: Window w = getPopup();
111: w.setLocation(p.x, p.y + dim.height);
112: w.setSize(new Dimension(dim.width, scrollPane
113: .getPreferredSize().height));
114: w.setVisible(true);
115: }
116: });
117: // Get rid of original popup
118: setModel(new DefaultComboBoxModel(new Object[] { "" })); // NOI18N
119: setRenderer(new DefaultListCellRenderer() {
120: @Override
121: public Component getListCellRendererComponent(JList list,
122: Object value, int index, boolean isSelected,
123: boolean cellHasFocus) {
124: if (index == -1) { // Make sure that the combo has a correct preferred size
125: return super .getListCellRendererComponent(list,
126: "null", index, isSelected, cellHasFocus); // NOI18N
127: } else {
128: JLabel comp = new JLabel();
129: comp.setPreferredSize(new Dimension(0, -10000));
130: return comp;
131: }
132: }
133: });
134: }
135:
136: /**
137: * Initializes the tree.
138: *
139: * @param treeModel tree model.
140: * @param treeCellRenderer tree cell renderer.
141: */
142: private void initTree(TreeModel treeModel,
143: TreeCellRenderer treeCellRenderer) {
144: tree = new JTree();
145: TreeSelectionModel selectionModel = new DefaultTreeSelectionModel();
146: selectionModel
147: .setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
148: tree.setRootVisible(false);
149: tree.setShowsRootHandles(true);
150: tree.setSelectionModel(selectionModel);
151: tree.setVisibleRowCount(10);
152: tree.addKeyListener(new KeyAdapter() {
153: @Override
154: public void keyPressed(KeyEvent e) {
155: if (!isPopupVisible()) {
156: setPopupVisible(true);
157: }
158: int code = e.getKeyCode();
159: if ((code == KeyEvent.VK_ENTER)
160: || (code == KeyEvent.VK_ESCAPE)) {
161: setPopupVisible(false);
162: }
163: }
164: });
165: tree.addMouseListener(new MouseAdapter() {
166: @Override
167: public void mouseClicked(MouseEvent e) {
168: if (!isPopupVisible()) {
169: setPopupVisible(true);
170: }
171: if (e.getClickCount() > 1) {
172: setPopupVisible(false);
173: }
174: }
175: });
176: tree.addTreeSelectionListener(new TreeSelectionListener() {
177: public void valueChanged(TreeSelectionEvent e) {
178: String value = converter.pathToString(e.getPath());
179: setSelectedItem(value);
180: }
181: });
182: tree.setModel(treeModel);
183: tree.setCellRenderer(treeCellRenderer);
184: }
185:
186: /**
187: * Returns the popup.
188: *
189: * @return the popup.
190: */
191: private Window getPopup() {
192: if (popup == null) {
193: popup = new Window(SwingUtilities.getWindowAncestor(this ));
194: scrollPane = new JScrollPane(tree);
195: // The scrollPane must be in JPopupMenu to ensure that
196: // it is not closed when components within it obtain the focus
197: scrollPane.setBorder(BorderFactory
198: .createLineBorder(Color.BLACK));
199: JPopupMenu menu = new JPopupMenu() {
200: // Cannot use setVisible(true) on JDK 6 due to changes in isPopupMenu()
201: @Override
202: public boolean isVisible() {
203: return true;
204: }
205: };
206: menu.setBorder(new EmptyBorder(0, 0, 0, 0));
207: menu.setLayout(new BorderLayout());
208: menu.add(scrollPane);
209: popup.add(menu);
210: }
211: return popup;
212: }
213:
214: /**
215: * Updates tree selection according to string in combo.
216: */
217: private void updateTreeSelection() {
218: final TreePath path = getSelectedTreePath();
219: if (path == null) {
220: tree.clearSelection();
221: } else {
222: tree.setSelectionPath(path);
223: EventQueue.invokeLater(new Runnable() {
224: public void run() {
225: tree.scrollPathToVisible(path);
226: }
227: });
228: }
229: }
230:
231: public TreePath getSelectedTreePath() {
232: String value = getEditor().getItem().toString();
233: TreePath path = converter.stringToPath(value);
234: return path;
235: }
236:
237: @Override
238: public void addNotify() {
239: super .addNotify();
240: Container cont = getParent();
241: while (!(cont instanceof Window) && (cont.getParent() != null)) {
242: cont = cont.getParent();
243: }
244: if (cont instanceof Window) {
245: ((Window) cont).addWindowListener(new WindowAdapter() {
246: @Override
247: public void windowDeactivated(WindowEvent e) {
248: if (isPopupVisible()) {
249: issue112997Hack = true;
250: }
251: }
252: });
253: }
254: }
255:
256: private boolean issue112997Hack = false;
257:
258: /**
259: * Converter between tree path and its string representation.
260: */
261: public static interface Converter {
262: /**
263: * Converts tree path to string representation.
264: *
265: * @param path path to convert.
266: * @return string representation of tree path.
267: */
268: String pathToString(TreePath path);
269:
270: /**
271: * Converts string representation to tree path.
272: *
273: * @param value string to convert.
274: * @return tree path that corresponds to the given string representation.
275: */
276: TreePath stringToPath(String value);
277: }
278:
279: }
|