KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jboss4 > ide > ui > JBInstantiatingIterator


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 package org.netbeans.modules.j2ee.jboss4.ide.ui;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.NoSuchElementException JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.swing.JComponent JavaDoc;
29 import javax.swing.SwingUtilities JavaDoc;
30 import javax.swing.event.ChangeEvent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32 import org.netbeans.modules.j2ee.deployment.impl.ui.wizard.AddServerInstanceWizard;
33 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceCreationException;
34 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
35 import org.netbeans.modules.j2ee.jboss4.JBDeploymentFactory;
36 import org.openide.DialogDisplayer;
37 import org.openide.ErrorManager;
38 import org.openide.NotifyDescriptor;
39 import org.openide.WizardDescriptor;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileUtil;
42 import org.openide.util.NbBundle;
43
44
45 /**
46  *
47  * @author Ivan Sidorkin
48  */

49 public class JBInstantiatingIterator implements WizardDescriptor.InstantiatingIterator, ChangeListener JavaDoc {
50     
51     
52     /**
53      * skipServerLocationStep allow to skip Select Location step in New Instance Wizard
54      * if this step allready was passed
55      */

56     public final boolean skipServerLocationStep = false;
57     
58     private transient AddServerLocationPanel locationPanel = null;
59     private transient AddServerPropertiesPanel propertiesPanel = null;
60     
61     private WizardDescriptor wizard;
62     private transient int index = 0;
63     private transient WizardDescriptor.Panel[] panels = null;
64     
65     
66     // private InstallPanel panel;
67
private transient Set JavaDoc listeners = new HashSet JavaDoc(1);
68     public void removeChangeListener(ChangeListener JavaDoc l) {
69         synchronized (listeners) {
70             listeners.remove(l);
71         }
72     }
73     
74     public void addChangeListener(ChangeListener JavaDoc l) {
75         synchronized (listeners) {
76             listeners.add(l);
77         }
78     }
79     
80     public void uninitialize(WizardDescriptor wizard) {
81     }
82     
83     public void initialize(WizardDescriptor wizard) {
84         this.wizard = wizard;
85     }
86     
87     public void previousPanel() {
88         index--;
89     }
90     
91     public void nextPanel() {
92         if (!hasNext()) throw new NoSuchElementException JavaDoc();
93         index++;
94     }
95     
96     public String JavaDoc name() {
97         return "JBoss Server AddInstanceIterator"; // NOI18N
98
}
99     
100     public static void showInformation(final String JavaDoc msg, final String JavaDoc title){
101         SwingUtilities.invokeLater(new Runnable JavaDoc() {
102             public void run() {
103                 NotifyDescriptor d = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE);
104                 d.setTitle(title);
105                 DialogDisplayer.getDefault().notify(d);
106             }
107         });
108         
109     }
110     
111     public Set JavaDoc instantiate() throws IOException JavaDoc {
112         Set JavaDoc result = new HashSet JavaDoc();
113         
114         String JavaDoc displayName = (String JavaDoc)wizard.getProperty(org.netbeans.modules.j2ee.deployment.impl.ui.wizard.AddServerInstanceWizard.PROP_DISPLAY_NAME);
115         
116         String JavaDoc url = JBDeploymentFactory.URI_PREFIX + host + ":" + port; // NOI18N
117
if (server != null && !server.equals("")) // NOI18N
118
url += "#" + server; // NOI18N
119
url += "&"+ installLocation; // NOI18N
120

121         try {
122             InstanceProperties ip = InstanceProperties.createInstanceProperties(url, userName, password, displayName);
123             ip.setProperty(JBPluginProperties.PROPERTY_SERVER, server);
124             ip.setProperty(JBPluginProperties.PROPERTY_DEPLOY_DIR, deployDir);
125             ip.setProperty(JBPluginProperties.PROPERTY_SERVER_DIR, serverPath);
126             ip.setProperty(JBPluginProperties.PROPERTY_ROOT_DIR, installLocation);
127             
128             ip.setProperty(JBPluginProperties.PROPERTY_HOST, host);
129             ip.setProperty(JBPluginProperties.PROPERTY_PORT, port);
130             
131             result.add(ip);
132         } catch (InstanceCreationException e){
133             showInformation(e.getLocalizedMessage(), NbBundle.getMessage(AddServerPropertiesVisualPanel.class, "MSG_INSTANCE_REGISTRATION_FAILED")); //NOI18N
134
ErrorManager.getDefault().log(ErrorManager.EXCEPTION, e.getMessage());
135         }
136         
137         return result;
138     }
139     
140     public boolean hasPrevious() {
141         return index > 0;
142     }
143     
144     public boolean hasNext() {
145         return index < getPanels().length - 1;
146     }
147     
148     protected String JavaDoc[] createSteps() {
149         if(!skipServerLocationStep){
150             return new String JavaDoc[] { NbBundle.getMessage(JBInstantiatingIterator.class, "STEP_ServerLocation"), // NOI18N
151
NbBundle.getMessage(JBInstantiatingIterator.class, "STEP_Properties") }; // NOI18N
152
}else{
153             if (!JBPluginProperties.getInstance().isCurrentServerLocationValid()){
154                 return new String JavaDoc[] { NbBundle.getMessage(JBInstantiatingIterator.class, "STEP_ServerLocation"), // NOI18N
155
NbBundle.getMessage(JBInstantiatingIterator.class, "STEP_Properties") }; // NOI18N
156
} else {
157                 return new String JavaDoc[] { NbBundle.getMessage(JBInstantiatingIterator.class, "STEP_Properties") }; // NOI18N
158
}
159         }
160     }
161     
162     protected final String JavaDoc[] getSteps() {
163         if (steps == null) {
164             steps = createSteps();
165         }
166         return steps;
167     }
168     
169     protected final WizardDescriptor.Panel[] getPanels() {
170         if (panels == null) {
171             panels = createPanels();
172         }
173         return panels;
174     }
175     
176     protected WizardDescriptor.Panel[] createPanels() {
177         if (locationPanel == null) {
178             locationPanel = new AddServerLocationPanel(this);
179             
180             locationPanel.addChangeListener(this);
181         }
182         if (propertiesPanel == null) {
183             propertiesPanel = new AddServerPropertiesPanel(this);
184             
185             propertiesPanel.addChangeListener(this);
186         }
187         
188         if (skipServerLocationStep){
189             if (!JBPluginProperties.getInstance().isCurrentServerLocationValid()){
190                 return new WizardDescriptor.Panel[] {
191                     (WizardDescriptor.Panel)locationPanel,
192                             (WizardDescriptor.Panel)propertiesPanel
193                 };
194             } else {
195                 return new WizardDescriptor.Panel[] {
196                     (WizardDescriptor.Panel)propertiesPanel
197                 };
198             }
199         }else{
200             return new WizardDescriptor.Panel[] {
201                 (WizardDescriptor.Panel)locationPanel,
202                         (WizardDescriptor.Panel)propertiesPanel
203             };
204         }
205     }
206     
207     private transient String JavaDoc[] steps = null;
208     
209     protected final int getIndex() {
210         return index;
211     }
212     
213     public WizardDescriptor.Panel current() {
214         WizardDescriptor.Panel result = getPanels()[index];
215         JComponent JavaDoc component = (JComponent JavaDoc)result.getComponent();
216         component.putClientProperty("WizardPanel_contentData", getSteps()); // NOI18N
217
component.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(getIndex()));// NOI18N
218
return result;
219     }
220     
221     public void stateChanged(javax.swing.event.ChangeEvent JavaDoc changeEvent) {
222         fireChangeEvent();
223     }
224     
225     protected final void fireChangeEvent() {
226         Iterator JavaDoc it;
227         synchronized (listeners) {
228             it = new HashSet JavaDoc(listeners).iterator();
229         }
230         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
231         while (it.hasNext()) {
232             ((ChangeListener JavaDoc) it.next()).stateChanged(ev);
233         }
234     }
235     
236     private String JavaDoc host;
237     private String JavaDoc port;
238     private String JavaDoc userName="";
239     private String JavaDoc password="";
240     private String JavaDoc server;
241     private String JavaDoc installLocation;
242     private String JavaDoc deployDir;
243     private String JavaDoc serverPath;
244     
245     
246     public void setHost(String JavaDoc host){
247         this.host = host.trim();
248     }
249     
250     public void setPort(String JavaDoc port){
251         this.port = port.trim();
252     }
253     
254     public void setServer(String JavaDoc server){
255         this.server = server;
256     }
257     
258     public void setServerPath(String JavaDoc serverPath){
259         this.serverPath = serverPath;
260     }
261     
262     public void setDeployDir(String JavaDoc deployDir){
263         this.deployDir = deployDir;
264     }
265     
266     public void setInstallLocation(String JavaDoc installLocation){
267         this.installLocation = installLocation;
268         propertiesPanel.installLocationChanged();
269     }
270     
271 }
272
Popular Tags