[22970] | 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 |
|
---|
[25776] | 15 | public class DatabaseTypeManager
|
---|
| 16 | {
|
---|
[22970] | 17 |
|
---|
[25776] | 18 | /** The size of this new collection dialog box. */
|
---|
| 19 | static private Dimension DIALOG_SIZE = new Dimension(600, 280);
|
---|
[22970] | 20 |
|
---|
[25776] | 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";
|
---|
[22970] | 24 |
|
---|
[25776] | 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";
|
---|
[22970] | 28 |
|
---|
[25776] | 29 | static final public String[] DATABASE_TYPES = { DATABASE_TYPE_GDBM, DATABASE_TYPE_JDBM, DATABASE_TYPE_SQLITE };
|
---|
[22970] | 30 |
|
---|
[25776] | 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;
|
---|
[22970] | 48 | }
|
---|
| 49 |
|
---|
[25776] | 50 | public void addDatabaseTypeListener(DatabaseTypeListener listener)
|
---|
| 51 | {
|
---|
| 52 | listeners.add(DatabaseTypeListener.class, listener);
|
---|
[22970] | 53 | }
|
---|
| 54 |
|
---|
[25776] | 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 | }
|
---|
[22970] | 67 |
|
---|
[25776] | 68 | public void promptForNewDatabaseType()
|
---|
| 69 | {
|
---|
[22970] | 70 |
|
---|
[25776] | 71 | DatabaseTypePrompt dtp = new DatabaseTypePrompt(database_type_meta.getValue(CollectionMeta.TEXT));
|
---|
| 72 | }
|
---|
[22970] | 73 |
|
---|
[25776] | 74 | public boolean isGDBM()
|
---|
| 75 | {
|
---|
[22970] | 76 |
|
---|
[25776] | 77 | return getDatabaseType().equals(DATABASE_TYPE_GDBM);
|
---|
| 78 | }
|
---|
[22970] | 79 |
|
---|
[25776] | 80 | public boolean isJDBM()
|
---|
| 81 | {
|
---|
[22970] | 82 |
|
---|
[25776] | 83 | return getDatabaseType().equals(DATABASE_TYPE_JDBM);
|
---|
[22970] | 84 | }
|
---|
| 85 |
|
---|
[25776] | 86 | public boolean isSQLITE()
|
---|
| 87 | {
|
---|
[22970] | 88 |
|
---|
[25776] | 89 | return getDatabaseType().equals(DATABASE_TYPE_SQLITE);
|
---|
| 90 | }
|
---|
[22970] | 91 |
|
---|
[25776] | 92 | public String getDatabaseType()
|
---|
| 93 | {
|
---|
| 94 | return database_type_meta.getValue(CollectionMeta.TEXT);
|
---|
| 95 | }
|
---|
[22970] | 96 |
|
---|
[25776] | 97 | public Control getControls()
|
---|
| 98 | {
|
---|
| 99 | if (controls == null)
|
---|
| 100 | {
|
---|
| 101 | controls = new DatabaseTypeControl();
|
---|
| 102 | }
|
---|
| 103 | return controls;
|
---|
| 104 | }
|
---|
[22970] | 105 |
|
---|
[25776] | 106 | public interface DatabaseTypeListener extends EventListener
|
---|
| 107 | {
|
---|
| 108 | public void databaseTypeChanged(String new_database_type);
|
---|
[22970] | 109 | }
|
---|
| 110 |
|
---|
[25776] | 111 | private class DatabaseTypeControl extends JPanel implements Control, DatabaseTypeListener
|
---|
| 112 | {
|
---|
[22970] | 113 |
|
---|
[25776] | 114 | JLabel label = null;
|
---|
| 115 | JButton change_button = null;
|
---|
[22970] | 116 |
|
---|
[25776] | 117 | public DatabaseTypeControl()
|
---|
| 118 | {
|
---|
| 119 | super();
|
---|
| 120 | this.setComponentOrientation(Dictionary.getOrientation());
|
---|
[22970] | 121 |
|
---|
[25776] | 122 | JPanel spacer_panel = new JPanel();
|
---|
| 123 | spacer_panel.setComponentOrientation(Dictionary.getOrientation());
|
---|
[22970] | 124 |
|
---|
[25776] | 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;
|
---|
[22970] | 173 | }
|
---|
[25776] | 174 | if (database_type.equals(DATABASE_TYPE_JDBM))
|
---|
| 175 | {
|
---|
| 176 | return DATABASE_TYPE_JDBM_STR;
|
---|
[22970] | 177 | }
|
---|
[25776] | 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 | }
|
---|
[22970] | 189 | }
|
---|
| 190 |
|
---|
[25776] | 191 | private class DatabaseTypePrompt extends ModalDialog
|
---|
| 192 | {
|
---|
[22970] | 193 |
|
---|
[25776] | 194 | private JDialog self;
|
---|
[22970] | 195 |
|
---|
[25776] | 196 | private JRadioButton gdbm_button = null;
|
---|
| 197 | private JRadioButton jdbm_button = null;
|
---|
| 198 | private JRadioButton sqlite_button = null;
|
---|
[22970] | 199 |
|
---|
[25776] | 200 | private JTextArea description_textarea = null;
|
---|
[22970] | 201 |
|
---|
[25776] | 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);
|
---|
[25777] | 230 | if (!Gatherer.GS3)
|
---|
| 231 | {
|
---|
| 232 | database_type_group.add(sqlite_button);
|
---|
| 233 | }
|
---|
[25776] | 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 | }
|
---|
[25777] | 244 | else if (current_database_type.equals(DATABASE_TYPE_SQLITE) && !Gatherer.GS3)
|
---|
[25776] | 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);
|
---|
[25777] | 254 | if (!Gatherer.GS3)
|
---|
| 255 | {
|
---|
| 256 | radio_pane.add(sqlite_button);
|
---|
| 257 | }
|
---|
[25776] | 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 | }
|
---|
[22970] | 355 | }
|
---|
| 356 | }
|
---|