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