1 19 20 package org.netbeans.modules.j2ee.clientproject.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.j2ee.clientproject.AppClientProjectUtil; 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 possibleMainClasses = SourceUtils.getMainClasses(sourcesRoots); 100 if (possibleMainClasses.isEmpty ()) { 101 SwingUtilities.invokeLater( new Runnable () { 102 public void run () { 103 jMainClassList.setListData (new String [] { NbBundle.getMessage (MainClassChooser.class, "LBL_ChooseMainClass_NO_CLASSES_NODE") } ); } 105 }); 106 } else { 107 final ElementHandle<TypeElement>[] arr = possibleMainClasses.toArray(new ElementHandle[possibleMainClasses.size()]); 108 Arrays.sort (arr, new MainClassComparator()); 110 SwingUtilities.invokeLater(new Runnable () { 111 public void run () { 112 jMainClassList.setListData (arr); 113 jMainClassList.setSelectedIndex (0); 114 } 115 }); 116 } 117 } 118 }); 119 120 if (dialogSubtitle != null) { 121 Mnemonics.setLocalizedText (jLabel1, dialogSubtitle); 122 } 123 } 124 125 private Object [] getWarmupList () { 126 return new Object [] {NbBundle.getMessage (MainClassChooser.class, "LBL_ChooseMainClass_WARMUP_MESSAGE")}; } 128 129 private boolean isValidMainClassName (Object value) { 130 return (possibleMainClasses != null) && (possibleMainClasses.contains (value)); 131 } 132 133 134 138 public String getSelectedMainClass () { 139 if (isValidMainClassName (jMainClassList.getSelectedValue ())) { 140 return ((ElementHandle)jMainClassList.getSelectedValue()).getQualifiedName(); 141 } else { 142 return null; 143 } 144 } 145 146 public void addChangeListener (ChangeListener l) { 147 changeListener = l; 148 } 149 150 public void removeChangeListener (ChangeListener l) { 151 changeListener = null; 152 } 153 154 public static Boolean unitTestingSupport_hasMainMethodResult = null; 157 158 164 public static boolean hasMainMethod (FileObject classFO) { 165 return AppClientProjectUtil.hasMainMethod (classFO); 166 } 167 168 173 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 175 176 jLabel1 = new javax.swing.JLabel (); 177 jScrollPane1 = new javax.swing.JScrollPane (); 178 jMainClassList = new javax.swing.JList (); 179 180 setLayout(new java.awt.GridBagLayout ()); 181 182 setPreferredSize(new java.awt.Dimension (380, 300)); 183 getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(MainClassChooser.class).getString("AD_MainClassChooser")); 184 jLabel1.setLabelFor(jMainClassList); 185 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getBundle(MainClassChooser.class).getString("CTL_AvaialableMainClasses")); 186 gridBagConstraints = new java.awt.GridBagConstraints (); 187 gridBagConstraints.gridx = 0; 188 gridBagConstraints.gridy = 0; 189 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 190 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 191 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 192 gridBagConstraints.weightx = 1.0; 193 gridBagConstraints.insets = new java.awt.Insets (12, 12, 2, 12); 194 add(jLabel1, gridBagConstraints); 195 196 jScrollPane1.setMinimumSize(new java.awt.Dimension (100, 200)); 197 jScrollPane1.setViewportView(jMainClassList); 198 jMainClassList.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(MainClassChooser.class).getString("AD_jMainClassList")); 199 200 gridBagConstraints = new java.awt.GridBagConstraints (); 201 gridBagConstraints.gridx = 0; 202 gridBagConstraints.gridy = 1; 203 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 204 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 205 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 206 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 207 gridBagConstraints.weightx = 1.0; 208 gridBagConstraints.weighty = 1.0; 209 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 12); 210 add(jScrollPane1, gridBagConstraints); 211 212 } 214 215 private javax.swing.JLabel jLabel1; 217 private javax.swing.JList jMainClassList; 218 private javax.swing.JScrollPane jScrollPane1; 219 221 private static final class MainClassRenderer extends DefaultListCellRenderer { 222 public Component getListCellRendererComponent (JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 223 String displayName; 224 if (value instanceof String ) { 225 displayName = (String ) value; 226 } if (value instanceof ElementHandle) { 227 displayName = ((ElementHandle)value).getQualifiedName(); 228 } else { 229 displayName = value.toString (); 230 } 231 return super.getListCellRendererComponent (list, displayName, index, isSelected, cellHasFocus); 232 } 233 } 234 235 private static class MainClassComparator implements Comparator <ElementHandle> { 236 237 public int compare(ElementHandle arg0, ElementHandle arg1) { 238 return arg0.getQualifiedName().compareTo(arg1.getQualifiedName()); 239 } 240 } 241 242 } 243 | Popular Tags |