use of gate.gui.annedit.AnnotationDataImpl in project gate-core by GateNLP.
the class AnnotationEditor method moveAnnotation.
/**
* Changes the span of an existing annotation by creating a new annotation
* with the same ID, type and features but with the new start and end offsets.
*
* @param set
* the annotation set
* @param oldAnnotation
* the annotation to be moved
* @param newStartOffset
* the new start offset
* @param newEndOffset
* the new end offset
*/
protected void moveAnnotation(AnnotationSet set, Annotation oldAnnotation, Long newStartOffset, Long newEndOffset) throws InvalidOffsetException {
// Moving is done by deleting the old annotation and creating a new one.
// If this was the last one of one type it would mess up the gui which
// "forgets" about this type and then it recreates it (with a different
// colour and not visible.
// In order to avoid this problem, we'll create a new temporary annotation.
Annotation tempAnn = null;
if (set.get(oldAnnotation.getType()).size() == 1) {
// create a clone of the annotation that will be deleted, to act as a
// placeholder
Integer tempAnnId = set.add(oldAnnotation.getStartNode(), oldAnnotation.getStartNode(), oldAnnotation.getType(), oldAnnotation.getFeatures());
tempAnn = set.get(tempAnnId);
}
Integer oldID = oldAnnotation.getId();
set.remove(oldAnnotation);
set.add(oldID, newStartOffset, newEndOffset, oldAnnotation.getType(), oldAnnotation.getFeatures());
Annotation newAnn = set.get(oldID);
// update the selection to the new annotation
getOwner().selectAnnotation(new AnnotationDataImpl(set, newAnn));
editAnnotation(newAnn, set);
// remove the temporary annotation
if (tempAnn != null)
set.remove(tempAnn);
owner.annotationChanged(newAnn, set, null);
}
use of gate.gui.annedit.AnnotationDataImpl in project gate-core by GateNLP.
the class AnnotationEditor method initListeners.
protected void initListeners() {
// resize the window when the table changes.
featuresEditor.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
// the table has changed size -> resize the window too!
popupWindow.pack();
}
});
KeyAdapter keyAdapter = new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
hideTimer.stop();
}
};
typeCombo.getEditor().getEditorComponent().addKeyListener(keyAdapter);
MouseListener windowMouseListener = new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent evt) {
hideTimer.stop();
}
// allow a JWindow to be dragged with a mouse
@Override
public void mousePressed(MouseEvent me) {
pressed = me;
}
};
MouseMotionListener windowMouseMotionListener = new MouseMotionAdapter() {
Point location;
// allow a JWindow to be dragged with a mouse
@Override
public void mouseDragged(MouseEvent me) {
location = popupWindow.getLocation(location);
int x = location.x - pressed.getX() + me.getX();
int y = location.y - pressed.getY() + me.getY();
popupWindow.setLocation(x, y);
pinnedButton.setSelected(true);
}
};
popupWindow.getRootPane().addMouseListener(windowMouseListener);
popupWindow.getRootPane().addMouseMotionListener(windowMouseMotionListener);
InputMap inputMap = ((JComponent) popupWindow.getContentPane()).getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
actionMap = ((JComponent) popupWindow.getContentPane()).getActionMap();
// add the key-action bindings of this Component to the parent window
solAction = new StartOffsetLeftAction("", MainFrame.getIcon("extend-left"), SOL_DESC, KeyEvent.VK_LEFT);
solButton.setAction(solAction);
setShortCuts(inputMap, SOL_KEY_STROKES, "solAction");
actionMap.put("solAction", solAction);
sorAction = new StartOffsetRightAction("", MainFrame.getIcon("extend-right"), SOR_DESC, KeyEvent.VK_RIGHT);
sorButton.setAction(sorAction);
setShortCuts(inputMap, SOR_KEY_STROKES, "sorAction");
actionMap.put("sorAction", sorAction);
delAction = new DeleteAnnotationAction("", MainFrame.getIcon("remove-annotation"), "Delete the annotation", KeyEvent.VK_DELETE);
delButton.setAction(delAction);
inputMap.put(KeyStroke.getKeyStroke("alt DELETE"), "delAction");
actionMap.put("delAction", delAction);
eolAction = new EndOffsetLeftAction("", MainFrame.getIcon("extend-left"), EOL_DESC, KeyEvent.VK_LEFT);
eolButton.setAction(eolAction);
setShortCuts(inputMap, EOL_KEY_STROKES, "eolAction");
actionMap.put("eolAction", eolAction);
eorAction = new EndOffsetRightAction("", MainFrame.getIcon("extend-right"), EOR_DESC, KeyEvent.VK_RIGHT);
eorButton.setAction(eorAction);
setShortCuts(inputMap, EOR_KEY_STROKES, "eorAction");
actionMap.put("eorAction", eorAction);
pinnedButton.setToolTipText("<html>Press to pin window in place" + " <font color=#667799><small>Ctrl-P" + " </small></font></html>");
inputMap.put(KeyStroke.getKeyStroke("control P"), "toggle pin");
actionMap.put("toggle pin", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
pinnedButton.doClick();
}
});
DismissAction dismissAction = new DismissAction("", null, "Close the window", KeyEvent.VK_ESCAPE);
dismissButton.setAction(dismissAction);
inputMap.put(KeyStroke.getKeyStroke("ESCAPE"), "dismissAction");
inputMap.put(KeyStroke.getKeyStroke("alt ESCAPE"), "dismissAction");
actionMap.put("dismissAction", dismissAction);
ApplyAction applyAction = new ApplyAction("Apply", null, "", KeyEvent.VK_ENTER);
inputMap.put(KeyStroke.getKeyStroke("alt ENTER"), "applyAction");
actionMap.put("applyAction", applyAction);
typeCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
String newType = typeCombo.getSelectedItem().toString();
if (ann == null || ann.getType().equals(newType))
return;
// annotation editing
Integer oldId = ann.getId();
Annotation oldAnn = ann;
set.remove(ann);
try {
set.add(oldId, oldAnn.getStartNode().getOffset(), oldAnn.getEndNode().getOffset(), newType, oldAnn.getFeatures());
Annotation newAnn = set.get(oldId);
// update the selection to the new annotation
getOwner().selectAnnotation(new AnnotationDataImpl(set, newAnn));
editAnnotation(newAnn, set);
owner.annotationChanged(newAnn, set, oldAnn.getType());
} catch (InvalidOffsetException ioe) {
throw new GateRuntimeException(ioe);
}
}
});
hideTimer = new Timer(HIDE_DELAY, new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
annotationEditorInstance.setVisible(false);
}
});
hideTimer.setRepeats(false);
AncestorListener textAncestorListener = new AncestorListener() {
@Override
public void ancestorAdded(AncestorEvent event) {
if (wasShowing) {
annotationEditorInstance.setVisible(true);
}
wasShowing = false;
}
@Override
public void ancestorRemoved(AncestorEvent event) {
if (isShowing()) {
wasShowing = true;
popupWindow.dispose();
}
}
@Override
public void ancestorMoved(AncestorEvent event) {
}
private boolean wasShowing = false;
};
owner.getTextComponent().addAncestorListener(textAncestorListener);
}
Aggregations