001: /*
002: * Sun Public License Notice
003: *
004: * The contents of this file are subject to the Sun Public License
005: * Version 1.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://www.sun.com/
008: *
009: * The Original Code is NetBeans. The Initial Developer of the Original
010: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
011: * Microsystems, Inc. All Rights Reserved.
012: */
013:
014: package org.netbeans.editor.ext.java;
015:
016: import java.awt.Dialog;
017: import java.awt.event.ActionEvent;
018: import java.awt.event.ActionListener;
019: import java.awt.event.MouseAdapter;
020: import java.awt.event.MouseEvent;
021: import java.util.List;
022:
023: import javax.swing.JButton;
024: import javax.swing.JList;
025: import javax.swing.ListCellRenderer;
026: import javax.swing.text.JTextComponent;
027:
028: import org.netbeans.editor.DialogSupport;
029: import org.netbeans.editor.LocaleSupport;
030: import org.netbeans.editor.Utilities;
031: import org.netbeans.editor.ext.ListCompletionView;
032:
033: /**
034: *
035: * @author Miloslav Metelka
036: * @version 1.0
037: */
038:
039: public class JavaFastImport implements ActionListener {
040:
041: protected JTextComponent target;
042:
043: private String exp;
044:
045: private JavaFastImportPanel panel;
046:
047: private ListCellRenderer cellRenderer;
048:
049: private JList resultList;
050:
051: private Dialog dialog;
052:
053: private JButton[] buttons;
054:
055: public JavaFastImport(JTextComponent target) {
056: this .target = target;
057:
058: exp = Utilities.getSelectionOrIdentifier(target);
059: }
060:
061: public void setDialogVisible(boolean visible) {
062: List result = null;
063: if (visible) {
064: result = evaluate();
065: if (result == null || result.size() == 0) { // no data
066: return;
067: }
068: populate(result);
069: }
070:
071: if (dialog == null) {
072: dialog = createDialog();
073: }
074:
075: getResultList().requestFocus();
076: dialog.setVisible(visible);
077:
078: if (visible) {
079: getPanel().popupNotify();
080:
081: } else {
082: dialog.dispose();
083: }
084: }
085:
086: protected void updateImport(Object item) {
087: }
088:
089: protected ListCellRenderer createCellRenderer() {
090: JCCellRenderer rr = new JCCellRenderer();
091: rr.setClassDisplayFullName(true);
092: return rr;
093: }
094:
095: protected JList createResultList() {
096: JList list = new ListCompletionView(getCellRenderer());
097: list.addMouseListener(new MouseAdapter() {
098: public void mouseClicked(MouseEvent e) {
099: if (e.getClickCount() == 2) {
100: actionPerformed(new ActionEvent(getButtons()[0], 0,
101: ""));
102: }
103: }
104: });
105: return list;
106: }
107:
108: private JButton[] getButtons() {
109: if (buttons == null) {
110: buttons = new JButton[] {
111: new JButton(LocaleSupport.getString(
112: "JFI_importButton", "Import Class")), // NOI18N
113: new JButton(LocaleSupport.getString(
114: "JFI_cancelButton", "Cancel")) // NOI18N
115: };
116: String mnemonic = LocaleSupport.getString(
117: "JFI_importButtonMnemonic", "I"); // NOI18N
118: if (mnemonic != null && mnemonic.length() > 0) {
119: buttons[0].setMnemonic(mnemonic.charAt(0));
120: }
121:
122: mnemonic = LocaleSupport.getString(
123: "JFI_cancelButtonMnemonic", "C"); // NOI18N
124: if (mnemonic != null && mnemonic.length() > 0) {
125: buttons[1].setMnemonic(mnemonic.charAt(0));
126: }
127: buttons[0].getAccessibleContext().setAccessibleDescription(
128: LocaleSupport.getString("ACSD_JFI_importButton")); // NOI18N
129: buttons[1].getAccessibleContext().setAccessibleDescription(
130: LocaleSupport.getString("ACSD_JFI_cancelButton")); // NOI18N
131: }
132:
133: return buttons;
134: }
135:
136: private Dialog createDialog() {
137: String title = LocaleSupport.getString("JFI_title",
138: "Import Class");
139:
140: Dialog dialog = DialogSupport.createDialog(title, getPanel(),
141: true, getButtons(), false, 0, 1, this );
142:
143: return dialog;
144: }
145:
146: private JavaFastImportPanel getPanel() {
147: if (panel == null) {
148: panel = new JavaFastImportPanel(this );
149: }
150: return panel;
151: }
152:
153: ListCellRenderer getCellRenderer() {
154: if (cellRenderer == null) {
155: cellRenderer = createCellRenderer();
156: }
157: return cellRenderer;
158: }
159:
160: JList getResultList() {
161: if (resultList == null) {
162: resultList = createResultList();
163: }
164: return resultList;
165: }
166:
167: List evaluate() {
168: List ret = null;
169:
170: if (exp != null && exp.length() > 0) {
171: JCFinder finder = JavaCompletion.getFinder();
172: ret = finder.findClasses(null, exp, true);
173:
174: /*
175: * Iterator it = ret.iterator(); while (it.hasNext()) { JCClass cls =
176: * (JCClass)it.next(); if (cls.getName().indexOf('.') >= 0) {
177: * it.remove(); } }
178: */
179: }
180:
181: return ret;
182: }
183:
184: void populate(List result) {
185: if (result != null) {
186: if (getResultList() instanceof ListCompletionView) {
187: ((ListCompletionView) getResultList())
188: .setResult(result);
189: }
190: }
191: }
192:
193: public void actionPerformed(ActionEvent evt) {
194: Object src = evt.getSource();
195:
196: if (src == buttons[0]) { // Open button
197: int selIndex = getResultList().getSelectedIndex();
198: if (selIndex >= 0) {
199: updateImport(getResultList().getModel().getElementAt(
200: selIndex));
201: }
202: setDialogVisible(false);
203:
204: } else if (src == buttons[1]) { // Close button
205: setDialogVisible(false);
206: }
207: }
208:
209: }
|