📄 pasteannotationtreecommand.java
字号:
/* * File: PasteAnnotationTreeCommand.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.AnnotationDataFlavor;import mpi.eudico.client.annotator.util.AnnotationDataRecord;import mpi.eudico.client.annotator.util.AnnotationRecreator;import mpi.eudico.client.annotator.util.AnnotationTreeDataFlavor;import mpi.eudico.client.annotator.util.ClientLogger;import mpi.eudico.server.corpora.clomimpl.abstr.TierImpl;import mpi.eudico.server.corpora.clomimpl.abstr.TranscriptionImpl;import java.awt.Toolkit;import java.awt.datatransfer.Transferable;import java.awt.datatransfer.UnsupportedFlavorException;import java.io.IOException;import java.util.Enumeration;import javax.swing.tree.DefaultMutableTreeNode;/** * A Command to paste an annotation with its depending annotations (i.e. a transferable AnnotationDataRecord or * DefaultMutabletreeNode) from the System's Clipboard. * * Note June 2006 transferal of application/x-java-serialized-object objects between jvm's * doesn't seem to work on Mac OS X. The Transferable sun.awt.datatransfer.ClipboardTransferable@8b4cb8 * doesn't support any flavor with human presentable name "application/x-java-serialized-object" */public class PasteAnnotationTreeCommand extends NewAnnotationCommand implements ClientLogger { private DefaultMutableTreeNode node; private AnnotationDataRecord record; private String destTierName; private Long newBeginTime; /** * Creates a new PasteAnnotationTreeCommand instance * @param name the name of the command */ public PasteAnnotationTreeCommand(String name) { super(name); } /** * @see mpi.eudico.client.annotator.commands.UndoableCommand#undo() */ public void undo() { super.undo(); } /** * @see mpi.eudico.client.annotator.commands.UndoableCommand#redo() */ public void redo() { if (transcription != null) { setWaitCursor(true); newAnnotation(); setWaitCursor(false); } //super.redo(); } /** * <b>Note: </b>it is assumed the types and order of the arguments are * correct. The arguments parameter can be null, of length 1 or of length 2<br> * If the arguments parameter is null the following order is used to decide on which tier the * annotation has te be pasted:<br> * - the tier of the same name as of the tier of the top level annotation in the tree, * if it is present in the destination transcription<br> * - the current active tier * * If there isn't an annotation tree (in the form of a DefualtMutableTreeNode) on the clipboard but * an AnnotationDataRecord instead this single toplevel annoation is pasted. * * @param receiver a transcription * @param arguments the arguments: <ul><li>null</li></ul> * or * <ul><li>arg[0] = destination tier name if the annotation should be pasted to a tier with another * name (String)</li> <li>arg[1] = destination begin time; if the annotation should be pasted at * another location (Long)</li> * </ul> */ public void execute(Object receiver, Object[] arguments) { transcription = (TranscriptionImpl) receiver; if (canAccessSystemClipboard()) { Object contents = null; Transferable ta; // get Clipboard contents. ta = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); try { contents = ta.getTransferData(AnnotationTreeDataFlavor.getInstance()); } catch (UnsupportedFlavorException nfe) { //LOG.warning("Unsupported flavor on the clipboord: " + nfe.getMessage()); // check if there is a single annotation on the clipboard try { contents = ta.getTransferData(AnnotationDataFlavor.getInstance()); } catch (Exception e) { LOG.warning("Unsupported flavor on the clipboord: " + nfe.getMessage()); return; } //return; } catch (IOException ioe) { LOG.warning("I/O error: " + ioe.getMessage()); return; } if (contents instanceof DefaultMutableTreeNode) { node = (DefaultMutableTreeNode) contents; record = (AnnotationDataRecord) node.getUserObject(); } else if (contents instanceof AnnotationDataRecord) { record = (AnnotationDataRecord) contents; } else { return; } destTierName = record.getTierName(); //LOG.info("Record: " + record.toString() + " " + record.getBeginTime() + // " " + record.getEndTime()); if ((arguments != null) && (arguments.length > 0)) { if (arguments[0] != null) { destTierName = (String) arguments[0]; } if (arguments.length > 1) { newBeginTime = (Long) arguments[1]; if ((node != null) && (record != null)) { if (newBeginTime.longValue() != record.getBeginTime()) { // adjust times if necessary adjustTimes(node, newBeginTime.longValue() - record.getBeginTime()); } } } } TierImpl tier = null; if (destTierName != null) { tier = (TierImpl) transcription.getTierWithId(destTierName); } // if the tier is not found try the active tier ?? if (tier == null) { tier = (TierImpl) ELANCommandFactory.getViewerManager(transcription) .getMultiTierControlPanel() .getActiveTier(); if (tier != null) { destTierName = tier.getName(); } } if (tier == null) { LOG.warning("Cannot paste annotation: tier not found: " + destTierName); return; } Long[] args = new Long[2]; if (newBeginTime != null) { args[0] = newBeginTime; args[1] = new Long(newBeginTime.longValue() + (record.getEndTime() - record.getBeginTime())); } else { args[0] = new Long(record.getBeginTime()); args[1] = new Long(record.getEndTime()); } super.execute(tier, args); } } /** * Create the new annotation tree. */ void newAnnotation() { if (node != null) { // annotation tree if (record != null) { // record is the root node object if (!record.getTierName().equals(destTierName)) { super.newAnnotation(); if (newAnnotation != null) { newAnnotation.setValue(record.getValue()); } } else { newAnnotation = AnnotationRecreator.createAnnotationFromTree(transcription, node); } } } else if (record != null) { // single annotation super.newAnnotation(); if (newAnnotation != null) { newAnnotation.setValue(record.getValue()); } } } /** * Adjusts all aligned begin and end times of the annotations in the tree. * * @param node the root of the tree * @param shift the amount of milliseconds to shift the annotations with */ private void adjustTimes(DefaultMutableTreeNode root, long shift) { if (root == null) { return; } DefaultMutableTreeNode n; AnnotationDataRecord adr; Enumeration nodeIt = root.breadthFirstEnumeration(); while (nodeIt.hasMoreElements()) { n = (DefaultMutableTreeNode) nodeIt.nextElement(); adr = (AnnotationDataRecord) n.getUserObject(); if (adr.getBeginTime() != -1) { adr.setBeginTime(adr.getBeginTime() + shift); } if (adr.getEndTime() != -1) { adr.setEndTime(adr.getEndTime() + shift); } } } /** * Performs a check on the accessibility of the system clipboard. * * @return true if the system clipboard is accessible, false otherwise */ protected boolean canAccessSystemClipboard() { if (System.getSecurityManager() != null) { try { System.getSecurityManager().checkSystemClipboardAccess(); return true; } catch (SecurityException se) { LOG.warning("Cannot access the clipboard"); return false; } } return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -