source: main/trunk/gli/src/org/greenstone/gatherer/gui/NumberedJTextArea.java@ 34266

Last change on this file since 34266 was 34266, checked in by ak19, 4 years ago
  1. Added rudimentary support for inserting the basic skeletons of security-related elements into collectionConfiguration.xml when using GLI's ConfigFileEditor. Hopefully this is useful. 2. Removed the self-evident tooltip from the ConfigFileEditor as it was always annoying floating over the editor, obscuring stuff. Had to addd a no-arg constructor in NumberedJTextArea for this to be possible.
File size: 7.3 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Copyright (C) 1999 New Zealand Digital Library Project
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *########################################################################
24 */
25
26package org.greenstone.gatherer.gui;
27
28import org.greenstone.gatherer.Configuration;
29import org.greenstone.gatherer.Dictionary;
30
31import org.fife.ui.rsyntaxtextarea.*;
32
33import java.awt.*;
34import java.awt.event.*;
35import java.util.*;
36import javax.swing.*;
37import javax.swing.event.*;
38import javax.swing.undo.*;
39
40/**
41 * A textarea with the line number next to each line of the text.
42 * It provides undo and redo buttons that are already hooked up to listeners
43 * You can add these buttons to a JComponent and they will behave just like the
44 * existing RSyntaxArea's Ctrl-Z and Ctrl-Y shortcuts and undo/redo popup menus.
45 */
46public class NumberedJTextArea extends RSyntaxTextArea /* JTextArea */
47 implements UndoableEditListener, ActionListener
48{
49
50 String id = null;
51
52 public final GLIButton undoButton;
53 public final GLIButton redoButton;
54
55 public NumberedJTextArea() {
56 this("");
57 }
58
59 public NumberedJTextArea(String tooltip) {
60 this("", tooltip);
61 }
62
63 public NumberedJTextArea (String id, String tooltip) {
64 super();
65
66 this.id = id;
67
68 // maybe use this textarea's id field to customise the undo/redo button tooltips?
69 undoButton = new GLIButton(Dictionary.get("General.Undo"), Dictionary.get("General.Undo_Tooltip"));
70 undoButton.setEnabled(false);
71
72 redoButton = new GLIButton(Dictionary.get("General.Redo"), Dictionary.get("General.Redo_Tooltip"));
73 redoButton.setEnabled(false);
74
75 undoButton.addActionListener(this);
76 redoButton.addActionListener(this);
77
78 // next, initialise this RSyntaxTextArea:
79
80 // Adding the UndoableEditListener has to come after instantiation of the undo and
81 // redo buttons, since the listener expects these buttons to already exist
82 this.getDocument().addUndoableEditListener(this);
83
84 /* Fields specific to RSyntaxQuery inherited class */
85 setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_XML);
86 setBracketMatchingEnabled(true);
87 setAnimateBracketMatching(true);
88 setAntiAliasingEnabled(true);
89 setAutoIndentEnabled(true);
90 setPaintMarkOccurrencesBorder(false);
91
92 /* Standard fields to JTextArea */
93 setOpaque(false);
94 setBackground(Configuration.getColor("coloring.editable_background", false));
95 setCaretPosition(0);
96 setLineWrap(true);
97 setRows(11);
98 setWrapStyleWord(false);
99 setToolTipText(tooltip);
100 }
101
102 public void paintComponent(Graphics g)
103 {
104 Insets insets = getInsets();
105 Rectangle rectangle = g.getClipBounds();
106 g.setColor(Color.white);
107 g.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
108
109 super.paintComponent(g);
110
111 if (rectangle.x < insets.left)
112 {
113 FontMetrics font_metrics = g.getFontMetrics();
114 int font_height = font_metrics.getHeight();
115 int y = font_metrics.getAscent() + insets.top;
116 int line_number_start_point = ((rectangle.y + insets.top) / font_height) + 1;
117 if (y < rectangle.y)
118 {
119 y = line_number_start_point * font_height - (font_height - font_metrics.getAscent());
120 }
121 int y_axis_end_point = y + rectangle.height + font_height;
122 int x_axis_start_point = insets.left;
123 x_axis_start_point -= getFontMetrics(getFont()).stringWidth(Math.max(getRows(), getLineCount() + 1) + " ");
124 if (!this.getText().trim().equals(""))
125 {
126 g.setColor(Color.DARK_GRAY);
127 }
128 else
129 {
130 g.setColor(Color.white);
131 }
132 int length = ("" + Math.max(getRows(), getLineCount() + 1)).length();
133 while (y < y_axis_end_point)
134 {
135 g.drawString(line_number_start_point + " ", x_axis_start_point, y);
136 y += font_height;
137 line_number_start_point++;
138 }
139 }
140 }
141
142
143 public Insets getInsets()
144 {
145 Insets insets = super.getInsets(new Insets(0, 0, 0, 0));
146 insets.left += getFontMetrics(getFont()).stringWidth(Math.max(getRows(), getLineCount() + 1) + " ");
147 return insets;
148 }
149
150
151 // Overriding, to ensure that even if ctrl-z was pressed to undo something,
152 // the undo and redo buttons are in sync with that.
153 public void undoLastAction() {
154 super.undoLastAction();
155 redoButton.setEnabled(true);
156
157 if (!this.canUndo()) {
158 undoButton.setEnabled(false);
159 } else {
160 undoButton.setEnabled(true);
161 }
162 }
163
164 // Overriding
165 public void redoLastAction() {
166 super.redoLastAction();
167 undoButton.setEnabled(true);
168
169 if (!this.canRedo()) {
170 redoButton.setEnabled(false);
171 } else {
172 redoButton.setEnabled(true);
173 }
174 }
175
176 // Overriding
177 public void discardAllEdits() {
178 // Buttons have to be disabled before discardAllEdits(). If done in reverse order, buttons get re-enabled
179 undoButton.setEnabled(false);
180 redoButton.setEnabled(false);
181 super.discardAllEdits();
182 }
183
184 // The RSyntaxTextarea class already provides undo and redo functionality hooked up to their
185 // usual keyboard shortcuts and to rightclick popup menus. The actionListener below merely
186 // reuses this behaviour and attaches it to the undoButton and redoButton.
187 public void actionPerformed(ActionEvent event)
188 {
189 // actionPerformed() not defined in any of the superclasses RTextArea, RTextAreaBase, JTextArea
190 //super.actionPerformed(event); // compile error
191
192 if(event.getSource() == undoButton) {
193
194 try {
195 // calls canUndo() and undoLastAction() defined by RTextArea
196 // internally calls its undoManager's canUndo() and undo() to handle compoundEdits
197 if (this.canUndo()) {
198 this.undoLastAction();
199 }
200
201 if (!this.canUndo()) {
202 undoButton.setEnabled(false);
203 } else {
204 undoButton.setEnabled(true);
205 }
206
207 } catch (Exception e) {
208 System.err.println("Exception trying to undo: " + e.getMessage());
209 e.printStackTrace();
210 }
211 }
212 else if (event.getSource() == redoButton) {
213 try {
214 // calls canRedo() and redoLastAction() defined by RTextArea
215 // internally calls its undoManager's canRedo() and redo() to handle compoundEdits
216
217 if (this.canRedo()) {
218 this.redoLastAction();
219 }
220
221 if (!this.canRedo()) {
222 redoButton.setEnabled(false);
223 } else {
224 redoButton.setEnabled(true);
225 }
226
227 } catch (Exception e) {
228 System.err.println("Exception trying to redo: " + e.getMessage());
229 e.printStackTrace();
230 }
231 }
232 }
233
234
235 // defined in interface UndoableEditListener
236 public void undoableEditHappened(UndoableEditEvent evt)
237 {
238 undoButton.setEnabled(true);
239 redoButton.setEnabled(false);
240 }
241}
Note: See TracBrowser for help on using the repository browser.