KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Color JavaDoc;
22 import java.awt.Dimension JavaDoc;
23 import java.awt.GridBagConstraints JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.awt.event.InputMethodEvent JavaDoc;
27 import java.awt.event.InputMethodListener JavaDoc;
28 import java.awt.event.KeyEvent JavaDoc;
29 import java.awt.event.KeyListener JavaDoc;
30 import java.io.File JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Set JavaDoc;
36 import javax.swing.AbstractListModel JavaDoc;
37 import javax.swing.BorderFactory JavaDoc;
38 import javax.swing.ComboBoxModel JavaDoc;
39 import javax.swing.JButton JavaDoc;
40 import javax.swing.JComboBox JavaDoc;
41 import javax.swing.JFileChooser JavaDoc;
42 import javax.swing.JFrame JavaDoc;
43 import javax.swing.JLabel JavaDoc;
44 import javax.swing.JPanel JavaDoc;
45 import javax.swing.JPasswordField JavaDoc;
46 import javax.swing.JTextField JavaDoc;
47 import javax.swing.event.ChangeEvent JavaDoc;
48 import javax.swing.event.ChangeListener JavaDoc;
49 import org.openide.util.NbBundle;
50
51
52 /**
53  *
54  * @author Ivan Sidorkin
55  */

56 public class AddServerPropertiesVisualPanel extends JPanel JavaDoc {
57     
58     private final Set JavaDoc listeners = new HashSet JavaDoc();
59             
60     private javax.swing.JComboBox JavaDoc domainField; // Domain name (list of registered domains) can be edited
61
private javax.swing.JTextField JavaDoc domainPathField; //
62
private javax.swing.JLabel JavaDoc domainLabel;
63     private javax.swing.JLabel JavaDoc domainPathLabel;
64     private javax.swing.JLabel JavaDoc label1;
65     private javax.swing.JPanel JavaDoc panel1;
66     private javax.swing.JLabel JavaDoc hostLabel;
67     private javax.swing.JTextField JavaDoc hostField;
68     private javax.swing.JLabel JavaDoc portLabel;
69     private javax.swing.JTextField JavaDoc portField;
70     private javax.swing.JLabel JavaDoc userLabel;
71     private javax.swing.JTextField JavaDoc userField;
72     private javax.swing.JLabel JavaDoc passwordLabel;
73     private javax.swing.JPasswordField JavaDoc passwordField;
74     private javax.swing.JComboBox JavaDoc serverType; // Local or Remote
75

76     
77     /** Creates a new instance of AddServerPropertiesVisualPanel */
78     public AddServerPropertiesVisualPanel() {
79         init();
80         setName(NbBundle.getMessage(AddServerPropertiesVisualPanel.class, "TITLE_ServerProperties")); //NOI18N
81
}
82       
83     public void addChangeListener(ChangeListener JavaDoc l) {
84         synchronized (listeners) {
85             listeners.add(l);
86         }
87     }
88     
89     public void removeChangeListener(ChangeListener JavaDoc l ) {
90         synchronized (listeners) {
91             listeners.remove(l);
92         }
93     }
94     
95     private void somethingChanged() {
96         fireChangeEvent();
97     }
98       
99     private void fireChangeEvent() {
100         Iterator JavaDoc it;
101         synchronized (listeners) {
102             it = new HashSet JavaDoc(listeners).iterator();
103         }
104         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
105         while (it.hasNext()) {
106             ((ChangeListener JavaDoc)it.next()).stateChanged(ev);
107         }
108     }
109     
110     public boolean isLocalServer(){
111         if (serverType.getSelectedItem().equals("Local"))
112             return true;
113         else
114             return false;
115     }
116     
117     public String JavaDoc getHost(){
118         return hostField.getText().trim();
119     }
120     
121     public String JavaDoc getPort(){
122         return portField.getText().trim();
123     }
124     
125     public String JavaDoc getUser(){
126         return userField.getText();
127     }
128     
129     public String JavaDoc getPassword(){
130         return new String JavaDoc(passwordField.getPassword());
131     }
132     
133     public String JavaDoc getDomainPath(){
134         return domainPathField.getText();
135     }
136     
137     public String JavaDoc getDomain(){
138         return (String JavaDoc)domainField.getSelectedItem();
139     }
140    
141     private void domainChanged(){
142         DomainComboModel model = (DomainComboModel)domainField.getModel();
143         
144         String JavaDoc path = model.getCurrentPath();
145         domainPathField.setText(path);
146         portField.setText(JBPluginUtils.getHTTPConnectorPort(path));
147        
148     // serverChanged();
149
fireChangeEvent();
150     }
151     
152     void installLocationChanged() {
153         DomainComboModel domainModel = (DomainComboModel) domainField.getModel();
154         String JavaDoc serverLocation = JBPluginProperties.getInstance().getInstallLocation();
155         domainModel.setDomains(JBPluginUtils.getRegisteredDomains(serverLocation));
156         domainChanged();
157     }
158             
159     private void serverTypeChanged(){
160         
161         if (serverType.getSelectedItem().equals("Local")){ //NOI18N
162
domainLabel.setVisible(true);
163             domainField.setVisible(true);
164                     
165             domainPathLabel.setVisible(true);
166             domainPathField.setVisible(true);
167 // browseButton.setVisible(true);
168

169             hostField.setEditable(false);
170       // portField.setEditable(false);
171

172 // serverChanged();
173

174         }else{ // REMOTE
175

176             domainLabel.setVisible(false);
177             domainField.setVisible(false);
178             
179             domainPathLabel.setVisible(false);
180             domainPathField.setVisible(false);
181 // browseButton.setVisible(false);
182

183 // serverListField.setVisible(false);
184
// serverListLabel.setVisible(false);
185

186             hostField.setEditable(true);
187             portField.setEditable(true);
188         }
189         
190         somethingChanged();
191     }
192     
193     private void init(){
194         
195         // Object[] domainsList = WLPluginUtils.getRegisteredDomains().keySet().toArray(new String[WLPluginUtils.getRegisteredDomains().keySet().size()]);
196

197         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
198         
199         label1 = new JLabel JavaDoc(NbBundle.getMessage(AddServerPropertiesVisualPanel.class, "TXT_PROPERTY_TEXT")); //NOI18N
200

201         serverType = new JComboBox JavaDoc(new String JavaDoc[]{"Local","Remote"});//NOI18N
202
serverType.addActionListener(new ActionListener JavaDoc(){
203             public void actionPerformed(ActionEvent JavaDoc e) {
204                 serverTypeChanged();
205             }
206         });
207         
208         domainLabel = new JLabel JavaDoc(NbBundle.getMessage(AddServerPropertiesVisualPanel.class, "LBL_Domain")); //NOI18N
209
domainPathLabel = new JLabel JavaDoc(NbBundle.getMessage(AddServerPropertiesVisualPanel.class, "LBL_DomainPath"));//NOI18N
210
domainPathField = new JTextField JavaDoc();
211         domainPathField.setColumns(20);
212         domainPathField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(AddServerPropertiesVisualPanel.class, "LBL_DomainPath"));
213         domainPathField.getAccessibleContext().setAccessibleName(NbBundle.getMessage(AddServerPropertiesVisualPanel.class, "LBL_DomainPath"));
214         
215                
216         panel1 = new JPanel JavaDoc();
217         
218         //Domain combobox
219
String JavaDoc serverLocation = JBPluginProperties.getInstance().getInstallLocation();
220         domainField = new JComboBox JavaDoc(new DomainComboModel(JBPluginUtils.getRegisteredDomains(serverLocation)));
221         domainField.getAccessibleContext().setAccessibleName(NbBundle.getMessage(AddServerPropertiesVisualPanel.class, "LBL_Domain"));
222         domainField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(AddServerPropertiesVisualPanel.class, "LBL_Domain"));
223         //domainField.setEditable(true);
224
domainField.addActionListener(new ActionListener JavaDoc(){
225             public void actionPerformed(ActionEvent JavaDoc e) {
226                 domainChanged();
227             }
228         });
229         
230         
231         
232         hostLabel = new JLabel JavaDoc(NbBundle.getMessage(AddServerPropertiesVisualPanel.class, "LBL_Host"));//NOI18N
233
hostField = new JTextField JavaDoc();
234         hostField.setColumns(20);
235         hostField.setEditable(false);
236         hostField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(AddServerPropertiesVisualPanel.class, "LBL_Host"));
237         hostField.getAccessibleContext().setAccessibleName(NbBundle.getMessage(AddServerPropertiesVisualPanel.class, "LBL_Host"));
238         hostField.addKeyListener(new SomeChangesListener());
239         
240         hostLabel.setLabelFor(hostField);
241         
242         portLabel = new JLabel JavaDoc(NbBundle.getMessage(AddServerPropertiesVisualPanel.class, "LBL_Port"));//NOI18N
243
portField = new JTextField JavaDoc();
244         portField.setColumns(20);
245         portField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(AddServerPropertiesVisualPanel.class, "LBL_Port"));
246         portField.getAccessibleContext().setAccessibleName(NbBundle.getMessage(AddServerPropertiesVisualPanel.class, "LBL_Port"));
247         //portField.setEditable(false);
248
portField.addKeyListener(new SomeChangesListener());
249
250         portLabel.setLabelFor(portField);
251         
252         userLabel = new JLabel JavaDoc(NbBundle.getMessage(AddServerPropertiesVisualPanel.class, "LBL_User"));//NOI18N
253
userField = new JTextField JavaDoc();
254         userField.addKeyListener(new SomeChangesListener());
255         
256         passwordLabel = new JLabel JavaDoc(NbBundle.getMessage(AddServerPropertiesVisualPanel.class, "LBL_Password"));//NOI18N
257
passwordField = new JPasswordField JavaDoc();
258         passwordField.addKeyListener(new SomeChangesListener());
259         
260        
261         setLayout(new java.awt.GridBagLayout JavaDoc());
262         
263         setFocusable(false);
264         
265         setMinimumSize(new java.awt.Dimension JavaDoc(280, 217));
266        // setNextFocusableComponent(domainPathField);
267

