001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.project.ui;
043:
044: import java.awt.Component;
045: import java.awt.event.MouseEvent;
046: import java.awt.event.MouseListener;
047: import java.util.Arrays;
048: import javax.swing.*;
049: import javax.swing.event.ChangeEvent;
050: import javax.swing.event.ChangeListener;
051: import javax.swing.event.ListSelectionEvent;
052: import javax.swing.event.ListSelectionListener;
053: import org.netbeans.api.project.Project;
054: import org.netbeans.api.project.ProjectUtils;
055: import org.openide.awt.MouseUtils;
056:
057: /** Show a warning that no main project is set and allows choose it.
058: *
059: * @author Jiri Rechtacek
060: */
061: public class NoMainProjectWarning extends JPanel {
062:
063: private ChangeListener changeListener;
064:
065: /** Creates new form NoMainProjectWarning */
066: public NoMainProjectWarning(Project[] projects) {
067: initComponents();
068:
069: Arrays.sort(projects, OpenProjectList.PROJECT_BY_DISPLAYNAME); // #88907
070: ProjectsListModel model = new ProjectsListModel(projects);
071: jList1.setModel(model);
072: jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
073: if (model.getSize() > 0) {
074: jList1.setSelectedIndex(0);
075: }
076: jList1.setCellRenderer(new ProjectsRenderer());
077: jList1.addListSelectionListener(new ListSelectionListener() {
078: public void valueChanged(ListSelectionEvent evt) {
079: if (changeListener != null) {
080: changeListener.stateChanged(new ChangeEvent(evt));
081: }
082: }
083: });
084: // support for double click to finish dialog with selected class
085: jList1.addMouseListener(new MouseListener() {
086: public void mouseClicked(MouseEvent e) {
087: if (MouseUtils.isDoubleClick(e)) {
088: if (getSelectedProject() != null) {
089: if (changeListener != null) {
090: changeListener
091: .stateChanged(new ChangeEvent(e));
092: }
093: }
094: }
095: }
096:
097: public void mousePressed(MouseEvent e) {
098: }
099:
100: public void mouseReleased(MouseEvent e) {
101: }
102:
103: public void mouseEntered(MouseEvent e) {
104: }
105:
106: public void mouseExited(MouseEvent e) {
107: }
108: });
109: }
110:
111: /** Returns the selected project or null if no project is selected.
112: *
113: * @return project or null if no project is selected
114: */
115: public Project getSelectedProject() {
116: if (jList1.getSelectedIndex() == -1) {
117: return null;
118: } else {
119: return (Project) jList1.getSelectedValue();
120: }
121: }
122:
123: /** This method is called from within the constructor to
124: * initialize the form.
125: * WARNING: Do NOT modify this code. The content of this method is
126: * always regenerated by the Form Editor.
127: */
128: private void initComponents() {//GEN-BEGIN:initComponents
129: java.awt.GridBagConstraints gridBagConstraints;
130:
131: jLabel1 = new javax.swing.JLabel();
132: jLabel2 = new javax.swing.JLabel();
133: jScrollPane2 = new javax.swing.JScrollPane();
134: jList1 = new javax.swing.JList();
135:
136: setLayout(new java.awt.GridBagLayout());
137:
138: setPreferredSize(new java.awt.Dimension(380, 300));
139: getAccessibleContext().setAccessibleDescription(
140: java.util.ResourceBundle.getBundle(
141: "org/netbeans/modules/project/ui/Bundle")
142: .getString("AD_NoMainProjectWarninig"));
143: org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
144: org.openide.util.NbBundle.getMessage(
145: NoMainProjectWarning.class,
146: "LBL_NoMainProjectWarning_jLabel1"));
147: gridBagConstraints = new java.awt.GridBagConstraints();
148: gridBagConstraints.gridx = 0;
149: gridBagConstraints.gridy = 0;
150: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
151: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
152: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
153: gridBagConstraints.weightx = 1.0;
154: gridBagConstraints.insets = new java.awt.Insets(12, 0, 6, 12);
155: add(jLabel1, gridBagConstraints);
156:
157: jLabel2.setLabelFor(jList1);
158: org.openide.awt.Mnemonics.setLocalizedText(jLabel2,
159: org.openide.util.NbBundle.getMessage(
160: NoMainProjectWarning.class,
161: "LBL_NoMainProjectWarning_jLabel2"));
162: gridBagConstraints = new java.awt.GridBagConstraints();
163: gridBagConstraints.gridx = 0;
164: gridBagConstraints.gridy = 1;
165: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
166: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
167: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
168: gridBagConstraints.weightx = 1.0;
169: gridBagConstraints.insets = new java.awt.Insets(12, 0, 2, 12);
170: add(jLabel2, gridBagConstraints);
171:
172: jScrollPane2.setMinimumSize(new java.awt.Dimension(100, 200));
173: jList1
174: .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
175: jScrollPane2.setViewportView(jList1);
176: jList1.getAccessibleContext().setAccessibleDescription(
177: java.util.ResourceBundle.getBundle(
178: "org/netbeans/modules/project/ui/Bundle")
179: .getString("AD_NoMainProjectWarning_jList1"));
180:
181: gridBagConstraints = new java.awt.GridBagConstraints();
182: gridBagConstraints.gridx = 0;
183: gridBagConstraints.gridy = 2;
184: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
185: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
186: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
187: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
188: gridBagConstraints.weightx = 1.0;
189: gridBagConstraints.weighty = 1.0;
190: gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 12);
191: add(jScrollPane2, gridBagConstraints);
192:
193: }//GEN-END:initComponents
194:
195: // Variables declaration - do not modify//GEN-BEGIN:variables
196: private javax.swing.JLabel jLabel1;
197: private javax.swing.JLabel jLabel2;
198: private javax.swing.JList jList1;
199: private javax.swing.JScrollPane jScrollPane2;
200:
201: // End of variables declaration//GEN-END:variables
202:
203: public void addChangeListener(ChangeListener l) {
204: changeListener = l;
205: }
206:
207: public void removeChangeListener(ChangeListener l) {
208: changeListener = null;
209: }
210:
211: private static final class ProjectsListModel extends
212: AbstractListModel {
213:
214: private Project[] openProjects;
215:
216: public ProjectsListModel(Project[] projects) {
217: openProjects = projects;
218: }
219:
220: public synchronized int getSize() {
221: return getProjects().length;
222: }
223:
224: public synchronized Object getElementAt(int index) {
225: if (index >= 0 && index < getProjects().length) {
226: return getProjects()[index];
227: } else {
228: return null;
229: }
230: }
231:
232: private Project[] getProjects() {
233: if (openProjects == null) {
234: return new Project[0];
235: } else {
236: return openProjects;
237: }
238: }
239: }
240:
241: private static final class ProjectsRenderer extends JLabel
242: implements ListCellRenderer {
243: ProjectsRenderer() {
244: setOpaque(true);
245: }
246:
247: public Component getListCellRendererComponent(JList list,
248: Object value, int index, boolean isSelected,
249: boolean cellHasFocus) {
250: if (value instanceof Project) {
251: Project prj = (Project) value;
252: setText(ProjectUtils.getInformation(prj)
253: .getDisplayName());
254: setIcon(ProjectUtils.getInformation(prj).getIcon());
255: } else {
256: setText(value.toString());
257: setIcon(null);
258: }
259: if (isSelected) {
260: setBackground(list.getSelectionBackground());
261: setForeground(list.getSelectionForeground());
262: //setBorder (BorderFactory.createLineBorder (Color.BLACK));
263: } else {
264: setBackground(list.getBackground());
265: setForeground(list.getForeground());
266: //setBorder (null);
267: }
268: return this;
269: }
270: }
271:
272: }
|