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: package org.netbeans.modules.gsfret.editor.hyperlink;
042:
043: import java.awt.Component;
044: import java.awt.Cursor;
045: import java.awt.Toolkit;
046: import java.awt.event.FocusEvent;
047: import java.awt.event.FocusListener;
048: import java.awt.event.KeyEvent;
049: import java.awt.event.MouseEvent;
050: import java.util.Collections;
051: import java.util.List;
052: import java.util.Set;
053: import javax.swing.DefaultListCellRenderer;
054: import javax.swing.DefaultListModel;
055: import javax.swing.ImageIcon;
056: import javax.swing.JList;
057: import javax.swing.JPanel;
058: import javax.swing.ListModel;
059: import org.netbeans.modules.gsf.api.DeclarationFinder.AlternativeLocation;
060: import org.netbeans.modules.gsf.api.DeclarationFinder.DeclarationLocation;
061: import org.netbeans.modules.gsf.api.ElementHandle;
062: import org.netbeans.modules.gsf.api.ElementKind;
063: import org.netbeans.modules.gsf.api.Modifier;
064: import org.netbeans.napi.gsfret.source.UiUtils;
065: import org.netbeans.modules.gsf.GsfHtmlFormatter;
066: import org.netbeans.modules.gsfret.navigation.Icons;
067: import org.openide.awt.StatusDisplayer;
068: import org.openide.filesystems.FileObject;
069: import org.openide.util.NbBundle;
070:
071: /**
072: * This file is originally from Retouche, the Java Support
073: * infrastructure in NetBeans. I have modified the file as little
074: * as possible to make merging Retouche fixes back as simple as
075: * possible.
076: *
077: * (This used to be IsOverriddenPopup in org.netbeans.modules.java.editor.overridden)
078: *
079: *
080: * @author Jan Lahoda
081: * @author Tor Norbye
082: */
083: public class DeclarationPopup extends JPanel implements FocusListener {
084:
085: private String caption;
086: private List<AlternativeLocation> declarations;
087:
088: /** Creates new form DeclarationPopup */
089: public DeclarationPopup(String caption,
090: List<AlternativeLocation> declarations) {
091: this .caption = caption;
092: this .declarations = declarations;
093:
094: initComponents();
095:
096: jList1
097: .setCursor(Cursor
098: .getPredefinedCursor(Cursor.HAND_CURSOR));
099:
100: addFocusListener(this );
101: }
102:
103: /** This method is called from within the constructor to
104: * initialize the form.
105: * WARNING: Do NOT modify this code. The content of this method is
106: * always regenerated by the Form Editor.
107: */
108: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
109: private void initComponents() {
110: java.awt.GridBagConstraints gridBagConstraints;
111:
112: jLabel1 = new javax.swing.JLabel();
113: jScrollPane1 = new javax.swing.JScrollPane();
114: jList1 = new javax.swing.JList();
115:
116: setFocusCycleRoot(true);
117: setLayout(new java.awt.GridBagLayout());
118:
119: jLabel1
120: .setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
121: jLabel1.setText(caption);
122: jLabel1.setFocusable(false);
123: gridBagConstraints = new java.awt.GridBagConstraints();
124: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
125: add(jLabel1, gridBagConstraints);
126:
127: jList1.setModel(createListModel());
128: jList1.setCellRenderer(new RendererImpl());
129: jList1.setSelectedIndex(0);
130: jList1.setVisibleRowCount(declarations.size());
131: jList1.addKeyListener(new java.awt.event.KeyAdapter() {
132: public void keyPressed(java.awt.event.KeyEvent evt) {
133: jList1KeyPressed(evt);
134: }
135: });
136: jList1.addMouseListener(new java.awt.event.MouseAdapter() {
137: public void mouseClicked(java.awt.event.MouseEvent evt) {
138: jList1MouseClicked(evt);
139: }
140: });
141: jScrollPane1.setViewportView(jList1);
142:
143: gridBagConstraints = new java.awt.GridBagConstraints();
144: gridBagConstraints.gridx = 0;
145: gridBagConstraints.gridy = 1;
146: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
147: gridBagConstraints.weightx = 1.0;
148: gridBagConstraints.weighty = 1.0;
149: add(jScrollPane1, gridBagConstraints);
150: }// </editor-fold>//GEN-END:initComponents
151:
152: private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
153: // TODO add your handling code here:
154: if (evt.getButton() == MouseEvent.BUTTON1
155: && evt.getClickCount() == 1) {
156: openSelected();
157: }
158: }//GEN-LAST:event_jList1MouseClicked
159:
160: private void jList1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jList1KeyPressed
161: // TODO add your handling code here:
162: if (evt.getKeyCode() == KeyEvent.VK_ENTER
163: && evt.getModifiers() == 0) {
164: openSelected();
165: }
166: }//GEN-LAST:event_jList1KeyPressed
167:
168: // Variables declaration - do not modify//GEN-BEGIN:variables
169: private javax.swing.JLabel jLabel1;
170: private javax.swing.JList jList1;
171: private javax.swing.JScrollPane jScrollPane1;
172:
173: // End of variables declaration//GEN-END:variables
174:
175: private void openSelected() {
176: AlternativeLocation desc = (AlternativeLocation) jList1
177: .getSelectedValue();
178:
179: if (desc != null) {
180: DeclarationLocation location = desc.getLocation();
181: if (location == DeclarationLocation.NONE) {
182: StatusDisplayer.getDefault().setStatusText(
183: NbBundle.getMessage(DeclarationPopup.class,
184: "InvalidLoc"));
185: Toolkit.getDefaultToolkit().beep();
186: } else {
187: UiUtils.open(location.getFileObject(), location
188: .getOffset());
189: }
190: }
191:
192: PopupUtil.hidePopup();
193: }
194:
195: private ListModel createListModel() {
196: DefaultListModel dlm = new DefaultListModel();
197:
198: for (AlternativeLocation el : declarations) {
199: dlm.addElement(el);
200: }
201:
202: return dlm;
203: }
204:
205: private static class RendererImpl extends DefaultListCellRenderer {
206: @Override
207: public Component getListCellRendererComponent(JList list,
208: Object value, int index, boolean isSelected,
209: boolean cellHasFocus) {
210: Component c = super .getListCellRendererComponent(list,
211: value, index, isSelected, cellHasFocus);
212:
213: if (value instanceof AlternativeLocation) {
214: AlternativeLocation desc = (AlternativeLocation) value;
215: ElementHandle e = desc.getElement();
216: ImageIcon icon = Icons.getElementIcon(e.getKind(), e
217: .getModifiers());
218: setIcon(icon);
219:
220: GsfHtmlFormatter formatter = new GsfHtmlFormatter();
221: String s = desc.getDisplayHtml(formatter);
222: setText("<html>" + s);
223: }
224:
225: return c;
226: }
227: }
228:
229: public void focusGained(FocusEvent arg0) {
230: jList1.requestFocus();
231: jList1.requestFocusInWindow();
232: }
233:
234: public void focusLost(FocusEvent arg0) {
235: }
236:
237: }
|