1 19 20 package org.netbeans.modules.project.ui; 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 javax.swing.*; 27 import javax.swing.event.ChangeEvent ; 28 import javax.swing.event.ChangeListener ; 29 import javax.swing.event.ListSelectionEvent ; 30 import javax.swing.event.ListSelectionListener ; 31 import org.netbeans.api.project.Project; 32 import org.netbeans.api.project.ProjectUtils; 33 import org.openide.awt.MouseUtils; 34 35 39 public class NoMainProjectWarning extends JPanel { 40 41 private ChangeListener changeListener; 42 43 44 public NoMainProjectWarning (Project[] projects) { 45 initComponents(); 46 47 Arrays.sort(projects, OpenProjectList.PROJECT_BY_DISPLAYNAME); ProjectsListModel model = new ProjectsListModel (projects); 49 jList1.setModel (model); 50 jList1.setSelectionMode (ListSelectionModel.SINGLE_SELECTION); 51 if (model.getSize () > 0) { 52 jList1.setSelectedIndex (0); 53 } 54 jList1.setCellRenderer(new ProjectsRenderer ()); 55 jList1.addListSelectionListener (new ListSelectionListener () { 56 public void valueChanged (ListSelectionEvent evt) { 57 if (changeListener != null) { 58 changeListener.stateChanged (new ChangeEvent (evt)); 59 } 60 } 61 }); 62 jList1.addMouseListener (new MouseListener () { 64 public void mouseClicked (MouseEvent e) { 65 if (MouseUtils.isDoubleClick (e)) { 66 if (getSelectedProject () != null) { 67 if (changeListener != null) { 68 changeListener.stateChanged (new ChangeEvent (e)); 69 } 70 } 71 } 72 } 73 public void mousePressed (MouseEvent e) {} 74 public void mouseReleased (MouseEvent e) {} 75 public void mouseEntered (MouseEvent e) {} 76 public void mouseExited (MouseEvent e) {} 77 }); 78 } 79 80 84 public Project getSelectedProject () { 85 if (jList1.getSelectedIndex () == -1) { 86 return null; 87 } else { 88 return (Project)jList1.getSelectedValue (); 89 } 90 } 91 92 97 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 99 100 jLabel1 = new javax.swing.JLabel (); 101 jLabel2 = new javax.swing.JLabel (); 102 jScrollPane2 = new javax.swing.JScrollPane (); 103 jList1 = new javax.swing.JList (); 104 105 setLayout(new java.awt.GridBagLayout ()); 106 107 setPreferredSize(new java.awt.Dimension (380, 300)); 108 getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/project/ui/Bundle").getString("AD_NoMainProjectWarninig")); 109 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(NoMainProjectWarning.class, "LBL_NoMainProjectWarning_jLabel1")); 110 gridBagConstraints = new java.awt.GridBagConstraints (); 111 gridBagConstraints.gridx = 0; 112 gridBagConstraints.gridy = 0; 113 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 114 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 115 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 116 gridBagConstraints.weightx = 1.0; 117 gridBagConstraints.insets = new java.awt.Insets (12, 0, 6, 12); 118 add(jLabel1, gridBagConstraints); 119 120 jLabel2.setLabelFor(jList1); 121 org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(NoMainProjectWarning.class, "LBL_NoMainProjectWarning_jLabel2")); 122 gridBagConstraints = new java.awt.GridBagConstraints (); 123 gridBagConstraints.gridx = 0; 124 gridBagConstraints.gridy = 1; 125 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 126 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 127 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 128 gridBagConstraints.weightx = 1.0; 129 gridBagConstraints.insets = new java.awt.Insets (12, 0, 2, 12); 130 add(jLabel2, gridBagConstraints); 131 132 jScrollPane2.setMinimumSize(new java.awt.Dimension (100, 200)); 133 jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); 134 jScrollPane2.setViewportView(jList1); 135 jList1.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/project/ui/Bundle").getString("AD_NoMainProjectWarning_jList1")); 136 137 gridBagConstraints = new java.awt.GridBagConstraints (); 138 gridBagConstraints.gridx = 0; 139 gridBagConstraints.gridy = 2; 140 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 141 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 142 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 143 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 144 gridBagConstraints.weightx = 1.0; 145 gridBagConstraints.weighty = 1.0; 146 gridBagConstraints.insets = new java.awt.Insets (0, 0, 12, 12); 147 add(jScrollPane2, gridBagConstraints); 148 149 } 151 152 private javax.swing.JLabel jLabel1; 154 private javax.swing.JLabel jLabel2; 155 private javax.swing.JList jList1; 156 private javax.swing.JScrollPane jScrollPane2; 157 159 160 public void addChangeListener (ChangeListener l) { 161 changeListener = l; 162 } 163 164 public void removeChangeListener (ChangeListener l) { 165 changeListener = null; 166 } 167 168 private static final class ProjectsListModel extends AbstractListModel { 169 170 private Project[] openProjects; 171 172 public ProjectsListModel (Project[] projects) { 173 openProjects = projects; 174 } 175 176 public synchronized int getSize() { 177 return getProjects ().length; 178 } 179 180 public synchronized Object getElementAt (int index) { 181 if (index >= 0 && index < getProjects ().length) { 182 return getProjects ()[index]; 183 } 184 else { 185 return null; 186 } 187 } 188 189 private Project[] getProjects () { 190 if (openProjects == null) { 191 return new Project[0]; 192 } else { 193 return openProjects; 194 } 195 } 196 } 197 198 private static final class ProjectsRenderer extends JLabel implements ListCellRenderer { 199 ProjectsRenderer () { 200 setOpaque (true); 201 } 202 203 public Component getListCellRendererComponent (JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 204 if (value instanceof Project) { 205 Project prj = (Project) value; 206 setText (ProjectUtils.getInformation (prj).getDisplayName ()); 207 setIcon (ProjectUtils.getInformation (prj).getIcon ()); 208 } else { 209 setText (value.toString ()); 210 setIcon (null); 211 } 212 if (isSelected) { 213 setBackground(list.getSelectionBackground()); 214 setForeground(list.getSelectionForeground()); 215 } 217 else { 218 setBackground(list.getBackground()); 219 setForeground(list.getForeground()); 220 } 222 return this; 223 } 224 } 225 226 } 227 | Popular Tags |