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

Last change on this file since 12110 was 12110, checked in by mdewsnip, 18 years ago

Tidied up the borders around the macros textarea.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 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 java.io.File;
35import java.io.FileInputStream;
36import java.io.FileOutputStream;
37import java.io.BufferedReader;
38import java.io.BufferedWriter;
39import java.io.InputStreamReader;
40import java.io.OutputStreamWriter;
41
42import org.greenstone.gatherer.collection.CollectionManager;
43import org.greenstone.gatherer.Configuration;
44import org.greenstone.gatherer.Dictionary;
45import org.greenstone.gatherer.gui.DesignPaneHeader;
46import org.greenstone.gatherer.gui.GLIButton;
47
48public class MacrosManager
49 extends JPanel {
50
51 /** The controls used to modify the general options. */
52 private Control controls;
53 /** Constructor. */
54 public MacrosManager() {
55 }
56
57
58 /** Destructor. */
59 public void destroy() {
60 if (controls != null) {
61 controls.destroy();
62 controls = null;
63 }
64 }
65
66 public void loseFocus() {
67 }
68
69 public void gainFocus() {
70
71 }
72 /** This class is resposible for generating the controls for the editing of general options.
73 * @return the Control for editing the general options
74 */
75 public Control getControls() {
76 if (controls == null) {
77 controls = new MacrosControl();
78 }
79 return controls;
80 }
81
82
83 /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden
84 * @param mode the new mode as an int
85 */
86 public void modeChanged(int mode) {
87
88 }
89
90
91 private class MacrosControl
92 extends JPanel
93 implements Control {
94
95 JTextArea macros_textarea = null;
96 JButton save_button = null;
97 JButton revert_button = null;
98
99 public MacrosControl() {
100 super();
101
102 JPanel header_pane = new DesignPaneHeader("CDM.GUI.Macros", "collectionspecificmacros");
103
104 JPanel main_pane = new JPanel();
105 main_pane.setLayout(new BorderLayout());
106 macros_textarea = new JTextArea();
107 macros_textarea.setBackground(Configuration.getColor("coloring.editable_background", false));
108 macros_textarea.setCaretPosition(0);
109 macros_textarea.setLineWrap(false);
110 macros_textarea.setWrapStyleWord(false);
111 Dictionary.registerTooltip(macros_textarea, "CDM.MacrosManager.Editor_Tooltip");
112
113 JPanel macros_pane = new JPanel();
114 macros_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
115 macros_pane.setLayout(new BorderLayout());
116 macros_pane.add(new JScrollPane(macros_textarea), BorderLayout.CENTER);
117
118 save_button = new GLIButton();
119 Dictionary.registerBoth(save_button, "CDM.MacrosManager.Save", "CDM.MacrosManager.Save_Tooltip");
120 revert_button = new GLIButton();
121 Dictionary.registerBoth(revert_button, "CDM.MacrosManager.Revert", "CDM.MacrosManager.Revert_Tooltip");
122
123 save_button.addActionListener(new SaveListener());
124 revert_button.addActionListener(new RevertListener());
125 JPanel button_pane = new JPanel();
126 button_pane.setLayout(new GridLayout(1,2));
127 button_pane.add(save_button);
128 button_pane.add(revert_button);
129
130 main_pane.add(macros_pane, BorderLayout.CENTER);
131 main_pane.add(button_pane, BorderLayout.SOUTH);
132
133 setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
134 setLayout(new BorderLayout());
135 add(header_pane, BorderLayout.NORTH);
136 add(main_pane, BorderLayout.CENTER);
137 }
138
139 public void destroy() {
140 }
141
142 public void loseFocus() {
143 }
144
145 public void gainFocus() {
146 readMacroFile();
147 }
148
149 private void readMacroFile() {
150 macros_textarea.setText("");
151 File extra_dm = new File(CollectionManager.getCollectionDirectoryPath()+"macros"+File.separator+"extra.dm");
152
153 if (extra_dm.exists()) {
154 try {
155 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(extra_dm), "UTF-8"));
156
157 String line;
158 while ((line = br.readLine()) != null) {
159 macros_textarea.append(line+"\n");
160 }
161 } catch (Exception e) {
162 System.err.println("Exception happened, "+e.getMessage());
163 }
164 }
165 }
166
167 private void writeMacroFile() {
168 File extra_dm = new File(CollectionManager.getCollectionDirectoryPath()+"macros"+File.separator+"extra.dm");
169 try {
170 if (!extra_dm.exists()) {
171 File parent_dir = extra_dm.getParentFile();
172 parent_dir.mkdirs();
173 extra_dm.createNewFile();
174 }
175
176 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(extra_dm), "UTF-8"));
177 String text = macros_textarea.getText();
178 out.write(text, 0, text.length());
179 out.newLine();
180 out.flush();
181 out.close();
182 } catch (Exception e) {
183
184 }
185 }
186 private class SaveListener
187 implements ActionListener {
188
189 public void actionPerformed(ActionEvent event) {
190 writeMacroFile();
191 }
192 }
193
194 private class RevertListener
195 implements ActionListener {
196
197 public void actionPerformed(ActionEvent event) {
198 readMacroFile();
199 }
200 }
201 }
202
203}
Note: See TracBrowser for help on using the repository browser.