001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: SimpleEditor.java,v $ 023 Revision 1.10 2004/05/13 23:23:08 markl 024 Use JToolBar, and return focus to JTextArea after toolbar button pressed. 025 026 Revision 1.9 2004/03/16 06:43:39 markl 027 LocaleManager method change 028 029 Revision 1.8 2004/01/23 00:03:58 markl 030 javadoc corrections 031 032 Revision 1.7 2003/01/19 09:46:48 markl 033 replaced JScrollPane instances with KScrollPane. 034 035 Revision 1.6 2001/03/20 00:54:53 markl 036 Fixed deprecated calls. 037 038 Revision 1.5 2001/03/12 09:27:59 markl 039 Source code and Javadoc cleanup. 040 041 Revision 1.4 1999/04/19 05:59:38 markl 042 I18N changes. 043 044 Revision 1.3 1999/02/28 00:24:44 markl 045 Added setEditable() method. 046 047 Revision 1.2 1999/01/10 03:00:07 markl 048 added GPL header & RCS tag 049 ---------------------------------------------------------------------------- 050*/ 051 052package kiwi.ui; 053 054import java.awt.*; 055import java.awt.event.*; 056import javax.swing.*; 057 058import kiwi.util.*; 059 060/** A simple text editor for entering unformatted text. The editor consists of 061 * a scrollable <code>JTextArea</code> and <i>Cut</i>, <i>Copy</i>, and 062 * <i>Paste</i> buttons. 063 * 064 * <p><center> 065 * <img src="snapshot/SimpleEditor.gif"><br> 066 * <i>An example SimpleEditor.</i> 067 * </center> 068 * 069 * @see kiwi.ui.SimpleStyledEditor 070 * 071 * @author Mark Lindner 072 */ 073 074public class SimpleEditor extends KPanel 075 { 076 private KButton b_cut, b_copy, b_paste; 077 /** The <code>JTextArea</code> that holds the text for this component. */ 078 protected JTextArea t_text; 079 private JToolBar toolBar; 080 081 /** Construct a new <code>SimpleEditor</code>. The editor is created with 082 * a default <code>JTextArea</code> size of 10 rows by 60 columns. 083 */ 084 085 public SimpleEditor() 086 { 087 this(10, 60); 088 } 089 090 /** Construct a new <code>SimpleEditor</code> with the specified number of 091 * rows and colunns. 092 * 093 * @param rows The number of rows for the <code>JTextArea</code>. 094 * @param columns The number of columns for the <code>JTextArea</code>. 095 */ 096 097 public SimpleEditor(int rows, int columns) 098 { 099 setLayout(new BorderLayout(5, 5)); 100 101 ResourceManager rm = KiwiUtils.getResourceManager(); 102 103 KPanel p_top = new KPanel(); 104 p_top.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); 105 add("North", p_top); 106 107 ActionListener al = new ActionListener() 108 { 109 public void actionPerformed(ActionEvent evt) 110 { 111 Object o = evt.getSource(); 112 113 if(o == b_cut) 114 cut(); 115 else if(o == b_copy) 116 copy(); 117 else if(o == b_paste) 118 paste(); 119 120 t_text.requestFocus(); 121 } 122 }; 123 124 LocaleData loc = LocaleManager.getDefault().getLocaleData("KiwiDialogs"); 125 126 toolBar = new JToolBar(); 127 toolBar.setFloatable(false); 128 129 p_top.add(toolBar); 130 131 b_cut = new KButton(rm.getIcon("cut.gif")); 132 b_cut.addActionListener(al); 133 b_cut.setToolTipText(loc.getMessage("kiwi.tooltip.cut")); 134 toolBar.add(b_cut); 135 136 b_copy = new KButton(rm.getIcon("copy.gif")); 137 b_copy.addActionListener(al); 138 b_copy.setToolTipText(loc.getMessage("kiwi.tooltip.copy")); 139 toolBar.add(b_copy); 140 141 b_paste = new KButton(rm.getIcon("paste.gif")); 142 b_paste.addActionListener(al); 143 b_paste.setToolTipText(loc.getMessage("kiwi.tooltip.paste")); 144 toolBar.add(b_paste); 145 146 t_text = new JTextArea(rows, columns); 147 add("Center", new KScrollPane(t_text)); 148 t_text.setLineWrap(true); 149 t_text.setWrapStyleWord(true); 150 } 151 152 /** Request focus for the editor. 153 */ 154 155 public void requestFocus() 156 { 157 if(t_text != null) 158 t_text.requestFocus(); 159 else 160 super.requestFocus(); 161 } 162 163 /** Get the <code>JTextArea</code> used by this <code>SimpleEditor</code>. 164 * 165 * @return The <code>JTextArea</code> for this editor. 166 */ 167 168 public JTextArea getJTextArea() 169 { 170 return(t_text); 171 } 172 173 /** Insert text into the editor, replacing the current selection (if any). 174 * 175 * @param text The text to insert. 176 */ 177 178 public synchronized void insertText(String text) 179 { 180 t_text.replaceSelection(text); 181 } 182 183 /** Set the editable state of the editor. 184 * 185 * @param flag If <code>true</code>, the editor will be editable, otherwise 186 * it will be non-editable. 187 */ 188 189 public void setEditable(boolean flag) 190 { 191 t_text.setEditable(flag); 192 b_cut.setEnabled(flag); 193 b_copy.setEnabled(flag); 194 b_paste.setEnabled(flag); 195 } 196 197 /** Set the text in the editor. 198 * 199 * @param text The text to display in the editor. 200 */ 201 202 public synchronized void setText(String text) 203 { 204 t_text.setText(text); 205 } 206 207 /** Get the text in the editor. 208 * 209 * @return The text currently in the editor. 210 */ 211 212 public synchronized String getText() 213 { 214 return(t_text.getText()); 215 } 216 217 /** Perform a <i>cut</i> operation on the editor. Removes the selected text 218 * from the editor, and stores it in the system clipboard. 219 */ 220 221 public void cut() 222 { 223 t_text.cut(); 224 } 225 226 /** Perform a <i>copy</i> operation on the editor. Copies the selected text 227 * from the editor to the system clipboard. 228 */ 229 230 public void copy() 231 { 232 t_text.copy(); 233 } 234 235 /** Perform a <i>paste</i> operation on the editor. Inserts text from the 236 * system clipboard into the editor. 237 */ 238 239 public void paste() 240 { 241 t_text.paste(); 242 } 243 244 /** Add a button to the editor's tool bar. The button is added to the right 245 * of the last button in the toolbar. This method does <i>not</i> register 246 * this editor as an <code>ActionListener</code> for the button. 247 * 248 * @param button The button to add. 249 */ 250 251 public void addButton(KButton button) 252 { 253 toolBar.add(button); 254 } 255 256 } 257 258/* end of source file */