UnrestrictedAnnotationEditor.java
001 /*  UnrestrictedAnnotationEditor.java
002  *
003  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
004  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
005  *
006  *  This file is part of GATE (see http://gate.ac.uk/), and is free
007  *  software, licenced under the GNU Library General Public License,
008  *  Version 2, June 1991 (in the distribution as file licence.html,
009  *  and also available at http://gate.ac.uk/gate/licence.html).
010  *
011  *  Cristian URSU,  13/July/2001
012  *
013  *  $Id: UnrestrictedAnnotationEditor.java 12006 2009-12-01 17:24:28Z thomas_heitz $
014  *
015  */
016 
017 package gate.gui;
018 
019 import javax.swing.*;
020 
021 import gate.*;
022 import gate.creole.AbstractVisualResource;
023 import gate.creole.AnnotationVisualResource;
024 import gate.gui.docview.AnnotationEditor;
025 import gate.util.*;
026 
027 /** This class visually adds/edits features and annot type of an annotation
028   * It does this without using an {@link gate.creole.AnnotationSchema}.
029   * The user can manipulate annotation and features at his own will.
030   * It's his responsability.
031   
032   * This class has been deprecated! The functionality is now provided by the 
033   {@link AnnotationEditor} class.
034   
035   @deprecated
036   */
037 public class UnrestrictedAnnotationEditor extends AbstractVisualResource
038                                           implements AnnotationVisualResource,
039                                                      ResizableVisualResource{
040 
041   /** Default constructor*/
042   public UnrestrictedAnnotationEditor() {}
043 
044   // Methods required by AnnotationVisualResource
045 
046   /**
047     * Used when the viewer/editor has to display/edit an existing annotation
048     @param ann the annotation to be displayed or edited. If ann is null then
049     * the method simply returns
050     */
051   public void editAnnotation(Annotation ann, AnnotationSet set){
052     // If ann is null, then simply return.
053     if (ann == nullreturn;
054     currentAnnot = ann;
055     currentAnnotSet = set;
056     currentStartOffset = currentAnnot.getStartNode().getOffset();
057     currentEndOffset = currentAnnot.getEndNode().getOffset();
058 
059     initLocalData();
060     initGuiComponents();
061 
062   }// setAnnotation();
063 
064 
065   /**
066    * Called by the GUI when the user has pressed the "OK" button. This should
067    * trigger the saving of the newly created annotation(s)
068    */
069   public void okAction() throws GateException {
070     if (annotTypeTextField.getText().equals("")){
071       throw new GateException("An annotation type must be specified !");
072     }// End if
073     // This code must be uncomented if the desired behaviour for
074     // UnrestrictedAnnoatationEditor is not to allow annotation types
075     // which have a schema present in the system.
076 /*
077     CreoleRegister creoleReg = Gate.getCreoleRegister();
078     List currentAnnotationSchemaList =
079                       creoleReg.getLrInstances("gate.creole.AnnotationSchema");
080     Iterator iter = currentAnnotationSchemaList.iterator();
081     while (iter.hasNext()){
082       AnnotationSchema annotSchema = (AnnotationSchema) iter.next();
083       if (annotTypeTextField.getText().equals(annotSchema.getAnnotationName()))
084         throw new GAteException("There is a schema type for this annotation");
085     }// End while
086 */
087     data.setAnnotType(annotTypeTextField.getText());
088     if (currentAnnot == null){
089       currentAnnotSet.addcurrentStartOffset,
090                            currentEndOffset,
091                            this.getAnnotType(),
092                            this.getCurrentAnnotationFeatures());
093     }else{
094       if (currentAnnot.getType().equals(this.getAnnotType())){
095         currentAnnot.setFeatures(this.getCurrentAnnotationFeatures());
096       }else{
097         currentAnnotSet.remove(currentAnnot);
098         currentAnnotSet.addcurrentStartOffset,
099                              currentEndOffset,
100                              this.getAnnotType(),
101                              this.getCurrentAnnotationFeatures());
102       }// End if
103     }// End if
104   }//okAction();
105 
106 
107   public void cancelAction() throws GateException {
108     //no need to do anything, because the editor has not modified anything
109     //on the document or the annotation sets
110     //Had to be added for the tree editor, which does
111     return;
112   }
113 
114   /**
115     * Checks whether this viewer/editor can handle a specific annotation type.
116     @param annotationType represents the annotation type being questioned.If
117     * it is <b>null</b> then the method will return false.
118     @return true if the SchemaAnnotationEditor can handle the annotationType
119     * or false otherwise.
120     */
121   public boolean canDisplayAnnotationType(String annotationType){
122     return true;
123   }// canDisplayAnnotationType();
124 
125 
126   /**
127    * Returns true
128    */
129   public boolean editingFinished() {
130     return true;
131   }
132 
133   /* (non-Javadoc)
134    * @see gate.creole.AnnotationVisualResource#getAnnotationCurrentlyEdited()
135    */
136   public Annotation getAnnotationCurrentlyEdited() {
137     return currentAnnot;
138   }
139 
140   /* (non-Javadoc)
141    * @see gate.creole.AnnotationVisualResource#getAnnotationSetCurrentlyEdited()
142    */
143   public AnnotationSet getAnnotationSetCurrentlyEdited() {
144     return currentAnnotSet;
145   }
146 
147   /* (non-Javadoc)
148    * @see gate.creole.AnnotationVisualResource#isActive()
149    */
150   public boolean isActive() {
151     return isVisible();
152   }
153 
154   /**
155    * Returns <tt>true</tt>.
156    */
157   public boolean supportsCancel() {
158     return true;
159   }  
160   
161   // The Unrestricted Editor functionality
162   // Local data
163   /** The curent annotation set used by the editor*/
164   AnnotationSet currentAnnotSet = null;
165   /** The curent annotation used by the editor*/
166   Annotation currentAnnot = null;
167   /** The start offset of the span covered by the currentAnnot*/
168   Long currentStartOffset = null;
169   /** The end offset of the span covered by the currentAnnot*/
170   Long currentEndOffset = null;
171 
172   // Local data
173   private MyCustomFeatureBearer data = null;
174 
175   // Gui Components
176   JLabel annotTypeLabel = null;
177   JTextField annotTypeTextField = null;
178 
179   JLabel featuresLabel = null;
180   FeaturesEditor  featuresEditor = null;
181 
182   /** Init local data*/
183   protected void initLocalData(){
184     data = new MyCustomFeatureBearer(currentAnnot);
185   }// initLocalData();
186 
187   /** Init GUI components with values taken from local data*/
188   protected void initGuiComponents(){
189     this.setLayout(new BoxLayoutthis, BoxLayout.Y_AXIS));
190     //create the main box
191     Box componentsBox = Box.createVerticalBox();
192 
193     componentsBox.add(Box.createVerticalStrut(10));
194 
195     // Add the Annot Type
196     Box box = Box.createVerticalBox();
197     Box box1 = Box.createHorizontalBox();
198     annotTypeLabel = new JLabel("Annotation type");
199     annotTypeLabel.setToolTipText("The type of the annotation you are" +
200                                                     " creating or editing");
201     annotTypeLabel.setOpaque(true);
202 
203     box1.add(annotTypeLabel);
204     box1.add(Box.createHorizontalGlue());
205     box.add(box1);
206 
207     annotTypeTextField = new JTextField(data.getAnnotType());
208     annotTypeTextField.setColumns(80);
209     annotTypeTextField.setPreferredSize(
210                                   annotTypeTextField.getPreferredSize());
211     annotTypeTextField.setMinimumSize(
212                                   annotTypeTextField.getPreferredSize());
213     annotTypeTextField.setMaximumSize(
214                                   annotTypeTextField.getPreferredSize());
215 
216 
217     box1 = Box.createHorizontalBox();
218     box1.add(annotTypeTextField);
219     box1.add(Box.createHorizontalGlue());
220     box.add(box1);
221     box.add(Box.createVerticalStrut(10));
222 
223     componentsBox.add(box);
224     // add the features editor
225     box = Box.createVerticalBox();
226 
227     featuresLabel = new JLabel("Features");
228     featuresLabel.setToolTipText("The features of the annotation you are" +
229                                                     " creating or editing");
230     featuresLabel.setOpaque(true);
231 
232     box1 = Box.createHorizontalBox();
233     box1.add(featuresLabel);
234     box1.add(Box.createHorizontalGlue());
235     box.add(box1);
236     box.add(Box.createVerticalStrut(5));
237 
238     featuresEditor = new FeaturesEditor();
239     featuresEditor.setFeatureBearer(data);
240 
241     box.add(featuresEditor);
242     box.add(Box.createVerticalStrut(10));
243 
244     componentsBox.add(box);
245     componentsBox.add(Box.createVerticalStrut(10));
246 
247     this.add(componentsBox);
248     this.add(Box.createVerticalStrut(10));
249   }//initGuiComponents()
250 
251   /** Init all the listeners*/
252   protected void initListeners(){
253   }//initListeners()
254 
255   /** Returns annot type edited with this tool*/
256   public String getAnnotType(){ return data.getAnnotType();}
257 
258   /** Returns the features edited with this tool*/
259   protected FeatureMap getCurrentAnnotationFeatures(){ return data.getFeatures();}
260 
261   // INNER CLASS
262   /** This class implements a feature bearer. It is used as internal data.
263     * The FeatureEditor will use an object belonging to this class.
264     */
265   class MyCustomFeatureBearer extends AbstractFeatureBearer
266                                                     implements FeatureBearer{
267 
268     // Members
269     private FeatureMap features = null;
270     private String annotType = null;
271 
272     /** Constructs a custom feature bearer. If annot is null then it creates
273       * empty annotType and fetures.
274       */
275     public MyCustomFeatureBearer(Annotation anAnnot){
276       if (anAnnot != null){
277         features = Factory.newFeatureMap();
278         features.putAll(anAnnot.getFeatures());
279         annotType = new String(anAnnot.getType());
280       }else{
281         features = Factory.newFeatureMap();
282         annotType = new String("");
283       }// End if
284     }//MyCustomFeatureBearer
285 
286     // Mutators and accesors
287     public void setFeatures(FeatureMap aFeatureMap){
288       features = aFeatureMap;
289     }// setFeatures();
290 
291     public FeatureMap getFeatures(){
292       return features;
293     }// getFeatures()
294 
295     public void setAnnotType(String anAnnotType){
296       annotType = anAnnotType;
297     }// setAnnotType();
298 
299     public String getAnnotType(){
300       return annotType;
301     }//getAnnotType()
302   }// End class MyCustomFeatureBearer
303 }// End class UnrestrictedAnnotationEditor