1 19 20 package org.netbeans.modules.ruby.rubyproject.ui.customizer; 21 22 import java.awt.event.MouseEvent ; 23 import java.awt.event.MouseListener ; 24 import java.util.Arrays ; 25 import java.util.List ; 26 import javax.swing.JPanel ; 27 import javax.swing.ListSelectionModel ; 28 import javax.swing.SwingUtilities ; 29 import javax.swing.event.ChangeEvent ; 30 import javax.swing.event.ChangeListener ; 31 import javax.swing.event.ListSelectionEvent ; 32 import javax.swing.event.ListSelectionListener ; 33 import org.netbeans.modules.ruby.rubyproject.RubyProjectUtil; 34 import org.openide.awt.Mnemonics; 36 import org.openide.awt.MouseUtils; 37 import org.openide.filesystems.FileObject; 38 import org.openide.util.NbBundle; 39 import org.openide.util.RequestProcessor; 40 41 45 public class MainClassChooser extends JPanel { 46 47 private ChangeListener changeListener; 48 private String dialogSubtitle = null; 49 private List possibleMainClasses; 50 51 52 public MainClassChooser (FileObject[] sourcesRoots) { 53 this (sourcesRoots, null); 54 } 55 56 public MainClassChooser (FileObject[] sourcesRoots, String subtitle) { 57 dialogSubtitle = subtitle; 58 initComponents(); 59 initClassesView (sourcesRoots); 60 } 61 62 private void initClassesView (final FileObject[] sourcesRoots) { 63 possibleMainClasses = null; 64 jMainClassList.setSelectionMode (ListSelectionModel.SINGLE_SELECTION); 65 jMainClassList.setListData (getWarmupList ()); 66 jMainClassList.addListSelectionListener (new ListSelectionListener () { 67 public void valueChanged (ListSelectionEvent evt) { 68 if (changeListener != null) { 69 changeListener.stateChanged (new ChangeEvent (evt)); 70 } 71 } 72 }); 73 jMainClassList.addMouseListener (new MouseListener () { 75 public void mouseClicked (MouseEvent e) { 76 if (MouseUtils.isDoubleClick (e)) { 77 if (getSelectedMainClass () != null) { 78 if (changeListener != null) { 79 changeListener.stateChanged (new ChangeEvent (e)); 80 } 81 } 82 } 83 } 84 public void mousePressed (MouseEvent e) {} 85 public void mouseReleased (MouseEvent e) {} 86 public void mouseEntered (MouseEvent e) {} 87 public void mouseExited (MouseEvent e) {} 88 }); 89 90 RequestProcessor.getDefault ().post (new Runnable () { 91 public void run () { 92 possibleMainClasses = RubyProjectUtil.getMainClasses (sourcesRoots); 93 if (possibleMainClasses.isEmpty ()) { 94 SwingUtilities.invokeLater( new Runnable () { 95 public void run () { 96 jMainClassList.setListData (new String [] { NbBundle.getMessage (MainClassChooser.class, "LBL_ChooseMainClass_NO_CLASSES_NODE") } ); } 98 }); 99 } else { 100 final Object [] arr = possibleMainClasses.toArray (); 101 Arrays.sort (arr); 103 SwingUtilities.invokeLater(new Runnable () { 104 public void run () { 105 jMainClassList.setListData (arr); 106 jMainClassList.setSelectedIndex (0); 107 } 108 }); 109 } 110 } 111 }); 112 113 if (dialogSubtitle != null) { 114 Mnemonics.setLocalizedText (jLabel1, dialogSubtitle); 115 } 116 } 117 118 private Object [] getWarmupList () { 119 return new Object [] {NbBundle.getMessage (MainClassChooser.class, "LBL_ChooseMainClass_WARMUP_MESSAGE")}; 123 } 124 125 private boolean isValidMainClassName (Object value) { 126 return (possibleMainClasses != null) && (possibleMainClasses.contains (value)); 127 } 128 129 130 134 public String getSelectedMainClass () { 135 if (isValidMainClassName (jMainClassList.getSelectedValue ())) { 136 return (String )jMainClassList.getSelectedValue (); 137 } else { 138 return null; 139 } 140 } 141 142 public void addChangeListener (ChangeListener l) { 143 changeListener = l; 144 } 145 146 public void removeChangeListener (ChangeListener l) { 147 changeListener = null; 148 } 149 150 public static Boolean unitTestingSupport_hasMainMethodResult = null; 153 154 160 public static boolean hasMainMethod (FileObject classFO) { 161 return RubyProjectUtil.hasMainMethod (classFO); 162 } 163 164 169 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 171 172 jLabel1 = new javax.swing.JLabel (); 173 jScrollPane1 = new javax.swing.JScrollPane (); 174 jMainClassList = new javax.swing.JList (); 175 176 setLayout(new java.awt.GridBagLayout ()); 177 178 setPreferredSize(new java.awt.Dimension (380, 300)); 179 getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(MainClassChooser.class).getString("AD_MainClassChooser")); 180 jLabel1.setLabelFor(jMainClassList); 181 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getBundle(MainClassChooser.class).getString("CTL_AvaialableMainClasses")); 182 gridBagConstraints = new java.awt.GridBagConstraints (); 183 gridBagConstraints.gridx = 0; 184 gridBagConstraints.gridy = 0; 185 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 186 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 187 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 188 gridBagConstraints.weightx = 1.0; 189 gridBagConstraints.insets = new java.awt.Insets (12, 12, 2, 12); 190 add(jLabel1, gridBagConstraints); 191 192 jScrollPane1.setMinimumSize(new java.awt.Dimension (100, 200)); 193 jScrollPane1.setViewportView(jMainClassList); 194 jMainClassList.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(MainClassChooser.class).getString("AD_jMainClassList")); 195 196 gridBagConstraints = new java.awt.GridBagConstraints (); 197 gridBagConstraints.gridx = 0; 198 gridBagConstraints.gridy = 1; 199 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 200 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 201 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 202 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 203 gridBagConstraints.weightx = 1.0; 204 gridBagConstraints.weighty = 1.0; 205 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 12); 206 add(jScrollPane1, gridBagConstraints); 207 208 } 210 211 private javax.swing.JLabel jLabel1; 213 private javax.swing.JList jMainClassList; 214 private javax.swing.JScrollPane jScrollPane1; 215 217 } 233 | Popular Tags |