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.platform;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.io.File;
047: import java.io.IOException;
048: import javax.swing.JFileChooser;
049: import javax.swing.filechooser.FileFilter;
050: import org.netbeans.modules.apisupport.project.ui.ModuleUISettings;
051: import org.netbeans.modules.apisupport.project.ui.wizard.BasicVisualPanel;
052: import org.netbeans.modules.apisupport.project.universe.NbPlatform;
053: import org.openide.WizardDescriptor;
054: import org.openide.filesystems.FileUtil;
055: import org.openide.util.NbBundle;
056:
057: /**
058: * First panel from <em>Adding New Platform</em> wizard panels. Allows user to
059: * choose platform directory.
060: *
061: * @author Martin Krauskopf
062: */
063: public class PlatformChooserVisualPanel extends BasicVisualPanel
064: implements PropertyChangeListener {
065:
066: /** Creates new form BasicInfoVisualPanel */
067: public PlatformChooserVisualPanel(WizardDescriptor setting) {
068: super (setting);
069: initComponents();
070: initAccessibility();
071: String location = ModuleUISettings.getDefault()
072: .getLastUsedNbPlatformLocation();
073: if (location != null) {
074: platformChooser.setCurrentDirectory(new File(location));
075: }
076: platformChooser.setAcceptAllFileFilterUsed(false);
077: platformChooser.setFileFilter(new FileFilter() {
078: public boolean accept(File f) {
079: return f.isDirectory();
080: }
081:
082: public String getDescription() {
083: return getMessage("CTL_PlatformFolder");
084: }
085: });
086: platformChooser.addPropertyChangeListener(this );
087: setName(NbPlatformCustomizer.CHOOSER_STEP);
088: platformChooser.putClientProperty(
089: "JFileChooser.appBundleIsTraversable", "always"); // NOI18N #73124
090: }
091:
092: public void addNotify() {
093: super .addNotify();
094: checkForm();
095: }
096:
097: /** Stores collected data into model. */
098: void storeData() {
099: File file = platformChooser.getSelectedFile();
100: if (file != null) {
101: getSettings().putProperty(
102: NbPlatformCustomizer.PLAF_DIR_PROPERTY,
103: file.getAbsolutePath());
104: getSettings().putProperty(
105: NbPlatformCustomizer.PLAF_LABEL_PROPERTY,
106: plafLabelValue.getText());
107: } // when wizard is cancelled file is null
108: }
109:
110: public void propertyChange(PropertyChangeEvent evt) {
111: String propName = evt.getPropertyName();
112: if (propName
113: .equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
114: checkForm();
115: }
116: }
117:
118: private void checkForm() {
119: File selFile = platformChooser.getSelectedFile();
120: boolean invalid = true;
121: if (selFile != null) { // #73123
122: File plafDir = FileUtil.normalizeFile(selFile);
123: if (/* #60133 */plafDir != null
124: && NbPlatform.isPlatformDirectory(plafDir)) {
125: try {
126: setPlafLabel(NbPlatform.computeDisplayName(plafDir));
127: } catch (IOException e) {
128: setPlafLabel(plafDir.getAbsolutePath());
129: }
130: if (!NbPlatform.isSupportedPlatform(plafDir)) {
131: setError(getMessage("MSG_UnsupportedPlatform"));
132: } else if (NbPlatform.contains(plafDir)) {
133: setError(getMessage("MSG_AlreadyAddedPlatform"));
134: } else if (!NbPlatform.isLabelValid(plafLabelValue
135: .getText())) {
136: setWarning(getMessage("MSG_NameIsAlreadyUsedGoToNext"));
137: } else {
138: markValid();
139: ModuleUISettings.getDefault()
140: .setLastUsedNbPlatformLocation(
141: plafDir.getParentFile()
142: .getAbsolutePath());
143: }
144: invalid = false;
145: }
146: }
147: if (invalid) {
148: markInvalid();
149: setPlafLabel(null);
150: storeData();
151: }
152: }
153:
154: private void setPlafLabel(String label) {
155: plafLabelValue.setText(label);
156: plafLabelValue.setCaretPosition(0);
157: storeData();
158: }
159:
160: private static String getMessage(String key) {
161: return NbBundle.getMessage(PlatformChooserVisualPanel.class,
162: key);
163: }
164:
165: /** This method is called from within the constructor to
166: * initialize the form.
167: * WARNING: Do NOT modify this code. The content of this method is
168: * always regenerated by the Form Editor.
169: */
170: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
171: private void initComponents() {
172: infoPanel = new javax.swing.JPanel();
173: inner = new javax.swing.JPanel();
174: plafLabel = new javax.swing.JLabel();
175: plafLabelValue = new javax.swing.JTextField();
176: platformChooser = new javax.swing.JFileChooser();
177:
178: infoPanel.setLayout(new java.awt.FlowLayout(
179: java.awt.FlowLayout.LEFT, 6, 0));
180:
181: inner.setLayout(new java.awt.GridLayout(2, 1, 0, 6));
182:
183: plafLabel.setLabelFor(plafLabelValue);
184: org.openide.awt.Mnemonics.setLocalizedText(plafLabel,
185: org.openide.util.NbBundle.getMessage(
186: PlatformChooserVisualPanel.class,
187: "LBL_PlatformName_P"));
188: inner.add(plafLabel);
189:
190: plafLabelValue.setColumns(15);
191: plafLabelValue.setEditable(false);
192: inner.add(plafLabelValue);
193:
194: infoPanel.add(inner);
195:
196: setLayout(new java.awt.BorderLayout());
197:
198: platformChooser.setAccessory(infoPanel);
199: platformChooser.setControlButtonsAreShown(false);
200: platformChooser
201: .setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
202: add(platformChooser, java.awt.BorderLayout.CENTER);
203:
204: }
205:
206: // </editor-fold>//GEN-END:initComponents
207:
208: // Variables declaration - do not modify//GEN-BEGIN:variables
209: private javax.swing.JPanel infoPanel;
210: private javax.swing.JPanel inner;
211: private javax.swing.JLabel plafLabel;
212: private javax.swing.JTextField plafLabelValue;
213: private javax.swing.JFileChooser platformChooser;
214:
215: // End of variables declaration//GEN-END:variables
216:
217: private void initAccessibility() {
218: this .getAccessibleContext().setAccessibleDescription(
219: getMessage("ACS_PlatformChooserVisualPanel"));
220: plafLabelValue.getAccessibleContext().setAccessibleDescription(
221: getMessage("ACS_CTL_plafLabelValue"));
222: }
223:
224: }
|