KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > wizards > TagInfoPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.wizards;
21
22 import java.awt.Component JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.swing.event.ChangeEvent JavaDoc;
27 import javax.swing.event.ChangeListener JavaDoc;
28 import org.netbeans.api.java.project.JavaProjectConstants;
29
30 import org.openide.WizardDescriptor;
31 import org.openide.util.HelpCtx;
32 import org.openide.loaders.TemplateWizard;
33 import org.openide.filesystems.FileObject;
34
35 import org.netbeans.api.project.Project;
36 import org.netbeans.api.project.ProjectUtils;
37 import org.netbeans.api.project.SourceGroup;
38 import org.netbeans.api.project.Sources;
39 import org.netbeans.spi.project.ui.templates.support.Templates;
40
41 /** A single panel descriptor for a wizard.
42  * You probably want to make a wizard iterator to hold it.
43  *
44  * @author Milan Kuchtiak
45  */

46 public class TagInfoPanel implements WizardDescriptor.Panel {
47     
48     /** The visual component that displays this panel.
49      * If you need to access the component from this class,
50      * just use getComponent().
51      */

52     private TagHandlerPanelGUI component;
53     private transient TemplateWizard wizard;
54     private transient Project proj;
55     private transient SourceGroup[] sourceGroups;
56     private String JavaDoc className;
57     
58     /** Create the wizard panel descriptor. */
59     public TagInfoPanel(TemplateWizard wizard, Project proj, SourceGroup[] sourceGroups) {
60         this.wizard=wizard;
61         this.proj=proj;
62         this.sourceGroups=sourceGroups;
63     }
64     
65     // Get the visual component for the panel. In this template, the component
66
// is kept separate. This can be more efficient: if the wizard is created
67
// but never displayed, or not all panels are displayed, it is better to
68
// create only those which really need to be visible.
69
public Component JavaDoc getComponent() {
70         if (component == null) {
71             component = new TagHandlerPanelGUI(wizard,this,proj,sourceGroups);
72         }
73         return component;
74     }
75     
76     public HelpCtx getHelp() {
77         //return new HelpCtx(TagInfoPanel.class); //NOI18N
78
return HelpCtx.DEFAULT_HELP;
79     }
80     
81     public boolean isValid() {
82         // If it is always OK to press Next or Finish, then:
83
if (writeToTLD() && getTLDFile()==null) {
84             wizard.putProperty ("WizardPanel_errorMessage", org.openide.util.NbBundle.getMessage(TagInfoPanel.class, "MSG_noTldSelected")); // NOI18N
85
return false;
86         } else if (!isValidTagName(getTagName())) {
87             wizard.putProperty ("WizardPanel_errorMessage", org.openide.util.NbBundle.getMessage(TagInfoPanel.class, "TXT_wrongTagName",getTagName())); // NOI18N
88
return false;
89         } else if (tagNameExists(getTagName())) {
90             wizard.putProperty ("WizardPanel_errorMessage", org.openide.util.NbBundle.getMessage(TagInfoPanel.class, "TXT_tagNameExists",getTagName())); // NOI18N
91
return false;
92         } else {
93             wizard.putProperty ("WizardPanel_errorMessage", ""); // NOI18N
94
return true;
95         }
96         
97         // If it depends on some condition (form filled out...), then:
98
// return someCondition ();
99
// and when this condition changes (last form field filled in...) then:
100
// fireChangeEvent ();
101
// and uncomment the complicated stuff below.
102
}
103  
104     //public final void addChangeListener(ChangeListener l) {}
105
//public final void removeChangeListener(ChangeListener l) {}
106

107     private final Set JavaDoc listeners = new HashSet JavaDoc (1); // Set<ChangeListener>
108
public final void addChangeListener (ChangeListener JavaDoc l) {
109         synchronized (listeners) {
110             listeners.add (l);
111         }
112     }
113     public final void removeChangeListener (ChangeListener JavaDoc l) {
114         synchronized (listeners) {
115             listeners.remove (l);
116         }
117     }
118     protected final void fireChangeEvent () {
119         Iterator JavaDoc it;
120         synchronized (listeners) {
121             it = new HashSet JavaDoc (listeners).iterator ();
122         }
123         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc (this);
124         while (it.hasNext ()) {
125             ((ChangeListener JavaDoc) it.next ()).stateChanged (ev);
126         }
127     }
128     
129     // You can use a settings object to keep track of state.
130
// Normally the settings object will be the WizardDescriptor,
131
// so you can use WizardDescriptor.getProperty & putProperty
132
// to store information entered by the user.
133
public void readSettings(Object JavaDoc settings) {
134         TemplateWizard w = (TemplateWizard)settings;
135         //Project project = Templates.getProject(w);
136
String JavaDoc targetName = w.getTargetName();
137         org.openide.filesystems.FileObject targetFolder = Templates.getTargetFolder(w);
138         Project project = Templates.getProject( w );
139         Sources sources = ProjectUtils.getSources(project);
140         SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
141         String JavaDoc packageName = null;
142         for (int i = 0; i < groups.length && packageName == null; i++) {
143             packageName = org.openide.filesystems.FileUtil.getRelativePath (groups [i].getRootFolder (), targetFolder);
144         }
145         if (packageName == null)
146             packageName = ""; //NOI18N
147
packageName = packageName.replace('/','.');
148         
149         if (targetName!=null) {
150             if (packageName.length()>0)
151                 className=packageName+"."+targetName;//NOI18N
152
else
153                 className=targetName;
154             component.setClassName(className);
155             if (component.getTagName().length()==0)
156                 component.setTagName(targetName);
157         }
158         Boolean JavaDoc bodySupport = (Boolean JavaDoc)w.getProperty("BODY_SUPPORT");//NOI18N
159
if (bodySupport!=null && bodySupport.booleanValue())
160             component.setBodySupport(true);
161         else component.setBodySupport(false);
162     }
163     public void storeSettings(Object JavaDoc settings) {
164     }
165     
166     public String JavaDoc getClassName() {
167         return className;
168     }
169     public String JavaDoc getTagName() {
170         return component.getTagName();
171     }
172     public FileObject getTLDFile() {
173         return component.getTLDFile();
174     }
175     public boolean isEmpty() {
176         return component.isEmpty();
177     }
178     public boolean isScriptless() {
179         return component.isScriptless();
180     }
181     public boolean isTegdependent() {
182         return component.isTegdependent();
183     }
184     public boolean writeToTLD() {
185         return component.writeToTLD();
186     }
187     public Object JavaDoc[][] getAttributes() {
188         return component.getAttributes();
189     }
190     
191     private boolean isValidTagName(String JavaDoc name) {
192         return org.apache.xerces.util.XMLChar.isValidNCName(name);
193     }
194     
195     private boolean tagNameExists(String JavaDoc name) {
196         java.util.Set JavaDoc tagValues = component.getTagValues();
197         if (tagValues!=null && tagValues.contains(name)) return true;
198         else return false;
199     }
200 }
201
Popular Tags