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.apache.jmeter.module.wizards;
043:
044: import java.awt.Component;
045: import java.io.File;
046: import java.io.IOException;
047: import java.util.ArrayList;
048: import java.util.Iterator;
049: import java.util.List;
050: import java.util.logging.Level;
051: import java.util.logging.Logger;
052: import java.util.regex.Pattern;
053: import javax.swing.event.ChangeEvent;
054: import javax.swing.event.ChangeListener;
055: import org.netbeans.api.project.Project;
056: import org.netbeans.api.project.SourceGroup;
057: import org.netbeans.spi.project.ui.templates.support.Templates;
058: import org.openide.WizardDescriptor;
059: import org.openide.filesystems.FileObject;
060: import org.openide.filesystems.FileUtil;
061: import org.openide.loaders.TemplateWizard;
062: import org.openide.util.HelpCtx;
063: import org.openide.util.NbBundle;
064:
065: /**
066: *
067: * @author Petr Hrebejk, mkuchtiak
068: */
069: final class TargetChooserPanel implements WizardDescriptor.Panel {
070:
071: private final List/*<ChangeListener>*/listeners = new ArrayList();
072: private TargetChooserPanelGUI gui;
073:
074: private Project project;
075: private TemplateWizard templateWizard;
076:
077: //TODO how to add [,] to the regular expression?
078: private static final Pattern INVALID_FILENAME_CHARACTERS = Pattern
079: .compile("[`~!@#$%^&*()=+\\|{};:'\",<>/?]"); // NOI18N
080:
081: TargetChooserPanel(Project project) {
082: this .project = project;
083: }
084:
085: TemplateWizard getTemplateWizard() {
086: return templateWizard;
087: }
088:
089: public Component getComponent() {
090: if (gui == null) {
091: gui = new TargetChooserPanelGUI(this , project);
092: }
093: return gui;
094: }
095:
096: public HelpCtx getHelp() {
097: return null;
098: //return new HelpCtx( this.getClass().getName() +"."+fileType.toString()); //NOI18N
099: }
100:
101: public boolean isValid() {
102: boolean ok = (gui != null && gui.getTargetName() != null);
103:
104: if (!ok) {
105: templateWizard
106: .putProperty("WizardPanel_errorMessage", null); // NOI18N
107: return false;
108: }
109:
110: String filename = gui.getTargetName();
111: if (INVALID_FILENAME_CHARACTERS.matcher(filename).find()) {
112: templateWizard.putProperty("WizardPanel_errorMessage",
113: NbBundle.getMessage(TargetChooserPanel.class,
114: "MSG_invalid_filename", filename)); // NOI18N
115: return false;
116: }
117:
118: // check if the file name can be created
119: String targetName = gui.getTargetName();
120: java.io.File file = gui.getTargetFile();
121: FileObject template = Templates.getTemplate(templateWizard);
122: String ext = template.getExt();
123:
124: String errorMessage = Utilities.canUseFileName(file, gui
125: .getRelativeTargetFolder(), targetName, ext);
126: if (errorMessage != null)
127: templateWizard.putProperty("WizardPanel_errorMessage",
128: errorMessage); // NOI18N
129: else
130: templateWizard.putProperty("WizardPanel_errorMessage", gui
131: .getErrorMessage()); //NOI18N
132:
133: boolean valid = gui.isPanelValid() && errorMessage == null;
134:
135: if (valid && targetName.indexOf(".") >= 0) {
136: // warning when file name contains dots
137: templateWizard.putProperty("WizardPanel_errorMessage", // NOI18N
138: NbBundle.getMessage(TargetChooserPanel.class,
139: "MSG_dotsInName", targetName + "." + ext));
140: }
141: return valid;
142: }
143:
144: public void addChangeListener(ChangeListener l) {
145: listeners.add(l);
146: }
147:
148: public void removeChangeListener(ChangeListener l) {
149: listeners.remove(l);
150: }
151:
152: protected void fireChange() {
153: ChangeEvent e = new ChangeEvent(this );
154: Iterator it = listeners.iterator();
155: while (it.hasNext()) {
156: ((ChangeListener) it.next()).stateChanged(e);
157: }
158: }
159:
160: public void readSettings(Object settings) {
161:
162: templateWizard = (TemplateWizard) settings;
163:
164: if (gui != null) {
165:
166: Project project = Templates.getProject(templateWizard);
167:
168: // Try to preselect a folder
169: // XXX The test should be rewritten if external project dirs are supported
170:
171: FileObject preselectedTarget = Templates
172: .getTargetFolder(templateWizard);
173:
174: // Init values
175: gui.initValues(project, Templates
176: .getTemplate(templateWizard), preselectedTarget);
177:
178: templateWizard.putProperty("NewFileWizard_Title", // NOI18N
179: NbBundle.getMessage(TargetChooserPanel.class,
180: "TITLE_JMXFile")); // NOI18N
181: }
182: }
183:
184: public void storeSettings(Object settings) {
185: if (WizardDescriptor.PREVIOUS_OPTION
186: .equals(((WizardDescriptor) settings).getValue())) {
187: return;
188: }
189: if (WizardDescriptor.CANCEL_OPTION
190: .equals(((WizardDescriptor) settings).getValue())) {
191: return;
192: }
193: if (isValid()) {
194: File f = new File(gui.getCreatedFilePath());
195: File ff = new File(f.getParentFile().getPath());
196: if (!ff.exists()) {
197: try {
198: FileUtil.createFolder(ff);
199: } catch (IOException exc) {
200: Logger.getLogger("global").log(Level.INFO, null,
201: exc);
202: }
203: }
204: FileObject folder = FileUtil.toFileObject(ff);
205:
206: Templates.setTargetFolder((WizardDescriptor) settings,
207: folder);
208: Templates.setTargetName((WizardDescriptor) settings, gui
209: .getTargetName());
210: }
211: ((WizardDescriptor) settings).putProperty(
212: "NewFileWizard_Title", null); // NOI18N
213: }
214: }
|