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 | * Author: Katherine Don, Greenstone Digital Library, University of Waikato
|
---|
9 | *
|
---|
10 | * Copyright (C) 2006 New Zealand Digital Library Project
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or modify
|
---|
13 | * it under the terms of the GNU General Public License as published by
|
---|
14 | * the Free Software Foundation; either version 2 of the License, or
|
---|
15 | * (at your option) any later version.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful,
|
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | * GNU General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, write to the Free Software
|
---|
24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
25 | *########################################################################
|
---|
26 | */
|
---|
27 | package org.greenstone.gatherer.cdm;
|
---|
28 |
|
---|
29 | import java.awt.*;
|
---|
30 | import java.awt.event.*;
|
---|
31 | import java.util.*;
|
---|
32 | import javax.swing.*;
|
---|
33 | import javax.swing.event.*;
|
---|
34 | import javax.swing.undo.*;
|
---|
35 | import java.io.File;
|
---|
36 | import java.io.FileInputStream;
|
---|
37 | import java.io.FileOutputStream;
|
---|
38 | import java.io.BufferedReader;
|
---|
39 | import java.io.BufferedWriter;
|
---|
40 | import java.io.InputStreamReader;
|
---|
41 | import java.io.OutputStreamWriter;
|
---|
42 |
|
---|
43 | import org.greenstone.gatherer.collection.CollectionManager;
|
---|
44 | import org.greenstone.gatherer.Configuration;
|
---|
45 | import org.greenstone.gatherer.Dictionary;
|
---|
46 | import org.greenstone.gatherer.Gatherer;
|
---|
47 | import org.greenstone.gatherer.gui.DesignPaneHeader;
|
---|
48 | import org.greenstone.gatherer.gui.GLIButton;
|
---|
49 |
|
---|
50 | public class MacrosManager {
|
---|
51 |
|
---|
52 | /** The controls used to modify the general options. */
|
---|
53 | private Control controls;
|
---|
54 | /** Constructor. */
|
---|
55 | public MacrosManager() {
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | /** Destructor. */
|
---|
60 | public void destroy() {
|
---|
61 | if (controls != null) {
|
---|
62 | controls.destroy();
|
---|
63 | controls = null;
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public void loseFocus() {
|
---|
68 | }
|
---|
69 |
|
---|
70 | public void gainFocus() {
|
---|
71 |
|
---|
72 | }
|
---|
73 | /** This class is resposible for generating the controls for the editing of general options.
|
---|
74 | * @return the Control for editing the general options
|
---|
75 | */
|
---|
76 | public Control getControls() {
|
---|
77 | if (controls == null) {
|
---|
78 | controls = new MacrosControl();
|
---|
79 | }
|
---|
80 | return controls;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden
|
---|
85 | * @param mode the new mode as an int
|
---|
86 | */
|
---|
87 | public void modeChanged(int mode) {
|
---|
88 |
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | private class MacrosControl
|
---|
93 | extends JPanel
|
---|
94 | implements Control, WindowFocusListener {
|
---|
95 |
|
---|
96 | JTextArea macros_textarea = null;
|
---|
97 | JButton undo_button = null;
|
---|
98 | JButton redo_button = null;
|
---|
99 | private final UndoManager undo = new UndoManager();
|
---|
100 | private boolean macros_changed = false;
|
---|
101 | public MacrosControl() {
|
---|
102 | super();
|
---|
103 |
|
---|
104 | JPanel header_pane = new DesignPaneHeader("CDM.GUI.Macros", "collectionspecificmacros");
|
---|
105 |
|
---|
106 | JPanel main_pane = new JPanel();
|
---|
107 | main_pane.setLayout(new BorderLayout());
|
---|
108 | macros_textarea = new JTextArea();
|
---|
109 | macros_textarea.setBackground(Configuration.getColor("coloring.editable_background", false));
|
---|
110 | macros_textarea.setLineWrap(false);
|
---|
111 | macros_textarea.setWrapStyleWord(false);
|
---|
112 | macros_textarea.setToolTipText(Dictionary.get("CDM.MacrosManager.Editor_Tooltip"));
|
---|
113 | readMacroFile(); // load in the original contents
|
---|
114 | macros_textarea.getDocument().addDocumentListener(new EditorListener());
|
---|
115 |
|
---|
116 | // Listen for undo and redo events
|
---|
117 | macros_textarea.getDocument().addUndoableEditListener(new UndoableEditListener() {
|
---|
118 | public void undoableEditHappened(UndoableEditEvent evt) {
|
---|
119 | undo.addEdit(evt.getEdit());
|
---|
120 | }
|
---|
121 | });
|
---|
122 | macros_textarea.setCaretPosition(0);
|
---|
123 |
|
---|
124 | JPanel macros_pane = new JPanel();
|
---|
125 | macros_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
|
---|
126 | macros_pane.setLayout(new BorderLayout());
|
---|
127 | macros_pane.add(new JScrollPane(macros_textarea), BorderLayout.CENTER);
|
---|
128 | undo_button = new GLIButton(Dictionary.get("General.Undo"), Dictionary.get("General.Undo_Tooltip"));
|
---|
129 | undo_button.setEnabled(false);
|
---|
130 | undo_button.addActionListener(new UndoListener());
|
---|
131 |
|
---|
132 | redo_button = new GLIButton(Dictionary.get("General.Redo"), Dictionary.get("General.Redo_Tooltip"));
|
---|
133 | redo_button.setEnabled(false);
|
---|
134 | redo_button.addActionListener(new RedoListener());
|
---|
135 |
|
---|
136 | JPanel button_pane = new JPanel();
|
---|
137 | button_pane.setLayout(new GridLayout(1,2));
|
---|
138 | button_pane.add(undo_button);
|
---|
139 | button_pane.add(redo_button);
|
---|
140 |
|
---|
141 | main_pane.add(macros_pane, BorderLayout.CENTER);
|
---|
142 | main_pane.add(button_pane, BorderLayout.SOUTH);
|
---|
143 |
|
---|
144 | setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
|
---|
145 | setLayout(new BorderLayout());
|
---|
146 | add(header_pane, BorderLayout.NORTH);
|
---|
147 | add(main_pane, BorderLayout.CENTER);
|
---|
148 | Gatherer.g_man.addWindowFocusListener(this);
|
---|
149 | }
|
---|
150 |
|
---|
151 | public void destroy() {
|
---|
152 | }
|
---|
153 |
|
---|
154 | public void loseFocus() {
|
---|
155 | if (macros_changed) {
|
---|
156 | writeMacroFile();
|
---|
157 | macros_changed = false;
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | public void gainFocus() {
|
---|
162 | macros_textarea.grabFocus();
|
---|
163 | }
|
---|
164 |
|
---|
165 | public void windowGainedFocus(WindowEvent e) {
|
---|
166 | }
|
---|
167 |
|
---|
168 | public void windowLostFocus(WindowEvent e) {
|
---|
169 | if (macros_changed) {
|
---|
170 | writeMacroFile();
|
---|
171 | macros_changed = false;
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | private void readMacroFile() {
|
---|
176 | macros_textarea.setText("");
|
---|
177 | File extra_dm = new File(CollectionManager.getCollectionDirectoryPath()+"macros"+File.separator+"extra.dm");
|
---|
178 |
|
---|
179 | if (extra_dm.exists()) {
|
---|
180 | try {
|
---|
181 | BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(extra_dm), "UTF-8"));
|
---|
182 |
|
---|
183 | String line;
|
---|
184 | while ((line = br.readLine()) != null) {
|
---|
185 | macros_textarea.append(line+"\n");
|
---|
186 | }
|
---|
187 |
|
---|
188 | br.close();
|
---|
189 | } catch (Exception e) {
|
---|
190 | System.err.println("MacrosManager Exception: "+e.getMessage());
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | private void writeMacroFile() {
|
---|
196 | File extra_dm = new File(CollectionManager.getCollectionDirectoryPath()+"macros"+File.separator+"extra.dm");
|
---|
197 | try {
|
---|
198 | if (!extra_dm.exists()) {
|
---|
199 | File parent_dir = extra_dm.getParentFile();
|
---|
200 | parent_dir.mkdirs();
|
---|
201 | extra_dm.createNewFile();
|
---|
202 | }
|
---|
203 |
|
---|
204 | BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(extra_dm), "UTF-8"));
|
---|
205 | String text = macros_textarea.getText();
|
---|
206 | out.write(text, 0, text.length());
|
---|
207 | out.newLine();
|
---|
208 | out.flush();
|
---|
209 | out.close();
|
---|
210 | } catch (Exception e) {
|
---|
211 | System.err.println("MacrosManager Exception: "+e.getMessage());
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | private class EditorListener
|
---|
216 | implements DocumentListener {
|
---|
217 | public void changedUpdate(DocumentEvent e) {
|
---|
218 | macros_changed = true;
|
---|
219 | undo_button.setEnabled(true);
|
---|
220 | }
|
---|
221 |
|
---|
222 | public void insertUpdate(DocumentEvent e) {
|
---|
223 | macros_changed = true;
|
---|
224 | undo_button.setEnabled(true);
|
---|
225 | }
|
---|
226 |
|
---|
227 | public void removeUpdate(DocumentEvent e) {
|
---|
228 | macros_changed = true;
|
---|
229 | undo_button.setEnabled(true);
|
---|
230 | }
|
---|
231 |
|
---|
232 | }
|
---|
233 |
|
---|
234 | private class UndoListener
|
---|
235 | implements ActionListener {
|
---|
236 |
|
---|
237 | public void actionPerformed(ActionEvent event) {
|
---|
238 | try {
|
---|
239 | if (undo.canUndo()) {
|
---|
240 | int pos = macros_textarea.getCaretPosition();
|
---|
241 | undo.undo();
|
---|
242 | macros_textarea.setCaretPosition(pos-1);
|
---|
243 | macros_textarea.grabFocus();
|
---|
244 | redo_button.setEnabled(true);
|
---|
245 |
|
---|
246 | }
|
---|
247 | if (!undo.canUndo()){
|
---|
248 | undo_button.setEnabled(false);
|
---|
249 | }
|
---|
250 | else{
|
---|
251 | undo_button.setEnabled(true);
|
---|
252 | }
|
---|
253 |
|
---|
254 | } catch (Exception e) {
|
---|
255 | System.err.println("MacrosManager Exception: "+e.getMessage());
|
---|
256 | }
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | private class RedoListener
|
---|
261 | implements ActionListener {
|
---|
262 |
|
---|
263 | public void actionPerformed(ActionEvent evt) {
|
---|
264 | try {
|
---|
265 | if (undo.canRedo()) {
|
---|
266 | int pos = macros_textarea.getCaretPosition();
|
---|
267 | undo.redo();
|
---|
268 | macros_textarea.setCaretPosition(pos+1);
|
---|
269 | macros_textarea.grabFocus();
|
---|
270 | }
|
---|
271 | if (!undo.canRedo()){
|
---|
272 | redo_button.setEnabled(false);
|
---|
273 | }
|
---|
274 | else{
|
---|
275 | redo_button.setEnabled(true);
|
---|
276 | }
|
---|
277 |
|
---|
278 | } catch (Exception e) {
|
---|
279 | System.err.println("MacrosManager Exception: "+e.getMessage());
|
---|
280 | }
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 |
|
---|
288 | }
|
---|