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.DebugStream;
|
---|
46 | import org.greenstone.gatherer.Dictionary;
|
---|
47 | import org.greenstone.gatherer.Gatherer;
|
---|
48 | import org.greenstone.gatherer.gui.DesignPaneHeader;
|
---|
49 | import org.greenstone.gatherer.gui.GLIButton;
|
---|
50 | import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
|
---|
51 |
|
---|
52 |
|
---|
53 | public class MacrosManager {
|
---|
54 |
|
---|
55 | /** The controls used to modify the general options. */
|
---|
56 | private Control controls;
|
---|
57 | /** Constructor. */
|
---|
58 | public MacrosManager() {
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | /** Destructor. */
|
---|
63 | public void destroy() {
|
---|
64 | if (controls != null) {
|
---|
65 | controls.destroy();
|
---|
66 | controls = null;
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public void loseFocus() {
|
---|
71 | }
|
---|
72 |
|
---|
73 | public void gainFocus() {
|
---|
74 |
|
---|
75 | }
|
---|
76 | /** This class is resposible for generating the controls for the editing of general options.
|
---|
77 | * @return the Control for editing the general options
|
---|
78 | */
|
---|
79 | public Control getControls() {
|
---|
80 | if (controls == null) {
|
---|
81 | controls = new MacrosControl();
|
---|
82 | }
|
---|
83 | return controls;
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden
|
---|
88 | * @param mode the new mode as an int
|
---|
89 | */
|
---|
90 | public void modeChanged(int mode) {
|
---|
91 |
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | private class MacrosControl
|
---|
96 | extends JPanel
|
---|
97 | implements Control, WindowFocusListener {
|
---|
98 |
|
---|
99 | JTextArea macros_textarea = null;
|
---|
100 | JButton undo_button = null;
|
---|
101 | JButton redo_button = null;
|
---|
102 | private final UndoManager undo = new UndoManager();
|
---|
103 | private boolean macros_changed = false;
|
---|
104 | public MacrosControl() {
|
---|
105 | super();
|
---|
106 |
|
---|
107 | JPanel header_pane = new DesignPaneHeader("CDM.GUI.Macros", "collectionspecificmacros");
|
---|
108 |
|
---|
109 | JPanel main_pane = new JPanel();
|
---|
110 | main_pane.setLayout(new BorderLayout());
|
---|
111 | macros_textarea = new JTextArea();
|
---|
112 | macros_textarea.setBackground(Configuration.getColor("coloring.editable_background", false));
|
---|
113 | macros_textarea.setLineWrap(false);
|
---|
114 | macros_textarea.setWrapStyleWord(false);
|
---|
115 | macros_textarea.setToolTipText(Dictionary.get("CDM.MacrosManager.Editor_Tooltip"));
|
---|
116 | readMacroFile(); // load in the original contents
|
---|
117 | macros_textarea.getDocument().addDocumentListener(new EditorListener());
|
---|
118 |
|
---|
119 | // Listen for undo and redo events
|
---|
120 | macros_textarea.getDocument().addUndoableEditListener(new UndoableEditListener() {
|
---|
121 | public void undoableEditHappened(UndoableEditEvent evt) {
|
---|
122 | undo.addEdit(evt.getEdit());
|
---|
123 | }
|
---|
124 | });
|
---|
125 | macros_textarea.setCaretPosition(0);
|
---|
126 |
|
---|
127 | JPanel macros_pane = new JPanel();
|
---|
128 | macros_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
|
---|
129 | macros_pane.setLayout(new BorderLayout());
|
---|
130 | macros_pane.add(new JScrollPane(macros_textarea), BorderLayout.CENTER);
|
---|
131 | undo_button = new GLIButton(Dictionary.get("General.Undo"), Dictionary.get("General.Undo_Tooltip"));
|
---|
132 | undo_button.setEnabled(false);
|
---|
133 | undo_button.addActionListener(new UndoListener());
|
---|
134 |
|
---|
135 | redo_button = new GLIButton(Dictionary.get("General.Redo"), Dictionary.get("General.Redo_Tooltip"));
|
---|
136 | redo_button.setEnabled(false);
|
---|
137 | redo_button.addActionListener(new RedoListener());
|
---|
138 |
|
---|
139 | JPanel button_pane = new JPanel();
|
---|
140 | button_pane.setLayout(new GridLayout(1,2));
|
---|
141 | button_pane.add(undo_button);
|
---|
142 | button_pane.add(redo_button);
|
---|
143 |
|
---|
144 | main_pane.add(macros_pane, BorderLayout.CENTER);
|
---|
145 | main_pane.add(button_pane, BorderLayout.SOUTH);
|
---|
146 |
|
---|
147 | setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
|
---|
148 | setLayout(new BorderLayout());
|
---|
149 | add(header_pane, BorderLayout.NORTH);
|
---|
150 | add(main_pane, BorderLayout.CENTER);
|
---|
151 | Gatherer.g_man.addWindowFocusListener(this);
|
---|
152 | }
|
---|
153 |
|
---|
154 | public void destroy() {
|
---|
155 | }
|
---|
156 |
|
---|
157 | public void loseFocus() {
|
---|
158 | if (macros_changed) {
|
---|
159 | writeMacroFile();
|
---|
160 | macros_changed = false;
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | public void gainFocus() {
|
---|
165 | macros_textarea.grabFocus();
|
---|
166 | }
|
---|
167 |
|
---|
168 | public void windowGainedFocus(WindowEvent e) {
|
---|
169 | }
|
---|
170 |
|
---|
171 | public void windowLostFocus(WindowEvent e) {
|
---|
172 | if (macros_changed) {
|
---|
173 | writeMacroFile();
|
---|
174 | macros_changed = false;
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | private void readMacroFile() {
|
---|
179 | macros_textarea.setText("");
|
---|
180 | File extra_dm = new File(CollectionManager.getCollectionDirectoryPath()+"macros"+File.separator+"extra.dm");
|
---|
181 |
|
---|
182 | if (extra_dm.exists()) {
|
---|
183 | try {
|
---|
184 | BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(extra_dm), "UTF-8"));
|
---|
185 |
|
---|
186 | String line;
|
---|
187 | while ((line = br.readLine()) != null) {
|
---|
188 | macros_textarea.append(line+"\n");
|
---|
189 | }
|
---|
190 |
|
---|
191 | br.close();
|
---|
192 | }
|
---|
193 | catch (Exception exception) {
|
---|
194 | DebugStream.printStackTrace(exception);
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | private void writeMacroFile() {
|
---|
200 | File extra_dm_file = new File(CollectionManager.getCollectionDirectoryPath()+"macros"+File.separator+"extra.dm");
|
---|
201 | try {
|
---|
202 | if (!extra_dm_file.exists()) {
|
---|
203 | File parent_dir = extra_dm_file.getParentFile();
|
---|
204 | parent_dir.mkdirs();
|
---|
205 | extra_dm_file.createNewFile();
|
---|
206 | }
|
---|
207 |
|
---|
208 | BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(extra_dm_file), "UTF-8"));
|
---|
209 | String text = macros_textarea.getText();
|
---|
210 | out.write(text, 0, text.length());
|
---|
211 | out.newLine();
|
---|
212 | out.flush();
|
---|
213 | out.close();
|
---|
214 |
|
---|
215 | // If we're using a remote Greenstone server, upload the new extra.dm file
|
---|
216 | // This will return immediately, since we're on the event dispatch thread
|
---|
217 | // Users need to wait until the upload has finished before pressing Preview Collection!
|
---|
218 | if (Gatherer.isGsdlRemote) {
|
---|
219 | String collection_name = Gatherer.c_man.getCollection().getName();
|
---|
220 | RemoteGreenstoneServer.uploadCollectionFile(collection_name, extra_dm_file);
|
---|
221 | }
|
---|
222 | }
|
---|
223 | catch (Exception exception) {
|
---|
224 | DebugStream.printStackTrace(exception);
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | private class EditorListener
|
---|
229 | implements DocumentListener {
|
---|
230 | public void changedUpdate(DocumentEvent e) {
|
---|
231 | macros_changed = true;
|
---|
232 | undo_button.setEnabled(true);
|
---|
233 | }
|
---|
234 |
|
---|
235 | public void insertUpdate(DocumentEvent e) {
|
---|
236 | macros_changed = true;
|
---|
237 | undo_button.setEnabled(true);
|
---|
238 | }
|
---|
239 |
|
---|
240 | public void removeUpdate(DocumentEvent e) {
|
---|
241 | macros_changed = true;
|
---|
242 | undo_button.setEnabled(true);
|
---|
243 | }
|
---|
244 |
|
---|
245 | }
|
---|
246 |
|
---|
247 | private class UndoListener
|
---|
248 | implements ActionListener {
|
---|
249 |
|
---|
250 | public void actionPerformed(ActionEvent event) {
|
---|
251 | try {
|
---|
252 | if (undo.canUndo()) {
|
---|
253 | int pos = macros_textarea.getCaretPosition();
|
---|
254 | undo.undo();
|
---|
255 | macros_textarea.setCaretPosition(pos-1);
|
---|
256 | macros_textarea.grabFocus();
|
---|
257 | redo_button.setEnabled(true);
|
---|
258 |
|
---|
259 | }
|
---|
260 | if (!undo.canUndo()){
|
---|
261 | undo_button.setEnabled(false);
|
---|
262 | }
|
---|
263 | else{
|
---|
264 | undo_button.setEnabled(true);
|
---|
265 | }
|
---|
266 | }
|
---|
267 | catch (Exception exception) {
|
---|
268 | DebugStream.printStackTrace(exception);
|
---|
269 | }
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | private class RedoListener
|
---|
275 | implements ActionListener {
|
---|
276 |
|
---|
277 | public void actionPerformed(ActionEvent evt) {
|
---|
278 | try {
|
---|
279 | if (undo.canRedo()) {
|
---|
280 | int pos = macros_textarea.getCaretPosition();
|
---|
281 | undo.redo();
|
---|
282 | macros_textarea.setCaretPosition(pos+1);
|
---|
283 | macros_textarea.grabFocus();
|
---|
284 | }
|
---|
285 | if (!undo.canRedo()){
|
---|
286 | redo_button.setEnabled(false);
|
---|
287 | }
|
---|
288 | else{
|
---|
289 | redo_button.setEnabled(true);
|
---|
290 | }
|
---|
291 | }
|
---|
292 | catch (Exception exception) {
|
---|
293 | DebugStream.printStackTrace(exception);
|
---|
294 | }
|
---|
295 | }
|
---|
296 | }
|
---|
297 | }
|
---|
298 | }
|
---|