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-2007 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.cnd.makewizard;
043:
044: import java.io.File;
045: import java.util.ArrayList;
046: import javax.swing.JTextField;
047: import javax.swing.event.DocumentEvent;
048: import javax.swing.event.DocumentListener;
049: import org.netbeans.modules.cnd.api.utils.IpeUtils;
050:
051: /**
052: * Create the second panel in the Makefile wizard.
053: */
054:
055: public class BaseDirectoryPanel extends DirectoryChooserPanel {
056:
057: /** Is the base directory a valid (existing) directory? */
058: private boolean baseIsValid;
059:
060: /** Serial version number */
061: static final long serialVersionUID = -4831717621793094L;
062:
063: private boolean initialized;
064:
065: /**
066: * Constructor for the Directory panel. Remember, most of the panel is
067: * inherited from WizardDescriptor.
068: */
069: public BaseDirectoryPanel(MakefileWizard wd) {
070: super (wd);
071: String subtitle = getString("LBL_BaseDirectoryPanel"); // NOI18N
072: setSubTitle(subtitle);
073: this .getAccessibleContext().setAccessibleDescription(subtitle);
074: initialized = false;
075: baseIsValid = false;
076: }
077:
078: /** Validate the requested directory. Warn the user if it doesn't exist */
079: public void validateData(ArrayList msgs, int key) {
080: File file = new File(getMakefileData().getBaseDirectory(
081: MakefileData.EXPAND));
082:
083: if (file.exists()) {
084: if (!file.isDirectory()) {
085: warn(msgs, WARN_CWD_NOT_DIR, file.getPath());
086: }
087: } else {
088: warn(msgs, WARN_CWD_DOES_NOT_EXIST, file.getPath());
089: }
090: }
091:
092: /**
093: * The default validation method. Most panels don't do validation so don't
094: * need to override this.
095: */
096: public boolean isPanelValid() {
097: return baseIsValid;
098: }
099:
100: /** Override the defualt and do some validation */
101: protected final void onOk() {
102: checkit();
103: }
104:
105: /**
106: * Validate the base directory currently typed into the text field. This method should
107: * not be confused with validateData(), which is called during Makefile generation.
108: * This validation occurs while the panel is posted. The validateData() occurs much
109: * later.
110: */
111: private void validateCurrentBase() {
112: String text;
113: JTextField tf = getText();
114: File dir = null;
115:
116: baseIsValid = false;
117: text = IpeUtils.expandPath(tf.getText());
118: if (text.length() > 0) {
119: if (text.charAt(0) == File.separatorChar) {
120: dir = new File(text);
121: } else {
122: dir = new File(".", text); // NOI18N
123: }
124: }
125:
126: if (dir != null && !dir.isFile()) {
127: baseIsValid = true;
128: MakefileWizard.getMakefileWizard().updateState();
129: }
130: }
131:
132: /**
133: * Create the panel. Do the superclasss first and then some panel-specific stuff
134: * afterwards.
135: */
136: private void create() {
137:
138: create(
139: getString("LBL_BaseDirectory"), // NOI18N
140: FileChooserPanel.ABSOLUTE_PATH,
141: getString("HLP_BaseDirectory")); // NOI18N
142:
143: JTextField tf = getText();
144: tf.getDocument().addDocumentListener(new DocumentListener() {
145:
146: public void changedUpdate(DocumentEvent ev) {
147: checkit();
148: }
149:
150: public void insertUpdate(DocumentEvent ev) {
151: checkit();
152: }
153:
154: public void removeUpdate(DocumentEvent ev) {
155: checkit();
156: }
157: });
158:
159: getLabel().setLabelFor(tf);
160: getLabel().setDisplayedMnemonic(
161: getString("MNEM_BaseDirectory").charAt(0)); // NOI18N
162: }
163:
164: private final void checkit() {
165: boolean oldVal = baseIsValid;
166:
167: validateCurrentBase();
168: if (baseIsValid != oldVal) {
169: MakefileWizard.getMakefileWizard().updateState();
170: }
171: }
172:
173: /** Set initial data in dialog */
174: public void addNotify() {
175: if (!initialized) {
176: initialized = true;
177: MakefileWizard.getMakefileWizard().initDirPaths();
178: create();
179: }
180: super .addNotify();
181:
182: getText().setText(getMakefileData().getBaseDirectory());
183: validateCurrentBase();
184: }
185:
186: /** Update MakefileData if the data was changed */
187: public void removeNotify() {
188: super .removeNotify();
189:
190: String base = getText().getText();
191: if (!base.equals(getMakefileData().getBaseDirectory())) {
192: getMakefileData().setBaseDirectory(base);
193: }
194:
195: MakefileWizard.getMakefileWizard().initMakefileName();
196: }
197: }
|