001/* Top-level window containing a simple text editor or viewer. 002 003 Copyright (c) 1998-2016 The Regents of the University of California. 004 All rights reserved. 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the above 008 copyright notice and the following two paragraphs appear in all copies 009 of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION_2 025 COPYRIGHTENDKEY 026 027 */ 028package ptolemy.actor.gui.syntax; 029 030import java.awt.BorderLayout; 031 032import javax.swing.JTextArea; 033import javax.swing.text.Document; 034 035import org.fife.ui.rsyntaxtextarea.ErrorStrip; 036import org.fife.ui.rsyntaxtextarea.RSyntaxDocument; 037import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; 038import org.fife.ui.rtextarea.RTextScrollPane; 039 040import ptolemy.actor.gui.Placeable; 041import ptolemy.actor.gui.TextEditor; 042import ptolemy.actor.injection.PortablePlaceable; 043import ptolemy.gui.UndoListener; 044 045/////////////////////////////////////////////////////////////////// 046//// SyntaxTextEditor 047 048/** 049 050 A top-level window containing a text editor or viewer that understands 051 the syntax of various languages. To get syntax-directed editing, 052 construct this object with an instance of org.fife.ui.rsyntaxtextarea.RSyntaxDocument 053 passed in as a constructor argument. 054 You can access the public member text to set the text, get the text, 055 or set the number of rows or columns. 056 After creating this, it is necessary to call show() for it to appear. 057 058 @author Edward A. Lee 059 @version $Id$ 060 @since Ptolemy II 11.0 061 @Pt.ProposedRating Yellow (eal) 062 @Pt.AcceptedRating Red (eal) 063 */ 064@SuppressWarnings("serial") 065public class SyntaxTextEditor extends TextEditor { 066 067 /** Construct an empty text editor with no name. 068 * After constructing this, it is necessary 069 * to call setVisible(true) to make the frame appear. 070 */ 071 public SyntaxTextEditor() { 072 this("Unnamed"); 073 } 074 075 /** Construct an empty text editor with the specified title. 076 * After constructing this, it is necessary 077 * to call setVisible(true) to make the frame appear. 078 * @param title The title to put in the title bar. 079 */ 080 public SyntaxTextEditor(String title) { 081 this(title, null); 082 } 083 084 /** Construct an empty text editor with the specified title and 085 * document. After constructing this, it is necessary 086 * to call setVisible(true) to make the frame appear. 087 * @param title The title to put in the title bar. 088 * @param document The document containing text, or null if none. 089 */ 090 public SyntaxTextEditor(String title, Document document) { 091 this(title, document, (Placeable) null); 092 } 093 094 /** Construct an empty text editor with the specified title and 095 * document and associated placeable. After constructing this, 096 * it is necessary to call setVisible(true) to make the frame 097 * appear. 098 * @param title The title to put in the title bar. 099 * @param document The document containing text, or null if none. 100 * @param placeable The associated placeable. 101 */ 102 public SyntaxTextEditor(String title, Document document, 103 Placeable placeable) { 104 // NOTE: Create with no status bar, since we have no use for it now. 105 super(title, document, placeable); 106 } 107 108 /** Construct an empty text editor with the specified title and 109 * document and associated PortablePlaceable. After constructing this, 110 * it is necessary to call setVisible(true) to make the frame 111 * appear. 112 * @param title The title to put in the title bar. 113 * @param document The document containing text, or null if none. 114 * @param portablePlaceable The associated PortablePlaceable. 115 */ 116 public SyntaxTextEditor(String title, Document document, 117 PortablePlaceable portablePlaceable) { 118 // NOTE: Create with no status bar, since we have no use for it now. 119 super(null, null, portablePlaceable); 120 } 121 122 /////////////////////////////////////////////////////////////////// 123 //// protected methods //// 124 125 /** Initializes an empty text editor with the specified title and 126 * document and associated placeable. After constructing this, 127 * it is necessary to call setVisible(true) to make the frame 128 * appear. 129 * 130 * @param title The title to put in the title bar. 131 * @param document The document containing text. 132 */ 133 @Override 134 protected void _init(String title, Document document) { 135 setTitle(title); 136 137 if (document instanceof RSyntaxDocument) { 138 text = new RSyntaxTextArea((RSyntaxDocument) document); 139 // The default tab size is odd: 5. 140 text.setTabSize(4); 141 text.setCaretPosition(0); 142 // ((RSyntaxTextArea)text).addHyperlinkListener(this); 143 text.requestFocusInWindow(); 144 ((RSyntaxTextArea) text).setMarkOccurrences(true); 145 ((RSyntaxTextArea) text).setCodeFoldingEnabled(true); 146 ((RSyntaxTextArea) text).setClearWhitespaceLinesEnabled(false); 147 } else if (document != null) { 148 text = new JTextArea(document); 149 } else { 150 text = new JTextArea(); 151 } 152 153 // Since the document may have been null, request it... 154 document = text.getDocument(); 155 document.addDocumentListener(this); 156 _scrollPane = new RTextScrollPane(text, true); 157 158 // To get bookmarking, do this: 159 // Gutter gutter = ((RTextScrollPane)_scrollPane).getGutter(); 160 // gutter.setBookmarkingEnabled(true); 161 // URL url = getClass().getClassLoader().getResource("img/bookmark.png"); 162 // gutter.setBookmarkIcon(new ImageIcon(url)); 163 // Will need to copy the img/bookmark.png from the rsyntaxtextarea_demo_2.5.1_Source dir. 164 165 ErrorStrip errorStrip = new ErrorStrip((RSyntaxTextArea) text); 166 getContentPane().add(errorStrip, BorderLayout.LINE_END); 167 168 getContentPane().add(_scrollPane, BorderLayout.CENTER); 169 _initialSaveAsFileName = "data.txt"; 170 171 // Set the undo listener, with default key mappings. 172 _undo = new UndoListener(text); 173 text.getDocument().addUndoableEditListener(_undo); 174 } 175}