source: trunk/gli/src/org/greenstone/gatherer/cdm/MacrosManager.java@ 13586

Last change on this file since 13586 was 13586, checked in by mdewsnip, 17 years ago

Added a static getLoadedCollectionName() function into CollectionManager, as part of making CollectionManager fully static.

  • Property svn:keywords set to Author Date Id Revision
File size: 8.7 KB
RevLine 
[12077]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.*;
[12367]34import javax.swing.undo.*;
[12077]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;
[12651]44import org.greenstone.gatherer.DebugStream;
[12077]45import org.greenstone.gatherer.Dictionary;
[12367]46import org.greenstone.gatherer.Gatherer;
[13586]47import org.greenstone.gatherer.collection.CollectionManager;
[12077]48import org.greenstone.gatherer.gui.DesignPaneHeader;
49import org.greenstone.gatherer.gui.GLIButton;
[12652]50import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
[12077]51
[12651]52
[12367]53public class MacrosManager {
[12077]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
[12367]97 implements Control, WindowFocusListener {
[12077]98
99 JTextArea macros_textarea = null;
[12367]100 JButton undo_button = null;
101 JButton redo_button = null;
102 private final UndoManager undo = new UndoManager();
103 private boolean macros_changed = false;
[12077]104 public MacrosControl() {
105 super();
106
[12104]107 JPanel header_pane = new DesignPaneHeader("CDM.GUI.Macros", "collectionspecificmacros");
[12077]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);
[12123]115 macros_textarea.setToolTipText(Dictionary.get("CDM.MacrosManager.Editor_Tooltip"));
[12367]116 readMacroFile(); // load in the original contents
117 macros_textarea.getDocument().addDocumentListener(new EditorListener());
[12077]118
[12367]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 });
[12368]125 macros_textarea.setCaretPosition(0);
126
[12110]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);
[12367]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());
[12123]138
[12077]139 JPanel button_pane = new JPanel();
140 button_pane.setLayout(new GridLayout(1,2));
[12367]141 button_pane.add(undo_button);
142 button_pane.add(redo_button);
[12077]143
[12110]144 main_pane.add(macros_pane, BorderLayout.CENTER);
[12077]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);
[12367]151 Gatherer.g_man.addWindowFocusListener(this);
[12110]152 }
[12077]153
[12110]154 public void destroy() {
[12077]155 }
156
157 public void loseFocus() {
[12367]158 if (macros_changed) {
159 writeMacroFile();
160 macros_changed = false;
161 }
[12077]162 }
163
164 public void gainFocus() {
[12368]165 macros_textarea.grabFocus();
[12077]166 }
[12367]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 }
[12077]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 }
[12298]190
191 br.close();
[12077]192 }
[12651]193 catch (Exception exception) {
194 DebugStream.printStackTrace(exception);
195 }
[12077]196 }
197 }
198
199 private void writeMacroFile() {
[12651]200 File extra_dm_file = new File(CollectionManager.getCollectionDirectoryPath()+"macros"+File.separator+"extra.dm");
[12077]201 try {
[12651]202 if (!extra_dm_file.exists()) {
203 File parent_dir = extra_dm_file.getParentFile();
[12077]204 parent_dir.mkdirs();
[12651]205 extra_dm_file.createNewFile();
[12077]206 }
207
[12651]208 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(extra_dm_file), "UTF-8"));
[12077]209 String text = macros_textarea.getText();
210 out.write(text, 0, text.length());
211 out.newLine();
212 out.flush();
213 out.close();
[12652]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) {
[13586]219 RemoteGreenstoneServer.uploadCollectionFile(CollectionManager.getLoadedCollectionName(), extra_dm_file);
[12652]220 }
[12077]221 }
[12651]222 catch (Exception exception) {
223 DebugStream.printStackTrace(exception);
224 }
[12077]225 }
226
[12367]227 private class EditorListener
228 implements DocumentListener {
229 public void changedUpdate(DocumentEvent e) {
230 macros_changed = true;
231 undo_button.setEnabled(true);
[12077]232 }
[12367]233
234 public void insertUpdate(DocumentEvent e) {
235 macros_changed = true;
236 undo_button.setEnabled(true);
237 }
238
239 public void removeUpdate(DocumentEvent e) {
240 macros_changed = true;
241 undo_button.setEnabled(true);
242 }
243
[12077]244 }
[12367]245
246 private class UndoListener
[12077]247 implements ActionListener {
[12367]248
[12077]249 public void actionPerformed(ActionEvent event) {
[12367]250 try {
251 if (undo.canUndo()) {
252 int pos = macros_textarea.getCaretPosition();
253 undo.undo();
254 macros_textarea.setCaretPosition(pos-1);
255 macros_textarea.grabFocus();
256 redo_button.setEnabled(true);
257
258 }
259 if (!undo.canUndo()){
260 undo_button.setEnabled(false);
261 }
262 else{
263 undo_button.setEnabled(true);
264 }
[12651]265 }
266 catch (Exception exception) {
267 DebugStream.printStackTrace(exception);
[12367]268 }
[12077]269 }
270 }
[12367]271
[12651]272
273 private class RedoListener
274 implements ActionListener {
[12367]275
[12651]276 public void actionPerformed(ActionEvent evt) {
277 try {
278 if (undo.canRedo()) {
279 int pos = macros_textarea.getCaretPosition();
280 undo.redo();
281 macros_textarea.setCaretPosition(pos+1);
282 macros_textarea.grabFocus();
283 }
284 if (!undo.canRedo()){
285 redo_button.setEnabled(false);
286 }
287 else{
288 redo_button.setEnabled(true);
289 }
290 }
291 catch (Exception exception) {
292 DebugStream.printStackTrace(exception);
293 }
294 }
295 }
[12077]296 }
297}
Note: See TracBrowser for help on using the repository browser.