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.web.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.netbeans.modules.web.api.webmodule.WebModule;
064: import org.openide.util.NbBundle;
065:
066: /**
067: *
068: * @author Petr Hrebejk, mkuchtiak
069: */
070: final class TargetChooserPanel implements WizardDescriptor.Panel {
071:
072: private final List/*<ChangeListener>*/listeners = new ArrayList();
073: private TargetChooserPanelGUI gui;
074:
075: private Project project;
076: private SourceGroup[] folders;
077: private FileType fileType;
078: private TemplateWizard templateWizard;
079: private String j2eeVersion;
080:
081: //TODO how to add [,] to the regular expression?
082: private static final Pattern INVALID_FILENAME_CHARACTERS = Pattern
083: .compile("[`~!@#$%^&*()=+\\|{};:'\",<>/?]"); // NOI18N
084:
085: TargetChooserPanel(Project project, SourceGroup[] folders,
086: FileType fileType) {
087: this .folders = folders;
088: this .project = project;
089: this .fileType = fileType;
090:
091: if (FileType.TAG.equals(fileType)) {
092: j2eeVersion = WebModule.J2EE_14_LEVEL;
093: if (folders != null && folders.length > 0) {
094: WebModule wm = WebModule.getWebModule(folders[0]
095: .getRootFolder());
096: if (wm != null)
097: j2eeVersion = wm.getJ2eePlatformVersion();
098: }
099: }
100: }
101:
102: TemplateWizard getTemplateWizard() {
103: return templateWizard;
104: }
105:
106: public Component getComponent() {
107: if (gui == null) {
108: gui = new TargetChooserPanelGUI(this , project, folders,
109: fileType);
110: }
111: return gui;
112: }
113:
114: public HelpCtx getHelp() {
115: return null;
116: //return new HelpCtx( this.getClass().getName() +"."+fileType.toString()); //NOI18N
117: }
118:
119: public boolean isValid() {
120: // cannot create tag files in j2ee1.3
121: if (FileType.TAG.equals(fileType)
122: && WebModule.J2EE_13_LEVEL.equals(j2eeVersion)) {
123: templateWizard.putProperty("WizardPanel_errorMessage", // NOI18N
124: NbBundle.getMessage(TargetChooserPanel.class,
125: "MSG_13notSupported"));
126: return false;
127: }
128:
129: boolean ok = (gui != null && gui.getTargetName() != null);
130:
131: if (!ok) {
132: templateWizard
133: .putProperty("WizardPanel_errorMessage", null); // NOI18N
134: return false;
135: }
136:
137: // check if the TLD info is correct
138: if (FileType.TAG.equals(fileType)
139: && gui.isTldCheckBoxSelected()) {
140: String mes = null;
141: FileObject tldFo = gui.getTldFileObject();
142: String tagName = gui.getTagName();
143: if (tldFo == null) {
144: mes = NbBundle.getMessage(TargetChooserPanel.class,
145: "MSG_noTldSelectedForTagFile");
146: } else if (TargetChooserPanelGUI.isTagNameEmpty(tagName)) {
147: mes = NbBundle.getMessage(TargetChooserPanel.class,
148: "TXT_missingTagName");
149: } else if (!TargetChooserPanelGUI.isValidTagName(tagName)) {
150: mes = NbBundle.getMessage(TargetChooserPanel.class,
151: "TXT_wrongTagName", tagName);
152: } else if (gui.tagNameExists(tagName)) {
153: mes = NbBundle.getMessage(TargetChooserPanel.class,
154: "TXT_tagNameExists", tagName);
155: }
156: if (mes != null) {
157: templateWizard.putProperty("WizardPanel_errorMessage",
158: mes); // NOI18N
159: return false;
160: }
161: }
162:
163: String filename = gui.getTargetName();
164: if (INVALID_FILENAME_CHARACTERS.matcher(filename).find()) {
165: templateWizard.putProperty("WizardPanel_errorMessage",
166: NbBundle.getMessage(TargetChooserPanel.class,
167: "MSG_invalid_filename", filename)); // NOI18N
168: return false;
169: }
170:
171: // check if the TLD info is correct
172: if (FileType.TAGLIBRARY.equals(fileType)) {
173: // XX precisely we should check for 'tokens composed of characters,
174: // digits, ".", ":", "-", and the characters defined by Unicode,
175: // such as "combining" or "extender"' to be sure that TLD will validate
176: String tldName = gui.getTargetName();
177: if (tldName.indexOf(' ') >= 0 || tldName.indexOf(',') >= 0) {
178: templateWizard.putProperty("WizardPanel_errorMessage",
179: NbBundle.getMessage(TargetChooserPanel.class,
180: "TXT_wrongTagLibName", tldName)); // NOI18N
181: return false;
182: }
183: }
184: // check if the file name can be created
185: String targetName = gui.getTargetName();
186: java.io.File file = gui.getTargetFile();
187: FileObject template = Templates.getTemplate(templateWizard);
188: String ext = template.getExt();
189: if (FileType.JSP.equals(fileType)
190: || FileType.TAG.equals(fileType)) {
191: if (isSegment())
192: ext += "f"; //NOI18N
193: else if (isXml())
194: ext += "x"; //NOI18N
195: }
196:
197: String errorMessage = Utilities.canUseFileName(file, gui
198: .getRelativeTargetFolder(), targetName, ext);
199: if (errorMessage != null)
200: templateWizard.putProperty("WizardPanel_errorMessage",
201: errorMessage); // NOI18N
202: else
203: templateWizard.putProperty("WizardPanel_errorMessage", gui
204: .getErrorMessage()); //NOI18N
205:
206: boolean valid = gui.isPanelValid() && errorMessage == null;
207:
208: if (valid && targetName.indexOf(".") >= 0) {
209: // warning when file name contains dots
210: templateWizard.putProperty("WizardPanel_errorMessage", // NOI18N
211: NbBundle.getMessage(TargetChooserPanel.class,
212: "MSG_dotsInName", targetName + "." + ext));
213: }
214: return valid;
215: }
216:
217: public void addChangeListener(ChangeListener l) {
218: listeners.add(l);
219: }
220:
221: public void removeChangeListener(ChangeListener l) {
222: listeners.remove(l);
223: }
224:
225: protected void fireChange() {
226: ChangeEvent e = new ChangeEvent(this );
227: Iterator it = listeners.iterator();
228: while (it.hasNext()) {
229: ((ChangeListener) it.next()).stateChanged(e);
230: }
231: }
232:
233: public void readSettings(Object settings) {
234:
235: templateWizard = (TemplateWizard) settings;
236:
237: if (gui != null) {
238:
239: Project project = Templates.getProject(templateWizard);
240:
241: // Try to preselect a folder
242: // XXX The test should be rewritten if external project dirs are supported
243:
244: FileObject preselectedTarget = Templates
245: .getTargetFolder(templateWizard);
246:
247: // Init values
248: gui.initValues(project, Templates
249: .getTemplate(templateWizard), preselectedTarget);
250:
251: if (FileType.JSP.equals(fileType))
252: templateWizard.putProperty("NewFileWizard_Title", // NOI18N
253: NbBundle.getMessage(TargetChooserPanel.class,
254: "TITLE_JspFile"));
255: else if (FileType.TAG.equals(fileType))
256: templateWizard.putProperty("NewFileWizard_Title", // NOI18N
257: NbBundle.getMessage(TargetChooserPanel.class,
258: "TITLE_TagFile"));
259: else if (FileType.TAGLIBRARY.equals(fileType))
260: templateWizard.putProperty("NewFileWizard_Title", // NOI18N
261: NbBundle.getMessage(TargetChooserPanel.class,
262: "TITLE_TLD"));
263: else if (FileType.HTML.equals(fileType))
264: templateWizard.putProperty("NewFileWizard_Title", // NOI18N
265: NbBundle.getMessage(TargetChooserPanel.class,
266: "TITLE_HTML"));
267: else if (FileType.CSS.equals(fileType))
268: templateWizard.putProperty("NewFileWizard_Title", // NOI18N
269: NbBundle.getMessage(TargetChooserPanel.class,
270: "TITLE_CSS"));
271: else if (FileType.XHTML.equals(fileType))
272: templateWizard.putProperty("NewFileWizard_Title", // NOI18N
273: NbBundle.getMessage(TargetChooserPanel.class,
274: "TITLE_XHTML"));
275: }
276: }
277:
278: public void storeSettings(Object settings) {
279: if (WizardDescriptor.PREVIOUS_OPTION
280: .equals(((WizardDescriptor) settings).getValue())) {
281: return;
282: }
283: if (WizardDescriptor.CANCEL_OPTION
284: .equals(((WizardDescriptor) settings).getValue())) {
285: return;
286: }
287: if (isValid()) {
288: File f = new File(gui.getCreatedFilePath());
289: File ff = new File(f.getParentFile().getPath());
290: if (!ff.exists()) {
291: try {
292: FileUtil.createFolder(ff);
293: } catch (IOException exc) {
294: Logger.getLogger("global").log(Level.INFO, null,
295: exc);
296: }
297: }
298: FileObject folder = FileUtil.toFileObject(ff);
299:
300: Templates.setTargetFolder((WizardDescriptor) settings,
301: folder);
302: Templates.setTargetName((WizardDescriptor) settings, gui
303: .getTargetName());
304: }
305: ((WizardDescriptor) settings).putProperty(
306: "NewFileWizard_Title", null); // NOI18N
307: }
308:
309: boolean isXml() {
310: return gui.isXml();
311: }
312:
313: boolean isSegment() {
314: return gui.isSegment();
315: }
316:
317: String getUri() {
318: return gui.getUri();
319: }
320:
321: String getPrefix() {
322: return gui.getPrefix();
323: }
324:
325: boolean isTldCheckBoxSelected() {
326: return gui.isTldCheckBoxSelected();
327: }
328:
329: String getTagName() {
330: return gui.getTagName();
331: }
332:
333: FileObject getTldFileObject() {
334: return gui.getTldFileObject();
335: }
336: }
|