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