1 19 20 package org.netbeans.modules.java.j2seproject.ui.customizer; 21 22 import java.awt.Component ; 23 import java.awt.event.MouseEvent ; 24 import java.awt.event.MouseListener ; 25 import java.util.Arrays ; 26 import java.util.Collection ; 27 import java.util.Comparator ; 28 import javax.lang.model.element.TypeElement; 29 import javax.swing.DefaultListCellRenderer ; 30 import javax.swing.JList ; 31 import javax.swing.JPanel ; 32 import javax.swing.ListSelectionModel ; 33 import javax.swing.SwingUtilities ; 34 import javax.swing.event.ChangeEvent ; 35 import javax.swing.event.ChangeListener ; 36 import javax.swing.event.ListSelectionEvent ; 37 import javax.swing.event.ListSelectionListener ; 38 import org.netbeans.api.java.source.ElementHandle; 39 import org.netbeans.api.java.source.SourceUtils; 40 import org.netbeans.modules.java.j2seproject.J2SEProjectUtil; 41 import org.openide.awt.Mnemonics; 42 import org.openide.awt.MouseUtils; 43 import org.openide.filesystems.FileObject; 44 import org.openide.util.NbBundle; 45 import org.openide.util.RequestProcessor; 46 47 51 public class MainClassChooser extends JPanel { 52 53 private ChangeListener changeListener; 54 private String dialogSubtitle = null; 55 private Collection <ElementHandle<TypeElement>> possibleMainClasses; 56 57 58 public MainClassChooser (FileObject[] sourcesRoots) { 59 this (sourcesRoots, null); 60 } 61 62 public MainClassChooser (FileObject[] sourcesRoots, String subtitle) { 63 dialogSubtitle = subtitle; 64 initComponents(); 65 jMainClassList.setCellRenderer(new MainClassRenderer()); 66 initClassesView (sourcesRoots); 67 } 68 69 private void initClassesView (final FileObject[] sourcesRoots) { 70 possibleMainClasses = null; 71 jMainClassList.setSelectionMode (ListSelectionModel.SINGLE_SELECTION); 72 jMainClassList.setListData (getWarmupList ()); 73 jMainClassList.addListSelectionListener (new ListSelectionListener () { 74 public void valueChanged (ListSelectionEvent evt) { 75 if (changeListener != null) { 76 changeListener.stateChanged (new ChangeEvent (evt)); 77 } 78 } 79 }); 80 jMainClassList.addMouseListener (new MouseListener () { 82 public void mouseClicked (MouseEvent e) { 83 if (MouseUtils.isDoubleClick (e)) { 84 if (getSelectedMainClass () != null) { 85 if (changeListener != null) { 86 changeListener.stateChanged (new ChangeEvent (e)); 87 } 88 } 89 } 90 } 91 public void mousePressed (MouseEvent e) {} 92 public void mouseReleased (MouseEvent e) {} 93 public void mouseEntered (MouseEvent e) {} 94 public void mouseExited (MouseEvent e) {} 95 }); 96 97 RequestProcessor.getDefault ().post (new Runnable () { 98 public void run () { 99 100 possibleMainClasses = SourceUtils.getMainClasses(sourcesRoots); 101 if (possibleMainClasses.isEmpty ()) { 102 SwingUtilities.invokeLater( new Runnable () { 103 public void run () { 104 jMainClassList.setListData (new String [] { NbBundle.getMessage (MainClassChooser.class, "LBL_ChooseMainClass_NO_CLASSES_NODE") } ); } 106 }); 107 } else { 108 final ElementHandle<TypeElement>[] arr = possibleMainClasses.toArray(new ElementHandle[possibleMainClasses.size()]); 109 Arrays.sort (arr, new MainClassComparator()); 111 SwingUtilities.invokeLater(new Runnable () { 112 public void run () { 113 jMainClassList.setListData (arr); 114 jMainClassList.setSelectedIndex (0); 115 } 116 }); 117 } 118 } 119 }); 120 121 if (dialogSubtitle != null) { 122 Mnemonics.setLocalizedText (jLabel1, dialogSubtitle); 123 } 124 } 125 126 private Object [] getWarmupList () { 127 return new Object [] {NbBundle.getMessage (MainClassChooser.class, "LBL_ChooseMainClass_WARMUP_MESSAGE")}; 131 } 132 133 private boolean isValidMainClassName (Object value) { 134 return (possibleMainClasses != null) && (possibleMainClasses.contains (value)); 135 } 136 137 138 142 public String getSelectedMainClass () { 143 if (isValidMainClassName (jMainClassList.getSelectedValue ())) { 144 return ((ElementHandle)jMainClassList.getSelectedValue()).getQualifiedName(); 145 } else { 146 return null; 147 } 148 } 149 150 public void addChangeListener (ChangeListener l) { 151 changeListener = l; 152 } 153 154 public void removeChangeListener (ChangeListener l) { 155 changeListener = null; 156 } 157 158 public static Boolean unitTestingSupport_hasMainMethodResult = null; 161 162 168 public static boolean hasMainMethod (FileObject classFO) { 169 return J2SEProjectUtil.hasMainMethod (classFO); 170 } 171 172 177 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 179 180 jLabel1 = new javax.swing.JLabel (); 181 jScrollPane1 = new javax.swing.JScrollPane (); 182 jMainClassList = new javax.swing.JList (); 183 184 setLayout(new java.awt.GridBagLayout ()); 185 186 setPreferredSize(new java.awt.Dimension (380, 300)); 187 getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(MainClassChooser.class).getString("AD_MainClassChooser")); 188 jLabel1.setLabelFor(jMainClassList); 189 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getBundle(MainClassChooser.class).getString("CTL_AvaialableMainClasses")); 190 gridBagConstraints = new java.awt.GridBagConstraints (); 191 gridBagConstraints.gridx = 0; 192 gridBagConstraints.gridy = 0; 193 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 194 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 195 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 196 gridBagConstraints.weightx = 1.0; 197 gridBagConstraints.insets = new java.awt.Insets (12, 12, 2, 12); 198 add(jLabel1, gridBagConstraints); 199 200 jScrollPane1.setMinimumSize(new java.awt.Dimension (100, 200)); 201 jScrollPane1.setViewportView(jMainClassList); 202 jMainClassList.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(MainClassChooser.class).getString("AD_jMainClassList")); 203 204 gridBagConstraints = new java.awt.GridBagConstraints (); 205 gridBagConstraints.gridx = 0; 206 gridBagConstraints.gridy = 1; 207 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 208 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 209 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 210 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 211 gridBagConstraints.weightx = 1.0; 212 gridBagConstraints.weighty = 1.0; 213 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 12); 214 add(jScrollPane1, gridBagConstraints); 215 216 } 218 219 private javax.swing.JLabel jLabel1; 221 private javax.swing.JList jMainClassList; 222 private javax.swing.JScrollPane jScrollPane1; 223 225 226 private static final class MainClassRenderer extends DefaultListCellRenderer { 227 public Component getListCellRendererComponent (JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 228 String displayName; 229 if (value instanceof String ) { 230 displayName = (String ) value; 231 } if (value instanceof ElementHandle) { 232 displayName = ((ElementHandle)value).getQualifiedName(); 233 } else { 234 displayName = value.toString (); 235 } 236 return super.getListCellRendererComponent (list, displayName, index, isSelected, cellHasFocus); 237 } 238 } 239 240 private static class MainClassComparator implements Comparator <ElementHandle> { 241 242 public int compare(ElementHandle arg0, ElementHandle arg1) { 243 return arg0.getQualifiedName().compareTo(arg1.getQualifiedName()); 244 } 245 } 246 247 } 248 | Popular Tags |