KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > codegen > ui > GenerateCodePanel


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
20 package org.netbeans.modules.java.editor.codegen.ui;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.event.KeyEvent JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.DefaultListCellRenderer JavaDoc;
27 import javax.swing.DefaultListModel JavaDoc;
28 import javax.swing.JList JavaDoc;
29 import javax.swing.KeyStroke JavaDoc;
30 import javax.swing.text.JTextComponent JavaDoc;
31 import org.netbeans.modules.java.editor.codegen.CodeGenerator;
32 import org.netbeans.modules.java.editor.overridden.PopupUtil;
33
34 /**
35  *
36  * @author Dusan Balek, Jan Lahoda
37  */

38 public class GenerateCodePanel extends javax.swing.JPanel JavaDoc {
39
40     private JTextComponent JavaDoc component;
41     
42     /** Creates new form GenerateCodePanel */
43     public GenerateCodePanel(JTextComponent JavaDoc component, List JavaDoc<? extends CodeGenerator> generators) {
44         this.component = component;
45         initComponents();
46         setFocusable(false);
47         setNextFocusableComponent(jList1);
48         setBackground(jList1.getBackground());
49         jScrollPane1.setBackground(jList1.getBackground());
50         jList1.setModel(createModel(generators));
51         jList1.setSelectedIndex(0);
52         jList1.setVisibleRowCount(generators.size() > 8 ? 8 : generators.size());
53         jList1.setCellRenderer(new Renderer JavaDoc(jList1));
54         jList1.grabFocus();
55     }
56     
57     /** This method is called from within the constructor to
58      * initialize the form.
59      * WARNING: Do NOT modify this code. The content of this method is
60      * always regenerated by the Form Editor.
61      */

62     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
63
private void initComponents() {
64
65         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
66         jList1 = new javax.swing.JList JavaDoc();
67         jLabel1 = new javax.swing.JLabel JavaDoc();
68
69         setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color JavaDoc(64, 64, 64)));
70         setLayout(new java.awt.BorderLayout JavaDoc());
71
72         jScrollPane1.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 4, 4, 4));
73
74         jList1.addKeyListener(new java.awt.event.KeyAdapter JavaDoc() {
75             public void keyReleased(java.awt.event.KeyEvent JavaDoc evt) {
76                 listKeyReleased(evt);
77             }
78         });
79         jList1.addMouseListener(new java.awt.event.MouseAdapter JavaDoc() {
80             public void mouseReleased(java.awt.event.MouseEvent JavaDoc evt) {
81                 listMouseReleased(evt);
82             }
83         });
84         jScrollPane1.setViewportView(jList1);
85
86         add(jScrollPane1, java.awt.BorderLayout.CENTER);
87
88         jLabel1.setText(org.openide.util.NbBundle.getMessage(CodeGenerator.class, "LBL_generate_code")); // NOI18N
89
jLabel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 4, 4, 4));
90         jLabel1.setOpaque(true);
91         add(jLabel1, java.awt.BorderLayout.PAGE_START);
92     }// </editor-fold>//GEN-END:initComponents
93

94     private void listMouseReleased(java.awt.event.MouseEvent JavaDoc evt) {//GEN-FIRST:event_listMouseReleased
95
invokeSelected();
96     }//GEN-LAST:event_listMouseReleased
97

98     private void listKeyReleased(java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_listKeyReleased
99
KeyStroke JavaDoc ks = KeyStroke.getKeyStrokeForEvent(evt);
100         if (ks.getKeyCode() == KeyEvent.VK_ENTER || ks.getKeyCode() == KeyEvent.VK_SPACE)
101             invokeSelected();
102     }//GEN-LAST:event_listKeyReleased
103

104     
105     // Variables declaration - do not modify//GEN-BEGIN:variables
106
public javax.swing.JLabel JavaDoc jLabel1;
107     public javax.swing.JList JavaDoc jList1;
108     public javax.swing.JScrollPane JavaDoc jScrollPane1;
109     // End of variables declaration//GEN-END:variables
110

111     private DefaultListModel JavaDoc createModel(List JavaDoc<? extends CodeGenerator> generators) {
112         DefaultListModel JavaDoc model = new DefaultListModel JavaDoc();
113         for (CodeGenerator generator : generators)
114             model.addElement(generator);
115         return model;
116     }
117     
118     private void invokeSelected() {
119         PopupUtil.hidePopup();
120         Object JavaDoc value = jList1.getSelectedValue();
121         if (value instanceof CodeGenerator)
122             ((CodeGenerator)value).invoke(component);
123     }
124             
125     private static class Renderer extends DefaultListCellRenderer JavaDoc {
126         
127         private static int DARKER_COLOR_COMPONENT = 5;
128         
129         private Color JavaDoc fgColor;
130         private Color JavaDoc bgColor;
131         private Color JavaDoc bgColorDarker;
132         private Color JavaDoc bgSelectionColor;
133         private Color JavaDoc fgSelectionColor;
134         
135         public Renderer(JList JavaDoc list) {
136             setFont(list.getFont());
137             fgColor = list.getForeground();
138             bgColor = list.getBackground();
139             bgColorDarker = new Color JavaDoc(Math.abs(bgColor.getRed() - DARKER_COLOR_COMPONENT),
140                     Math.abs(bgColor.getGreen() - DARKER_COLOR_COMPONENT),
141                     Math.abs(bgColor.getBlue() - DARKER_COLOR_COMPONENT));
142             bgSelectionColor = list.getSelectionBackground();
143             fgSelectionColor = list.getSelectionForeground();
144         }
145         
146         @Override JavaDoc
147         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index,
148                 boolean isSelected, boolean hasFocus) {
149             if (isSelected) {
150                 setForeground(fgSelectionColor);
151                 setBackground(bgSelectionColor);
152             } else {
153                 setForeground(fgColor);
154                 setBackground(index % 2 == 0 ? bgColor : bgColorDarker);
155             }
156             setText(value instanceof CodeGenerator ? ((CodeGenerator)value).getDisplayName() : value.toString());
157             return this;
158         }
159     }
160 }
161
Popular Tags