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.apisupport.project.ui.wizard.project;
043:
044: import java.awt.event.ItemEvent;
045: import java.awt.event.ItemListener;
046: import java.io.File;
047: import java.io.IOException;
048: import java.util.Arrays;
049: import javax.swing.DefaultComboBoxModel;
050: import javax.swing.JFileChooser;
051: import javax.swing.SwingUtilities;
052: import org.netbeans.api.project.Project;
053: import org.netbeans.api.project.ProjectManager;
054: import org.netbeans.api.project.ProjectUtils;
055: import org.netbeans.api.project.Sources;
056: import org.netbeans.api.project.ui.OpenProjects;
057: import org.netbeans.modules.apisupport.project.Util;
058: import org.netbeans.modules.apisupport.project.ui.UIUtil;
059: import org.netbeans.modules.apisupport.project.ui.wizard.BasicWizardIterator;
060: import org.netbeans.spi.project.ui.support.ProjectChooser;
061: import org.openide.ErrorManager;
062: import org.openide.WizardDescriptor;
063: import org.openide.filesystems.FileObject;
064: import org.openide.filesystems.FileUtil;
065: import org.openide.util.HelpCtx;
066: import org.openide.util.NbBundle;
067:
068: /**
069: * Wizard for creating new project templates - selection of project to become template.
070: *
071: * @author Milos Kleint
072: */
073: final class SelectProjectPanel extends BasicWizardIterator.Panel {
074:
075: private NewProjectIterator.DataModel data;
076: private final Object EMPTY = getMessage("MSG_No_Projects");
077:
078: /** Creates new form SelectProjectPanel */
079: public SelectProjectPanel(WizardDescriptor setting,
080: NewProjectIterator.DataModel data) {
081: super (setting);
082: this .data = data;
083: initComponents();
084: initAccessibility();
085: putClientProperty("NewFileWizard_Title",
086: getMessage("LBL_ProjectWizardTitle"));
087: loadComboBox();
088: comProject.addItemListener(new ItemListener() {
089: public void itemStateChanged(ItemEvent e) {
090: checkValidity();
091: }
092: });
093: comProject.setRenderer(UIUtil.createProjectRenderer());
094: }
095:
096: private static String getMessage(String key) {
097: return NbBundle.getMessage(SelectProjectPanel.class, key);
098: }
099:
100: /** This method is called from within the constructor to
101: * initialize the form.
102: * WARNING: Do NOT modify this code. The content of this method is
103: * always regenerated by the Form Editor.
104: */
105: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
106: private void initComponents() {
107: java.awt.GridBagConstraints gridBagConstraints;
108:
109: lblProject = new javax.swing.JLabel();
110: comProject = new javax.swing.JComboBox();
111: btnProject = new javax.swing.JButton();
112: pnlHeightAdjuster = new javax.swing.JPanel();
113:
114: setLayout(new java.awt.GridBagLayout());
115:
116: lblProject.setLabelFor(comProject);
117: org.openide.awt.Mnemonics.setLocalizedText(lblProject,
118: org.openide.util.NbBundle.getMessage(
119: SelectProjectPanel.class, "LBL_Project_Label"));
120: gridBagConstraints = new java.awt.GridBagConstraints();
121: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
122: gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 0);
123: add(lblProject, gridBagConstraints);
124:
125: gridBagConstraints = new java.awt.GridBagConstraints();
126: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
127: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
128: gridBagConstraints.weightx = 0.1;
129: gridBagConstraints.insets = new java.awt.Insets(12, 12, 0, 0);
130: add(comProject, gridBagConstraints);
131:
132: org.openide.awt.Mnemonics.setLocalizedText(btnProject,
133: org.openide.util.NbBundle.getMessage(
134: SelectProjectPanel.class, "LBL_Browse"));
135: btnProject
136: .addActionListener(new java.awt.event.ActionListener() {
137: public void actionPerformed(
138: java.awt.event.ActionEvent evt) {
139: btnProjectActionPerformed(evt);
140: }
141: });
142:
143: gridBagConstraints = new java.awt.GridBagConstraints();
144: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
145: gridBagConstraints.insets = new java.awt.Insets(12, 12, 0, 0);
146: add(btnProject, gridBagConstraints);
147:
148: gridBagConstraints = new java.awt.GridBagConstraints();
149: gridBagConstraints.gridx = 0;
150: gridBagConstraints.gridy = 1;
151: gridBagConstraints.gridwidth = 3;
152: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
153: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
154: gridBagConstraints.weighty = 0.1;
155: add(pnlHeightAdjuster, gridBagConstraints);
156:
157: }
158:
159: // </editor-fold>//GEN-END:initComponents
160:
161: private void btnProjectActionPerformed(
162: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProjectActionPerformed
163: JFileChooser chooser = ProjectChooser.projectChooser();
164: int res = chooser.showOpenDialog(SwingUtilities
165: .getWindowAncestor(this ));
166: if (res == JFileChooser.APPROVE_OPTION) {
167: File fil = chooser.getSelectedFile();
168: FileObject fo = FileUtil.toFileObject(fil);
169: if (fo != null) {
170: try {
171: Project p = ProjectManager.getDefault()
172: .findProject(fo);
173: DefaultComboBoxModel model = (DefaultComboBoxModel) comProject
174: .getModel();
175: model.addElement(p);
176: model.setSelectedItem(p);
177: if (EMPTY == model.getElementAt(0)) {
178: model.removeElement(EMPTY);
179: }
180: } catch (IOException exc) {
181: ErrorManager.getDefault().notify(exc);
182: }
183: }
184: }
185: }//GEN-LAST:event_btnProjectActionPerformed
186:
187: protected void storeToDataModel() {
188: data.setTemplate((Project) comProject.getSelectedItem());
189: }
190:
191: protected void readFromDataModel() {
192: checkValidity();
193: }
194:
195: private void loadComboBox() {
196: DefaultComboBoxModel model = new DefaultComboBoxModel();
197: Project[] prjs = OpenProjects.getDefault().getOpenProjects();
198: Arrays.sort(prjs, Util.projectDisplayNameComparator());
199: for (int i = 0; i < prjs.length; i++) {
200: if (prjs[i] != data.getProject()) {
201: // ignore the currently active project..
202: model.addElement(prjs[i]);
203: }
204: }
205: if (model.getSize() == 0) {
206: model.addElement(EMPTY);
207: }
208: comProject.setModel(model);
209: }
210:
211: private void checkValidity() {
212: Object sel = comProject.getModel().getSelectedItem();
213: if (sel == EMPTY) {
214: setError(getMessage("MSG_NoProjectSelected"));
215: return;
216: }
217: Sources srcs = ProjectUtils.getSources((Project) sel); // #63247: don't use lookup directly
218: if (srcs.getSourceGroups(Sources.TYPE_GENERIC).length > 1) {
219: setError(getMessage("MSG_NoExternalRoots"));
220: return;
221: }
222: markValid();
223: }
224:
225: protected String getPanelName() {
226: return getMessage("LBL_ProjectSelection_Title");
227: }
228:
229: protected HelpCtx getHelp() {
230: return new HelpCtx(SelectProjectPanel.class);
231: }
232:
233: private void initAccessibility() {
234: this .getAccessibleContext().setAccessibleDescription(
235: getMessage("ACS_SelectProjectPanel"));
236: btnProject.getAccessibleContext().setAccessibleDescription(
237: getMessage("ACS_CTL_btnProject"));
238: comProject.getAccessibleContext().setAccessibleDescription(
239: getMessage("ACS_CTL_comProject"));
240: }
241:
242: // Variables declaration - do not modify//GEN-BEGIN:variables
243: private javax.swing.JButton btnProject;
244: private javax.swing.JComboBox comProject;
245: private javax.swing.JLabel lblProject;
246: private javax.swing.JPanel pnlHeightAdjuster;
247: // End of variables declaration//GEN-END:variables
248:
249: }
|