📄 textwindow.java
字号:
/* -*- tab-width: 4 -*- * * Electric(tm) VLSI Design System * * File: TextWindow.java * * Copyright (c) 2003 Sun Microsystems and Static Free Software * * Electric(tm) 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 3 of the License, or * (at your option) any later version. * * Electric(tm) 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 Electric(tm); see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, Mass 02111-1307, USA. */package com.sun.electric.tool.user.ui;import com.sun.electric.database.hierarchy.Cell;import com.sun.electric.database.text.TextUtils;import com.sun.electric.database.text.WeakReferences;import com.sun.electric.database.variable.CodeExpression;import com.sun.electric.database.variable.TextDescriptor;import com.sun.electric.database.variable.VarContext;import com.sun.electric.database.variable.Variable;import com.sun.electric.tool.Job;import com.sun.electric.tool.JobException;import com.sun.electric.tool.io.FileType;import com.sun.electric.tool.user.Highlighter;import com.sun.electric.tool.user.User;import com.sun.electric.tool.user.dialogs.OpenFile;import java.awt.BorderLayout;import java.awt.Cursor;import java.awt.Font;import java.awt.Graphics;import java.awt.Point;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import java.awt.geom.Point2D;import java.awt.image.BufferedImage;import java.awt.print.PageFormat;import java.beans.PropertyChangeListener;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.URL;import java.util.Iterator;import java.util.List;import java.util.Set;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;import javax.swing.event.UndoableEditEvent;import javax.swing.event.UndoableEditListener;import javax.swing.text.BadLocationException;import javax.swing.text.Document;import javax.swing.text.Element;import javax.swing.tree.MutableTreeNode;import javax.swing.undo.CannotRedoException;import javax.swing.undo.CannotUndoException;import javax.swing.undo.UndoManager;/** * This class defines a text window for displaying text cells. */public class TextWindow implements WindowContent{ /** the cell that is in the window */ private Cell cell; /** the window frame containing this editwindow */ private WindowFrame wf; /** the overall panel with disp area and sliders */ private JPanel overall; /** true if text in the window is closing. */ private boolean finishing; /** true if text in the window is being reloaded. */ private boolean reloading; private JTextArea textArea; private JScrollPane scrollPane; private UndoManager undo = new UndoManager(); /** * Factory method to create a new TextWindow with a given cell, in a given WindowFrame. * @param cell the cell in this TextWindow. * @param wf the WindowFrame that this TextWindow lives in. */ public TextWindow(Cell cell, WindowFrame wf) { this.wf = wf; finishing = false; reloading = false; textArea = new JTextArea(); scrollPane = new JScrollPane(textArea); overall = new JPanel(); overall.setLayout(new BorderLayout()); overall.add(scrollPane, BorderLayout.CENTER); setCell(cell, VarContext.globalContext, null); TextWindowDocumentListener twDocumentListener = new TextWindowDocumentListener(this); textArea.getDocument().addDocumentListener(twDocumentListener); textArea.getDocument().addUndoableEditListener(new MyUndoableEditListener()); textArea.addFocusListener(twDocumentListener); } public void setCursor(Cursor cursor) { // nothing implemented in TextWindow } private void setCellFont(Cell cell) { String fontName = User.getDefaultTextCellFont(); int fontSize = User.getDefaultTextCellSize(); if (cell != null) { fontName = cell.getVarValue(Cell.TEXT_CELL_FONT_NAME, String.class, fontName); fontSize = cell.getVarValue(Cell.TEXT_CELL_FONT_SIZE, Integer.class, new Integer(fontSize)).intValue(); } textArea.setFont(new Font(fontName, 0, fontSize)); } private class MyUndoableEditListener implements UndoableEditListener { public void undoableEditHappened(UndoableEditEvent e) { // Remember the edit and update the menus undo.addEdit(e.getEdit()); updateUndoRedo(); } } private static WeakReferences<PropertyChangeListener> undoListeners = new WeakReferences<PropertyChangeListener>(); private static WeakReferences<PropertyChangeListener> redoListeners = new WeakReferences<PropertyChangeListener>(); public static void addTextUndoListener(PropertyChangeListener l) { undoListeners.add(l); } public static void addTextRedoListener(PropertyChangeListener l) { redoListeners.add(l); } public static void removeTextUndoListener(PropertyChangeListener l) { undoListeners.remove(l); } public static void removeTextRedoListener(PropertyChangeListener l) { redoListeners.remove(l); } private void updateUndoRedo() { // Commented temporarily DN 10-Apr-2006// TopLevel tl = TopLevel.getCurrentJFrame();// PropertyChangeEvent un = new PropertyChangeEvent(tl, UserInterfaceMain.propUndoEnabled, null, new Boolean(undo.canUndo()));// PropertyChangeEvent re = new PropertyChangeEvent(tl, UserInterfaceMain.propRedoEnabled, null, new Boolean(undo.canRedo()));// if (tl != null)// {// tl.getToolBar().propertyChange(un);// tl.getToolBar().propertyChange(re);// }// for (Iterator<PropertyChangeListener> it = undoListeners.reverseIterator(); it.hasNext(); )// it.next().propertyChange(un);// for (Iterator<PropertyChangeListener> it = redoListeners.reverseIterator(); it.hasNext(); )// it.next().propertyChange(un); } /** * Method to undo changes to text in this TextWindow. */ public void undo() { try { undo.undo(); updateUndoRedo(); } catch (CannotUndoException e) { System.out.println("Cannot undo"); } } /** * Method to redo changes to text in this TextWindow. */ public void redo() { try { undo.redo(); updateUndoRedo(); } catch (CannotRedoException e) { System.out.println("Cannot redo"); } } /** * Method to repaint this TextWindow. */ public void paint(Graphics g) { // to enable keys to be received if (cell != null && cell == WindowFrame.getCurrentCell()) textArea.requestFocus(); textArea.paint(g); } /** * Method to update the font information in this window. */ public void updateFontInformation() { textArea.setFont(new Font(User.getDefaultTextCellFont(), 0, User.getDefaultTextCellSize())); } /** * Class to handle special changes to changes to design rules. */ private static class TextWindowDocumentListener implements DocumentListener, FocusListener { private TextWindow tw; TextWindowDocumentListener(TextWindow tw) { this.tw = tw; } public void changedUpdate(DocumentEvent e) { tw.textWindowContentChanged(); } public void insertUpdate(DocumentEvent e) { tw.textWindowContentChanged(); } public void removeUpdate(DocumentEvent e) { tw.textWindowContentChanged(); } public void focusGained(FocusEvent e) { TopLevel top = TopLevel.getCurrentJFrame(); top.getTheMenuBar().setIgnoreTextEditKeys(true); } public void focusLost(FocusEvent e) { TopLevel top = TopLevel.getCurrentJFrame(); top.getTheMenuBar().setIgnoreTextEditKeys(false); } } private void textWindowContentChanged() { if (!reloading) new SaveCellText(this); } public List<MutableTreeNode> loadExplorerTrees() { return wf.loadDefaultExplorerTree(); } /** * Method to return the top-level JPanel for this TextWindow. * @return the top-level JPanel for this TextWindow. */ public JPanel getPanel() { return overall; } /** * Method to get rid of this EditWindow. Called by WindowFrame when * that windowFrame gets closed. */ public void finished() {} /** * Class to save a cell's text in a new Job. */ private static class SaveCellText extends Job { private Cell cell; private String [] strings; private SaveCellText(TextWindow tw) { super("Save Cell Text", User.getUserTool(), Job.Type.CHANGE, null, null, Job.Priority.USER); this.cell = tw.cell; this.strings = tw.convertToStrings(); startJob(); } public boolean doIt() throws JobException { if (cell != null) cell.setTextViewContents(strings); return true; } } /** * Method to set the window title. */ public void setWindowTitle() { if (wf == null) return; wf.setTitle(wf.composeTitle(cell, "", 0)); } /** * Method to return the cell that is shown in this window. * @return the cell that is shown in this window. */ public Cell getCell() { return cell; } public Highlighter getHighlighter() { return null; } /** * Method to set the cell that is shown in the window to "cell". */ public void setCell(Cell cell, VarContext context, WindowFrame.DisplayAttributes displayAttributes) { this.cell = cell; String [] lines = (cell != null) ? cell.getTextViewContents() : null; String oneLine = (lines != null) ? oneLine = makeOneString(lines) : ""; setCellFont(cell); reloading = true; textArea.setText(oneLine); textArea.setSelectionStart(0); textArea.setSelectionEnd(0); reloading = false; setWindowTitle(); } /** * Method to read a text disk file into this TextWindow. */ public static void readTextCell() { WindowFrame wf = WindowFrame.getCurrentWindowFrame(); if (wf == null) return; WindowContent content = wf.getContent(); if (!(content instanceof TextWindow)) { Job.getUserInterface().showErrorMessage("You must first be editing a text cell (a cell whose view is textual, such as 'doc').", "Cannot import text file"); return; } TextWindow tw = (TextWindow)content; String fileName = OpenFile.chooseInputFile(FileType.TEXT, null); if (fileName == null) return; tw.readTextCell(fileName); } public void readTextCell(String fileName) { if (fileName == null) { System.out.println("Bad file name: "+fileName); return; } // start a job to do the input URL fileURL = TextUtils.makeURLToFile(fileName); InputStream stream = TextUtils.getURLStream(fileURL); if (stream == null) { System.out.println("Could not open file: " + fileURL.getFile()); return; } try { fileURL.openConnection(); } catch (IOException e) { System.out.println("Could not find file: " + fileURL.getFile()); return; } // clear the buffer textArea.setText(""); final int READ_BUFFER_SIZE = 65536; char [] buf = new char[READ_BUFFER_SIZE]; InputStreamReader is = new InputStreamReader(stream); try { for(;;) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -