|
ParameterDisjunction |
|
1 /* 2 * Copyright (c) 1998-2001, The University of Sheffield. 3 * 4 * This file is part of GATE (see http://gate.ac.uk/), and is free 5 * software, licenced under the GNU Library General Public License, 6 * Version 2, June 1991 (in the distribution as file licence.html, 7 * and also available at http://gate.ac.uk/gate/licence.html). 8 * 9 * Valentin Tablan 04/10/2001 10 * 11 * $Id: ParameterDisjunction.java,v 1.6 2003/05/13 14:09:18 valyt Exp $ 12 * 13 */ 14 15 package gate.gui; 16 17 import gate.*; 18 import gate.creole.*; 19 import gate.util.*; 20 21 import java.util.*; 22 import gate.event.*; 23 24 /** 25 * Represents a list of Parameters which are alternative to each other. 26 * This class only gives access to one of those parameters ot any one moment. 27 * The currently accessible (selected) parameter can be changed using the 28 * {@link setSelectedIndex(int index)} method. 29 */ 30 public class ParameterDisjunction implements CreoleListener { 31 32 /** 33 * Creation from a resources and a list of names. 34 * The initial values of the parameters will be read from the resource. If any 35 * of these values is null than the default value will be used. After 36 * initialisation the values will be cached inside this object; any changes 37 * made to these values will not affect the actual values on the resource. 38 * 39 * @param the resource these parameters belong to. 40 * @param parameters a list containing the parameters in this paramater d 41 * isjunction; each element is a {@link gate.creole.Parameter}. 42 */ 43 public ParameterDisjunction(Resource resource, List parameters){ 44 Gate.getCreoleRegister().addCreoleListener(this); 45 this.resource = resource; 46 params = new Parameter[parameters.size()]; 47 names = new String[parameters.size()]; 48 values = new Object[parameters.size()]; 49 comments = new String[parameters.size()]; 50 types = new String[parameters.size()]; 51 required = new Boolean[parameters.size()]; 52 53 for(int i = 0; i < parameters.size(); i++){ 54 params[i] = (Parameter)parameters.get(i); 55 names[i] = params[i].getName(); 56 comments[i] = params[i].getComment(); 57 types[i] = params[i].getTypeName(); 58 try{ 59 values[i] = (resource == null) ? 60 null : resource.getParameterValue(params[i].getName()); 61 if(values[i] == null) values[i] = params[i].getDefaultValue(); 62 63 }catch(ResourceInstantiationException rie){ 64 throw new GateRuntimeException( 65 "Could not get read accessor method for \"" + names[i] + 66 "\"property of " + resource.getClass().getName()); 67 }catch(ParameterException pe){ 68 throw new GateRuntimeException( 69 "Could not get default value for \"" + names[i] + 70 "\"property of " + resource.getClass().getName()); 71 } 72 required[i] = new Boolean(!params[i].isOptional()); 73 } 74 75 setSelectedIndex(0); 76 } 77 78 /** 79 * Sets the currently selected parameter for this disjunction. 80 */ 81 public void setSelectedIndex(int index){ 82 selectedIndex = index; 83 } 84 85 /** 86 * gets the number of parameters in this disjunction. 87 */ 88 public int size(){ 89 return params.length; 90 } 91 92 /** 93 * is the currently selected parameter required? 94 */ 95 public Boolean isRequired(){ 96 return required[selectedIndex]; 97 } 98 99 /** 100 * returns the name of the curently selected parameter. 101 */ 102 public String getName(){ 103 return names[selectedIndex]; 104 } 105 106 /** 107 * returns the comment for the curently selected parameter. 108 */ 109 public String getComment(){ 110 return comments[selectedIndex]; 111 } 112 113 /** 114 * returns the type for the curently selected parameter. 115 */ 116 public String getType(){ 117 return types[selectedIndex]; 118 } 119 120 /** 121 * Returns the names of the parameters in this disjunction. 122 */ 123 public String[] getNames(){ 124 return names; 125 } 126 127 public void setValue(Object value){ 128 values[selectedIndex] = value; 129 } 130 131 public Object getValue(){ 132 return values[selectedIndex]; 133 } 134 135 public Parameter[] getParameters(){ 136 return params; 137 } 138 139 public Parameter getParameter(){ 140 return params[selectedIndex]; 141 } 142 143 public void cleanup(){ 144 Gate.getCreoleRegister().removeCreoleListener(this); 145 resource = null; 146 } 147 148 /** 149 * Called by other GUI classes that use this as a subcomponent that doesn't 150 * need to update with the creole register changes. 151 */ 152 void removeCreoleListenerLink(){ 153 Gate.getCreoleRegister().removeCreoleListener(this); 154 } 155 156 /** 157 * Called when a resource has been unloaded from the system; 158 * If any of the parameters has this resource as value then the value will be 159 * deleted. 160 * If the resource is null then an attempt will be made to reinitialise the 161 * null values. 162 */ 163 protected void updateValues(Resource res){ 164 for(int i =0 ; i < values.length; i++){ 165 if(values[i] == res){ 166 values[i] = null; 167 try{ 168 values[i] = (resource == null) ? 169 null : resource.getParameterValue(params[i].getName()); 170 if(values[i] == null) values[i] = params[i].getDefaultValue(); 171 }catch(ResourceInstantiationException rie){ 172 throw new GateRuntimeException( 173 "Could not get read accessor method for \"" + names[i] + 174 "\"property of " + resource.getClass().getName()); 175 }catch(ParameterException pe){ 176 throw new GateRuntimeException( 177 "Could not get default value for \"" + names[i] + 178 "\"property of " + resource.getClass().getName()); 179 } 180 } 181 } 182 } 183 184 185 int selectedIndex; 186 String[] names; 187 String[] comments; 188 String[] types; 189 Object[] values; 190 Boolean[] required; 191 Parameter[] params; 192 Resource resource; 193 194 public void resourceLoaded(CreoleEvent e) { 195 updateValues(null); 196 } 197 198 public void resourceUnloaded(CreoleEvent e) { 199 updateValues(e.getResource()); 200 } 201 202 public void resourceRenamed(Resource resource, String oldName, 203 String newName){ 204 updateValues(resource); 205 } 206 public void datastoreOpened(CreoleEvent e) { 207 } 208 public void datastoreCreated(CreoleEvent e) { 209 } 210 public void datastoreClosed(CreoleEvent e) { 211 } 212 }////// class ParameterDisjunction
|
ParameterDisjunction |
|