001 /*
002 * AbstractVisualResource.java
003 *
004 * Copyright (c) 1995-2010, The University of Sheffield. See the file
005 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006 *
007 * This file is part of GATE (see http://gate.ac.uk/), and is free
008 * software, licenced under the GNU Library General Public License,
009 * Version 2, June 1991 (in the distribution as file licence.html,
010 * and also available at http://gate.ac.uk/gate/licence.html).
011 *
012 * Cristian URSU, 24/Jan/2001
013 *
014 * $Id: AbstractVisualResource.java 12006 2009-12-01 17:24:28Z thomas_heitz $
015 */
016
017 package gate.creole;
018
019 import java.beans.BeanInfo;
020 import java.beans.Introspector;
021
022 import javax.swing.JPanel;
023
024 import gate.*;
025 import gate.gui.Handle;
026 import gate.util.Strings;
027
028 /** A convenience implementation of VisualResource with some default code. */
029 public abstract class AbstractVisualResource extends JPanel
030 implements VisualResource{
031
032 /**
033 * Package access constructor to stop normal initialisation.
034 * This kind of resources should only be created by the Factory class
035 */
036 public AbstractVisualResource(){
037 }
038
039 /** Accessor for features. */
040 public FeatureMap getFeatures(){
041 return features;
042 }//getFeatures()
043
044 /** Mutator for features*/
045 public void setFeatures(FeatureMap features){
046 this.features = features;
047 }// setFeatures()
048
049 /** Initialise this resource, and return it. */
050 public Resource init() throws ResourceInstantiationException {
051 return this;
052 }//init()
053
054 /** Does nothing now, but meant to clear all internal data **/
055 public void cleanup() {
056 this.handle = null;
057 features.clear();
058 }//clear()
059
060 /**
061 * Called by the GUI when this viewer/editor has to initialise itself for a
062 * specific object.
063 * @param target the object (be it a {@link gate.Resource},
064 * {@link gate.DataStore} or whatever) this viewer has to display
065 */
066 public void setTarget(Object target){
067 throw new RuntimeException(
068 "Class " + getClass() + " hasn't implemented the setTarget() method!");
069 }
070
071
072 /**
073 * Used by the main GUI to tell this VR what handle created it. The VRs can
074 * use this information e.g. to add items to the popup for the resource.
075 */
076 public void setHandle(Handle handle){
077 this.handle = handle;
078 }
079
080 //Parameters utility methods
081 /**
082 * Gets the value of a parameter of this resource.
083 * @param paramaterName the name of the parameter
084 * @return the current value of the parameter
085 */
086 public Object getParameterValue(String paramaterName)
087 throws ResourceInstantiationException{
088 return AbstractResource.getParameterValue(this, paramaterName);
089 }
090
091 /**
092 * Sets the value for a specified parameter.
093 *
094 * @param paramaterName the name for the parameteer
095 * @param parameterValue the value the parameter will receive
096 */
097 public void setParameterValue(String paramaterName, Object parameterValue)
098 throws ResourceInstantiationException{
099 // get the beaninfo for the resource bean, excluding data about Object
100 BeanInfo resBeanInf = null;
101 try {
102 resBeanInf = Introspector.getBeanInfo(this.getClass(), Object.class);
103 } catch(Exception e) {
104 throw new ResourceInstantiationException(
105 "Couldn't get bean info for resource " + this.getClass().getName()
106 + Strings.getNl() + "Introspector exception was: " + e
107 );
108 }
109 AbstractResource.setParameterValue(this, resBeanInf, paramaterName, parameterValue);
110 }
111
112 /**
113 * Sets the values for more parameters in one step.
114 *
115 * @param parameters a feature map that has paramete names as keys and
116 * parameter values as values.
117 */
118 public void setParameterValues(FeatureMap parameters)
119 throws ResourceInstantiationException{
120 AbstractResource.setParameterValues(this, parameters);
121 }
122
123 // Properties for the resource
124 protected FeatureMap features;
125
126 /**
127 * The handle for this visual resource
128 */
129 protected Handle handle;
130
131 }//AbstractVisualResource
|