KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > overridden > IsOverriddenPopup


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.editor.overridden;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.Cursor JavaDoc;
23 import java.awt.Toolkit JavaDoc;
24 import java.awt.event.FocusEvent JavaDoc;
25 import java.awt.event.FocusListener JavaDoc;
26 import java.awt.event.KeyEvent JavaDoc;
27 import java.awt.event.MouseEvent JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.lang.model.element.Element;
30 import javax.lang.model.element.ExecutableElement;
31 import javax.swing.DefaultListCellRenderer JavaDoc;
32 import javax.swing.DefaultListModel JavaDoc;
33 import javax.swing.JList JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35 import javax.swing.ListModel JavaDoc;
36 import org.netbeans.api.java.source.JavaSource;
37 import org.netbeans.api.java.source.SourceUtils;
38 import org.netbeans.api.java.source.UiUtils;
39 import org.openide.filesystems.FileObject;
40
41 /**
42  *
43  * @author Jan Lahoda
44  */

45 public class IsOverriddenPopup extends JPanel JavaDoc implements FocusListener JavaDoc {
46     
47     private String JavaDoc caption;
48     private List JavaDoc<ElementDescription> declarations;
49     
50     /** Creates new form IsOverriddenPopup */
51     public IsOverriddenPopup(String JavaDoc caption, List JavaDoc<ElementDescription> declarations) {
52         this.caption = caption;
53         this.declarations = declarations;
54         
55         initComponents();
56         
57         jList1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
58         
59         addFocusListener(this);
60     }
61     
62     /** This method is called from within the constructor to
63      * initialize the form.
64      * WARNING: Do NOT modify this code. The content of this method is
65      * always regenerated by the Form Editor.
66      */

67     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
68
private void initComponents() {
69         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
70
71         jLabel1 = new javax.swing.JLabel JavaDoc();
72         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
73         jList1 = new javax.swing.JList JavaDoc();
74
75         setFocusCycleRoot(true);
76         setLayout(new java.awt.GridBagLayout JavaDoc());
77
78         jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
79         jLabel1.setText(caption
80         );
81         jLabel1.setFocusable(false);
82         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
83         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
84         add(jLabel1, gridBagConstraints);
85
86         jList1.setModel(createListModel());
87         jList1.setCellRenderer(new RendererImpl());
88         jList1.setSelectedIndex(0);
89         jList1.setVisibleRowCount(declarations.size()
90         );
91         jList1.addKeyListener(new java.awt.event.KeyAdapter JavaDoc() {
92             public void keyPressed(java.awt.event.KeyEvent JavaDoc evt) {
93                 jList1KeyPressed(evt);
94             }
95         });
96         jList1.addMouseListener(new java.awt.event.MouseAdapter JavaDoc() {
97             public void mouseClicked(java.awt.event.MouseEvent JavaDoc evt) {
98                 jList1MouseClicked(evt);
99             }
100         });
101         jScrollPane1.setViewportView(jList1);
102
103         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
104         gridBagConstraints.gridx = 0;
105         gridBagConstraints.gridy = 1;
106         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
107         gridBagConstraints.weightx = 1.0;
108         gridBagConstraints.weighty = 1.0;
109         add(jScrollPane1, gridBagConstraints);
110     }// </editor-fold>//GEN-END:initComponents
111

112     private void jList1MouseClicked(java.awt.event.MouseEvent JavaDoc evt) {//GEN-FIRST:event_jList1MouseClicked
113
// TODO add your handling code here:
114
if (evt.getButton() == MouseEvent.BUTTON1 && evt.getClickCount() == 1) {
115             openSelected();
116         }
117     }//GEN-LAST:event_jList1MouseClicked
118

119     private void jList1KeyPressed(java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_jList1KeyPressed
120
// TODO add your handling code here:
121
if (evt.getKeyCode() == KeyEvent.VK_ENTER && evt.getModifiers() == 0) {
122             openSelected();
123         }
124     }//GEN-LAST:event_jList1KeyPressed
125

126     
127     // Variables declaration - do not modify//GEN-BEGIN:variables
128
private javax.swing.JLabel JavaDoc jLabel1;
129     private javax.swing.JList JavaDoc jList1;
130     private javax.swing.JScrollPane JavaDoc jScrollPane1;
131     // End of variables declaration//GEN-END:variables
132

133     private void openSelected() {
134         ElementDescription desc = (ElementDescription) jList1.getSelectedValue();
135         
136         if (desc != null) {
137             FileObject file = desc.getSourceFile();
138             
139             if (file != null) {
140                 UiUtils.open(file, desc.getHandle());
141             } else {
142                 Toolkit.getDefaultToolkit().beep();
143             }
144         }
145         
146         PopupUtil.hidePopup();
147     }
148     
149     private ListModel JavaDoc createListModel() {
150         DefaultListModel JavaDoc dlm = new DefaultListModel JavaDoc();
151         
152         for (ElementDescription el: declarations) {
153             dlm.addElement(el);
154         }
155         
156         return dlm;
157     }
158     
159     private static class RendererImpl extends DefaultListCellRenderer JavaDoc {
160         public Component JavaDoc getListCellRendererComponent(
161                 JList JavaDoc list,
162                 Object JavaDoc value,
163                 int index,
164                 boolean isSelected,
165                 boolean cellHasFocus) {
166             Component JavaDoc c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
167             
168             if (value instanceof ElementDescription) {
169                 ElementDescription desc = (ElementDescription) value;
170                 
171                 setIcon(desc.getIcon());
172                 setText(desc.getDisplayName());
173             }
174             
175             return c;
176         }
177     }
178     
179     public void focusGained(FocusEvent JavaDoc arg0) {
180         jList1.requestFocus();
181         jList1.requestFocusInWindow();
182     }
183     
184     public void focusLost(FocusEvent JavaDoc arg0) {
185     }
186     
187 }
188
Popular Tags