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 | * <BR><BR>
|
---|
9 | *
|
---|
10 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
11 | *
|
---|
12 | * <BR><BR>
|
---|
13 | *
|
---|
14 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
15 | *
|
---|
16 | * <BR><BR>
|
---|
17 | *
|
---|
18 | * This program is free software; you can redistribute it and/or modify
|
---|
19 | * it under the terms of the GNU General Public License as published by
|
---|
20 | * the Free Software Foundation; either version 2 of the License, or
|
---|
21 | * (at your option) any later version.
|
---|
22 | *
|
---|
23 | * <BR><BR>
|
---|
24 | *
|
---|
25 | * This program is distributed in the hope that it will be useful,
|
---|
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
28 | * GNU General Public License for more details.
|
---|
29 | *
|
---|
30 | * <BR><BR>
|
---|
31 | *
|
---|
32 | * You should have received a copy of the GNU General Public License
|
---|
33 | * along with this program; if not, write to the Free Software
|
---|
34 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
35 | *########################################################################
|
---|
36 | */
|
---|
37 |
|
---|
38 |
|
---|
39 |
|
---|
40 |
|
---|
41 |
|
---|
42 |
|
---|
43 | package org.greenstone.gatherer.gui;
|
---|
44 |
|
---|
45 | import java.awt.Font;
|
---|
46 | import java.awt.event.ActionEvent;
|
---|
47 | import java.awt.event.ActionListener;
|
---|
48 | import java.awt.event.FocusEvent;
|
---|
49 | import java.awt.event.FocusListener;
|
---|
50 | import java.io.File;
|
---|
51 | import java.io.FileInputStream;
|
---|
52 | import java.io.FileOutputStream;
|
---|
53 | import javax.swing.JButton;
|
---|
54 | import javax.swing.JEditorPane;
|
---|
55 | import javax.swing.JPanel;
|
---|
56 | import javax.swing.JScrollPane;
|
---|
57 | import javax.swing.event.UndoableEditEvent;
|
---|
58 | import javax.swing.event.UndoableEditListener;
|
---|
59 | import javax.swing.undo.CannotRedoException;
|
---|
60 | import javax.swing.undo.CannotUndoException;
|
---|
61 | import javax.swing.undo.UndoManager;
|
---|
62 | import org.greenstone.gatherer.Gatherer;
|
---|
63 | /** A JEditorPane subclass that internally supports the undoable interface
|
---|
64 | * and allows a designer to specify if the component should recieve focus
|
---|
65 | * on a tab event.
|
---|
66 | */
|
---|
67 | public class GEditorPane
|
---|
68 | extends JEditorPane
|
---|
69 | implements ActionListener, FocusListener, UndoableEditListener {
|
---|
70 |
|
---|
71 | private boolean focusable = true;
|
---|
72 |
|
---|
73 | private File file = null;
|
---|
74 |
|
---|
75 | private Gatherer gatherer = null;
|
---|
76 |
|
---|
77 | private JButton redo_button = null;
|
---|
78 | private JButton undo_button = null;
|
---|
79 |
|
---|
80 | private JEditorPane editor = null;
|
---|
81 |
|
---|
82 | private UndoManager undo = null;
|
---|
83 |
|
---|
84 | /** This constructor simply sets whether this pane is focusable.
|
---|
85 | * @param focusable A Boolean indicating whether this component should
|
---|
86 | * be focusable.
|
---|
87 | */
|
---|
88 | public GEditorPane(boolean focusable) {
|
---|
89 | this.focusable = focusable;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /** This constructor sets up the initial JEditorPane settings, and
|
---|
93 | * creates the necessary classes to support undo actions.
|
---|
94 | * @param gatherer A reference to Gatherer for purposes of messaging
|
---|
95 | * and dictionary.
|
---|
96 | */
|
---|
97 | public GEditorPane(Gatherer gatherer) {
|
---|
98 | this.gatherer = gatherer;
|
---|
99 | //this.setContentType("text/html");
|
---|
100 | this.setEnabled(true);
|
---|
101 | this.setFont(new Font("Courier New", Font.PLAIN, 12));
|
---|
102 | // Undo support.
|
---|
103 | undo = new UndoManager();
|
---|
104 | // Create components.
|
---|
105 | redo_button = new JButton(gatherer.dictionary.get("UndoManager.Redo"));
|
---|
106 | redo_button.addActionListener(this);
|
---|
107 | redo_button.setEnabled(false);
|
---|
108 | undo_button = new JButton(gatherer.dictionary.get("UndoManager.Undo"));
|
---|
109 | undo_button.addActionListener(this);
|
---|
110 | undo_button.setEnabled(false);
|
---|
111 | }
|
---|
112 |
|
---|
113 | /** This initially calls GEditorPane(focusable), but then uses
|
---|
114 | * the given file to find the initial content of this pane. Moreover
|
---|
115 | * it will now respond to loss of focus events by saving the document
|
---|
116 | * content to this file.
|
---|
117 | * this pane is focusable.
|
---|
118 | * @param file A File to be used as a data source/destination.
|
---|
119 | * @param focusable A Boolean indicating whether this component should
|
---|
120 | * be focusable.
|
---|
121 | */
|
---|
122 | public GEditorPane(File file, boolean focusable) {
|
---|
123 | this(focusable);
|
---|
124 | loadFile(file);
|
---|
125 | this.addFocusListener(this);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /** This initially calls GEditorPane(gatherer), but then sets whether
|
---|
129 | * this pane is focusable.
|
---|
130 | * @param gatherer A reference to Gatherer for purposes of messaging
|
---|
131 | * and dictionary.
|
---|
132 | * @param focusable A Boolean indicating whether this component should
|
---|
133 | * be focusable.
|
---|
134 | */
|
---|
135 | public GEditorPane(Gatherer gatherer, boolean focusable) {
|
---|
136 | this(gatherer);
|
---|
137 | this.focusable = focusable;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /** This initially calls GEditorPane(focusable), but then sets
|
---|
141 | * the initial content for this panel.
|
---|
142 | * this pane is focusable.
|
---|
143 | * @param content A String indicating the initial content of this pane.
|
---|
144 | * @param focusable A Boolean indicating whether this component should
|
---|
145 | * be focusable.
|
---|
146 | */
|
---|
147 | public GEditorPane(String content, boolean focusable) {
|
---|
148 | this(focusable);
|
---|
149 | this.setText(content);
|
---|
150 | }
|
---|
151 |
|
---|
152 | /** This initially calls GEditorPane(gatherer, focusable), but then sets
|
---|
153 | * the initial content for this panel.
|
---|
154 | * this pane is focusable.
|
---|
155 | * @param gatherer A reference to Gatherer for purposes of messaging
|
---|
156 | * and dictionary.
|
---|
157 | * @param content A String indicating the initial content of this pane.
|
---|
158 | * @param focusable A Boolean indicating whether this component should
|
---|
159 | * be focusable.
|
---|
160 | */
|
---|
161 | public GEditorPane(Gatherer gatherer, String content, boolean focusable) {
|
---|
162 | this(gatherer, focusable);
|
---|
163 | this.setText(content);
|
---|
164 | }
|
---|
165 |
|
---|
166 | /** This initially calls GEditorPane(gatherer, focusable), but then uses
|
---|
167 | * the given file to find the initial content of this pane. Moreover
|
---|
168 | * it will now respond to loss of focus events by saving the document
|
---|
169 | * content to this file.
|
---|
170 | * this pane is focusable.
|
---|
171 | * @param gatherer A reference to Gatherer for purposes of messaging
|
---|
172 | * and dictionary.
|
---|
173 | * @param file A File to be used as a data source/destination.
|
---|
174 | * @param focusable A Boolean indicating whether this component should
|
---|
175 | * be focusable.
|
---|
176 | */
|
---|
177 | public GEditorPane(Gatherer gatherer, File file, boolean focusable) {
|
---|
178 | this(gatherer, focusable);
|
---|
179 | loadFile(file);
|
---|
180 | this.addFocusListener(this);
|
---|
181 | }
|
---|
182 |
|
---|
183 | /** Any implementation of ActionListener must include this method so we
|
---|
184 | * can be informed whenever an action has occured.
|
---|
185 | * @param event An ActionEvent.
|
---|
186 | */
|
---|
187 | public void actionPerformed(ActionEvent event) {
|
---|
188 | try {
|
---|
189 | if(event.getSource() == redo_button) {
|
---|
190 | undo.redo();
|
---|
191 | }
|
---|
192 | else {
|
---|
193 | undo.undo();
|
---|
194 | }
|
---|
195 | }
|
---|
196 | catch (CannotRedoException error) {
|
---|
197 | error.printStackTrace();
|
---|
198 | }
|
---|
199 | catch (CannotUndoException error) {
|
---|
200 | error.printStackTrace();
|
---|
201 | }
|
---|
202 | // Update controls to reflect change.
|
---|
203 | redo_button.setEnabled(undo.canRedo());
|
---|
204 | undo_button.setEnabled(undo.canUndo());
|
---|
205 | }
|
---|
206 |
|
---|
207 | /** Any implementation of FocusListener must include this method so we
|
---|
208 | * can be notified when focus is gained.
|
---|
209 | * @param event A FocusEvent.
|
---|
210 | */
|
---|
211 | public void focusGained(FocusEvent event) {
|
---|
212 | }
|
---|
213 |
|
---|
214 | /** Any implementation of FocusListener must include this method so we
|
---|
215 | * can be notified when focus is lost.
|
---|
216 | * @param event A FocusEvent.
|
---|
217 | */
|
---|
218 | public void focusLost(FocusEvent event) {
|
---|
219 | if(file != null) {
|
---|
220 | // Save the document to file.
|
---|
221 | try {
|
---|
222 | FileOutputStream out = new FileOutputStream(file);
|
---|
223 | out.write(this.getText().getBytes());
|
---|
224 | out.close();
|
---|
225 | }
|
---|
226 | catch (Exception error) {
|
---|
227 | error.printStackTrace();
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | /** Retrieves the redo button for this edit pane.
|
---|
233 | * @return A JButton.
|
---|
234 | */
|
---|
235 | public JButton getRedoButton() {
|
---|
236 | return redo_button;
|
---|
237 | }
|
---|
238 |
|
---|
239 | /** Retrieves the undo button for this edit pane.
|
---|
240 | * @return A JButton.
|
---|
241 | */
|
---|
242 | public JButton getUndoButton() {
|
---|
243 | return undo_button;
|
---|
244 | }
|
---|
245 |
|
---|
246 | /** This method is called by any window manager to determine if this
|
---|
247 | * particular component is focusable.
|
---|
248 | * @return A Boolean specifying if this component is focus traversable.
|
---|
249 | * @deprecated
|
---|
250 | */
|
---|
251 | /** public boolean isFocusTraversable() {
|
---|
252 | return focusable;
|
---|
253 | }
|
---|
254 | */
|
---|
255 |
|
---|
256 | /** Loads the contents of the specified file into the document.
|
---|
257 | * @param file The target File.
|
---|
258 | */
|
---|
259 | public void loadFile(File file) {
|
---|
260 | // Load the file.
|
---|
261 | Gatherer.println("GEditorPane.loadFile():Opening config file " + file.getAbsolutePath());
|
---|
262 | try {
|
---|
263 | FileInputStream in = new FileInputStream(file);
|
---|
264 | int character = -1;
|
---|
265 | String content = "";
|
---|
266 | while((character = in.read()) != -1) {
|
---|
267 | content = content + (char)character;
|
---|
268 | }
|
---|
269 | in.close();
|
---|
270 | this.file = file;
|
---|
271 | this.setText(content);
|
---|
272 | }
|
---|
273 | catch (Exception error) {
|
---|
274 | //error.printStackTrace();
|
---|
275 | Gatherer.println("GEditorPane.loadFile():Error: No Such File.");
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | /** Any implementation of Undoable must include this method so we can
|
---|
280 | * be informed when an undoable event has occured.
|
---|
281 | * @param event An UndoableEditEvent.
|
---|
282 | */
|
---|
283 | public void undoableEditHappened(UndoableEditEvent event) {
|
---|
284 | if(undo != null) {
|
---|
285 | undo.addEdit(event.getEdit());
|
---|
286 | // Update controls to reflect change.
|
---|
287 | redo_button.setEnabled(undo.canRedo());
|
---|
288 | undo_button.setEnabled(undo.canUndo());
|
---|
289 | }
|
---|
290 | }
|
---|
291 | }
|
---|