Parent: [a7a9ff] (diff)

Download this file

CreateAnnotationView.java    402 lines (366 with data), 16.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* Copyright 2013-2014 TECO - Karlsruhe Institute of Technology.
*
* This file is part of TACET.
*
* TACET is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TACET is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TACET. If not, see <http://www.gnu.org/licenses/>.
*/
package squirrel.view;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import net.miginfocom.swing.MigLayout;
import squirrel.controller.ValidationResult;
import squirrel.model.Annotation;
import squirrel.model.AnnotationFactory;
import squirrel.model.AnnotationModel;
import squirrel.model.ModelFacade;
import squirrel.model.io.DataColumn;
import squirrel.model.io.DataSource;
import squirrel.util.Range;
public class CreateAnnotationView extends JPanel {
/**
*
*/
private JDialog dialog = null;
/**
* Swing components of the ui
*/
private JSpinner jspFrom, jspTo;
private JList<DataColumn> lstAnnotationTracks;
private JSpinner jspNumberValue;
private JComboBox<String> cbbValues;
private JPanel pnlCurrentValueComponent;
private JButton btnAnnotate, btnCancel, btnDelete;
/**
* Data set
*/
private DataSource<? extends DataColumn> dataSource;
private AnnotationFactory annotFactory;
/**
* vector of all possible annotations
*/
private Vector<DataColumn> annotationColumns;
/**
* Range of new annotation
*/
private Range parsedRange;
/**
* the new annotation
*/
private Annotation newAnnotation;
private AnnotationModel annotModel;
private boolean editing = false;
/**
* Property Listener
*/
private PropertyChangeListener propertyListener = new PropertyChangeListener() {
// this is only a dummy implementation
@Override
public void propertyChange(PropertyChangeEvent evt) {}
};
/**
* Constructor of Create Annotation view
* @param model - instance of ModelFacade representing the model of mvc
*/
public CreateAnnotationView(ModelFacade model) {
this(model, null);
}
/**
* Constructor of Create Annotation view
* @param model - instance of ModelFacade representing the model of mvc
* @param annotation - pre selected with shortcuts
*/
public CreateAnnotationView(ModelFacade model, Annotation annotation) {
this.editing = annotation == null ? false : true;
this.setDataSource(model.getCurrentDataSource());
this.annotModel = model.getAnnotationModel();
this.initGUI();
this.setSelectedTrack(0);
this.newAnnotation = annotation;
if (editing) {
setSelectedRange(annotation.getRange());
}
}
/**
* Sets a new data set
* @param dataSource - new data set
*/
public void setDataSource(DataSource<? extends DataColumn> dataSource) {
this.dataSource = dataSource;
this.annotFactory = dataSource.getAnnotationFactory();
this.annotationColumns = new Vector<DataColumn>();
for (DataColumn dc : dataSource.getAnnotationColumns()) {
annotationColumns.add(dc);
}
}
/**
* Selects track no @code track
* @param track - number of annotation track
*/
public void setSelectedTrack(int track) {
this.lstAnnotationTracks.setSelectedIndex(track);
this.updateValueComponents();
}
/**
* Sets the Range for the annotation
* @param range - of annotation
*/
public void setSelectedRange(Range range) {
jspFrom.setValue(range.getStart());
jspTo.setValue(range.getEnd());
}
/**
* Sets the new annotation
* @param annotation - new annotation
*/
public void setAnnotation(Annotation annotation) {
if (editing)
this.newAnnotation = annotation;
}
/**
* Get selected track
* @return index of selected track
*/
public int getSelectedTrack() {
int idx = lstAnnotationTracks.getSelectedIndex();
return idx;
}
/**
* initalise the ui of @code CreateAnnotationView
*/
private void initGUI() {
JLabel lblFrom = new JLabel("From: ");
JLabel lblTo = new JLabel("To: ");
this.jspFrom = new JSpinner();
this.jspTo = new JSpinner();
this.lstAnnotationTracks = new JList<DataColumn>();
this.lstAnnotationTracks.setListData(annotationColumns);
this.lstAnnotationTracks.setCellRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index,
isSelected, cellHasFocus);
label.setText(value instanceof DataColumn ?
((DataColumn) value).getName() : value.toString());
return label;
}
});
this.jspNumberValue = new JSpinner();
this.cbbValues = new JComboBox<String>();
this.pnlCurrentValueComponent = new JPanel(new BorderLayout());
this.btnAnnotate = new JButton("Annotate");
this.btnDelete = new JButton("Delete");
this.btnCancel = new JButton("Cancel");
this.setLayout(new MigLayout("", "", "[nogrid][][][nogrid]"));
this.add(lblFrom);
this.add(jspFrom);
this.add(lblTo);
this.add(jspTo, "wrap, gapbottom unrel");
this.add(new JLabel("Track:"));
this.add(new JLabel("Select a value:"), "wrap");
this.add(new JScrollPane(lstAnnotationTracks), "grow, push, gapright unrel");
this.add(pnlCurrentValueComponent, "aligny top, growx, wrap");
this.add(btnAnnotate, " tag apply");
if (editing)
this.add(btnDelete, "tag apply");
this.add(btnCancel, "tag cancel");
CAActionListener al = new CAActionListener();
this.btnAnnotate.addActionListener(al);
this.btnCancel.addActionListener(al);
this.btnDelete.addActionListener(al);
this.lstAnnotationTracks.addListSelectionListener(new CAListSelectionListener());
if (editing)
this.lstAnnotationTracks.disable();
}
/**
* Depending on {@code type} the correct component is initialized with a new
* model.
* @param type the selected annotation track's type
* @param selectedTrack the selected track's number
*/
private void updateValueComponents() {
DataColumn.Type type = this.dataSource.getDataColumn(
dataSource.annotationIndexToColumn(this.getSelectedTrack())).getType();
if (type.isFloatingpointAnnotation()) {
jspNumberValue.setModel(new SpinnerNumberModel(
annotFactory.getMinValueFloating(this.getSelectedTrack()),
annotFactory.getMinValueFloating(this.getSelectedTrack()),
annotFactory.getMaxValueFloating(this.getSelectedTrack()), 1.0));
this.pnlCurrentValueComponent.removeAll();
this.pnlCurrentValueComponent.add(jspNumberValue, BorderLayout.CENTER);
} else if (type.isIntegerAnnotation()) {
jspNumberValue.setModel(new SpinnerNumberModel(
annotFactory.getMinValueInteger(this.getSelectedTrack()),
annotFactory.getMinValueInteger(this.getSelectedTrack()),
annotFactory.getMaxValueInteger(this.getSelectedTrack()), 1));
this.pnlCurrentValueComponent.removeAll();
this.pnlCurrentValueComponent.add(jspNumberValue, BorderLayout.CENTER);
} else if (type.isLabelAnnotation()) {
cbbValues.setModel(new DefaultComboBoxModel<String>(
annotFactory.getAllowedValues(this.getSelectedTrack())
.toArray(new String[] {})));
this.pnlCurrentValueComponent.removeAll();
this.pnlCurrentValueComponent.add(cbbValues, BorderLayout.CENTER);
} else if (type.isTrainAnnotation()) {
cbbValues.setModel(new DefaultComboBoxModel<String>(new String[] {"training"}));
this.pnlCurrentValueComponent.removeAll();
this.pnlCurrentValueComponent.add(cbbValues, BorderLayout.CENTER);
}
this.validate();
}
// Action Listener
public void setPropertyChangedListener(PropertyChangeListener l) {
this.propertyListener = l;
}
private class CAActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if (src == btnAnnotate && !editing) {
newAnnotation = null;
ValidationResult vr = validateRange(((Number) jspFrom.getValue()).longValue(),
((Number) jspTo.getValue()).longValue());
if (vr.result && annotModel.canInsertAt(parsedRange, getSelectedTrack())) {
DataColumn.Type type = dataSource.getDataColumn(
dataSource.annotationIndexToColumn(getSelectedTrack())).getType();
if (type.isIntegerAnnotation()) {
newAnnotation = annotFactory.createIntegerAnnotation(getSelectedTrack(),
parsedRange, (Integer) jspNumberValue.getValue());
} else if (type.isFloatingpointAnnotation()) {
newAnnotation =
annotFactory.createFloatingpointAnnotation(getSelectedTrack(),
parsedRange, (Double) jspNumberValue.getValue());
} else if (type.isLabelAnnotation()) {
newAnnotation = annotFactory.createLabelAnnotation(getSelectedTrack(),
parsedRange, (String) cbbValues.getSelectedItem());
} else if (type.isTrainAnnotation()) {
newAnnotation = annotFactory.createTrainAnnotation(getSelectedTrack(),
parsedRange);
}
if (newAnnotation != null) {
CreateAnnotationView.this.propertyListener.propertyChange(
new PropertyChangeEvent(CreateAnnotationView.this, "newAnnotation",
newAnnotation, newAnnotation));
CreateAnnotationView.this.dialog.dispose();
}
} else if (!vr.result) {
presentErrorDialog(vr);
} else if (!annotModel.canInsertAt(parsedRange, getSelectedTrack())) {
JOptionPane.showMessageDialog(CreateAnnotationView.this,
"Annotations may not overlap.", "Wrong Input",
JOptionPane.ERROR_MESSAGE);
}
} else if (src == btnAnnotate && editing) {
newAnnotation = null;
ValidationResult vr = validateRange(((Number) jspFrom.getValue()).longValue(),
((Number) jspTo.getValue()).longValue());
if (vr.result) {
DataColumn.Type type = dataSource.getDataColumn(
dataSource.annotationIndexToColumn(getSelectedTrack())).getType();
if (type.isIntegerAnnotation()) {
newAnnotation = annotFactory.createIntegerAnnotation(getSelectedTrack(),
parsedRange, (Integer) jspNumberValue.getValue());
} else if (type.isFloatingpointAnnotation()) {
newAnnotation =
annotFactory.createFloatingpointAnnotation(getSelectedTrack(),
parsedRange, (Double) jspNumberValue.getValue());
} else if (type.isLabelAnnotation()) {
newAnnotation = annotFactory.createLabelAnnotation(getSelectedTrack(),
parsedRange, (String) cbbValues.getSelectedItem());
}
if (newAnnotation != null) {
CreateAnnotationView.this.propertyListener.propertyChange(
new PropertyChangeEvent(CreateAnnotationView.this,
"editAnnotation",
newAnnotation, newAnnotation));
CreateAnnotationView.this.dialog.dispose();
}
} else if (!vr.result) {
presentErrorDialog(vr);
}
} else if (src == btnDelete) {
ValidationResult vr = validateRange(((Number) jspFrom.getValue()).longValue(),
((Number) jspTo.getValue()).longValue());
if (newAnnotation != null && vr.result) {
CreateAnnotationView.this.propertyListener.propertyChange(
new PropertyChangeEvent(CreateAnnotationView.this,
"deleteAnnotation",
parsedRange, parsedRange));
CreateAnnotationView.this.dialog.dispose();
} else if (!vr.result) {
presentErrorDialog(vr);
}
} else if (src == btnCancel) {
if (CreateAnnotationView.this.dialog != null) {
CreateAnnotationView.this.dialog.dispose();
}
}
}
}
private class CAListSelectionListener implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting())
updateValueComponents();
}
}
public void presentAsDialog(Frame owner) {
dialog = new JDialog(owner, "Create an Annotation", false);
dialog.add(this, BorderLayout.CENTER);
dialog.pack();
dialog.setVisible(true);
}
private void presentErrorDialog(ValidationResult vr) {
JOptionPane.showMessageDialog(this, vr.reason, "Wrong Input", JOptionPane.ERROR_MESSAGE);
}
public Annotation getCreatedAnnotation() {
return newAnnotation;
}
/**
* Validates the range of the new annotation
* @param from - start of annotation
* @param to - end of annotation
* @return valid - if Range can be created
*/
public ValidationResult validateRange(Long from, Long to) {
try {
this.parsedRange = new Range(from, to);
} catch (IllegalArgumentException e) {
return ValidationResult.createErrorResult(
String.format("The range from %d to %d is not valid.", from, to));
}
return ValidationResult.createValidResult();
}
}