1 19 20 package org.netbeans.modules.java.editor.codegen.ui; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.event.KeyEvent ; 25 import java.util.List ; 26 import javax.swing.DefaultListCellRenderer ; 27 import javax.swing.DefaultListModel ; 28 import javax.swing.JList ; 29 import javax.swing.KeyStroke ; 30 import javax.swing.text.JTextComponent ; 31 import org.netbeans.modules.java.editor.codegen.CodeGenerator; 32 import org.netbeans.modules.java.editor.overridden.PopupUtil; 33 34 38 public class GenerateCodePanel extends javax.swing.JPanel { 39 40 private JTextComponent component; 41 42 43 public GenerateCodePanel(JTextComponent component, List <? 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 (jList1)); 54 jList1.grabFocus(); 55 } 56 57 62 private void initComponents() { 64 65 jScrollPane1 = new javax.swing.JScrollPane (); 66 jList1 = new javax.swing.JList (); 67 jLabel1 = new javax.swing.JLabel (); 68 69 setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color (64, 64, 64))); 70 setLayout(new java.awt.BorderLayout ()); 71 72 jScrollPane1.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 4, 4, 4)); 73 74 jList1.addKeyListener(new java.awt.event.KeyAdapter () { 75 public void keyReleased(java.awt.event.KeyEvent evt) { 76 listKeyReleased(evt); 77 } 78 }); 79 jList1.addMouseListener(new java.awt.event.MouseAdapter () { 80 public void mouseReleased(java.awt.event.MouseEvent 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")); jLabel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 4, 4, 4)); 90 jLabel1.setOpaque(true); 91 add(jLabel1, java.awt.BorderLayout.PAGE_START); 92 } 94 private void listMouseReleased(java.awt.event.MouseEvent evt) { invokeSelected(); 96 } 98 private void listKeyReleased(java.awt.event.KeyEvent evt) { KeyStroke ks = KeyStroke.getKeyStrokeForEvent(evt); 100 if (ks.getKeyCode() == KeyEvent.VK_ENTER || ks.getKeyCode() == KeyEvent.VK_SPACE) 101 invokeSelected(); 102 } 104 105 public javax.swing.JLabel jLabel1; 107 public javax.swing.JList jList1; 108 public javax.swing.JScrollPane jScrollPane1; 109 111 private DefaultListModel createModel(List <? extends CodeGenerator> generators) { 112 DefaultListModel model = new DefaultListModel (); 113 for (CodeGenerator generator : generators) 114 model.addElement(generator); 115 return model; 116 } 117 118 private void invokeSelected() { 119 PopupUtil.hidePopup(); 120 Object value = jList1.getSelectedValue(); 121 if (value instanceof CodeGenerator) 122 ((CodeGenerator)value).invoke(component); 123 } 124 125 private static class Renderer extends DefaultListCellRenderer { 126 127 private static int DARKER_COLOR_COMPONENT = 5; 128 129 private Color fgColor; 130 private Color bgColor; 131 private Color bgColorDarker; 132 private Color bgSelectionColor; 133 private Color fgSelectionColor; 134 135 public Renderer(JList list) { 136 setFont(list.getFont()); 137 fgColor = list.getForeground(); 138 bgColor = list.getBackground(); 139 bgColorDarker = new Color (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 147 public Component getListCellRendererComponent(JList list, Object 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 |