268         
269         //-------------- some label --------------------
270
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
271         gridBagConstraints.gridwidth = 3;
272         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
273         gridBagConstraints.weightx = 1.0;
274         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 5);
275         add(label1, gridBagConstraints);
276         
277         
278         
279         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
280         gridBagConstraints.gridy = 0;
281         gridBagConstraints.gridx = 2;
282         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 5);
283         
284         add(serverType, gridBagConstraints);
285         
286         
287         //-------------- domain ---------------
288
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
289         gridBagConstraints.gridx = 0;
290         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 5);
291         gridBagConstraints.anchor = GridBagConstraints.WEST;
292         add(domainLabel, gridBagConstraints);
293         
294         
295         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
296         gridBagConstraints.gridy = 1;
297         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
298         gridBagConstraints.weightx = 1.0;
299         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 5);
300         add(domainField, gridBagConstraints);
301         
302         
303         
304         
305         //-------------- domain path ---------------
306
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
307         gridBagConstraints.gridx = 0;
308         gridBagConstraints.gridy = 2;
309         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 5);
310         add(domainPathLabel, gridBagConstraints);
311         
312         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
313         gridBagConstraints.gridy = 2;
314         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
315         gridBagConstraints.weightx = 1.0;
316         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 5);
317         add(domainPathField, gridBagConstraints);
318         
319 // gridBagConstraints = new java.awt.GridBagConstraints();
320
// gridBagConstraints.gridy = 2;
321
// gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
322
// add(browseButton, gridBagConstraints);
323
//
324

