📄 modifyannotationtimecommand.java
字号:
/* * File: ModifyAnnotationTimeCommand.java * Project: MPI Linguistic Application * Date: 02 May 2007 * * Copyright (C) 2001-2007 Max Planck Institute for Psycholinguistics * * This program 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 2 of the License, or * (at your option) any later version. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package mpi.eudico.client.annotator.commands;import mpi.eudico.client.annotator.util.AnnotationDataRecord;import mpi.eudico.client.annotator.util.AnnotationRecreator;import mpi.eudico.client.annotator.util.TimeShiftRecord;import mpi.eudico.server.corpora.clom.Transcription;import mpi.eudico.server.corpora.clomimpl.abstr.AbstractAnnotation;import mpi.eudico.server.corpora.clomimpl.abstr.AlignableAnnotation;import mpi.eudico.server.corpora.clomimpl.abstr.TierImpl;import mpi.eudico.server.corpora.clomimpl.abstr.TranscriptionImpl;import java.awt.Cursor;import java.util.ArrayList;import java.util.Vector;import javax.swing.tree.DefaultMutableTreeNode;/** * A command for modifying an annotation's begin and/or end time. * * @author Han Sloetjes */public class ModifyAnnotationTimeCommand implements UndoableCommand { private String commandName; private AlignableAnnotation annotation; private DefaultMutableTreeNode rootNode; private TranscriptionImpl transcription; /** * note: this presumes that tier object references stay the same even if * they have been removed from and re-added to the transcription */ private TierImpl tier; private TierImpl rootTier; private int timePropMode; private int leftOffset; private int rightOffset; private long oldBeginTime; private long oldEndTime; private long newBeginTime; private long newEndTime; private ArrayList removedAnnotations; private ArrayList changedAnnotations; /** * Creates a new ModifyAnnotationTimeCommand instance. * * @param name the name of the command */ public ModifyAnnotationTimeCommand(String name) { commandName = name; } /** * <b>Note: </b>it is assumed the types and order of the arguments are * correct. * * @param receiver the Annotation * @param arguments the arguments: <ul><li>arg[0] = the new begin time of * the annotation (Long)</li> <li>arg[1] = the new end time of the * annotation (Long)</li> </ul> */ public void execute(Object receiver, Object[] arguments) { if (receiver instanceof AlignableAnnotation) { annotation = (AlignableAnnotation) receiver; } else { return; } oldBeginTime = annotation.getBeginTimeBoundary(); oldEndTime = annotation.getEndTimeBoundary(); newBeginTime = ((Long) arguments[0]).longValue(); newEndTime = ((Long) arguments[1]).longValue(); leftOffset = (int) (newBeginTime - oldBeginTime); rightOffset = (int) (newEndTime - oldEndTime); // only do something if begin and/or end time has changed if ((oldBeginTime == newBeginTime) && (oldEndTime == newEndTime)) { return; } tier = (TierImpl) annotation.getTier(); if (tier.hasParentTier()) { rootTier = tier.getRootTier(); } transcription = (TranscriptionImpl) tier.getParent(); setWaitCursor(true); changedAnnotations = new ArrayList(); removedAnnotations = new ArrayList(); // store information on all annotations that will be effected timePropMode = transcription.getTimeChangePropagationMode(); switch (timePropMode) { case Transcription.NORMAL: storeNormal(); break; case Transcription.BULLDOZER: storeBulldozer(); break; case Transcription.SHIFT: storeShift(); } // finally make the change annotation.updateTimeInterval(newBeginTime, newEndTime); setWaitCursor(false); } /** * Undo the changes made by this command. */ public void undo() { // only do something if begin and/or end time has changed if ((oldBeginTime == newBeginTime) && (oldEndTime == newEndTime)) { return; } transcription.setNotifying(false); setWaitCursor(true); switch (timePropMode) { case Transcription.NORMAL: restoreNormal(); break; case Transcription.BULLDOZER: restoreBulldozer(); break; case Transcription.SHIFT: restoreShift(); } transcription.setNotifying(true); setWaitCursor(false); } /** * Redo the changes made by this command. */ public void redo() { // only do something if begin and/or end time has changed if ((oldBeginTime == newBeginTime) && (oldEndTime == newEndTime)) { return; } setWaitCursor(true); AlignableAnnotation annotation = (AlignableAnnotation) tier.getAnnotationAtTime(oldBeginTime); if (annotation != null) { annotation.updateTimeInterval(newBeginTime, newEndTime); } setWaitCursor(false); } /** * Stores information of all effected annotations in normal time * propagation mode. Assumption: no annotations on parenttiers will be * effected. */ private void storeNormal() { // in case the annotation is on a time-subdivision tier // store the whole tree with the parent annotation as root if (rootTier != null) { AbstractAnnotation aa = (AbstractAnnotation) rootTier.getAnnotationAtTime(oldBeginTime); if (aa != null) { rootNode = AnnotationRecreator.createTreeForAnnotation(aa); } return; } if ((leftOffset > 0) || (rightOffset < 0)) { // children could have been destroyed rootNode = AnnotationRecreator.createTreeForAnnotation(annotation); } if (leftOffset < 0) { Vector v = tier.getOverlappingAnnotations(newBeginTime, oldBeginTime); if (v.size() > 0) { AbstractAnnotation ann = (AbstractAnnotation) v.get(0); if (ann.getBeginTimeBoundary() < newBeginTime) { // this one will be changed, children might be deleted // so store the whole tree with this annotation as root changedAnnotations.add(AnnotationRecreator.createTreeForAnnotation( ann)); } else { removedAnnotations.add(AnnotationRecreator.createTreeForAnnotation( ann)); } for (int i = 1; i < v.size(); i++) { removedAnnotations.add(AnnotationRecreator.createTreeForAnnotation( (AbstractAnnotation) v.get(i))); } } } if (rightOffset > 0) { Vector v = tier.getOverlappingAnnotations(oldEndTime, newEndTime); if (v.size() > 0) { for (int i = 0; i < v.size(); i++) { if (i != (v.size() - 1)) { removedAnnotations.add(AnnotationRecreator.createTreeForAnnotation( (AbstractAnnotation) v.get(i))); } else { //last ann AbstractAnnotation ann = (AbstractAnnotation) v.get(i); if (ann.getEndTimeBoundary() > newEndTime) { changedAnnotations.add(AnnotationRecreator.createTreeForAnnotation( ann)); } else { removedAnnotations.add(AnnotationRecreator.createTreeForAnnotation( ann)); } } } } } } /** * Restore the situation before the edit action; normal mode. */ private void restoreNormal() { if (tier == null) { return; //warn?? } int curPropMode = 0; curPropMode = transcription.getTimeChangePropagationMode(); if (curPropMode != Transcription.NORMAL) { transcription.setTimeChangePropagationMode(Transcription.NORMAL); } // in case the annotation is on a time-subdivision tier, first remove the root annotation // and then restore it. if (rootTier != null) { AbstractAnnotation aa = (AbstractAnnotation) rootTier.getAnnotationAtTime(newBeginTime); if (aa != null) { rootTier.removeAnnotation(aa); AnnotationRecreator.createAnnotationFromTree(transcription, rootNode); } } else { AlignableAnnotation actAnnotation = (AlignableAnnotation) tier.getAnnotationAtTime(newBeginTime); // first delete changed annotations DefaultMutableTreeNode node; AnnotationDataRecord dataRecord; AbstractAnnotation aa; if (changedAnnotations.size() > 0) { for (int i = 0; i < changedAnnotations.size(); i++) { node = (DefaultMutableTreeNode) changedAnnotations.get(i); dataRecord = (AnnotationDataRecord) node.getUserObject(); if (dataRecord.getBeginTime() < oldBeginTime) { aa = (AbstractAnnotation) tier.getAnnotationAtTime(dataRecord.getBeginTime()); } else { aa = (AbstractAnnotation) tier.getAnnotationAtTime(dataRecord.getEndTime() - 1); } if (aa != null) { tier.removeAnnotation(aa); } } } if ((leftOffset > 0) || (rightOffset < 0)) { // children could have been destroyed if (actAnnotation != null) { tier.removeAnnotation(actAnnotation); } AnnotationRecreator.createAnnotationFromTree(transcription, rootNode); } else { if (actAnnotation != null) { actAnnotation.updateTimeInterval(oldBeginTime, oldEndTime); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -