source: main/trunk/gli/src/org/greenstone/gatherer/cdm/DatabaseTypeManager.java@ 22970

Last change on this file since 22970 was 22970, checked in by sjm84, 14 years ago

Added the ability to change the database type between GDBM, JDBM and SQLite

File size: 10.3 KB
Line 
1package org.greenstone.gatherer.cdm;
2
3import java.awt.*;
4import java.awt.event.*;
5import javax.swing.*;
6import java.util.EventListener;
7import javax.swing.event.EventListenerList;
8
9import org.greenstone.gatherer.Configuration;
10import org.greenstone.gatherer.Dictionary;
11import org.greenstone.gatherer.Gatherer;
12import org.greenstone.gatherer.gui.GLIButton;
13import org.greenstone.gatherer.gui.ModalDialog;
14
15public class DatabaseTypeManager {
16
17 /** The size of this new collection dialog box. */
18 static private Dimension DIALOG_SIZE = new Dimension(600, 280);
19
20 static final public String DATABASE_TYPE_GDBM = "gdbm";
21 static final public String DATABASE_TYPE_JDBM = "jdbm";
22 static final public String DATABASE_TYPE_SQLITE = "sqlite";
23
24 static final public String DATABASE_TYPE_GDBM_STR = "GDBM";
25 static final public String DATABASE_TYPE_JDBM_STR = "JDBM";
26 static final public String DATABASE_TYPE_SQLITE_STR = "SQLITE";
27
28 static final public String[] DATABASE_TYPES = { DATABASE_TYPE_GDBM, DATABASE_TYPE_JDBM, DATABASE_TYPE_SQLITE };
29
30 private EventListenerList listeners = null;
31 /** the databasetype element in the config file - uses CollectionMeta */
32 public CollectionMeta database_type_meta = null;
33 private Control controls = null;
34
35 protected DatabaseTypeManager manager = null;
36 public DatabaseTypeManager() {
37 database_type_meta = new CollectionMeta(CollectionDesignManager.collect_config.getDatabaseType());
38 if (getDatabaseType().equals("")) {
39 database_type_meta.setValue(DATABASE_TYPE_GDBM);
40 // must have an old collection, assume MG
41 }
42 listeners = new EventListenerList();
43 manager = this;
44 }
45
46 public void addDatabaseTypeListener(DatabaseTypeListener listener) {
47 listeners.add(DatabaseTypeListener.class, listener);
48 }
49
50 protected void notifyListeners(String new_database_type) {
51 Object[] concerned = listeners.getListenerList();
52 for(int i = 0; i < concerned.length ; i++) {
53 if(concerned[i] == DatabaseTypeListener.class) {
54 ((DatabaseTypeListener)concerned[i+1]).databaseTypeChanged(new_database_type);
55 }
56 }
57 concerned = null;
58 }
59
60
61 public void promptForNewDatabaseType() {
62
63 DatabaseTypePrompt dtp = new DatabaseTypePrompt(database_type_meta.getValue(CollectionMeta.TEXT));
64 }
65
66 public boolean isGDBM () {
67
68 return getDatabaseType().equals(DATABASE_TYPE_GDBM);
69 }
70 public boolean isJDBM () {
71
72 return getDatabaseType().equals(DATABASE_TYPE_JDBM);
73 }
74 public boolean isSQLITE () {
75
76 return getDatabaseType().equals(DATABASE_TYPE_SQLITE);
77 }
78
79
80 public String getDatabaseType() {
81 return database_type_meta.getValue(CollectionMeta.TEXT);
82 }
83
84 public Control getControls() {
85 if (controls == null) {
86 controls = new DatabaseTypeControl();
87 }
88 return controls;
89 }
90
91 public interface DatabaseTypeListener
92 extends EventListener {
93 public void databaseTypeChanged(String new_database_type);
94 }
95
96 private class DatabaseTypeControl
97 extends JPanel
98 implements Control, DatabaseTypeListener {
99
100 JLabel label = null;
101 JButton change_button = null;
102
103 public DatabaseTypeControl() {
104 super();
105 this.setComponentOrientation(Dictionary.getOrientation());
106
107 JPanel spacer_panel = new JPanel();
108 spacer_panel.setComponentOrientation(Dictionary.getOrientation());
109
110 JPanel main_panel = new JPanel();
111 main_panel.setComponentOrientation(Dictionary.getOrientation());
112 /* may be CDM.DatabaseTypeManager.gdbm, CDM.DatabaseTypeManager.jdbm, CDM.DatabaseTypeManager.sqlite */
113 label = new JLabel(Dictionary.get("CDM.DatabaseTypeManager.Current_Type", getDatabaseTypeString(getDatabaseType())));
114 label.setComponentOrientation(Dictionary.getOrientation());
115
116 change_button = new GLIButton(Dictionary.get("CDM.DatabaseTypeManager.Change"), Dictionary.get("CDM.DatabaseTypeManager.Change_Tooltip"));
117
118 change_button.addActionListener(new ActionListener() {
119 public void actionPerformed(ActionEvent event) {
120 promptForNewDatabaseType();
121 }
122 });
123
124 main_panel.setLayout(new BorderLayout(10,10));
125 main_panel.add(label, BorderLayout.CENTER);
126 main_panel.add(change_button, BorderLayout.LINE_END);
127
128 setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
129 setLayout(new BorderLayout());
130 add(spacer_panel, BorderLayout.CENTER);
131 add(main_panel, BorderLayout.LINE_END);
132
133 manager.addDatabaseTypeListener(this);
134 }
135
136 public void loseFocus() {}
137 public void gainFocus() {}
138 public void destroy() {}
139
140 private String getDatabaseTypeString(String database_type) {
141 if (database_type.equals(DATABASE_TYPE_GDBM)) {
142 return DATABASE_TYPE_GDBM_STR;
143 }
144 if (database_type.equals(DATABASE_TYPE_JDBM)) {
145 return DATABASE_TYPE_JDBM_STR;
146 }
147 if (database_type.equals(DATABASE_TYPE_SQLITE)) {
148 return DATABASE_TYPE_SQLITE_STR;
149 }
150 return "";
151 }
152
153
154 public void databaseTypeChanged(String new_database_type) {
155 label.setText(Dictionary.get("CDM.DatabaseTypeManager.Current_Type", getDatabaseTypeString(new_database_type)));
156 }
157 }
158
159 private class DatabaseTypePrompt
160 extends ModalDialog {
161
162 private JDialog self;
163
164 private JRadioButton gdbm_button = null;
165 private JRadioButton jdbm_button = null;
166 private JRadioButton sqlite_button = null;
167
168 private JTextArea description_textarea = null;
169
170 JButton ok_button = null;
171 JButton cancel_button = null;
172
173 public DatabaseTypePrompt(String current_database_type) {
174 super(Gatherer.g_man, true);
175 this.self = this;
176 setSize(DIALOG_SIZE);
177 setTitle(Dictionary.get("CDM.DatabaseTypeManager.Title"));
178 this.setComponentOrientation(Dictionary.getOrientation());
179 gdbm_button = new JRadioButton(DATABASE_TYPE_GDBM_STR);
180 gdbm_button.setComponentOrientation(Dictionary.getOrientation());
181 gdbm_button.setActionCommand(DATABASE_TYPE_GDBM);
182 jdbm_button = new JRadioButton(DATABASE_TYPE_JDBM_STR);
183 jdbm_button.setComponentOrientation(Dictionary.getOrientation());
184 jdbm_button.setActionCommand(DATABASE_TYPE_JDBM);
185 sqlite_button = new JRadioButton(DATABASE_TYPE_SQLITE_STR);
186 sqlite_button.setComponentOrientation(Dictionary.getOrientation());
187 sqlite_button.setActionCommand(DATABASE_TYPE_SQLITE);
188
189 DatabaseTypeButtonListener dtbl = new DatabaseTypeButtonListener();
190 gdbm_button.addActionListener(dtbl);
191 jdbm_button.addActionListener(dtbl);
192 sqlite_button.addActionListener(dtbl);
193
194 ButtonGroup database_type_group = new ButtonGroup();
195 database_type_group.add(gdbm_button);
196 database_type_group.add(jdbm_button);
197 database_type_group.add(sqlite_button);
198
199 if (current_database_type != null) {
200 if (current_database_type.equals(DATABASE_TYPE_GDBM)) {
201 gdbm_button.setSelected(true);
202 } else if (current_database_type.equals(DATABASE_TYPE_JDBM)) {
203 jdbm_button.setSelected(true);
204 } else if (current_database_type.equals(DATABASE_TYPE_SQLITE)) {
205 sqlite_button.setSelected(true);
206 }
207 }
208
209 JPanel radio_pane = new JPanel();
210 radio_pane.setLayout(new GridLayout(3,1));
211 radio_pane.add(gdbm_button);
212 radio_pane.add(jdbm_button);
213 radio_pane.add(sqlite_button);
214 radio_pane.setComponentOrientation(Dictionary.getOrientation());
215
216 description_textarea = new JTextArea();
217 description_textarea.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
218 /* may be CDM.DatabaseTypeManager.gdbm_Description, CDM.DatabaseTypeManager.jdbm_Description, CDM.DatabaseTypeManager.sqlite_Description */
219 description_textarea.setText(Dictionary.get("CDM.DatabaseTypeManager."+current_database_type+"_Description"));
220 description_textarea.setLineWrap(true);
221 description_textarea.setWrapStyleWord(true);
222 description_textarea.setComponentOrientation(Dictionary.getOrientation());
223
224 cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel_Tooltip"));
225
226 cancel_button.addActionListener(new ActionListener() {
227 public void actionPerformed(ActionEvent event) {
228 self.dispose();
229 }
230 });
231
232 ok_button = new GLIButton(Dictionary.get("General.OK"), Dictionary.get("General.OK_Tooltip"));
233
234 ok_button.addActionListener(new ActionListener() {
235 public void actionPerformed(ActionEvent event) {
236 String new_database_type = DATABASE_TYPE_GDBM;
237 if (gdbm_button.isSelected()) {
238 new_database_type = DATABASE_TYPE_GDBM;
239 } else if (jdbm_button.isSelected()) {
240 new_database_type = DATABASE_TYPE_JDBM;
241 } else if (sqlite_button.isSelected()) {
242 new_database_type = DATABASE_TYPE_SQLITE;
243 }
244 if (!database_type_meta.getValue(CollectionMeta.TEXT).equals(new_database_type)) {
245 manager.notifyListeners(new_database_type);
246 database_type_meta.setValue(new_database_type);
247 }
248 self.dispose();
249 }
250 });
251 // tell the CDM that we have (possibly) changed
252 ok_button.addActionListener(CollectionDesignManager.databasecol_change_listener);
253 JPanel button_pane = new JPanel();
254 button_pane.setLayout(new GridLayout(1,2));
255 button_pane.add(ok_button);
256 button_pane.add(cancel_button);
257 button_pane.setComponentOrientation(Dictionary.getOrientation());
258
259 JPanel content_pane = (JPanel) getContentPane();
260 content_pane.setOpaque(true);
261 content_pane.setLayout(new BorderLayout());
262 content_pane.add(radio_pane, BorderLayout.NORTH);
263 content_pane.add(new JScrollPane(description_textarea), BorderLayout.CENTER);
264 content_pane.add(button_pane, BorderLayout.SOUTH);
265 content_pane.setComponentOrientation(Dictionary.getOrientation());
266
267 // Center and display.
268 Dimension screen_size = Configuration.screen_size;
269 this.setLocation((screen_size.width - DIALOG_SIZE.width) / 2, (screen_size.height - DIALOG_SIZE.height) / 2);
270 this.setVisible(true); // blocks until the dialog is killed
271
272 }
273
274 public void loseFocus() {
275
276 }
277 public void gainFocus() {
278
279 }
280 public void destroy() {
281
282 }
283
284 private class DatabaseTypeButtonListener
285 implements ActionListener {
286
287 public void actionPerformed(ActionEvent event) {
288 description_textarea.setText(Dictionary.get("CDM.DatabaseTypeManager."+event.getActionCommand()+"_Description"));
289 }
290 }
291 }
292}
Note: See TracBrowser for help on using the repository browser.