325         
326         
327         
328         
329         //-------------- host ---------------
330
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
331         gridBagConstraints.gridx = 0;
332         gridBagConstraints.gridy = 3;
333         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 5);
334         gridBagConstraints.anchor = GridBagConstraints.WEST;
335         add(hostLabel, gridBagConstraints);
336         
337         
338         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
339         gridBagConstraints.gridy = 3;
340         gridBagConstraints.gridx = 1;
341         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
342         gridBagConstraints.weightx = 1.0;
343         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 5);
344         add(hostField, gridBagConstraints);
345         
346         
347         //-------------- port ---------------
348
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
349         gridBagConstraints.gridx = 0;
350         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
351         gridBagConstraints.anchor = GridBagConstraints.WEST;
352         add(portLabel, gridBagConstraints);
353         
354         
355         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
356         gridBagConstraints.gridy = 4;
357         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
358         gridBagConstraints.weightx = 1.0;
359         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 5);
360         add(portField, gridBagConstraints);
361         
362         
363         //-------------- User ---------------
364
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
365         gridBagConstraints.gridx = 0;
366         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 5);
367         gridBagConstraints.anchor = GridBagConstraints.WEST;
368         add(userLabel, gridBagConstraints);
369         
370         
371         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
372         gridBagConstraints.gridy = 5;
373         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
374         gridBagConstraints.weightx = 1.0;
375         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 5);
376         add(userField, gridBagConstraints);
377         
378         
379         
380         //-------------- Password ---------------
381
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
382         gridBagConstraints.gridx = 0;
383         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 5);
384         gridBagConstraints.anchor = GridBagConstraints.WEST;
385         add(passwordLabel, gridBagConstraints);
386         
387         
388         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
389         gridBagConstraints.gridy = 6;
390         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
391         gridBagConstraints.weightx = 1.0;
392         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 5);
393         add(passwordField, gridBagConstraints);
394         
395         //------------- panell to fill out free space ------------------------
396
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
397         gridBagConstraints.gridx = 0;
398         gridBagConstraints.gridwidth = 3;
399         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
400         gridBagConstraints.weightx = 1.0;
401         gridBagConstraints.weighty = 1.0;
402         
403         
404         domainPathField.setEnabled(false);
405         portField.setEditable(false);
406         
407         userField.setVisible(false);
408         userLabel.setVisible(false);
409         passwordField.setVisible(false);
410         passwordLabel.setVisible(false);
411         
412         
413         serverType.setVisible(false);
414         
415         add(panel1, gridBagConstraints);
416         
417         hostField.setText("localhost");//NOI18N
418
portField.setText(JBPluginUtils.getHTTPConnectorPort(domainPathField.getText()));//NOI18N
419
// serverTypeChanged();
420
domainChanged();
421         
422     }
423     
424     
425     class SomeChangesListener implements KeyListener JavaDoc{
426         
427         public void keyTyped(KeyEvent JavaDoc e){}
428         
429         public void keyPressed(KeyEvent JavaDoc e){}
430         
431         public void keyReleased(KeyEvent JavaDoc e){ somethingChanged();}
432         
433     }
434     
435     private String JavaDoc browseDomainLocation(){
436         String JavaDoc insLocation = null;
437         JFileChooser JavaDoc chooser = getJFileChooser();
438         int returnValue = chooser.showDialog(this, NbBundle.getMessage(AddServerPropertiesVisualPanel.class, "LBL_ChooseButton")); //NOI18N
439

440         if(returnValue == JFileChooser.APPROVE_OPTION){
441             insLocation = chooser.getSelectedFile().getAbsolutePath();
442         }
443         return insLocation;
444     }
445     
446     private JFileChooser JavaDoc getJFileChooser(){
447         JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
448         
449         chooser.setDialogTitle("LBL_Chooser_Name"); //NOI18N
450
chooser.setDialogType(JFileChooser.CUSTOM_DIALOG);
451         
452         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
453         chooser.setApproveButtonMnemonic("Choose_Button_Mnemonic".charAt(0)); //NOI18N
454
chooser.setMultiSelectionEnabled(false);
455         chooser.addChoosableFileFilter(new dirFilter());
456         chooser.setAcceptAllFileFilterUsed(false);
457         chooser.setApproveButtonToolTipText("LBL_Chooser_Name"); //NOI18N
458

459         chooser.getAccessibleContext().setAccessibleName("LBL_Chooser_Name"); //NOI18N
460
chooser.getAccessibleContext().setAccessibleDescription("LBL_Chooser_Name"); //NOI18N
461

462         return chooser;
463     }
464     
465     private static class dirFilter extends javax.swing.filechooser.FileFilter JavaDoc {
466         
467         public boolean accept(File JavaDoc f) {
468             if(!f.exists() || !f.canRead() || !f.isDirectory() ) {
469                 return false;
470             }else{
471                 return true;
472             }
473         }
474         
475         public String JavaDoc getDescription() {
476             return NbBundle.getMessage(AddServerPropertiesVisualPanel.class, "LBL_DirType"); //NOI18N
477
}
478         
479     }
480     
481 }
482
483
484 class DomainComboModel extends AbstractListModel JavaDoc implements ComboBoxModel JavaDoc{
485     private int current = -1;
486     private String JavaDoc[][] domains = null;
487     
488     
489     public void addDomain(String JavaDoc domain, String JavaDoc path){
490         String JavaDoc[][] newDomains = new String JavaDoc[domains.length+1][2];
491         int i = 0;
492         for(;i<domains.length; i++){
493             newDomains[i][0] = domains[i][0];
494             newDomains[i][1] = domains[i][1];
495         }
496         newDomains[i][0] = domain;
497         newDomains[i][1] = path;
498         domains = newDomains;
499         
500     }
501     
502     public DomainComboModel(Hashtable JavaDoc domains){
503         setDomains(domains);
504     }
505     
506     public void setDomains(Hashtable JavaDoc domains) {
507         
508         current = -1;
509         this.domains = null;
510         
511         int len = domains.size();
512         this.domains = new String JavaDoc[len][2];
513         Enumeration JavaDoc en = domains.keys();
514         
515         if (len > 0) current = 0;
516         
517         int i = 0;
518         while(en.hasMoreElements()){
519             this.domains[i][0] = (String JavaDoc)en.nextElement();
520             this.domains[i][1] = (String JavaDoc)domains.get(this.domains[i][0]);
521             if(this.domains[i][0].equalsIgnoreCase("default")) //NOI18N
522
current=i;
523             i++;
524         }
525     }
526     
527     public Object JavaDoc getSelectedItem() {
528         if (current ==-1 )
529             return "";
530         return domains[current][0];
531     }
532     
533     public void setSelectedItem(Object JavaDoc anItem) {
534         for (int i = 0; i < getSize(); i++){
535             if (domains[i][0].equals(anItem)){
536                 current = i;
537                 fireContentsChanged(this, -1, -1);
538                 return;
539             }
540         }
541         current = -1;
542         //currentVal = (String)anItem;
543
fireContentsChanged(this, -1, -1);
544     }
545     
546     public Object JavaDoc getElementAt(int index){
547         return domains[index][0];
548     }
549     
550     public int getSize(){
551         return domains.length;
552     }
553     //----------------------------------------------------
554

555     public String JavaDoc getCurrentPath(){
556         if (current == -1) return "";
557         return domains[current][1];
558     }
559     
560     public boolean hasDomain(String JavaDoc domain){
561         for (int i = 0; i < getSize(); i++){
562             if (domains[i][0].equals(domain)){
563                 return true;
564             }
565         }
566         return false;
567     }
568     
569 }
570
571
572
573
574
575
Popular Tags