001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package org.quickserver.net.qsadmin.gui;
016:
017: import java.awt.*;
018: import java.awt.event.*;
019: import javax.swing.*;
020: import javax.swing.event.*;
021: import java.util.*;
022: import java.util.logging.*;
023: import org.quickserver.util.MyString;
024:
025: /**
026: * A Simple class that Stores information about QSAdmin Properties
027: * @author Akshathkumar Shetty
028: */
029: public class Propertie {
030: private String name;
031: private String target = "server";
032: private String command;
033: private boolean get = false;
034: private boolean set = false;
035: private String type = "edit";
036: private String select;
037: private String desc;
038: private String targetNeeded = "yes";
039: private String version = "1.3";//when AdminUI was added
040:
041: //gui components
042: private JLabel namelabel;
043: private JTextField editField;
044: private JComboBox selectList;
045: private JButton saveButton;
046:
047: public String getGetCommand() {
048: if (targetNeeded.equals("yes"))
049: return "get " + target + " " + command;
050: else
051: return "get " + command;
052: }
053:
054: public String getSetCommand(String value) {
055: if (targetNeeded.equals("yes"))
056: return "set " + target + " " + command + " " + value;
057: else
058: return "set " + command + " " + value;
059:
060: }
061:
062: public String getName() {
063: return name;
064: }
065:
066: public void setName(String name) {
067: if (name != null && name.equals("") == false)
068: this .name = name;
069: }
070:
071: public String getTarget() {
072: return target;
073: }
074:
075: public void setTarget(String target) {
076: if (target != null && target.equals("") == false)
077: this .target = target;
078: }
079:
080: public String getCommand() {
081: return command;
082: }
083:
084: public void setCommand(String command) {
085: if (command != null && command.equals("") == false)
086: this .command = command;
087: }
088:
089: public void setGet(String getValue) {
090: if (getValue != null && getValue.toLowerCase().equals("yes"))
091: get = true;
092: else
093: get = false;
094: }
095:
096: public boolean isGet() {
097: return get;
098: }
099:
100: public void setSet(String setValue) {
101: if (setValue != null && setValue.toLowerCase().equals("yes"))
102: set = true;
103: else
104: set = false;
105: }
106:
107: public boolean isSet() {
108: return set;
109: }
110:
111: public String getType() {
112: return type;
113: }
114:
115: public void setType(String type) {
116: if (type != null && type.equals("") == false)
117: this .type = type;
118: }
119:
120: public String getDesc() {
121: return desc;
122: }
123:
124: public void setDesc(String desc) {
125: if (desc != null && desc.equals("") == false)
126: this .desc = desc;
127: }
128:
129: public String getSelect() {
130: return select;
131: }
132:
133: public void setSelect(String select) {
134: if (select != null && select.equals("") == false)
135: this .select = select;
136: }
137:
138: public String getTargetNeeded() {
139: return targetNeeded;
140: }
141:
142: public void setTargetNeeded(String targetNeeded) {
143: this .targetNeeded = targetNeeded.toLowerCase();
144: }
145:
146: public String getVersion() {
147: return version;
148: }
149:
150: public float getVersionNo() {
151: String ver = version;
152: float version = 0;
153: int i = ver.indexOf(" "); //check if beta
154: if (i == -1)
155: i = ver.length();
156: ver = ver.substring(0, i);
157:
158: i = ver.indexOf("."); //check for sub versions
159: if (i != -1) {
160: int j = ver.indexOf(".", i);
161: if (j != -1) {
162: ver = ver.substring(0, i)
163: + "."
164: + MyString.replaceAll(ver.substring(i + 1),
165: ".", "");
166: }
167: }
168:
169: try {
170: version = Float.parseFloat(ver);
171: } catch (NumberFormatException e) {
172: //ignoring
173: }
174: return version;
175: }
176:
177: public void setVersion(String version) {
178: if (version != null && version.equals("") == false)
179: this .version = version.toLowerCase();
180: }
181:
182: public String toXML() {
183: StringBuffer sb = new StringBuffer();
184: sb.append("<propertie>\n");
185: sb.append("\t<name>" + name + "</name>\n");
186: sb.append("\t<command>" + command + "</command>\n");
187: if (get == true)
188: sb.append("\t<get>yes</get>\n");
189: else
190: sb.append("\t<get>no</get>\n");
191: if (set == true)
192: sb.append("\t<set>yes</set>\n");
193: else
194: sb.append("\t<set>no</set>\n");
195: sb.append("\t<type>" + type + "</type>\n");
196: if (select != null)
197: sb.append("\t<select>" + select + "</select>\n");
198: if (desc != null)
199: sb.append("\t<desc>" + desc + "</desc>\n");
200: sb.append("\t<version>" + version + "</version>\n");
201: if (targetNeeded != null && targetNeeded.equals("yes"))
202: sb.append("\t<target-needed>yes</target-needed>\n");
203: else
204: sb.append("\t<target-needed>no</target-needed>\n");
205: sb.append("</propertie>\n");
206: return sb.toString();
207: }
208:
209: //--- gui methods---
210: public void load(PropertiePanel pp, QSAdminMain qsadminMain) {
211: setTarget(pp.getTarget());
212: String temp = null;
213: if (isGet() == false) {
214: temp = "+OK ";
215: } else {
216: try {
217: temp = qsadminMain.sendCommunicationSilent(
218: getGetCommand(), false, false);
219: } catch (Exception e) {
220: temp = "Could not get parameter : " + e.getMessage();
221: }
222: }
223:
224: if (temp == null)
225: return;
226:
227: boolean got = false;
228: if (temp.startsWith("+OK"))
229: got = true;
230: temp = temp.substring(temp.indexOf(" ") + 1);
231: //temp = temp.trim();
232:
233: if (getType().equals("edit")) {
234: editField.setText(temp);
235: if (got == true) {
236: editField.setEnabled(true);
237: editField.setEditable(isSet());
238: }
239: } else if (getType().equals("select")) {
240: selectList.setSelectedItem(temp);
241: if (got == true) {
242: selectList.setEnabled(true);
243: }
244: }
245: }
246:
247: public void addToPanel(Container cp, GridBagConstraints gbc,
248: PropertiePanel pp, QSAdminMain qsadminMain) {
249: gbc.weighty = 0.0;
250: gbc.weightx = 0.0;
251: gbc.gridy++;
252: gbc.gridheight = 1;
253: gbc.gridwidth = 1;
254: gbc.anchor = GridBagConstraints.WEST;
255: gbc.fill = GridBagConstraints.NONE;
256:
257: String temp = getType().toLowerCase();
258: if (temp == null)
259: temp = "edit";
260:
261: //space
262: gbc.gridx = 0;
263: gbc.weightx = 0.0;
264: gbc.anchor = GridBagConstraints.WEST;
265: gbc.fill = GridBagConstraints.NONE;
266: cp.add(Box.createRigidArea(new Dimension(10, 10)), gbc);
267:
268: //label
269: gbc.gridx++;
270: gbc.anchor = GridBagConstraints.WEST;
271: gbc.fill = GridBagConstraints.NONE;
272: namelabel = new JLabel(getName());
273: namelabel.setToolTipText(getDesc());
274: cp.add(namelabel, gbc);
275:
276: //space
277: gbc.gridx++;
278: gbc.weightx = 0.0;
279: gbc.anchor = GridBagConstraints.WEST;
280: gbc.fill = GridBagConstraints.NONE;
281: cp.add(Box.createRigidArea(new Dimension(10, 10)), gbc);
282:
283: //value
284: gbc.gridx++;
285: gbc.weightx = 1.0;
286: gbc.fill = GridBagConstraints.HORIZONTAL;
287: if (temp.equals("edit")) {
288: editField = new JTextField();
289: editField.setEnabled(false);
290: editField.setToolTipText(getDesc());
291: cp.add(editField, gbc);
292: } else if (temp.equals("select")) {
293: temp = getSelect();
294: StringTokenizer st = new StringTokenizer(temp, "|");
295: String[] valStrings = new String[st.countTokens()];
296: for (int i = 0; st.hasMoreTokens(); i++) {
297: valStrings[i] = st.nextToken();
298: }
299: selectList = new JComboBox(valStrings);
300: selectList.setMaximumRowCount(3);
301: selectList.setEditable(false);
302: selectList.setEnabled(false);
303: cp.add(selectList, gbc);
304: }
305:
306: //space
307: gbc.weightx = 0.0;
308: gbc.anchor = GridBagConstraints.WEST;
309: gbc.fill = GridBagConstraints.NONE;
310: gbc.gridx++;
311: cp.add(Box.createRigidArea(new Dimension(10, 10)), gbc);
312:
313: //control
314: gbc.gridx++;
315: gbc.weightx = 0.5;
316: gbc.fill = GridBagConstraints.HORIZONTAL;
317: if (isSet() == true) {
318: saveButton = new JButton("Save");
319: saveButton.setEnabled(false);
320: saveButton.addActionListener(getSaveAction(qsadminMain,
321: Propertie.this ));
322: cp.add(saveButton, gbc);
323: } else {
324: cp.add(new JLabel(), gbc);
325: }
326:
327: //extra space
328: gbc.gridx++;
329: gbc.weightx = 0.0;
330: gbc.anchor = GridBagConstraints.WEST;
331: gbc.fill = GridBagConstraints.NONE;
332: cp.add(Box.createRigidArea(new Dimension(10, 10)), gbc);
333:
334: if (temp.equals("edit")) {
335: editField.getDocument().addDocumentListener(
336: new EditFieldDocumentListener(saveButton));
337: } else {
338: selectList.addItemListener(new ItemListener() {
339: public void itemStateChanged(ItemEvent e) {
340: saveButton.setEnabled(true);
341: }
342: });
343: }
344: }
345:
346: public JTextField getEditField() {
347: return editField;
348: }
349:
350: public JComboBox getComboBox() {
351: return selectList;
352: }
353:
354: public JButton getSaveButton() {
355: return saveButton;
356: }
357:
358: private ActionListener getSaveAction(QSAdminMain qsadminMain,
359: Propertie propertie) {
360: return new SaveActionListener(qsadminMain, propertie);
361: }
362: }
|