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; 031import java.awt.Toolkit; 032import java.awt.event.KeyEvent; 033 034import javax.swing.JTextArea; 035import javax.swing.KeyStroke; 036import javax.swing.SwingUtilities; 037import javax.swing.text.Document; 038 039import org.fife.ui.rsyntaxtextarea.ErrorStrip; 040import org.fife.ui.rsyntaxtextarea.RSyntaxDocument; 041import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; 042import org.fife.ui.rsyntaxtextarea.RSyntaxTextAreaEditorKit; 043import org.fife.ui.rtextarea.RTextScrollPane; 044 045import diva.gui.GUIUtilities; 046import ptolemy.gui.UndoListener; 047import ptolemy.kernel.util.Attribute; 048import ptolemy.vergil.toolbox.TextEditorFactory; 049import ptolemy.vergil.toolbox.TextEditorForStringAttributes; 050 051/////////////////////////////////////////////////////////////////// 052//// SyntaxTextEditorForStringAttributes 053 054/** 055 056 A text editor to edit a specified string attribute using a syntax-directed editor. 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 SyntaxTextEditorForStringAttributes 066 extends TextEditorForStringAttributes { 067 068 /** Create a annotation text editor for the specified attribute. 069 * @param factory The factory that created this editor. 070 * @param attributeToEdit The string attribute to edit. 071 * @param rows The number of rows. 072 * @param columns The number of columns. 073 * @param title The window title to use. 074 * @param document The document 075 */ 076 public SyntaxTextEditorForStringAttributes(TextEditorFactory factory, 077 Attribute attributeToEdit, int rows, int columns, String title, 078 Document document) { 079 super(factory, attributeToEdit, rows, columns, title, document); 080 } 081 082 /////////////////////////////////////////////////////////////////// 083 //// protected methods //// 084 085 /** Create an edit menu. 086 */ 087 @Override 088 protected void _addMenus() { 089 super._addMenus(); 090 091 _editMenu.addSeparator(); 092 093 GUIUtilities.addMenuItem(_editMenu, 094 new RSyntaxTextAreaEditorKit.IncreaseFontSizeAction( 095 "Increase Font", null, "Increase the font size.", 8, 096 KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS, 097 Toolkit.getDefaultToolkit() 098 .getMenuShortcutKeyMask()))); 099 GUIUtilities.addMenuItem(_editMenu, 100 new RSyntaxTextAreaEditorKit.DecreaseFontSizeAction( 101 "Decrease Font", null, "Decrease the font size.", 9, 102 KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, 103 Toolkit.getDefaultToolkit() 104 .getMenuShortcutKeyMask()))); 105 } 106 107 /** Initializes an empty text editor with the specified title and 108 * document and associated placeable. After constructing this, 109 * it is necessary to call setVisible(true) to make the frame 110 * appear. 111 * 112 * @param title The title to put in the title bar. 113 * @param document The document containing text. 114 */ 115 @Override 116 protected void _init(final String title, Document document) { 117 // No idea why this needs to be invoked later, but if it's invoked now, 118 // the title on the window ends up being "Unnamed". 119 SwingUtilities.invokeLater(new Runnable() { 120 @Override 121 public void run() { 122 setTitle(title); 123 } 124 }); 125 126 if (document instanceof RSyntaxDocument) { 127 text = new RSyntaxTextArea((RSyntaxDocument) document); 128 // The default tab size is odd: 5. 129 text.setTabSize(4); 130 text.setCaretPosition(0); 131 // ((RSyntaxTextArea)text).addHyperlinkListener(this); 132 text.requestFocusInWindow(); 133 // ((RSyntaxTextArea)text).setMarkOccurrences(true); 134 ((RSyntaxTextArea) text).setCodeFoldingEnabled(true); 135 ((RSyntaxTextArea) text).setClearWhitespaceLinesEnabled(false); 136 } else if (document != null) { 137 text = new JTextArea(document); 138 } else { 139 text = new JTextArea(); 140 } 141 142 // Since the document may have been null, request it... 143 document = text.getDocument(); 144 document.addDocumentListener(this); 145 _scrollPane = new RTextScrollPane(text, true); 146 147 // To get bookmarking, do this: 148 // Gutter gutter = ((RTextScrollPane)_scrollPane).getGutter(); 149 // gutter.setBookmarkingEnabled(true); 150 // URL url = getClass().getClassLoader().getResource("img/bookmark.png"); 151 // gutter.setBookmarkIcon(new ImageIcon(url)); 152 // Will need to copy the img/bookmark.png from the rsyntaxtextarea_demo_2.5.1_Source dir. 153 154 ErrorStrip errorStrip = new ErrorStrip((RSyntaxTextArea) text); 155 getContentPane().add(errorStrip, BorderLayout.LINE_END); 156 157 getContentPane().add(_scrollPane, BorderLayout.CENTER); 158 _initialSaveAsFileName = "data.txt"; 159 160 // Set the undo listener, with default key mappings. 161 _undo = new UndoListener(text); 162 text.getDocument().addUndoableEditListener(_undo); 163 } 164}