1 14 15 package org.quickserver.net.qsadmin.gui; 16 17 import java.awt.*; 18 import java.awt.event.*; 19 import javax.swing.*; 20 import javax.swing.event.*; 21 import java.util.*; 22 import java.util.logging.*; 23 import org.quickserver.util.MyString; 24 25 29 public class Propertie { 30 private String name; 31 private String target = "server"; 32 private String command; 33 private boolean get = false; 34 private boolean set = false; 35 private String type = "edit"; 36 private String select; 37 private String desc; 38 private String targetNeeded = "yes"; 39 private String version = "1.3"; 41 private JLabel namelabel; 43 private JTextField editField; 44 private JComboBox selectList; 45 private JButton saveButton; 46 47 public String getGetCommand() { 48 if(targetNeeded.equals("yes")) 49 return "get "+target+" "+command; 50 else 51 return "get "+command; 52 } 53 public String getSetCommand(String value) { 54 if(targetNeeded.equals("yes")) 55 return "set "+target+" "+command+" "+value; 56 else 57 return "set "+command+" "+value; 58 59 } 60 61 public String getName(){ 62 return name; 63 } 64 public void setName(String name) { 65 if(name!=null && name.equals("")==false) 66 this.name = name; 67 } 68 69 public String getTarget() { 70 return target; 71 } 72 public void setTarget(String target) { 73 if(target!=null && target.equals("")==false) 74 this.target = target; 75 } 76 77 public String getCommand() { 78 return command; 79 } 80 public void setCommand(String command) { 81 if(command!=null && command.equals("")==false) 82 this.command = command; 83 } 84 85 public void setGet(String getValue) { 86 if(getValue!=null && getValue.toLowerCase().equals("yes")) 87 get = true; 88 else 89 get = false; 90 } 91 public boolean isGet() { 92 return get; 93 } 94 95 public void setSet(String setValue) { 96 if(setValue!=null && setValue.toLowerCase().equals("yes")) 97 set = true; 98 else 99 set = false; 100 } 101 public boolean isSet() { 102 return set; 103 } 104 105 public String getType() { 106 return type; 107 } 108 public void setType(String type) { 109 if(type!=null && type.equals("")==false) 110 this.type = type; 111 } 112 113 public String getDesc() { 114 return desc; 115 } 116 public void setDesc(String desc) { 117 if(desc!=null && desc.equals("")==false) 118 this.desc = desc; 119 } 120 121 public String getSelect() { 122 return select; 123 } 124 public void setSelect(String select) { 125 if(select!=null && select.equals("")==false) 126 this.select = select; 127 } 128 129 public String getTargetNeeded() { 130 return targetNeeded; 131 } 132 public void setTargetNeeded(String targetNeeded) { 133 this.targetNeeded = targetNeeded.toLowerCase(); 134 } 135 136 public String getVersion() { 137 return version; 138 } 139 public float getVersionNo() { 140 String ver = version; 141 float version = 0; 142 int i = ver.indexOf(" "); if(i == -1) 144 i = ver.length(); 145 ver = ver.substring(0, i); 146 147 i = ver.indexOf("."); if(i!=-1) { 149 int j = ver.indexOf(".", i); 150 if(j!=-1) { 151 ver = ver.substring(0, i)+"."+ 152 MyString.replaceAll(ver.substring(i+1), ".", ""); 153 } 154 } 155 156 try { 157 version = Float.parseFloat(ver); 158 } catch(NumberFormatException e) { 159 } 161 return version; 162 } 163 public void setVersion(String version) { 164 if(version!=null && version.equals("")==false) 165 this.version = version.toLowerCase(); 166 } 167 168 public String toXML() { 169 StringBuffer sb = new StringBuffer (); 170 sb.append("<propertie>\n"); 171 sb.append("\t<name>"+name+"</name>\n"); 172 sb.append("\t<command>"+command+"</command>\n"); 173 if(get==true) 174 sb.append("\t<get>yes</get>\n"); 175 else 176 sb.append("\t<get>no</get>\n"); 177 if(set==true) 178 sb.append("\t<set>yes</set>\n"); 179 else 180 sb.append("\t<set>no</set>\n"); 181 sb.append("\t<type>"+type+"</type>\n"); 182 if(select!=null) 183 sb.append("\t<select>"+select+"</select>\n"); 184 if(desc!=null) 185 sb.append("\t<desc>"+desc+"</desc>\n"); 186 sb.append("\t<version>"+version+"</version>\n"); 187 if(targetNeeded!=null && targetNeeded.equals("yes")) 188 sb.append("\t<target-needed>yes</target-needed>\n"); 189 else 190 sb.append("\t<target-needed>no</target-needed>\n"); 191 sb.append("</propertie>\n"); 192 return sb.toString(); 193 } 194 195 public void load(PropertiePanel pp, QSAdminMain qsadminMain) { 197 setTarget(pp.getTarget()); 198 String temp = null; 199 if(isGet()==false) { 200 temp = "+OK "; 201 } else { 202 try { 203 temp = qsadminMain.sendCommunicationSilent(getGetCommand(), 204 false, false); 205 } catch(Exception e) { 206 temp = "Could not get parameter : "+e.getMessage(); 207 } 208 } 209 210 if(temp==null) return; 211 212 boolean got = false; 213 if(temp.startsWith("+OK")) 214 got = true; 215 temp = temp.substring(temp.indexOf(" ")+1); 216 218 if(getType().equals("edit")) { 219 editField.setText(temp); 220 if(got==true) { 221 editField.setEnabled(true); 222 editField.setEditable(isSet()); 223 } 224 } else if(getType().equals("select")) { 225 selectList.setSelectedItem(temp); 226 if(got==true) { 227 selectList.setEnabled(true); 228 } 229 } 230 } 231 232 public void addToPanel(Container cp, GridBagConstraints gbc, 233 PropertiePanel pp, QSAdminMain qsadminMain) { 234 gbc.weighty = 0.0; 235 gbc.weightx = 0.0; 236 gbc.gridy++; 237 gbc.gridheight = 1; 238 gbc.gridwidth = 1; 239 gbc.anchor = GridBagConstraints.WEST; 240 gbc.fill = GridBagConstraints.NONE; 241 242 String temp = getType().toLowerCase(); 243 if(temp==null) temp = "edit"; 244 245 gbc.gridx = 0; 247 gbc.weightx = 0.0; 248 gbc.anchor = GridBagConstraints.WEST; 249 gbc.fill = GridBagConstraints.NONE; 250 cp.add(Box.createRigidArea(new Dimension(10,10)), gbc); 251 252 gbc.gridx++; 254 gbc.anchor = GridBagConstraints.WEST; 255 gbc.fill = GridBagConstraints.NONE; 256 namelabel = new JLabel(getName()); 257 namelabel.setToolTipText(getDesc()); 258 cp.add(namelabel, gbc); 259 260 gbc.gridx++; 262 gbc.weightx = 0.0; 263 gbc.anchor = GridBagConstraints.WEST; 264 gbc.fill = GridBagConstraints.NONE; 265 cp.add(Box.createRigidArea(new Dimension(10,10)), gbc); 266 267 268 gbc.gridx++; 270 gbc.weightx = 1.0; 271 gbc.fill = GridBagConstraints.HORIZONTAL; 272 if(temp.equals("edit")) { 273 editField = new JTextField(); 274 editField.setEnabled(false); 275 editField.setToolTipText(getDesc()); 276 cp.add(editField, gbc); 277 } else if(temp.equals("select")) { 278 temp = getSelect(); 279 StringTokenizer st = new StringTokenizer(temp,"|"); 280 String [] valStrings = new String [st.countTokens()]; 281 for(int i=0;st.hasMoreTokens();i++) { 282 valStrings[i]=st.nextToken(); 283 } 284 selectList = new JComboBox(valStrings); 285 selectList.setMaximumRowCount(3); 286 selectList.setEditable(false); 287 selectList.setEnabled(false); 288 cp.add(selectList, gbc); 289 } 290 291 gbc.weightx = 0.0; 293 gbc.anchor = GridBagConstraints.WEST; 294 gbc.fill = GridBagConstraints.NONE; 295 gbc.gridx++; 296 cp.add(Box.createRigidArea(new Dimension(10,10)), gbc); 297 298 gbc.gridx++; 300 gbc.weightx = 0.5; 301 gbc.fill = GridBagConstraints.HORIZONTAL; 302 if(isSet()==true) { 303 saveButton = new JButton("Save"); 304 saveButton.setEnabled(false); 305 saveButton.addActionListener( 306 getSaveAction(qsadminMain, Propertie.this)); 307 cp.add(saveButton, gbc); 308 } else { 309 cp.add(new JLabel(), gbc); 310 } 311 312 gbc.gridx++; 314 gbc.weightx = 0.0; 315 gbc.anchor = GridBagConstraints.WEST; 316 gbc.fill = GridBagConstraints.NONE; 317 cp.add(Box.createRigidArea(new Dimension(10,10)), gbc); 318 319 if(temp.equals("edit")) { 320 editField.getDocument().addDocumentListener( 321 new EditFieldDocumentListener(saveButton)); 322 } else { 323 selectList.addItemListener(new ItemListener() { 324 public void itemStateChanged(ItemEvent e) { 325 saveButton.setEnabled(true); 326 } 327 }); 328 } 329 } 330 331 public JTextField getEditField() { 332 return editField; 333 } 334 335 public JComboBox getComboBox() { 336 return selectList; 337 } 338 339 public JButton getSaveButton() { 340 return saveButton; 341 } 342 343 private ActionListener getSaveAction(QSAdminMain qsadminMain, 344 Propertie propertie) { 345 return new SaveActionListener(qsadminMain, propertie); 346 } 347 } 348 | Popular Tags |