KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > webadmin > WebAdminPlugin


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.webadmin;
20
21 import java.awt.Dimension JavaDoc;
22 import java.awt.GridLayout JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25
26 import javax.swing.JButton JavaDoc;
27 import javax.swing.JCheckBox JavaDoc;
28 import javax.swing.JLabel JavaDoc;
29 import javax.swing.JPasswordField JavaDoc;
30 import javax.swing.JTextField JavaDoc;
31
32 import org.lucane.client.*;
33 import org.lucane.client.widgets.DialogBox;
34 import org.lucane.client.widgets.ManagedWindow;
35 import org.lucane.common.*;
36 import org.lucane.common.net.ObjectConnection;
37
38 public class WebAdminPlugin extends StandalonePlugin
39 implements ActionListener JavaDoc
40 {
41     private ConnectInfo service;
42     private ManagedWindow window;
43     private JTextField JavaDoc txtRealm;
44     
45     private JCheckBox JavaDoc chkHttpEnabled;
46     private JTextField JavaDoc txtHttpPort;
47
48     private JCheckBox JavaDoc chkHttpsEnabled;
49     private JTextField JavaDoc txtHttpsPort;
50     private JTextField JavaDoc txtHttpsKeystorePassword;
51     private JTextField JavaDoc txtHttpsKeyPassword;
52
53     private JButton JavaDoc btnApply;
54     
55     public WebAdminPlugin()
56     {
57     }
58
59     public Plugin newInstance(ConnectInfo[] friends)
60     {
61         return new WebAdminPlugin();
62     }
63
64     public void start()
65     {
66         this.service = Communicator.getInstance().getConnectInfo(getName());
67         
68         showWindow();
69     }
70     
71     private void showWindow()
72     {
73         WebConfig config = getConfig();
74         if(config == null)
75         {
76             exit();
77             return;
78         }
79         
80         this.window = new ManagedWindow(this, getTitle());
81         window.setName("window");
82         window.setExitPluginOnClose(true);
83         window.getContentPane().setLayout(new GridLayout JavaDoc(8, 2));
84         
85         this.txtRealm = new JTextField JavaDoc(config.realmName);
86
87         this.chkHttpEnabled = new JCheckBox JavaDoc();
88         chkHttpEnabled.setSelected(config.httpEnabled);
89         chkHttpEnabled.addActionListener(this);
90         this.txtHttpPort = new JTextField JavaDoc("" + config.httpPort);
91         txtHttpPort.setEnabled(config.httpEnabled);
92         
93         this.chkHttpsEnabled = new JCheckBox JavaDoc();
94         chkHttpsEnabled.setSelected(config.httpsEnabled);
95         chkHttpsEnabled.addActionListener(this);
96         this.txtHttpsPort = new JTextField JavaDoc("" + config.httpsPort);
97         txtHttpsPort.setEnabled(config.httpsEnabled);
98         this.txtHttpsKeystorePassword = new JPasswordField JavaDoc(config.httpsKeystorePassword);
99         txtHttpsKeystorePassword.setEnabled(config.httpsEnabled);
100         this.txtHttpsKeyPassword = new JPasswordField JavaDoc(config.httpsKeyPassword);
101         txtHttpsKeyPassword.setEnabled(config.httpsEnabled);
102
103         window.getContentPane().add(new JLabel JavaDoc(tr("lbl.realm")));
104         window.getContentPane().add(txtRealm);
105         window.getContentPane().add(new JLabel JavaDoc(tr("lbl.http.enabled")));
106         window.getContentPane().add(chkHttpEnabled);
107         window.getContentPane().add(new JLabel JavaDoc(tr("lbl.http.port")));
108         window.getContentPane().add(txtHttpPort);
109         window.getContentPane().add(new JLabel JavaDoc(tr("lbl.https.enabled")));
110         window.getContentPane().add(chkHttpsEnabled);
111         window.getContentPane().add(new JLabel JavaDoc(tr("lbl.https.port")));
112         window.getContentPane().add(txtHttpsPort);
113         window.getContentPane().add(new JLabel JavaDoc(tr("lbl.https.keystorePasswd")));
114         window.getContentPane().add(txtHttpsKeystorePassword);
115         window.getContentPane().add(new JLabel JavaDoc(tr("lbl.https.keyPasswd")));
116         window.getContentPane().add(txtHttpsKeyPassword);
117
118         this.btnApply = new JButton JavaDoc(tr("btn.apply"), Client.getImageIcon("ok.png"));
119         btnApply.addActionListener(this);
120         window.getContentPane().add(new JLabel JavaDoc());
121         window.getContentPane().add(btnApply);
122         
123         window.setPreferredSize(new Dimension JavaDoc(300, 250));
124         window.show();
125     }
126     
127     public void actionPerformed(ActionEvent JavaDoc ae)
128     {
129         if(ae.getSource() == btnApply)
130         {
131             WebConfig config = new WebConfig(txtRealm.getText(),
132                     chkHttpEnabled.isSelected(),
133                     Integer.parseInt(txtHttpPort.getText()),
134                     chkHttpsEnabled.isSelected(),
135                     Integer.parseInt(txtHttpsPort.getText()),
136                     txtHttpsKeystorePassword.getText(),
137                     txtHttpsKeyPassword.getText());
138             
139             if(config.httpsEnabled && (config.httpsKeyPassword.length() == 0
140                     || config.httpsKeystorePassword.length() == 0))
141             {
142                 DialogBox.error(tr("msg.passwordNeeded"));
143                 return;
144             }
145             
146             if(setConfig(config))
147             {
148                 window.dispose();
149                 this.exit();
150             }
151         }
152         
153         else if(ae.getSource() == chkHttpEnabled)
154         {
155             txtHttpPort.setEnabled(chkHttpEnabled.isSelected());
156         }
157         else if(ae.getSource() == chkHttpsEnabled)
158         {
159             txtHttpsPort.setEnabled(chkHttpsEnabled.isSelected());
160             txtHttpsKeyPassword.setEnabled(chkHttpsEnabled.isSelected());
161             txtHttpsKeystorePassword.setEnabled(chkHttpsEnabled.isSelected());
162         }
163     }
164     
165     private boolean setConfig(WebConfig config)
166     {
167         String JavaDoc ack = null;
168         try {
169             WebAdminAction action = new WebAdminAction(WebAdminAction.SET_CONFIG, config);
170             ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
171             ack = oc.readString();
172             oc.close();
173         } catch(Exception JavaDoc e) {
174             ack = tr("err.unableToSaveConfig") + e;
175         }
176         
177         if(!ack.equals("OK"))
178         {
179             DialogBox.error(ack);
180             return false;
181         }
182         
183         return true;
184     }
185
186     private WebConfig getConfig()
187     {
188         String JavaDoc ack = null;
189         WebConfig config = null;
190         try {
191             WebAdminAction action = new WebAdminAction(WebAdminAction.GET_CONFIG);
192             ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
193             ack = oc.readString();
194             config = (WebConfig)oc.read();
195             oc.close();
196         } catch(Exception JavaDoc e) {
197             ack = tr("err.unableToGetConfig") + e;
198         }
199         
200         if(!ack.equals("OK"))
201             DialogBox.error(ack);
202         
203         return config;
204     }
205 }
206
Popular Tags