📄 filtertiercommand.java
字号:
/* * File: FilterTierCommand.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.ElanLocale;import mpi.eudico.client.annotator.gui.IndeterminateProgressMonitor;import mpi.eudico.client.annotator.util.AnnotationDataRecord;import mpi.eudico.client.annotator.util.ClientLogger;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.TierImpl;import mpi.eudico.server.corpora.clomimpl.abstr.TranscriptionImpl;import java.awt.Cursor;import java.util.ArrayList;import java.util.Iterator;import java.util.Vector;/** * A Command that filters the contents of the annotations of a source tier to * new annotations on a destination tier. Can also be used to copy a tier. The * destination is a tier of type symbolic association (one-to-one * relationship). * * @author Han Sloetjes */public class FilterTierCommand implements UndoableCommand, ClientLogger { private String commandName; private TranscriptionImpl transcription; private TierImpl sourceTier; private TierImpl destTier; private String[] filters; private boolean preserve; private boolean createEmpty; // store for undo /** backup data of existing annotations on the destination tier */ private ArrayList existAnnotations; /** store the data of the newly created annotations */ private ArrayList newAnnotations; // store for redo /** a list of source annotations that have been changed */ private ArrayList existChangedAnnotations; /** * Creates a new FilterTierCommand instance. * * @param name the name of the command */ public FilterTierCommand(String name) { commandName = name; } /** * Undo the changes made by this command. */ public void undo() { if ((transcription == null) || (sourceTier == null) || (destTier == null)) { return; } int curPropMode = 0; curPropMode = transcription.getTimeChangePropagationMode(); if (curPropMode != Transcription.NORMAL) { transcription.setTimeChangePropagationMode(Transcription.NORMAL); } transcription.setNotifying(false); setWaitCursor(true); // delete created annotations if (newAnnotations.size() > 0) { AnnotationDataRecord destRecord; AbstractAnnotation destAnn; for (int i = 0; i < newAnnotations.size(); i++) { destRecord = (AnnotationDataRecord) newAnnotations.get(i); destAnn = (AbstractAnnotation) destTier.getAnnotationAtTime(destRecord.getBeginTime()); if (destAnn != null) { destTier.removeAnnotation(destAnn); } else { LOG.warning( "Undo filter tier: could not remove annotation: " + destRecord.getValue() + " " + destRecord.getBeginTime() + " - " + destRecord.getEndTime()); } } } // restore annotation values that have been overwritten if (!preserve && (existAnnotations.size() > 0)) { AnnotationDataRecord extRecord; AbstractAnnotation extAnn; for (int i = 0; i < existAnnotations.size(); i++) { extRecord = (AnnotationDataRecord) existAnnotations.get(i); extAnn = (AbstractAnnotation) destTier.getAnnotationAtTime(extRecord.getBeginTime()); if (extAnn != null) { extAnn.setValue(extRecord.getValue()); } else { LOG.warning( "Undo filter tier: could not restore annotation value: " + extRecord.getValue() + " " + extRecord.getBeginTime() + " - " + extRecord.getEndTime()); } } } transcription.setNotifying(true); setWaitCursor(false); // restore the time propagation mode transcription.setTimeChangePropagationMode(curPropMode); } /** * Redo the changes made by this command. */ public void redo() { if ((transcription == null) || (sourceTier == null) || (destTier == null)) { return; } int curPropMode = 0; curPropMode = transcription.getTimeChangePropagationMode(); if (curPropMode != Transcription.NORMAL) { transcription.setTimeChangePropagationMode(Transcription.NORMAL); } transcription.setNotifying(false); setWaitCursor(true); if (newAnnotations.size() > 0) { AnnotationDataRecord destRecord; AbstractAnnotation destAnn; for (int i = 0; i < newAnnotations.size(); i++) { destRecord = (AnnotationDataRecord) newAnnotations.get(i); long time = (long) (destRecord.getBeginTime() + destRecord.getEndTime()) / 2; destAnn = (AbstractAnnotation) destTier.createAnnotation(time, time); if (destAnn != null) { destAnn.setValue(destRecord.getValue()); } else { LOG.warning( "Redo filter tier: could not recreate annotation: " + destRecord.getValue() + " " + destRecord.getBeginTime() + " - " + destRecord.getEndTime()); } } } // redo values changes on existing annotations if (!preserve && (existChangedAnnotations.size() > 0)) { AnnotationDataRecord extRecord; AbstractAnnotation extAnn; for (int i = 0; i < existChangedAnnotations.size(); i++) { extRecord = (AnnotationDataRecord) existChangedAnnotations.get(i); extAnn = (AbstractAnnotation) destTier.getAnnotationAtTime(extRecord.getBeginTime()); if (extAnn != null) { extAnn.setValue(extRecord.getValue()); } else { LOG.warning( "Redo filter tier: could not recreate annotation value: " + extRecord.getValue() + " " + extRecord.getBeginTime() + " - " + extRecord.getEndTime()); } } } transcription.setNotifying(true); setWaitCursor(false); // restore the time propagation mode transcription.setTimeChangePropagationMode(curPropMode); } /** * <b>Note: </b>it is assumed the types and order of the arguments are * correct. * * @param receiver the TranscriptionImpl * @param arguments the arguments: <ul><li>arg[0] = the name of the source * tier (String)<li> <li>arg[1] = the name of the destination tier (String) * </li> <li>arg[2] = the filter strings (String[])</li> <li>arg[3] = * a flag denoting whether or not to preserve existing annotations * on the destination tier (Boolean)</li> <li>arg[4] = a flag denoting * whether or not to create new annotations for empty source annotations * (Boolean)</li></ul> */ public void execute(Object receiver, Object[] arguments) { transcription = (TranscriptionImpl) receiver; String sourceName = (String) arguments[0]; String destName = (String) arguments[1]; filters = (String[]) arguments[2]; //can be null preserve = ((Boolean) arguments[3]).booleanValue(); createEmpty = ((Boolean) arguments[4]).booleanValue(); sourceTier = (TierImpl) transcription.getTierWithId(sourceName); destTier = (TierImpl) transcription.getTierWithId(destName); if ((transcription == null) || (sourceTier == null) ||
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -