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.3 2001/10/16 12:30:24 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        }catch(ResourceInstantiationException rie){
63          throw new GateRuntimeException(
64            "Could not get read accessor method for \"" + names[i] +
65            "\"property of " + resource.getClass().getName());
66        }catch(ParameterException pe){
67          throw new GateRuntimeException(
68            "Could not get default value for \"" + names[i] +
69            "\"property of " + resource.getClass().getName());
70        }
71        required[i] = new Boolean(!params[i].isOptional());
72      }
73  
74      setSelectedIndex(0);
75    }
76  
77    /**
78     * Sets the currently selected parameter for this disjunction.
79     */
80    public void setSelectedIndex(int index){
81      selectedIndex = index;
82    }
83  
84    /**
85     * gets the number of parameters in this disjunction.
86     */
87    public int size(){
88      return params.length;
89    }
90  
91    /**
92     * is the currently selected parameter required?
93     */
94    public Boolean isRequired(){
95      return required[selectedIndex];
96    }
97  
98    /**
99     * returns the name of the curently selected parameter.
100    */
101   public String getName(){
102     return names[selectedIndex];
103   }
104 
105   /**
106    * returns the comment for the curently selected parameter.
107    */
108   public String getComment(){
109     return comments[selectedIndex];
110   }
111 
112   /**
113    * returns the type for the curently selected parameter.
114    */
115   public String getType(){
116     return types[selectedIndex];
117   }
118 
119   /**
120    * Returns the names of the parameters in this disjunction.
121    */
122   public String[] getNames(){
123     return names;
124   }
125 
126   public void setValue(Object value){
127     values[selectedIndex] = value;
128   }
129 
130   public Object getValue(){
131     return values[selectedIndex];
132   }
133 
134   public Parameter[] getParameters(){
135     return params;
136   }
137 
138   public Parameter getParameter(){
139     return params[selectedIndex];
140   }
141 
142   /**
143    * Called when a resource has been unloaded from the system;
144    * If any of the parameters has this resource as value then the value will be
145    * deleted.
146    * If the resource is null then an attempt will be made to reinitialise the
147    * null values.
148    */
149   protected void updateValues(Resource res){
150     for(int i =0 ; i < values.length; i++){
151       if(values[i] == res){
152         values[i] = null;
153         try{
154           values[i] = (resource == null) ?
155                       null : resource.getParameterValue(params[i].getName());
156           if(values[i] == null) values[i] = params[i].getDefaultValue();
157         }catch(ResourceInstantiationException rie){
158           throw new GateRuntimeException(
159             "Could not get read accessor method for \"" + names[i] +
160             "\"property of " + resource.getClass().getName());
161         }catch(ParameterException pe){
162           throw new GateRuntimeException(
163             "Could not get default value for \"" + names[i] +
164             "\"property of " + resource.getClass().getName());
165         }
166       }
167     }
168   }
169 
170 
171   int selectedIndex;
172   String[] names;
173   String[] comments;
174   String[] types;
175   Object[] values;
176   Boolean[] required;
177   Parameter[] params;
178   Resource resource;
179 
180   public void resourceLoaded(CreoleEvent e) {
181     updateValues(null);
182   }
183 
184   public void resourceUnloaded(CreoleEvent e) {
185     updateValues(e.getResource());
186   }
187   public void datastoreOpened(CreoleEvent e) {
188   }
189   public void datastoreCreated(CreoleEvent e) {
190   }
191   public void datastoreClosed(CreoleEvent e) {
192   }
193 }////// class ParameterDisjunction