KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > j2ee > ui > AddDomainHostPortPanel


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.j2ee.sun.ide.j2ee.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.net.InetAddress JavaDoc;
24 import java.net.InetSocketAddress JavaDoc;
25 import java.net.UnknownHostException JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.swing.event.ChangeEvent JavaDoc;
30 import javax.swing.event.ChangeListener JavaDoc;
31 import org.openide.WizardDescriptor;
32 import org.openide.util.HelpCtx;
33 import org.openide.util.NbBundle;
34
35 /** Panel to query for the host and port when registering a remote instance.
36  *
37  * TODO add additional sanity testing for the port
38  */

39 class AddDomainHostPortPanel implements WizardDescriptor.FinishablePanel,
40         ChangeListener JavaDoc {
41     
42     /**
43      * The visual component that displays this panel. If you need to access the
44      * component from this class, just use getComponent().
45      */

46     private AddInstanceVisualHostPortPanel component;
47     private WizardDescriptor wiz;
48     
49     // Get the visual component for the panel. In this template, the component
50
// is kept separate. This can be more efficient: if the wizard is created
51
// but never displayed, or not all panels are displayed, it is better to
52
// create only those which really need to be visible.
53
public Component JavaDoc getComponent() {
54         if (component == null) {
55             component = new AddInstanceVisualHostPortPanel();
56             component.addChangeListener(this);
57             
58         }
59         return component;
60     }
61     
62     public HelpCtx getHelp() {
63         return new HelpCtx("AS_RegServ_EnterRemoteInfo"); //NOI18N
64
}
65         
66     /** Determine if the page has acceptable values.
67      *
68      * Fill in the WizardDescriptor properties
69      *
70      * Using a host name which is "unknown" at registration time is probably wrong.
71      * Using an invalid port number is totally wrong.
72      */

73     public boolean isValid() {
74         String JavaDoc h = component.getHost();
75         if (h.length() < 1) {
76             wiz.putProperty(AddDomainWizardIterator.PROP_ERROR_MESSAGE,
77                     NbBundle.getMessage(AddDomainHostPortPanel.class,
78                     "MSG_EnterHost",h)); //NOI18N
79
return false;
80         }
81         if (h.indexOf("://") > -1) {
82             // IZ 77187
83
wiz.putProperty(AddDomainWizardIterator.PROP_ERROR_MESSAGE,
84                     NbBundle.getMessage(AddDomainHostPortPanel.class,
85                     "MSG_exclude_protocol",h)); //NOI18N
86
return false;
87         }
88         int p = component.getPort();
89         try {
90             InetAddress JavaDoc ia = InetAddress.getByName(h);
91             new InetSocketAddress JavaDoc(ia,p);
92         } catch (UnknownHostException JavaDoc uhe) {
93             wiz.putProperty(AddDomainWizardIterator.PROP_ERROR_MESSAGE,
94                     NbBundle.getMessage(AddDomainHostPortPanel.class,
95                     "MSG_UnknownHost2",h)); //NOI18N
96
return false;
97         } catch (IllegalArgumentException JavaDoc iae) {
98             wiz.putProperty(AddDomainWizardIterator.PROP_ERROR_MESSAGE,
99                     NbBundle.getMessage(AddDomainHostPortPanel.class,
100                     "Msg_ValidPortNumber")); //NOI18N
101
return false;
102         }
103         // TODO verify no listener OR listener is an admin instance
104
wiz.putProperty(AddDomainWizardIterator.PROP_ERROR_MESSAGE,null);
105         wiz.putProperty(AddDomainWizardIterator.HOST,h);
106         wiz.putProperty(AddDomainWizardIterator.PORT,p+"");
107         return true;
108     }
109     
110     // Event handling
111
//
112
private final Set JavaDoc/*<ChangeListener> */listeners = new HashSet JavaDoc/*<ChangeListener>*/(1);
113     public final void addChangeListener(ChangeListener JavaDoc l) {
114         synchronized (listeners) {
115             listeners.add(l);
116         }
117     }
118     public final void removeChangeListener(ChangeListener JavaDoc l) {
119         synchronized (listeners) {
120             listeners.remove(l);
121         }
122     }
123     protected final void fireChangeEvent() {
124         Iterator JavaDoc/*<ChangeListener>*/ it;
125         synchronized (listeners) {
126             it = new HashSet JavaDoc/*<ChangeListener>*/(listeners).iterator();
127         }
128         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
129         while (it.hasNext()) {
130             ((ChangeListener JavaDoc)it.next()).stateChanged(ev);
131         }
132     }
133
134     // You can use a settings object to keep track of state. Normally the
135
// settings object will be the WizardDescriptor, so you can use
136
// WizardDescriptor.getProperty & putProperty to store information entered
137
// by the user.
138
public void readSettings(Object JavaDoc settings) {
139         wiz = (WizardDescriptor) settings;
140     }
141     public void storeSettings(Object JavaDoc settings) {
142         // TODO implement?
143
}
144
145     public void stateChanged(ChangeEvent JavaDoc e) {
146         fireChangeEvent();
147     }
148
149     /** this can be a finish page
150      */

151     public boolean isFinishPanel() {
152         return true;
153     }
154 }
155
Popular Tags