1 19 package org.lucane.applications.webadmin; 20 21 import java.awt.Dimension ; 22 import java.awt.GridLayout ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 26 import javax.swing.JButton ; 27 import javax.swing.JCheckBox ; 28 import javax.swing.JLabel ; 29 import javax.swing.JPasswordField ; 30 import javax.swing.JTextField ; 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 40 { 41 private ConnectInfo service; 42 private ManagedWindow window; 43 private JTextField txtRealm; 44 45 private JCheckBox chkHttpEnabled; 46 private JTextField txtHttpPort; 47 48 private JCheckBox chkHttpsEnabled; 49 private JTextField txtHttpsPort; 50 private JTextField txtHttpsKeystorePassword; 51 private JTextField txtHttpsKeyPassword; 52 53 private JButton 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 (8, 2)); 84 85 this.txtRealm = new JTextField (config.realmName); 86 87 this.chkHttpEnabled = new JCheckBox (); 88 chkHttpEnabled.setSelected(config.httpEnabled); 89 chkHttpEnabled.addActionListener(this); 90 this.txtHttpPort = new JTextField ("" + config.httpPort); 91 txtHttpPort.setEnabled(config.httpEnabled); 92 93 this.chkHttpsEnabled = new JCheckBox (); 94 chkHttpsEnabled.setSelected(config.httpsEnabled); 95 chkHttpsEnabled.addActionListener(this); 96 this.txtHttpsPort = new JTextField ("" + config.httpsPort); 97 txtHttpsPort.setEnabled(config.httpsEnabled); 98 this.txtHttpsKeystorePassword = new JPasswordField (config.httpsKeystorePassword); 99 txtHttpsKeystorePassword.setEnabled(config.httpsEnabled); 100 this.txtHttpsKeyPassword = new JPasswordField (config.httpsKeyPassword); 101 txtHttpsKeyPassword.setEnabled(config.httpsEnabled); 102 103 window.getContentPane().add(new JLabel (tr("lbl.realm"))); 104 window.getContentPane().add(txtRealm); 105 window.getContentPane().add(new JLabel (tr("lbl.http.enabled"))); 106 window.getContentPane().add(chkHttpEnabled); 107 window.getContentPane().add(new JLabel (tr("lbl.http.port"))); 108 window.getContentPane().add(txtHttpPort); 109 window.getContentPane().add(new JLabel (tr("lbl.https.enabled"))); 110 window.getContentPane().add(chkHttpsEnabled); 111 window.getContentPane().add(new JLabel (tr("lbl.https.port"))); 112 window.getContentPane().add(txtHttpsPort); 113 window.getContentPane().add(new JLabel (tr("lbl.https.keystorePasswd"))); 114 window.getContentPane().add(txtHttpsKeystorePassword); 115 window.getContentPane().add(new JLabel (tr("lbl.https.keyPasswd"))); 116 window.getContentPane().add(txtHttpsKeyPassword); 117 118 this.btnApply = new JButton (tr("btn.apply"), Client.getImageIcon("ok.png")); 119 btnApply.addActionListener(this); 120 window.getContentPane().add(new JLabel ()); 121 window.getContentPane().add(btnApply); 122 123 window.setPreferredSize(new Dimension (300, 250)); 124 window.show(); 125 } 126 127 public void actionPerformed(ActionEvent 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 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 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 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 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 |