source: main/trunk/greenstone3/src/java/org/greenstone/admin/guiext/Option.java@ 22185

Last change on this file since 22185 was 22185, checked in by sjm84, 14 years ago

Added password, browsing and checking functionality as well as some basic extension state checking

File size: 5.1 KB
Line 
1package org.greenstone.admin.guiext;
2
3import java.lang.reflect.Constructor;
4import java.lang.reflect.Method;
5
6import javax.swing.ImageIcon;
7import javax.swing.JTable;
8
9import org.greenstone.admin.GAI;
10
11import org.w3c.dom.Element;
12
13public class Option
14{
15 protected String _type = null;
16 protected String _name = null;
17 protected String _id = null;
18 protected String _value = null;
19 protected OptionList _parent = null;
20
21 protected Object _classObject = null;
22 protected Method _checkMethod = null;
23
24 protected ImageIcon _image = null;
25
26 private boolean _canCheck = false;
27 private int _checkResult = -1;
28
29 private final String GOOD_ICON = GAI.getGSDL3Home() + System.getProperty("file.separator") + "resources" + System.getProperty("file.separator") + "images" + System.getProperty("file.separator") + "icongreentick.png";
30 private final String BAD_ICON = GAI.getGSDL3Home() + System.getProperty("file.separator") + "resources" + System.getProperty("file.separator") + "images" + System.getProperty("file.separator") + "iconredexclamation.png";
31 private final String ERROR_ICON = GAI.getGSDL3Home() + System.getProperty("file.separator") + "resources" + System.getProperty("file.separator") + "images" + System.getProperty("file.separator") + "iconorangetriange.png";
32
33 public Option(Element optionElement, OptionList parent)
34 {
35 _parent = parent;
36
37 if(optionElement != null){
38 _id = optionElement.getAttribute("id");
39 if(_id == null || _id.equals("")){
40 System.err.println("This option element does not have an id");
41 }
42
43 _name = optionElement.getAttribute("label");
44 if(_name == null || _name.equals("")){
45 _name = _id;
46 }
47
48 _value = ExtXMLHelper.getValueFromSingleElement(optionElement, false);
49
50 if(_value == null){
51 _value = "";
52 }
53
54 String checkClass = optionElement.getAttribute("checkClass");
55 String checkMethod = optionElement.getAttribute("checkMethod");
56
57 if(!checkClass.equals("") && !checkMethod.equals("")){
58 loadCheckMethod(checkClass, checkMethod);
59 }
60
61 performCheck();
62
63 _type = optionElement.getAttribute("type");
64
65 if(!(_type.equals("") || _type.equals("password") || _type.equals("filebrowse") || _type.equals("folderbrowse"))){
66 System.err.println("This option element specifies a type that is not \"password\", \"filebrowse\" or \"folderbrowse\"");
67 }
68 }
69 else{
70 System.err.println("This <" + ExtXMLHelper.OPTION + "> element is null");
71 }
72 }
73
74 private void loadCheckMethod(String checkClassString, String checkMethodString)
75 {
76 _parent.getParent().getParent().getParent().loadGuiExtFile();
77
78 Class checkClass = null;
79 try{
80 checkClass = Class.forName(checkClassString);
81 }
82 catch(Exception ex){
83 System.err.println("Could not get an instance of the check class, either the class name is incorrect or the class does not exist inside the guiext.jar file");
84 return;
85 }
86
87 try{
88 Constructor classConstructor = checkClass.getConstructor(new Class[0]);
89 _classObject = classConstructor.newInstance(new Object[0]);
90 }
91 catch(Exception ex){
92 System.err.println("Could not instantiate the check class, either the class name is incorrect or the class does not exist inside the guiext.jar file");
93 return;
94 }
95
96 try{
97 _checkMethod = checkClass.getDeclaredMethod(checkMethodString, new Class[]{Object.class});
98 }
99 catch(Exception ex){
100 System.err.println("Could not get the check method, either the method name is incorrect or the method does not take an Object as a parameter");
101 return;
102 }
103
104 if(!(_checkMethod.getReturnType().equals(Boolean.TYPE))){
105 System.err.println("The check method does not return a boolean value as is required");
106 return;
107 }
108 _canCheck = true;
109 }
110
111 private void setImageFromCheck()
112 {
113 if(_canCheck){
114 if(_checkResult == 0){
115 _image = new ImageIcon(GOOD_ICON);
116 }
117 else if(_checkResult == 1){
118 _image = new ImageIcon(BAD_ICON);
119 }
120 else{
121 _image = new ImageIcon(ERROR_ICON);
122 }
123 }
124 else{
125 _image = new ImageIcon(GOOD_ICON);
126 }
127 }
128
129 private void performCheck()
130 {
131 if(_canCheck){
132 Boolean result = null;
133 try{
134 result = ((Boolean)_checkMethod.invoke(_classObject, new Object[]{_value}));
135 }
136 catch(Exception ex){
137 _checkResult = -1;
138 }
139 if(result){
140 _checkResult = 0;
141 }
142 else{
143 _checkResult = 1;
144 }
145 }
146 setImageFromCheck();
147 }
148
149 public boolean isCheckable()
150 {
151 return _canCheck;
152 }
153
154 public int getCheckResult()
155 {
156 return _checkResult;
157 }
158
159 public String getName()
160 {
161 return _name;
162 }
163
164 public String getValue()
165 {
166 return _value;
167 }
168
169 public void setValue(String value)
170 {
171 _value = value;
172 performCheck();
173
174 JTable table = _parent.getParent().getTableFromOptionList(_parent);
175
176 if(table != null){
177 table.repaint();
178 table.revalidate();
179 }
180 }
181
182 public String getId()
183 {
184 return _id;
185 }
186
187 public OptionList getParent()
188 {
189 return _parent;
190 }
191
192 public String getType()
193 {
194 return _type;
195 }
196
197 public ImageIcon getImage()
198 {
199 return _image;
200 }
201}
Note: See TracBrowser for help on using the repository browser.