📄 textviewer.java
字号:
/* * File: TextViewer.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.viewer;import mpi.eudico.client.annotator.Constants;import mpi.eudico.client.annotator.ElanLocale;import mpi.eudico.client.annotator.gui.InlineEditBox;import mpi.eudico.client.mediacontrol.ControllerEvent;import mpi.eudico.server.corpora.clom.Annotation;import mpi.eudico.server.corpora.clom.Tier;import mpi.eudico.server.corpora.clomimpl.abstr.TierImpl;import mpi.eudico.server.corpora.util.ACMEditEvent;import mpi.eudico.server.corpora.util.ACMEditListener;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Point;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ComponentAdapter;import java.awt.event.ComponentEvent;import java.awt.event.KeyEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionAdapter;import java.util.Arrays;import java.util.Enumeration;import java.util.Vector;import javax.swing.ButtonGroup;import javax.swing.JCheckBoxMenuItem;import javax.swing.JComponent;import javax.swing.JMenu;import javax.swing.JMenuItem;import javax.swing.JPopupMenu;import javax.swing.JRadioButtonMenuItem;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.KeyStroke;import javax.swing.SwingUtilities;//import javax.swing.text.DefaultHighlighter;import javax.swing.text.Highlighter;/** * Viewer for text of a selected tier with highligting where the cursor is. * @version Aug 2005 Identity removed */public class TextViewer extends AbstractViewer implements SingleTierViewer, ACMEditListener, ActionListener { private JMenu fontMenu; private ButtonGroup fontSizeBG; private int fontSize; private JPopupMenu popup; private JMenuItem centerMI; private JTextArea taText; private JScrollPane jspText; private TierImpl tier; private Vector annotations = new Vector(); //private int index = 0; private long begintime = 0; private long endtime = 0; private long[] arrTagTimes; private int[] arrTagPositions; private String tierText = ""; private Highlighter highlighter; private StyledHighlightPainter selectionPainter; private StyledHighlightPainter currentPainter; private StyledHighlightPainter activeAnnotationPainter; private Object selectionHighLightInfo; private Object currentHighLightInfo; private Object activeHighLightInfo; private int indexActiveAnnotationBegin = 0; private int indexActiveAnnotationEnd = 0; private int indexSelectionBegin = 0; private int indexSelectionEnd = 0; private int indexMediaTime = 0; private boolean bVisDotted; //used for visualization with dots private int extraLength; //used for visualization with dots private boolean centerVertically = true; /** Holds value of property DOCUMENT ME! */ private final Color transparent; /** * Constructor */ public TextViewer() { bVisDotted = true; extraLength = 4; transparent = new Color(Constants.SELECTIONCOLOR.getRed(), Constants.SELECTIONCOLOR.getGreen(), Constants.SELECTIONCOLOR.getBlue(), 0); try { setLayout(new BorderLayout()); taText = new JTextArea(4, 10) { //don't eat up any key events protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) { return false; } }; taText.setFont(Constants.DEFAULTFONT); fontSize = 12; taText.setLineWrap(true); taText.setWrapStyleWord(true); taText.setForeground(Constants.DEFAULTFOREGROUNDCOLOR); taText.setEditable(false); taText.addMouseListener(new TextViewerMouseListener(taText)); taText.addMouseMotionListener(new TextViewerMouseMotionListener()); taText.getCaret().setSelectionVisible(false); taText.setBackground(Constants.DEFAULTBACKGROUNDCOLOR); taText.setSelectionColor(taText.getBackground()); highlighter = taText.getHighlighter(); selectionPainter = new StyledHighlightPainter(Constants.SELECTIONCOLOR, 1, StyledHighlightPainter.FILLED); selectionPainter.setVisible(false); currentPainter = new StyledHighlightPainter(Constants.CROSSHAIRCOLOR, 0); currentPainter.setVisible(false); activeAnnotationPainter = new StyledHighlightPainter(Constants.ACTIVEANNOTATIONCOLOR, 1); activeAnnotationPainter.setVisible(false); currentHighLightInfo = highlighter.addHighlight(0, 0, currentPainter); activeHighLightInfo = highlighter.addHighlight(0, 0, activeAnnotationPainter); selectionHighLightInfo = highlighter.addHighlight(0, 0, selectionPainter); jspText = new JScrollPane(taText); jspText.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); jspText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); // jspText.setViewportView(taText); add(jspText, BorderLayout.CENTER); /////////////////////////////////////////////////////////////////////// // // Temporary code // highlighter.addHighlight in doUpdate gives an error when tabpane is // not visible. When not visible (in sync mode) the size of tabpane is // set to 0. So check size in doUpdate and if 0 return. // /////////////////////////////////////////////////////////////////////// addComponentListener(new TextViewerComponentListener()); setVisible(true); } catch (Exception ex) { ex.printStackTrace(); } } /** * AR notification that the selection has changed method from SelectionUser * not implemented in AbstractViewer */ public void updateSelection() { doUpdate(); } /** * AR heeft dit hier neergezet, zie abstract viewer voor get en set * methodes van ActiveAnnotation. Update method from ActiveAnnotationUser */ public void updateActiveAnnotation() { doUpdate(); } /** * AR heeft dit hier neergezet Er moet nog gepraat worden over wat hier te * doen valt * * @param e DOCUMENT ME! */ public void ACMEdited(ACMEditEvent e) { switch (e.getOperation()) { case ACMEditEvent.ADD_ANNOTATION_HERE: case ACMEditEvent.ADD_ANNOTATION_BEFORE: case ACMEditEvent.ADD_ANNOTATION_AFTER: case ACMEditEvent.CHANGE_ANNOTATIONS: case ACMEditEvent.REMOVE_ANNOTATION: case ACMEditEvent.CHANGE_ANNOTATION_TIME: case ACMEditEvent.CHANGE_ANNOTATION_VALUE: { setTier(getTier()); doUpdate(); } } } /** * method from ElanLocaleListener not implemented in AbstractViewer */ public void updateLocale() { createPopup(); } private void createPopup() { popup = new JPopupMenu(""); fontSizeBG = new ButtonGroup(); fontMenu = new JMenu(ElanLocale.getString("Menu.View.FontSize")); JRadioButtonMenuItem fontRB; for (int i = 0; i < Constants.FONT_SIZES.length; i++) { fontRB = new JRadioButtonMenuItem(String.valueOf( Constants.FONT_SIZES[i])); fontRB.setActionCommand("font" + Constants.FONT_SIZES[i]); if (fontSize == Constants.FONT_SIZES[i]) { fontRB.setSelected(true); } fontRB.addActionListener(this); fontSizeBG.add(fontRB); fontMenu.add(fontRB); } popup.add(fontMenu); popup.addSeparator(); //add visualization toggle JMenuItem menuItem = new JMenuItem(ElanLocale.getString( "TextViewer.ToggleVisualization")); menuItem.setActionCommand("TOGGLEVISUALIZATION"); menuItem.addActionListener(this); popup.add(menuItem); centerMI = new JCheckBoxMenuItem(ElanLocale.getString( "TextViewer.CenterVertical")); centerMI.setSelected(centerVertically); centerMI.setActionCommand("centerVert"); centerMI.addActionListener(this); popup.add(centerMI); } /** * DOCUMENT ME! * * @param e DOCUMENT ME! */ public void actionPerformed(ActionEvent e) { String strAction = e.getActionCommand(); if (strAction.indexOf("font") != -1) { int index = strAction.indexOf("font") + 4; try { fontSize = Integer.parseInt(strAction.substring(index)); //repaint(); } catch (Exception ex) { ex.printStackTrace(); } //taText.setFont(getFont().deriveFont((float) fontSize)); taText.setFont(Constants.DEFAULTFONT.deriveFont((float) fontSize)); } else if (strAction.equals("TOGGLEVISUALIZATION")) { bVisDotted = !bVisDotted; setTier(getTier()); } else if (strAction == "centerVert") { setCenteredVertically(centerMI.isSelected()); } } /** * AR notification that some media related event happened method from * ControllerListener not implemented in AbstractViewer * * @param event DOCUMENT ME! */ public void controllerUpdate(ControllerEvent event) { doUpdate(); } /** * Calculates the position and size of the crosshair painter and requests * the text area to scroll the resulting rectangle to be visible in the view. * In this calculation the <code>centerVertically</code> value is taken into * account. */ private void scrollIfNeeded() { try { Highlighter.Highlight[] h_arr = highlighter.getHighlights(); for (int i = 0; i < h_arr.length; i++) { if (h_arr[i].getPainter() == currentPainter) { int idx = h_arr[i].getStartOffset(); int ide = h_arr[i].getEndOffset(); Rectangle rect = taText.modelToView(idx); Rectangle endRect = taText.modelToView(ide); if ((rect == null) || (endRect == null)) { return; } Rectangle union = rect.union(endRect); if (centerVertically) { int restY = jspText.getViewport().getHeight() - union.height; union.y = union.y - (restY / 2); if (union.y < 0) { union.y = 0; } // the +1 for some reason results in a smoother scrolling... union.height = union.height + restY + 1; taText.scrollRectToVisible(union); } else { //taText.scrollRectToVisible(taText.modelToView(idx)); taText.scrollRectToVisible(union); } break; } } } catch (Exception ex) { ex.printStackTrace(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -