📄 newannotationcommand.java
字号:
/* * File: NewAnnotationCommand.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.ViewerManager2;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.Annotation;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 the creation of new annotations on a tier.<br> * Should become undoable eventually. Because existing annotations can be * destroyed when creating a new annotation undo /redo is not yet * implemented. * * @author Han Sloetjes */public class NewAnnotationCommand implements UndoableCommand { private String commandName; private TierImpl tier; private TierImpl rootTier; private int timePropMode; /** Holds value of property DOCUMENT ME! */ TranscriptionImpl transcription; /** Holds value of property DOCUMENT ME! */ Annotation newAnnotation; private long begin; private long end; private long newAnnBegin; private long newAnnEnd; private ArrayList removedAnnotations; private ArrayList changedAnnotations; private int leftOffset; private int rightOffset; /** * Creates a new NewAnnotationCommand instance * * @param name the name of the command */ public NewAnnotationCommand(String name) { commandName = name; } /** * The undo action. */ public void undo() { if ((tier != null) && (newAnnotation != null)) { setWaitCursor(true); Annotation aa = tier.getAnnotationAtTime((newAnnBegin + newAnnEnd) / 2); if (aa != null) { tier.removeAnnotation(aa); } if (tier.isTimeAlignable()) { transcription.setNotifying(false); switch (timePropMode) { case Transcription.NORMAL: restoreNormal(); break; case Transcription.BULLDOZER: restoreBulldozer(); break; case Transcription.SHIFT: restoreShift(); } transcription.setNotifying(true); } setWaitCursor(false); } } /** * The redo action. */ public void redo() { if (tier != null) { setWaitCursor(true); newAnnotation = tier.createAnnotation(begin, end); setWaitCursor(false); } } /** * <b>Note: </b>it is assumed the types and order of the arguments are * correct.<br> * July 2006: removed the ViewerManager as one of the objects in the arguments array * * @param receiver the TierImpl * @param arguments the arguments: <ul> <li>arg[0] = the begin time of the * annotation (Long)</li> <li>arg[1] = the end time of the * annotation (Long)</li> </ul> */ public void execute(Object receiver, Object[] arguments) { tier = (TierImpl) receiver; begin = ((Long) arguments[0]).longValue(); end = ((Long) arguments[1]).longValue(); transcription = (TranscriptionImpl) tier.getParent(); ViewerManager2 vm = ELANCommandFactory.getViewerManager(transcription); Command c = ELANCommandFactory.createCommand(transcription, ELANCommandFactory.ACTIVE_ANNOTATION); c.execute(vm, new Object[] { null }); setWaitCursor(true); if (!tier.isTimeAlignable()) { // symbolic subdivision or symbolic association // nothing gets lost //newAnnotation = tier.createAnnotation(begin, end); newAnnotation(); } else { changedAnnotations = new ArrayList(); removedAnnotations = new ArrayList(); if (tier.hasParentTier()) { rootTier = tier.getRootTier(); } timePropMode = transcription.getTimeChangePropagationMode(); switch (timePropMode) { case Transcription.NORMAL: storeNormal(); break; case Transcription.BULLDOZER: storeBulldozer(); break; case Transcription.SHIFT: storeShift(); } // finally create the annotation //newAnnotation = tier.createAnnotation(begin, end); newAnnotation(); } /* if (newAnnotation != null) { newAnnBegin = newAnnotation.getBeginTimeBoundary(); newAnnEnd = newAnnotation.getEndTimeBoundary(); } */ setWaitCursor(false); } /** * The creation of the new annotation in a separate method to allow overriding. */ void newAnnotation() { newAnnotation = tier.createAnnotation(begin, end); if (newAnnotation != null) { newAnnBegin = newAnnotation.getBeginTimeBoundary(); newAnnEnd = newAnnotation.getEndTimeBoundary(); } } /** * Stores information of all effected annotations in normal time * propagation mode. Assumption: no annotations on parenttiers will be * effected. */ private void storeNormal() { if (rootTier != null) { Vector possiblyEffectedAnn = rootTier.getOverlappingAnnotations(begin, end); AbstractAnnotation aa; // use the changedAnnotations arraylist for (int i = 0; i < possiblyEffectedAnn.size(); i++) { aa = (AbstractAnnotation) possiblyEffectedAnn.get(i); changedAnnotations.add(AnnotationRecreator.createTreeForAnnotation( aa)); } } else { Vector effectedAnn = tier.getOverlappingAnnotations(begin, end); AbstractAnnotation aa; for (int i = 0; i < effectedAnn.size(); i++) { aa = (AbstractAnnotation) effectedAnn.get(i); if (aa.getBeginTimeBoundary() < begin) { changedAnnotations.add(AnnotationRecreator.createTreeForAnnotation( aa)); } else if (aa.getEndTimeBoundary() > end) { changedAnnotations.add(AnnotationRecreator.createTreeForAnnotation( aa)); } else { removedAnnotations.add(AnnotationRecreator.createTreeForAnnotation( aa)); } } } } /** * Restore the situation before the edit action; normal mode. */ private void restoreNormal() { int curPropMode = 0; curPropMode = transcription.getTimeChangePropagationMode(); if (curPropMode != Transcription.NORMAL) { transcription.setTimeChangePropagationMode(Transcription.NORMAL); } if (rootTier != null) { long mid = (newAnnBegin + newAnnEnd) / 2; DefaultMutableTreeNode node = null; AnnotationDataRecord annRecord = null; for (int i = 0; i < changedAnnotations.size(); i++) { node = (DefaultMutableTreeNode) changedAnnotations.get(i); annRecord = (AnnotationDataRecord) node.getUserObject(); if ((annRecord.getBeginTime() <= mid) && (annRecord.getEndTime() >= mid)) { break; } } if (node == null) { return; } Annotation rootAnn = rootTier.getAnnotationAtTime(mid); if (rootAnn != null) { rootTier.removeAnnotation(rootAnn); AnnotationRecreator.createAnnotationFromTree(transcription, node); } } else { // 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() < begin) { aa = (AbstractAnnotation) tier.getAnnotationAtTime(dataRecord.getBeginTime()); } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -