1   /*
2    *  AbstractVisualResource.java
3    *
4    *  Copyright (c) 1998-2005, The University of Sheffield.
5    *
6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
7    *  software, licenced under the GNU Library General Public License,
8    *  Version 2, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *
11   *  Cristian URSU, 24/Jan/2001
12   *
13   *  $Id: AbstractVisualResource.java,v 1.15 2005/01/11 13:51:31 ian Exp $
14   */
15  
16  package gate.creole;
17  
18  import java.beans.BeanInfo;
19  import java.beans.Introspector;
20  
21  import javax.swing.JPanel;
22  
23  import gate.*;
24  import gate.gui.Handle;
25  import gate.util.Strings;
26  
27  /** A convenience implementation of VisualResource with some default code. */
28  public abstract class AbstractVisualResource extends JPanel
29                                               implements VisualResource{
30  
31    /**
32     * Package access constructor to stop normal initialisation.
33     * This kind of resources should only be created by the Factory class
34     */
35    public AbstractVisualResource(){
36    }
37  
38    /** Accessor for features. */
39    public FeatureMap getFeatures(){
40      return features;
41    }//getFeatures()
42  
43    /** Mutator for features*/
44    public void setFeatures(FeatureMap features){
45      this.features = features;
46    }// setFeatures()
47  
48    /** Initialise this resource, and return it. */
49    public Resource init() throws ResourceInstantiationException {
50      return this;
51    }//init()
52  
53    /** Does nothing now, but meant to clear all internal data **/
54    public void cleanup() {
55      this.handle = null;
56      features.clear();
57    }//clear()
58  
59    /**
60     * Called by the GUI when this viewer/editor has to initialise itself for a
61     * specific object.
62     * @param target the object (be it a {@link gate.Resource},
63     * {@link gate.DataStore} or whatever) this viewer has to display
64     */
65    public void setTarget(Object target){
66      throw new RuntimeException(
67        "Class " + getClass() + " hasn't implemented the setTarget() method!");
68    }
69  
70  
71    /**
72     * Used by the main GUI to tell this VR what handle created it. The VRs can
73     * use this information e.g. to add items to the popup for the resource.
74     */
75    public void setHandle(Handle handle){
76      this.handle = handle;
77    }
78  
79    //Parameters utility methods
80    /**
81     * Gets the value of a parameter of this resource.
82     * @param paramaterName the name of the parameter
83     * @return the current value of the parameter
84     */
85    public Object getParameterValue(String paramaterName)
86                  throws ResourceInstantiationException{
87      return AbstractResource.getParameterValue(this, paramaterName);
88    }
89  
90    /**
91     * Sets the value for a specified parameter.
92     *
93     * @param paramaterName the name for the parameteer
94     * @param parameterValue the value the parameter will receive
95     */
96    public void setParameterValue(String paramaterName, Object parameterValue)
97                throws ResourceInstantiationException{
98      // get the beaninfo for the resource bean, excluding data about Object
99      BeanInfo resBeanInf = null;
100     try {
101       resBeanInf = Introspector.getBeanInfo(this.getClass(), Object.class);
102     } catch(Exception e) {
103       throw new ResourceInstantiationException(
104         "Couldn't get bean info for resource " + this.getClass().getName()
105         + Strings.getNl() + "Introspector exception was: " + e
106       );
107     }
108     AbstractResource.setParameterValue(this, resBeanInf, paramaterName, parameterValue);
109   }
110 
111   /**
112    * Sets the values for more parameters in one step.
113    *
114    * @param parameters a feature map that has paramete names as keys and
115    * parameter values as values.
116    */
117   public void setParameterValues(FeatureMap parameters)
118               throws ResourceInstantiationException{
119     AbstractResource.setParameterValues(this, parameters);
120   }
121 
122   // Properties for the resource
123   protected FeatureMap features;
124   
125   /**
126    * The handle for this visual resource
127    */
128   protected Handle handle;
129 
130 }//AbstractVisualResource