001 /*
002 * Copyright (c) 1995-2010, The University of Sheffield. See the file
003 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004 *
005 * This file is part of GATE (see http://gate.ac.uk/), and is free
006 * software, licenced under the GNU Library General Public License,
007 * Version 2, June 1991 (in the distribution as file licence.html,
008 * and also available at http://gate.ac.uk/gate/licence.html).
009 *
010 * Valentin Tablan 04/10/2001
011 *
012 * $Id: ParameterDisjunction.java 12246 2010-02-12 17:16:48Z valyt $
013 *
014 */
015
016 package gate.gui;
017
018 import java.util.List;
019
020 import gate.Gate;
021 import gate.Resource;
022 import gate.creole.*;
023 import gate.event.CreoleEvent;
024 import gate.event.CreoleListener;
025 import gate.util.GateRuntimeException;
026
027 /**
028 * Represents a list of Parameters which are alternative to each other.
029 * This class only gives access to one of those parameters ot any one moment.
030 * The currently accessible (selected) parameter can be changed using the
031 * {@link #setSelectedIndex(int)} method.
032 */
033 public class ParameterDisjunction implements CreoleListener {
034
035 /**
036 * Creation from a resources and a list of names.
037 * The initial values of the parameters will be read from the resource. If any
038 * of these values is null than the default value will be used. After
039 * initialisation the values will be cached inside this object; any changes
040 * made to these values will not affect the actual values on the resource.
041 *
042 * @param resource the resource these parameters belong to.
043 * @param parameters a list containing the parameters in this paramater d
044 * isjunction; each element is a {@link gate.creole.Parameter}.
045 */
046 public ParameterDisjunction(Resource resource, List parameters){
047 Gate.getCreoleRegister().addCreoleListener(this);
048 this.resource = resource;
049 params = new Parameter[parameters.size()];
050 names = new String[parameters.size()];
051 values = new Object[parameters.size()];
052 comments = new String[parameters.size()];
053 types = new String[parameters.size()];
054 required = new Boolean[parameters.size()];
055
056 for(int i = 0; i < parameters.size(); i++){
057 params[i] = (Parameter)parameters.get(i);
058 names[i] = params[i].getName();
059 comments[i] = params[i].getComment();
060 types[i] = params[i].getTypeName();
061 try{
062 values[i] = (resource == null) ?
063 null : resource.getParameterValue(params[i].getName());
064 if(values[i] == null) values[i] = params[i].getDefaultValue();
065
066 }catch(ResourceInstantiationException rie){
067 throw new GateRuntimeException(
068 "Could not get read accessor method for \"" + names[i] +
069 "\"property of " + resource.getClass().getName());
070 }catch(ParameterException pe){
071 throw new GateRuntimeException(
072 "Could not get default value for \"" + names[i] +
073 "\"property of " + resource.getClass().getName());
074 }
075 required[i] = new Boolean(!params[i].isOptional());
076 }
077
078 setSelectedIndex(0);
079 }
080
081 /**
082 * Sets the currently selected parameter for this disjunction.
083 */
084 public void setSelectedIndex(int index){
085 selectedIndex = index;
086 }
087
088
089 /**
090 * Gets the currently selected parameter for this disjunction.
091 */
092 public int getSelectedIndex() {
093 return selectedIndex;
094 }
095
096 /**
097 * gets the number of parameters in this disjunction.
098 */
099 public int size(){
100 return params.length;
101 }
102
103 /**
104 * is the currently selected parameter required?
105 */
106 public Boolean isRequired(){
107 return required[selectedIndex];
108 }
109
110 /**
111 * returns the name of the curently selected parameter.
112 */
113 public String getName(){
114 return names[selectedIndex];
115 }
116
117 /**
118 * returns the comment for the curently selected parameter.
119 */
120 public String getComment(){
121 return comments[selectedIndex];
122 }
123
124 /**
125 * returns the type for the curently selected parameter.
126 */
127 public String getType(){
128 return types[selectedIndex];
129 }
130
131 /**
132 * Returns the names of the parameters in this disjunction.
133 */
134 public String[] getNames(){
135 return names;
136 }
137
138 public void setValue(Object value){
139 values[selectedIndex] = value;
140 }
141
142 public Object getValue(){
143 return values[selectedIndex];
144 }
145
146 public Parameter[] getParameters(){
147 return params;
148 }
149
150 public Parameter getParameter(){
151 return params[selectedIndex];
152 }
153
154 public void cleanup(){
155 Gate.getCreoleRegister().removeCreoleListener(this);
156 resource = null;
157 }
158
159 /**
160 * Called by other GUI classes that use this as a subcomponent that doesn't
161 * need to update with the creole register changes.
162 */
163 void removeCreoleListenerLink(){
164 Gate.getCreoleRegister().removeCreoleListener(this);
165 }
166
167 /**
168 * Called when a resource has been unloaded from the system;
169 * If any of the parameters has this resource as value then the value will be
170 * deleted.
171 * If the resource is null then an attempt will be made to reinitialise the
172 * null values.
173 */
174 protected void updateValues(Resource res){
175 for(int i =0 ; i < values.length; i++){
176 if(values[i] == res){
177 values[i] = null;
178 try{
179 values[i] = (resource == null) ?
180 null : resource.getParameterValue(params[i].getName());
181 if(values[i] == null) values[i] = params[i].getDefaultValue();
182 }catch(ResourceInstantiationException rie){
183 throw new GateRuntimeException(
184 "Could not get read accessor method for \"" + names[i] +
185 "\"property of " + resource.getClass().getName());
186 }catch(ParameterException pe){
187 throw new GateRuntimeException(
188 "Could not get default value for \"" + names[i] +
189 "\"property of " + resource.getClass().getName());
190 }
191 }
192 }
193 }
194
195
196 int selectedIndex;
197 String[] names;
198 String[] comments;
199 String[] types;
200 Object[] values;
201 Boolean[] required;
202 Parameter[] params;
203 Resource resource;
204
205 public void resourceLoaded(CreoleEvent e) {
206 updateValues(null);
207 }
208
209 public void resourceUnloaded(CreoleEvent e) {
210 updateValues(e.getResource());
211 }
212
213 public void resourceRenamed(Resource resource, String oldName,
214 String newName){
215 updateValues(resource);
216 }
217 public void datastoreOpened(CreoleEvent e) {
218 }
219 public void datastoreCreated(CreoleEvent e) {
220 }
221 public void datastoreClosed(CreoleEvent e) {
222 }
223 }////// class ParameterDisjunction
|