KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > NoMainProjectWarning


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.project.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.event.MouseEvent JavaDoc;
24 import java.awt.event.MouseListener JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import javax.swing.*;
27 import javax.swing.event.ChangeEvent JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
29 import javax.swing.event.ListSelectionEvent JavaDoc;
30 import javax.swing.event.ListSelectionListener JavaDoc;
31 import org.netbeans.api.project.Project;
32 import org.netbeans.api.project.ProjectUtils;
33 import org.openide.awt.MouseUtils;
34
35 /** Show a warning that no main project is set and allows choose it.
36  *
37  * @author Jiri Rechtacek
38  */

39 public class NoMainProjectWarning extends JPanel {
40     
41     private ChangeListener JavaDoc changeListener;
42     
43     /** Creates new form NoMainProjectWarning */
44     public NoMainProjectWarning (Project[] projects) {
45         initComponents();
46
47         Arrays.sort(projects, OpenProjectList.PROJECT_BY_DISPLAYNAME); // #88907
48
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 JavaDoc () {
56             public void valueChanged (ListSelectionEvent JavaDoc evt) {
57                 if (changeListener != null) {
58                     changeListener.stateChanged (new ChangeEvent JavaDoc (evt));
59                 }
60             }
61         });
62         // support for double click to finish dialog with selected class
63
jList1.addMouseListener (new MouseListener JavaDoc () {
64             public void mouseClicked (MouseEvent JavaDoc e) {
65                 if (MouseUtils.isDoubleClick (e)) {
66                     if (getSelectedProject () != null) {
67                         if (changeListener != null) {
68                             changeListener.stateChanged (new ChangeEvent JavaDoc (e));
69                         }
70                     }
71                 }
72             }
73             public void mousePressed (MouseEvent JavaDoc e) {}
74             public void mouseReleased (MouseEvent JavaDoc e) {}
75             public void mouseEntered (MouseEvent JavaDoc e) {}
76             public void mouseExited (MouseEvent JavaDoc e) {}
77         });
78     }
79     
80     /** Returns the selected project or null if no project is selected.
81      *
82      * @return project or null if no project is selected
83      */

84     public Project getSelectedProject () {
85         if (jList1.getSelectedIndex () == -1) {
86             return null;
87         } else {
88             return (Project)jList1.getSelectedValue ();
89         }
90     }
91
92     /** This method is called from within the constructor to
93      * initialize the form.
94      * WARNING: Do NOT modify this code. The content of this method is
95      * always regenerated by the Form Editor.
96      */

97     private void initComponents() {//GEN-BEGIN:initComponents
98
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
99
100         jLabel1 = new javax.swing.JLabel JavaDoc();
101         jLabel2 = new javax.swing.JLabel JavaDoc();
102         jScrollPane2 = new javax.swing.JScrollPane JavaDoc();
103         jList1 = new javax.swing.JList JavaDoc();
104
105         setLayout(new java.awt.GridBagLayout JavaDoc());
106
107         setPreferredSize(new java.awt.Dimension JavaDoc(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 JavaDoc();
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 JavaDoc(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 JavaDoc();
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 JavaDoc(12, 0, 2, 12);
130         add(jLabel2, gridBagConstraints);
131
132         jScrollPane2.setMinimumSize(new java.awt.Dimension JavaDoc(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 JavaDoc();
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 JavaDoc(0, 0, 12, 12);
147         add(jScrollPane2, gridBagConstraints);
148
149     }//GEN-END:initComponents
150

151
152     // Variables declaration - do not modify//GEN-BEGIN:variables
153
private javax.swing.JLabel JavaDoc jLabel1;
154     private javax.swing.JLabel JavaDoc jLabel2;
155     private javax.swing.JList JavaDoc jList1;
156     private javax.swing.JScrollPane JavaDoc jScrollPane2;
157     // End of variables declaration//GEN-END:variables
158

159
160     public void addChangeListener (ChangeListener JavaDoc l) {
161         changeListener = l;
162     }
163     
164     public void removeChangeListener (ChangeListener JavaDoc 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 JavaDoc 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 JavaDoc getListCellRendererComponent (JList list, Object JavaDoc 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                 //setBorder (BorderFactory.createLineBorder (Color.BLACK));
216
}
217             else {
218                 setBackground(list.getBackground());
219                 setForeground(list.getForeground());
220                 //setBorder (null);
221
}
222             return this;
223         }
224     }
225
226 }
227
Popular Tags