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

Last change on this file since 12139 was 12125, checked in by kjdon, 18 years ago

don't reload the macro file on gainFocus

  • 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 macros_textarea.setToolTipText(Dictionary.get("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(Dictionary.get("CDM.MacrosManager.Save"), Dictionary.get("CDM.MacrosManager.Save_Tooltip"));
119 revert_button = new GLIButton(Dictionary.get("CDM.MacrosManager.Revert"), Dictionary.get("CDM.MacrosManager.Revert_Tooltip"));
120
121 save_button.addActionListener(new SaveListener());
122 revert_button.addActionListener(new RevertListener());
123 JPanel button_pane = new JPanel();
124 button_pane.setLayout(new GridLayout(1,2));
125 button_pane.add(save_button);
126 button_pane.add(revert_button);
127
128 main_pane.add(macros_pane, BorderLayout.CENTER);
129 main_pane.add(button_pane, BorderLayout.SOUTH);
130
131 setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
132 setLayout(new BorderLayout());
133 add(header_pane, BorderLayout.NORTH);
134 add(main_pane, BorderLayout.CENTER);
135 readMacroFile();
136 }
137
138 public void destroy() {
139 }
140
141 public void loseFocus() {
142 }
143
144 public void gainFocus() {
145 //readMacroFile();
146 }
147
148 private void readMacroFile() {
149 macros_textarea.setText("");
150 File extra_dm = new File(CollectionManager.getCollectionDirectoryPath()+"macros"+File.separator+"extra.dm");
151
152 if (extra_dm.exists()) {
153 try {
154 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(extra_dm), "UTF-8"));
155
156 String line;
157 while ((line = br.readLine()) != null) {
158 macros_textarea.append(line+"\n");
159 }
160 } catch (Exception e) {
161 System.err.println("Exception happened, "+e.getMessage());
162 }
163 }
164 }
165
166 private void writeMacroFile() {
167 File extra_dm = new File(CollectionManager.getCollectionDirectoryPath()+"macros"+File.separator+"extra.dm");
168 try {
169 if (!extra_dm.exists()) {
170 File parent_dir = extra_dm.getParentFile();
171 parent_dir.mkdirs();
172 extra_dm.createNewFile();
173 }
174
175 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(extra_dm), "UTF-8"));
176 String text = macros_textarea.getText();
177 out.write(text, 0, text.length());
178 out.newLine();
179 out.flush();
180 out.close();
181 } catch (Exception e) {
182
183 }
184 }
185 private class SaveListener
186 implements ActionListener {
187
188 public void actionPerformed(ActionEvent event) {
189 writeMacroFile();
190 }
191 }
192
193 private class RevertListener
194 implements ActionListener {
195
196 public void actionPerformed(ActionEvent event) {
197 readMacroFile();
198 }
199 }
200 }
201
202}
Note: See TracBrowser for help on using the repository browser.