Search in sources :

Example 1 with AnnotationDataImpl

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);
}
Also used : AnnotationDataImpl(gate.gui.annedit.AnnotationDataImpl) Annotation(gate.Annotation)

Example 2 with AnnotationDataImpl

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" + "&nbsp;&nbsp;<font color=#667799><small>Ctrl-P" + "&nbsp;&nbsp;</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);
}
Also used : ActionEvent(java.awt.event.ActionEvent) KeyAdapter(java.awt.event.KeyAdapter) MouseMotionListener(java.awt.event.MouseMotionListener) AncestorEvent(javax.swing.event.AncestorEvent) KeyEvent(java.awt.event.KeyEvent) MouseListener(java.awt.event.MouseListener) GateRuntimeException(gate.util.GateRuntimeException) AnnotationDataImpl(gate.gui.annedit.AnnotationDataImpl) AncestorListener(javax.swing.event.AncestorListener) AbstractAction(javax.swing.AbstractAction) ComponentAdapter(java.awt.event.ComponentAdapter) MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) JComponent(javax.swing.JComponent) InvalidOffsetException(gate.util.InvalidOffsetException) Point(java.awt.Point) Point(java.awt.Point) Annotation(gate.Annotation) MouseMotionAdapter(java.awt.event.MouseMotionAdapter) ActionListener(java.awt.event.ActionListener) Timer(javax.swing.Timer) InputMap(javax.swing.InputMap) ComponentEvent(java.awt.event.ComponentEvent)

Aggregations

Annotation (gate.Annotation)2 AnnotationDataImpl (gate.gui.annedit.AnnotationDataImpl)2 GateRuntimeException (gate.util.GateRuntimeException)1 InvalidOffsetException (gate.util.InvalidOffsetException)1 Point (java.awt.Point)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 ComponentAdapter (java.awt.event.ComponentAdapter)1 ComponentEvent (java.awt.event.ComponentEvent)1 KeyAdapter (java.awt.event.KeyAdapter)1 KeyEvent (java.awt.event.KeyEvent)1 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 MouseListener (java.awt.event.MouseListener)1 MouseMotionAdapter (java.awt.event.MouseMotionAdapter)1 MouseMotionListener (java.awt.event.MouseMotionListener)1 AbstractAction (javax.swing.AbstractAction)1 InputMap (javax.swing.InputMap)1 JComponent (javax.swing.JComponent)1 Timer (javax.swing.Timer)1