KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > net > qsadmin > gui > Propertie


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

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 /**
26  * A Simple class that Stores information about QSAdmin Properties
27  * @author Akshathkumar Shetty
28  */

29 public class Propertie {
30     private String JavaDoc name;
31     private String JavaDoc target = "server";
32     private String JavaDoc command;
33     private boolean get = false;
34     private boolean set = false;
35     private String JavaDoc type = "edit";
36     private String JavaDoc select;
37     private String JavaDoc desc;
38     private String JavaDoc targetNeeded = "yes";
39     private String JavaDoc version = "1.3";//when AdminUI was added
40

41     //gui components
42
private JLabel namelabel;
43     private JTextField editField;
44     private JComboBox selectList;
45     private JButton saveButton;
46
47     public String JavaDoc getGetCommand() {
48         if(targetNeeded.equals("yes"))
49             return "get "+target+" "+command;
50         else
51             return "get "+command;
52     }
53     public String JavaDoc getSetCommand(String JavaDoc 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 JavaDoc getName(){
62         return name;
63     }
64     public void setName(String JavaDoc name) {
65         if(name!=null && name.equals("")==false)
66             this.name = name;
67     }
68
69     public String JavaDoc getTarget() {
70         return target;
71     }
72     public void setTarget(String JavaDoc target) {
73         if(target!=null && target.equals("")==false)
74             this.target = target;
75     }
76
77     public String JavaDoc getCommand() {
78         return command;
79     }
80     public void setCommand(String JavaDoc command) {
81         if(command!=null && command.equals("")==false)
82             this.command = command;
83     }
84
85     public void setGet(String JavaDoc 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 JavaDoc 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 JavaDoc getType() {
106         return type;
107     }
108     public void setType(String JavaDoc type) {
109         if(type!=null && type.equals("")==false)
110             this.type = type;
111     }
112
113     public String JavaDoc getDesc() {
114         return desc;
115     }
116     public void setDesc(String JavaDoc desc) {
117         if(desc!=null && desc.equals("")==false)
118             this.desc = desc;
119     }
120
121     public String JavaDoc getSelect() {
122         return select;
123     }
124     public void setSelect(String JavaDoc select) {
125         if(select!=null && select.equals("")==false)
126             this.select = select;
127     }
128
129     public String JavaDoc getTargetNeeded() {
130         return targetNeeded;
131     }
132     public void setTargetNeeded(String JavaDoc targetNeeded) {
133         this.targetNeeded = targetNeeded.toLowerCase();
134     }
135
136     public String JavaDoc getVersion() {
137         return version;
138     }
139     public float getVersionNo() {
140         String JavaDoc ver = version;
141         float version = 0;
142         int i = ver.indexOf(" "); //check if beta
143
if(i == -1)
144             i = ver.length();
145         ver = ver.substring(0, i);
146
147         i = ver.indexOf("."); //check for sub versions
148
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 JavaDoc e) {
159             //ignoring
160
}
161         return version;
162     }
163     public void setVersion(String JavaDoc version) {
164         if(version!=null && version.equals("")==false)
165             this.version = version.toLowerCase();
166     }
167
168     public String JavaDoc toXML() {
169         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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     //--- gui methods---
196
public void load(PropertiePanel pp, QSAdminMain qsadminMain) {
197         setTarget(pp.getTarget());
198         String JavaDoc temp = null;
199         if(isGet()==false) {
200             temp = "+OK ";
201         } else {
202             try {
203                 temp = qsadminMain.sendCommunicationSilent(getGetCommand(),
204                     false, false);
205             } catch(Exception JavaDoc 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         //temp = temp.trim();
217

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 JavaDoc temp = getType().toLowerCase();
243         if(temp==null) temp = "edit";
244
245         //space
246
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         //label
253
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         //space
261
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         //value
269
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 JavaDoc[] valStrings = new String JavaDoc[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         //space
292
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         //control
299
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         //extra space
313
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