source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/cdm/MacrosManager.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 9.1 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 * 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 */
27package org.greenstone.gatherer.cdm;
28
29import java.awt.*;
30import java.awt.event.*;
31import java.util.*;
32import javax.swing.*;
33import javax.swing.event.*;
34import javax.swing.undo.*;
35import java.io.File;
36import java.io.FileInputStream;
37import java.io.FileOutputStream;
38import java.io.BufferedReader;
39import java.io.BufferedWriter;
40import java.io.InputStreamReader;
41import java.io.OutputStreamWriter;
42
43import org.greenstone.gatherer.Configuration;
44import org.greenstone.gatherer.DebugStream;
45import org.greenstone.gatherer.Dictionary;
46import org.greenstone.gatherer.Gatherer;
47import org.greenstone.gatherer.collection.CollectionManager;
48import org.greenstone.gatherer.gui.DesignPaneHeader;
49import org.greenstone.gatherer.gui.GLIButton;
50import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
51
52
53public 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 this.setComponentOrientation(Dictionary.getOrientation());
107
108 JPanel header_pane = new DesignPaneHeader("CDM.GUI.Macros", "collectionspecificmacros");
109 JPanel main_pane = new JPanel();
110 main_pane.setComponentOrientation(Dictionary.getOrientation());
111 main_pane.setLayout(new BorderLayout());
112 macros_textarea = new JTextArea();
113 macros_textarea.setComponentOrientation(Dictionary.getOrientation());
114 macros_textarea.setBackground(Configuration.getColor("coloring.editable_background", false));
115 macros_textarea.setLineWrap(false);
116 macros_textarea.setWrapStyleWord(false);
117 macros_textarea.setToolTipText(Dictionary.get("CDM.MacrosManager.Editor_Tooltip"));
118 readMacroFile(); // load in the original contents
119 macros_textarea.getDocument().addDocumentListener(new EditorListener());
120
121 // Listen for undo and redo events
122 macros_textarea.getDocument().addUndoableEditListener(new UndoableEditListener() {
123 public void undoableEditHappened(UndoableEditEvent evt) {
124 undo.addEdit(evt.getEdit());
125 }
126 });
127 macros_textarea.setCaretPosition(0);
128
129 JPanel macros_pane = new JPanel();
130 macros_pane.setComponentOrientation(Dictionary.getOrientation());
131 macros_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
132 macros_pane.setLayout(new BorderLayout());
133 macros_pane.add(new JScrollPane(macros_textarea), BorderLayout.CENTER);
134 undo_button = new GLIButton(Dictionary.get("General.Undo"), Dictionary.get("General.Undo_Tooltip"));
135 undo_button.setEnabled(false);
136 undo_button.addActionListener(new UndoListener());
137
138 redo_button = new GLIButton(Dictionary.get("General.Redo"), Dictionary.get("General.Redo_Tooltip"));
139 redo_button.setEnabled(false);
140 redo_button.addActionListener(new RedoListener());
141
142 JPanel button_pane = new JPanel();
143 button_pane.setComponentOrientation(Dictionary.getOrientation());
144 button_pane.setLayout(new GridLayout(1,2));
145 button_pane.add(undo_button);
146 button_pane.add(redo_button);
147
148 main_pane.add(macros_pane, BorderLayout.CENTER);
149 main_pane.add(button_pane, BorderLayout.SOUTH);
150
151 setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
152 setLayout(new BorderLayout());
153 add(header_pane, BorderLayout.NORTH);
154 add(main_pane, BorderLayout.CENTER);
155 Gatherer.g_man.addWindowFocusListener(this);
156 }
157
158 public void destroy() {
159 }
160
161 public void loseFocus() {
162 if (macros_changed) {
163 writeMacroFile();
164 macros_changed = false;
165 }
166 }
167
168 public void gainFocus() {
169 macros_textarea.grabFocus();
170 }
171
172 public void windowGainedFocus(WindowEvent e) {
173 }
174
175 public void windowLostFocus(WindowEvent e) {
176 if (macros_changed) {
177 writeMacroFile();
178 macros_changed = false;
179 }
180 }
181
182 private void readMacroFile() {
183 macros_textarea.setText("");
184 File extra_dm = new File(CollectionManager.getLoadedCollectionMacrosDirectoryPath() + "extra.dm");
185
186 if (extra_dm.exists()) {
187 try {
188 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(extra_dm), "UTF-8"));
189
190 String line;
191 while ((line = br.readLine()) != null) {
192 macros_textarea.append(line+"\n");
193 }
194
195 br.close();
196 }
197 catch (Exception exception) {
198 DebugStream.printStackTrace(exception);
199 }
200 }
201 }
202
203 private void writeMacroFile() {
204 File extra_dm_file = new File(CollectionManager.getLoadedCollectionMacrosDirectoryPath() + "extra.dm");
205 try {
206 if (!extra_dm_file.exists()) {
207 File parent_dir = extra_dm_file.getParentFile();
208 parent_dir.mkdirs();
209 extra_dm_file.createNewFile();
210 }
211
212 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(extra_dm_file), "UTF-8"));
213 String text = macros_textarea.getText();
214 out.write(text, 0, text.length());
215 out.newLine();
216 out.flush();
217 out.close();
218
219 // If we're using a remote Greenstone server, upload the new extra.dm file
220 // This will return immediately, since we're on the event dispatch thread
221 // Users need to wait until the upload has finished before pressing Preview Collection!
222 if (Gatherer.isGsdlRemote) {
223 Gatherer.remoteGreenstoneServer.uploadCollectionFile(CollectionManager.getLoadedCollectionName(), extra_dm_file);
224 }
225 }
226 catch (Exception exception) {
227 DebugStream.printStackTrace(exception);
228 }
229 }
230
231 private class EditorListener
232 implements DocumentListener {
233 public void changedUpdate(DocumentEvent e) {
234 macros_changed = true;
235 undo_button.setEnabled(true);
236 }
237
238 public void insertUpdate(DocumentEvent e) {
239 macros_changed = true;
240 undo_button.setEnabled(true);
241 }
242
243 public void removeUpdate(DocumentEvent e) {
244 macros_changed = true;
245 undo_button.setEnabled(true);
246 }
247
248 }
249
250 private class UndoListener
251 implements ActionListener {
252
253 public void actionPerformed(ActionEvent event) {
254 try {
255 if (undo.canUndo()) {
256 int pos = macros_textarea.getCaretPosition();
257 undo.undo();
258 macros_textarea.setCaretPosition(pos-1);
259 macros_textarea.grabFocus();
260 redo_button.setEnabled(true);
261
262 }
263 if (!undo.canUndo()){
264 undo_button.setEnabled(false);
265 }
266 else{
267 undo_button.setEnabled(true);
268 }
269 }
270 catch (Exception exception) {
271 DebugStream.printStackTrace(exception);
272 }
273 }
274 }
275
276
277 private class RedoListener
278 implements ActionListener {
279
280 public void actionPerformed(ActionEvent evt) {
281 try {
282 if (undo.canRedo()) {
283 int pos = macros_textarea.getCaretPosition();
284 undo.redo();
285 macros_textarea.setCaretPosition(pos+1);
286 macros_textarea.grabFocus();
287 }
288 if (!undo.canRedo()){
289 redo_button.setEnabled(false);
290 }
291 else{
292 redo_button.setEnabled(true);
293 }
294 }
295 catch (Exception exception) {
296 DebugStream.printStackTrace(exception);
297 }
298 }
299 }
300 }
301}
Note: See TracBrowser for help on using the repository browser.