|
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.5 2002/02/27 14:34:53 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 /** 144 * Called when a resource has been unloaded from the system; 145 * If any of the parameters has this resource as value then the value will be 146 * deleted. 147 * If the resource is null then an attempt will be made to reinitialise the 148 * null values. 149 */ 150 protected void updateValues(Resource res){ 151 for(int i =0 ; i < values.length; i++){ 152 if(values[i] == res){ 153 values[i] = null; 154 try{ 155 values[i] = (resource == null) ? 156 null : resource.getParameterValue(params[i].getName()); 157 if(values[i] == null) values[i] = params[i].getDefaultValue(); 158 }catch(ResourceInstantiationException rie){ 159 throw new GateRuntimeException( 160 "Could not get read accessor method for \"" + names[i] + 161 "\"property of " + resource.getClass().getName()); 162 }catch(ParameterException pe){ 163 throw new GateRuntimeException( 164 "Could not get default value for \"" + names[i] + 165 "\"property of " + resource.getClass().getName()); 166 } 167 } 168 } 169 } 170 171 172 int selectedIndex; 173 String[] names; 174 String[] comments; 175 String[] types; 176 Object[] values; 177 Boolean[] required; 178 Parameter[] params; 179 Resource resource; 180 181 public void resourceLoaded(CreoleEvent e) { 182 updateValues(null); 183 } 184 185 public void resourceUnloaded(CreoleEvent e) { 186 updateValues(e.getResource()); 187 } 188 189 public void resourceRenamed(Resource resource, String oldName, 190 String newName){ 191 updateValues(resource); 192 } 193 public void datastoreOpened(CreoleEvent e) { 194 } 195 public void datastoreCreated(CreoleEvent e) { 196 } 197 public void datastoreClosed(CreoleEvent e) { 198 } 199 }////// class ParameterDisjunction
|
ParameterDisjunction |
|