1
15
16 package gate.gui;
17
18 import javax.swing.*;
19
20 import gate.*;
21 import gate.creole.AbstractVisualResource;
22 import gate.creole.AnnotationVisualResource;
23 import gate.util.*;
24
25
30 public class UnrestrictedAnnotationEditor extends AbstractVisualResource
31 implements AnnotationVisualResource,
32 ResizableVisualResource{
33
34
35 public UnrestrictedAnnotationEditor() {}
36
37
39
44 public void setTarget(Object target){
45 currentAnnotSet = (AnnotationSet) target;
46 }
48
53 public void setAnnotation(Annotation ann){
54 if (ann == null) return;
56 currentAnnot = ann;
57 currentStartOffset = currentAnnot.getStartNode().getOffset();
58 currentEndOffset = currentAnnot.getEndNode().getOffset();
59
60 initLocalData();
61 initGuiComponents();
62
63 }
65
72 public void setSpan(Long startOffset, Long endOffset, String annotationType){
73 if (startOffset == null || endOffset == null) return;
75
76 currentStartOffset = startOffset;
77 currentEndOffset = endOffset;
78 currentAnnot = null;
79
80 initLocalData();
81 initGuiComponents();
82 }
84
88 public void okAction() throws GateException {
89 if (annotTypeTextField.getText().equals("")){
90 throw new GateException("An annotation type must be specified !");
91 }
106 data.setAnnotType(annotTypeTextField.getText());
107 if (currentAnnot == null){
108 currentAnnotSet.add( currentStartOffset,
109 currentEndOffset,
110 this.getAnnotType(),
111 this.getCurrentAnnotationFeatures());
112 }else{
113 if (currentAnnot.getType().equals(this.getAnnotType())){
114 currentAnnot.setFeatures(this.getCurrentAnnotationFeatures());
115 }else{
116 currentAnnotSet.remove(currentAnnot);
117 currentAnnotSet.add( currentStartOffset,
118 currentEndOffset,
119 this.getAnnotType(),
120 this.getCurrentAnnotationFeatures());
121 } } }
125
126 public void cancelAction() throws GateException {
127 return;
131 }
132
133
140 public boolean canDisplayAnnotationType(String annotationType){
141 return true;
142 }
144
147
148 AnnotationSet currentAnnotSet = null;
149
150 Annotation currentAnnot = null;
151
152 Long currentStartOffset = null;
153
154 Long currentEndOffset = null;
155
156 private MyCustomFeatureBearer data = null;
158
159 JLabel annotTypeLabel = null;
161 JTextField annotTypeTextField = null;
162
163 JLabel featuresLabel = null;
164 FeaturesEditor featuresEditor = null;
165
166
167 protected void initLocalData(){
168 data = new MyCustomFeatureBearer(currentAnnot);
169 }
171
172 protected void initGuiComponents(){
173 this.setLayout(new BoxLayout( this, BoxLayout.Y_AXIS));
174 Box componentsBox = Box.createVerticalBox();
176
177 componentsBox.add(Box.createVerticalStrut(10));
178
179 Box box = Box.createVerticalBox();
181 Box box1 = Box.createHorizontalBox();
182 annotTypeLabel = new JLabel("Annotation type");
183 annotTypeLabel.setToolTipText("The type of the annotation you are" +
184 " creating or editing");
185 annotTypeLabel.setOpaque(true);
186
187 box1.add(annotTypeLabel);
188 box1.add(Box.createHorizontalGlue());
189 box.add(box1);
190
191 annotTypeTextField = new JTextField(data.getAnnotType());
192 annotTypeTextField.setColumns(80);
193 annotTypeTextField.setPreferredSize(
194 annotTypeTextField.getPreferredSize());
195 annotTypeTextField.setMinimumSize(
196 annotTypeTextField.getPreferredSize());
197 annotTypeTextField.setMaximumSize(
198 annotTypeTextField.getPreferredSize());
199
200
201 box1 = Box.createHorizontalBox();
202 box1.add(annotTypeTextField);
203 box1.add(Box.createHorizontalGlue());
204 box.add(box1);
205 box.add(Box.createVerticalStrut(10));
206
207 componentsBox.add(box);
208 box = Box.createVerticalBox();
210
211 featuresLabel = new JLabel("Features");
212 featuresLabel.setToolTipText("The features of the annotation you are" +
213 " creating or editing");
214 featuresLabel.setOpaque(true);
215
216 box1 = Box.createHorizontalBox();
217 box1.add(featuresLabel);
218 box1.add(Box.createHorizontalGlue());
219 box.add(box1);
220 box.add(Box.createVerticalStrut(5));
221
222 featuresEditor = new FeaturesEditor();
223 featuresEditor.setFeatureBearer(data);
224
225 box.add(featuresEditor);
226 box.add(Box.createVerticalStrut(10));
227
228 componentsBox.add(box);
229 componentsBox.add(Box.createVerticalStrut(10));
230
231 this.add(componentsBox);
232 this.add(Box.createVerticalStrut(10));
233 }
235
236 protected void initListeners(){
237 }
239
240 public String getAnnotType(){ return data.getAnnotType();}
241
242
243 protected FeatureMap getCurrentAnnotationFeatures(){ return data.getFeatures();}
244
245
249 class MyCustomFeatureBearer extends AbstractFeatureBearer
250 implements FeatureBearer{
251
252 private FeatureMap features = null;
254 private String annotType = null;
255
256
259 public MyCustomFeatureBearer(Annotation anAnnot){
260 if (anAnnot != null){
261 features = Factory.newFeatureMap();
262 features.putAll(anAnnot.getFeatures());
263 annotType = new String(anAnnot.getType());
264 }else{
265 features = Factory.newFeatureMap();
266 annotType = new String("");
267 } }
270 public void setFeatures(FeatureMap aFeatureMap){
272 features = aFeatureMap;
273 }
275 public FeatureMap getFeatures(){
276 return features;
277 }
279 public void setAnnotType(String anAnnotType){
280 annotType = anAnnotType;
281 }
283 public String getAnnotType(){
284 return annotType;
285 } }}