source: gli/trunk/src/org/greenstone/gatherer/cdm/IndexOptionManager.java@ 17114

Last change on this file since 17114 was 17114, checked in by kjdon, 16 years ago

added separate_cjk option to index options. added tooltips to all options

  • Property svn:keywords set to Author Date Id Revision
File size: 16.6 KB
Line 
1package org.greenstone.gatherer.cdm;
2
3import java.awt.*;
4import java.awt.event.*;
5import java.util.*;
6import javax.swing.*;
7import javax.swing.event.*;
8import org.greenstone.gatherer.Configuration;
9import org.greenstone.gatherer.DebugStream;
10import org.greenstone.gatherer.Dictionary;
11import org.greenstone.gatherer.Gatherer;
12import org.greenstone.gatherer.util.JarTools;
13import org.greenstone.gatherer.util.StaticStrings;
14import org.w3c.dom.*;
15
16
17public class IndexOptionManager
18 implements BuildTypeManager.BuildTypeListener {
19
20 // we have an IndexOptionList (DOMProxyListModel) for each option type
21 private IndexOptionList level_model = null;
22 private IndexOptionList stem_model = null;
23
24 /** the default level option - for mgpp and lucene */
25 private IndexOption default_level = null;
26
27 private String build_type = null;
28 private Control controls = null;
29
30 public IndexOptionManager(String current_build_type) {
31 level_model = new IndexOptionList(CollectionDesignManager.collect_config.getLevels());
32 stem_model = new IndexOptionList(CollectionDesignManager.collect_config.getStemOptions());
33
34 DebugStream.println("IndexOptionManager: " + level_model.getSize() + " levels parsed.");
35 DebugStream.println("IndexOptionManager: " + stem_model.getSize() + " stem options parsed.");
36
37 build_type = current_build_type;
38 if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
39 level_model.setAssigned(false);
40 }
41 else if (build_type.equals(BuildTypeManager.BUILD_TYPE_LUCENE)) {
42 stem_model.setAssigned(false);
43 }
44 // Parse and retrieve the default level
45 Element default_level_element = CollectionDesignManager.collect_config.getLevelDefault();
46 default_level = new IndexOption(default_level_element);
47 if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
48 default_level.setAssigned(false);
49 }
50
51 }
52
53
54 public void buildTypeChanged(String new_build_type) {
55 boolean levels_enabled = true;
56 boolean stem_enabled = true;
57 if (new_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
58 levels_enabled = false;
59 } else if (new_build_type.equals(BuildTypeManager.BUILD_TYPE_LUCENE)) {
60 stem_enabled = false;
61 }
62 level_model.setAssigned(levels_enabled);
63 default_level.setAssigned(levels_enabled);
64 stem_model.setAssigned(stem_enabled);
65 ((IndexOptionControl)controls).buildTypeChanged(new_build_type);
66 build_type = new_build_type;
67 }
68
69 /** Method to acquire the controls for editing the indexes.
70 * @return the Control
71 */
72 public Control getControls() {
73 if(controls == null) {
74 // Build controls
75 controls = new IndexOptionControl();
76 }
77 return controls;
78 }
79
80 public int getNumLevels() {
81 return level_model.getSize();
82 }
83 public ArrayList getLevels() {
84 return level_model.children();
85 }
86
87 private void addLevel(String level) {
88 if (level_model.getOption(level) == null) {
89 int position = 0;
90
91 if (level.equals(StaticStrings.SECTION_STR) && level_model.getSize()==1) {
92 position = 1;
93 }
94 level_model.add(position, new IndexOption(level));
95 // add in a default collection meta
96 CollectionMeta meta = new CollectionMeta(StaticStrings.STOP_CHARACTER + level);
97 meta.setAssigned(true);
98 meta.setValue(level);
99 CollectionDesignManager.collectionmeta_manager.addMetadatum(meta);
100 }
101 }
102
103 private IndexOption getLevel(String level) {
104 return level_model.getOption(level);
105 }
106
107 private void removeLevel(String level_name) {
108 // Remove any current metadata from this level
109 CollectionDesignManager.collectionmeta_manager.removeMetadata(StaticStrings.STOP_CHARACTER + level_name);
110 // Remove the level
111 level_model.removeOption(level_name);
112 }
113
114 /** Method to set the default level
115 * @param level the new default level
116 */
117 private void setDefaultLevel(String level) {
118 if(level != null) {
119 default_level.setValue(level);
120 if (!build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
121 default_level.setAssigned(true);
122 }
123 } else {
124 default_level.setAssigned(false);
125 }
126 }
127
128 private class IndexOptionControl
129 extends JPanel
130 implements Control {
131
132 public StemmingControl stem_control = null;
133 public LevelControl level_control = null;
134
135 public IndexOptionControl() {
136
137 stem_control = new StemmingControl();
138 level_control = new LevelControl();
139
140 setLayout(new BorderLayout());
141 if (!build_type.equals(BuildTypeManager.BUILD_TYPE_LUCENE)) {
142 add(stem_control, BorderLayout.NORTH);
143 }
144 if (!build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
145 add(level_control, BorderLayout.CENTER);
146 }
147 }
148
149 public void destroy() {}
150 public void gainFocus() {}
151 public void loseFocus() {}
152
153 public void buildTypeChanged(String new_build_type)
154 {
155 if (build_type.equals(new_build_type)) {
156 // shouldn't happen
157 return;
158 }
159 // tell our sub parts about the change in build type
160 stem_control.buildTypeChanged(new_build_type);
161 level_control.buildTypeChanged(new_build_type);
162
163 // then display/hide as appropriate
164 if (build_type.equals(BuildTypeManager.BUILD_TYPE_LUCENE)) {
165 // Changing to MG/MGPP
166 add(stem_control, BorderLayout.NORTH);
167 }
168 else if (new_build_type.equals(BuildTypeManager.BUILD_TYPE_LUCENE)) {
169 // Changing to Lucene
170 remove(stem_control);
171 }
172 if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
173 // changing from MG
174 add(level_control, BorderLayout.CENTER);
175 }
176 else if (new_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
177 // changing to MG
178 remove(level_control);
179 }
180
181 }
182
183 }
184
185 private class StemmingControl
186 extends JPanel
187 implements BuildTypeManager.BuildTypeListener
188 {
189 private JCheckBox stem_checkbox = new JCheckBox();
190 private JCheckBox casefold_checkbox = new JCheckBox();
191 private JCheckBox accentfold_checkbox = new JCheckBox();
192 private JCheckBox separate_cjk_checkbox =new JCheckBox();
193
194 public StemmingControl()
195 {
196 setLayout(new BorderLayout(10,10));
197 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
198
199 stem_checkbox.setText(Dictionary.get("CDM.IndexingManager.Stem"));
200 stem_checkbox.setToolTipText(Dictionary.get("CDM.IndexingManager.Stem_Tooltip"));
201 stem_checkbox.setActionCommand(StaticStrings.STEM_OPTION_STR);
202
203 casefold_checkbox.setText(Dictionary.get("CDM.IndexingManager.Casefold"));
204 casefold_checkbox.setToolTipText(Dictionary.get("CDM.IndexingManager.Casefold_Tooltip"));
205 casefold_checkbox.setActionCommand(StaticStrings.CASEFOLD_OPTION_STR);
206
207 accentfold_checkbox.setText(Dictionary.get("CDM.IndexingManager.Accent_fold"));
208 accentfold_checkbox.setToolTipText(Dictionary.get("CDM.IndexingManager.Accent_fold_Tooltip"));
209 accentfold_checkbox.setActionCommand(StaticStrings.ACCENTFOLD_OPTION_STR);
210 // Accent-folding is currently not available for MG
211 accentfold_checkbox.setEnabled(!build_type.equals(BuildTypeManager.BUILD_TYPE_MG));
212
213 separate_cjk_checkbox.setText(Dictionary.get("CDM.IndexingManager.Separate_cjk"));
214 separate_cjk_checkbox.setToolTipText(Dictionary.get("CDM.IndexingManager.Separate_cjk_Tooltip"));
215 separate_cjk_checkbox.setActionCommand(StaticStrings.SEPARATE_CJK_OPTION_STR);
216
217 JPanel checkbox_panel = new JPanel();
218 checkbox_panel.setLayout(new GridLayout(2, 1));
219
220 JPanel stem_panel = new JPanel();
221 stem_panel.setLayout(new GridLayout(1,3));
222 stem_panel.add(stem_checkbox);
223 stem_panel.add(casefold_checkbox);
224 stem_panel.add(accentfold_checkbox);
225
226 JPanel other_panel = new JPanel();
227 other_panel.setLayout(new GridLayout(1,1));
228 other_panel.add(separate_cjk_checkbox);
229
230 checkbox_panel.add(stem_panel);
231 checkbox_panel.add(other_panel);
232
233 add(new JLabel(Dictionary.get("CDM.IndexingManager.Options")), BorderLayout.WEST);
234 add(checkbox_panel, BorderLayout.CENTER);
235
236 // set up the checked/unchecked boxes
237 if (stem_model.getSize()==0) {
238 // select all
239 stem_checkbox.setSelected(true);
240 stem_model.addOption(StaticStrings.STEM_OPTION_STR);
241 casefold_checkbox.setSelected(true);
242 stem_model.addOption(StaticStrings.CASEFOLD_OPTION_STR);
243 if (accentfold_checkbox.isEnabled()) {
244 accentfold_checkbox.setSelected(true);
245 stem_model.addOption(StaticStrings.ACCENTFOLD_OPTION_STR);
246 }
247 separate_cjk_checkbox.setSelected(false);
248 }
249 else {
250 if (stem_model.getOption(StaticStrings.STEM_OPTION_STR)!=null) {
251 stem_checkbox.setSelected(true);
252 }
253 if (stem_model.getOption(StaticStrings.CASEFOLD_OPTION_STR)!=null) {
254 casefold_checkbox.setSelected(true);
255 }
256 if (stem_model.getOption(StaticStrings.ACCENTFOLD_OPTION_STR)!=null) {
257 accentfold_checkbox.setSelected(true);
258 }
259 if (stem_model.getOption(StaticStrings.SEPARATE_CJK_OPTION_STR) != null) {
260 separate_cjk_checkbox.setSelected(true);
261 }
262
263
264 }
265
266 CheckBoxListener cbl = new CheckBoxListener();
267 stem_checkbox.addActionListener(cbl);
268 casefold_checkbox.addActionListener(cbl);
269 accentfold_checkbox.addActionListener(cbl);
270 separate_cjk_checkbox.addActionListener(cbl);
271
272 // changing stem indexes changes build settings
273 stem_checkbox.addActionListener(CollectionDesignManager.buildcol_change_listener);
274 casefold_checkbox.addActionListener(CollectionDesignManager.buildcol_change_listener);
275 accentfold_checkbox.addActionListener(CollectionDesignManager.buildcol_change_listener);
276 separate_cjk_checkbox.addActionListener(CollectionDesignManager.buildcol_change_listener);
277
278 }
279
280 public void buildTypeChanged(String new_build_type) {
281 // Accent-folding is currently not available for MG
282 if (new_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
283 //changing to MG
284 accentfold_checkbox.setSelected(false);
285 accentfold_checkbox.setEnabled(false);
286 if (accentfold_checkbox.isSelected()) {
287 stem_model.removeOption(StaticStrings.ACCENTFOLD_OPTION_STR);
288 }
289 }
290 else if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
291 //Changing from MG
292 accentfold_checkbox.setEnabled(true);
293 if (accentfold_checkbox.isSelected()) {
294 stem_model.addOption(StaticStrings.ACCENTFOLD_OPTION_STR);
295 }
296 }
297
298 }
299
300 private class CheckBoxListener
301 implements ActionListener {
302 public void actionPerformed(ActionEvent event) {
303 if (!((JCheckBox)event.getSource()).isSelected()) {
304 // deselecting
305 stem_model.removeOption(event.getActionCommand());
306 }
307 else {
308 // selecting a new one
309 stem_model.addOption(event.getActionCommand());
310 }
311 }
312 }
313 }
314
315
316
317 /** This class creates a set of controls for editing the levels. */
318 private class LevelControl
319 extends JPanel
320 implements BuildTypeManager.BuildTypeListener
321 {
322
323 JCheckBox document_checkbox = null;
324 JCheckBox section_checkbox = null;
325 JRadioButton document_default_radio = null;
326 JRadioButton section_default_radio = null;
327
328 public LevelControl() {
329
330 JPanel checkbox_panel = new JPanel();
331 JPanel default_panel = new JPanel();
332 document_checkbox = new JCheckBox();
333 document_checkbox.setText(Dictionary.get("CDM.LevelManager.Document"));
334 document_default_radio = new JRadioButton();
335 document_default_radio.setBackground(Configuration.getColor("coloring.collection_tree_background", false));
336 document_default_radio.setOpaque(false);
337 document_default_radio.setActionCommand(StaticStrings.DOCUMENT_STR);
338 document_default_radio.setIcon(JarTools.getImage("radio_unselected.gif"));
339 document_default_radio.setSelectedIcon(JarTools.getImage("radio_selected.gif"));
340
341 section_checkbox = new JCheckBox();
342 section_checkbox.setText(Dictionary.get("CDM.LevelManager.Section"));
343 section_default_radio = new JRadioButton();
344 section_default_radio.setOpaque(true);
345 section_default_radio.setBackground(Configuration.getColor("coloring.collection_tree_background", false));
346 section_default_radio.setOpaque(false);
347 section_default_radio.setIcon(JarTools.getImage("radio_unselected.gif"));
348 section_default_radio.setSelectedIcon(JarTools.getImage("radio_selected.gif"));
349
350 section_default_radio.setActionCommand(StaticStrings.SECTION_STR);
351 ButtonGroup default_group = new ButtonGroup();
352 default_group.add(document_default_radio);
353 default_group.add(section_default_radio);
354
355
356
357 if (level_model.getSize()==0) {
358 // no levels yet, set document
359 document_checkbox.setSelected(true);
360 addLevel(StaticStrings.DOCUMENT_STR);
361 } else {
362 if (getLevel(StaticStrings.DOCUMENT_STR)!= null) {
363 document_checkbox.setSelected(true);
364 }
365 if (getLevel(StaticStrings.SECTION_STR) != null) {
366 section_checkbox.setSelected(true);
367 }
368 }
369
370 if (default_level.isAssigned()) {
371 // set the default based on specified default
372 if (default_level.getValue().equals(StaticStrings.DOCUMENT_STR) && document_checkbox.isSelected()) {
373 document_default_radio.setSelected(true);
374 }
375 else if (default_level.getValue().equals(StaticStrings.SECTION_STR) && section_checkbox.isSelected()) {
376 section_default_radio.setSelected(true);
377 } else {
378 default_level.setAssigned(false);
379 }
380 }
381 // if have no valid specified default
382 if (!default_level.isAssigned()) {
383 String default_l = StaticStrings.DOCUMENT_STR;
384 if (document_checkbox.isSelected()) {
385 document_default_radio.setSelected(true);
386 default_l = StaticStrings.DOCUMENT_STR;
387 } else if (section_checkbox.isSelected()) {
388 section_default_radio.setSelected(true);
389 default_l = StaticStrings.SECTION_STR;
390 }
391 setDefaultLevel(default_l);
392 }
393
394 validateControls();
395 JLabel checkbox_label = new JLabel(Dictionary.get("CDM.LevelManager.Level_Title"));
396 checkbox_panel.setLayout(new GridLayout(3,1));
397 checkbox_panel.setToolTipText(Dictionary.get("CDM.IndexManager.Level_Tooltip"));
398 checkbox_panel.add(checkbox_label);
399 checkbox_panel.add(document_checkbox);
400 checkbox_panel.add(section_checkbox);
401
402 JLabel default_label = new JLabel(Dictionary.get("CDM.LevelManager.Default"));
403 default_panel.setLayout(new GridLayout(3,1));
404 default_panel.setToolTipText(Dictionary.get("CDM.LevelManager.Default_Tooltip"));
405 default_panel.add(default_label);
406 default_panel.add(document_default_radio);
407 default_panel.add(section_default_radio);
408
409
410 CheckBoxListener cbl = new CheckBoxListener();
411 document_checkbox.addActionListener(cbl);
412 section_checkbox.addActionListener(cbl);
413
414 // changing selected levels changes build settings
415 document_checkbox.addActionListener(CollectionDesignManager.buildcol_change_listener);
416 section_checkbox.addActionListener(CollectionDesignManager.buildcol_change_listener);
417
418 RadioListener rl = new RadioListener();
419 document_default_radio.addActionListener(rl);
420 section_default_radio.addActionListener(rl);
421 // changing the default doesn't need a rebuild
422
423 setLayout(new BorderLayout(10,10));
424 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
425 add(checkbox_panel, BorderLayout.WEST);
426 add(default_panel, BorderLayout.CENTER);
427 } // LevelControl
428
429 public void buildTypeChanged(String new_build_type) {};
430 public void gainFocus() {
431
432 }
433
434 public void loseFocus() {
435
436 }
437
438 public void destroy() {
439 }
440
441 private class RadioListener
442 implements ActionListener {
443 public void actionPerformed(ActionEvent event) {
444 String new_level = event.getActionCommand();
445 setDefaultLevel(new_level);
446 }
447 }
448 private class CheckBoxListener
449 implements ActionListener {
450
451 public void actionPerformed(ActionEvent event) {
452 if (!((JCheckBox)event.getSource()).isSelected()) {
453 // deselecting
454 if (noLevelsSelected()) {
455 // can't do this
456 ((JCheckBox)event.getSource()).setSelected(true);
457 } else {
458 // remove the level
459 removeLevel(event.getActionCommand());
460 }
461 } else {
462 // selecting a new one
463 addLevel(event.getActionCommand());
464 }
465 validateControls();
466 }
467 }
468
469 private boolean noLevelsSelected() {
470 if (!document_checkbox.isSelected() && !section_checkbox.isSelected()) {
471 return true;
472
473 }
474 return false;
475 }
476
477 private void validateControls() {
478
479 boolean document_enabled = document_checkbox.isSelected();
480 document_default_radio.setEnabled(document_enabled);
481
482 boolean section_enabled = section_checkbox.isSelected();
483 section_default_radio.setEnabled(section_enabled);
484
485 // have we disabled our default??
486 if (!document_enabled && document_default_radio.isSelected()) {
487 section_default_radio.setSelected(true);
488 setDefaultLevel(StaticStrings.SECTION_STR);
489
490 } else if (!section_enabled && section_default_radio.isSelected()) {
491 document_default_radio.setSelected(true);
492 setDefaultLevel(StaticStrings.DOCUMENT_STR);
493 }
494
495
496 }
497
498 }
499
500}
Note: See TracBrowser for help on using the repository browser.