source: gli/branches/rtl-gli/src/org/greenstone/gatherer/cdm/IndexOptionManager.java@ 18352

Last change on this file since 18352 was 18352, checked in by kjdon, 15 years ago

updated the rtl-gli branch with files from trunk. Result of a merge 14807:18318

  • Property svn:keywords set to Author Date Id Revision
File size: 16.7 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_checkbox.setActionCommand(StaticStrings.DOCUMENT_STR);
335 document_default_radio = new JRadioButton();
336 document_default_radio.setBackground(Configuration.getColor("coloring.collection_tree_background", false));
337 document_default_radio.setOpaque(false);
338 document_default_radio.setActionCommand(StaticStrings.DOCUMENT_STR);
339 document_default_radio.setIcon(JarTools.getImage("radio_unselected.gif"));
340 document_default_radio.setSelectedIcon(JarTools.getImage("radio_selected.gif"));
341
342 section_checkbox = new JCheckBox();
343 section_checkbox.setText(Dictionary.get("CDM.LevelManager.Section"));
344 section_checkbox.setActionCommand(StaticStrings.SECTION_STR);
345 section_default_radio = new JRadioButton();
346 section_default_radio.setOpaque(true);
347 section_default_radio.setBackground(Configuration.getColor("coloring.collection_tree_background", false));
348 section_default_radio.setOpaque(false);
349 section_default_radio.setIcon(JarTools.getImage("radio_unselected.gif"));
350 section_default_radio.setSelectedIcon(JarTools.getImage("radio_selected.gif"));
351
352 section_default_radio.setActionCommand(StaticStrings.SECTION_STR);
353 ButtonGroup default_group = new ButtonGroup();
354 default_group.add(document_default_radio);
355 default_group.add(section_default_radio);
356
357
358
359 if (level_model.getSize()==0) {
360 // no levels yet, set document
361 document_checkbox.setSelected(true);
362 addLevel(StaticStrings.DOCUMENT_STR);
363 } else {
364 if (getLevel(StaticStrings.DOCUMENT_STR)!= null) {
365 document_checkbox.setSelected(true);
366 }
367 if (getLevel(StaticStrings.SECTION_STR) != null) {
368 section_checkbox.setSelected(true);
369 }
370 }
371
372 if (default_level.isAssigned()) {
373 // set the default based on specified default
374 if (default_level.getValue().equals(StaticStrings.DOCUMENT_STR) && document_checkbox.isSelected()) {
375 document_default_radio.setSelected(true);
376 }
377 else if (default_level.getValue().equals(StaticStrings.SECTION_STR) && section_checkbox.isSelected()) {
378 section_default_radio.setSelected(true);
379 } else {
380 default_level.setAssigned(false);
381 }
382 }
383 // if have no valid specified default
384 if (!default_level.isAssigned()) {
385 String default_l = StaticStrings.DOCUMENT_STR;
386 if (document_checkbox.isSelected()) {
387 document_default_radio.setSelected(true);
388 default_l = StaticStrings.DOCUMENT_STR;
389 } else if (section_checkbox.isSelected()) {
390 section_default_radio.setSelected(true);
391 default_l = StaticStrings.SECTION_STR;
392 }
393 setDefaultLevel(default_l);
394 }
395
396 validateControls();
397 JLabel checkbox_label = new JLabel(Dictionary.get("CDM.LevelManager.Level_Title"));
398 checkbox_panel.setLayout(new GridLayout(3,1));
399 checkbox_panel.setToolTipText(Dictionary.get("CDM.IndexManager.Level_Tooltip"));
400 checkbox_panel.add(checkbox_label);
401 checkbox_panel.add(document_checkbox);
402 checkbox_panel.add(section_checkbox);
403
404 JLabel default_label = new JLabel(Dictionary.get("CDM.LevelManager.Default"));
405 default_panel.setLayout(new GridLayout(3,1));
406 default_panel.setToolTipText(Dictionary.get("CDM.LevelManager.Default_Tooltip"));
407 default_panel.add(default_label);
408 default_panel.add(document_default_radio);
409 default_panel.add(section_default_radio);
410
411
412 CheckBoxListener cbl = new CheckBoxListener();
413 document_checkbox.addActionListener(cbl);
414 section_checkbox.addActionListener(cbl);
415
416 // changing selected levels changes build settings
417 document_checkbox.addActionListener(CollectionDesignManager.buildcol_change_listener);
418 section_checkbox.addActionListener(CollectionDesignManager.buildcol_change_listener);
419
420 RadioListener rl = new RadioListener();
421 document_default_radio.addActionListener(rl);
422 section_default_radio.addActionListener(rl);
423 // changing the default doesn't need a rebuild
424
425 setLayout(new BorderLayout(10,10));
426 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
427 add(checkbox_panel, BorderLayout.WEST);
428 add(default_panel, BorderLayout.CENTER);
429 } // LevelControl
430
431 public void buildTypeChanged(String new_build_type) {};
432 public void gainFocus() {
433
434 }
435
436 public void loseFocus() {
437
438 }
439
440 public void destroy() {
441 }
442
443 private class RadioListener
444 implements ActionListener {
445 public void actionPerformed(ActionEvent event) {
446 String new_level = event.getActionCommand();
447 setDefaultLevel(new_level);
448 }
449 }
450 private class CheckBoxListener
451 implements ActionListener {
452
453 public void actionPerformed(ActionEvent event) {
454 if (!((JCheckBox)event.getSource()).isSelected()) {
455 // deselecting
456 if (noLevelsSelected()) {
457 // can't do this
458 ((JCheckBox)event.getSource()).setSelected(true);
459 } else {
460 // remove the level
461 removeLevel(event.getActionCommand());
462 }
463 } else {
464 // selecting a new one
465 addLevel(event.getActionCommand());
466 }
467 validateControls();
468 }
469 }
470
471 private boolean noLevelsSelected() {
472 if (!document_checkbox.isSelected() && !section_checkbox.isSelected()) {
473 return true;
474
475 }
476 return false;
477 }
478
479 private void validateControls() {
480
481 boolean document_enabled = document_checkbox.isSelected();
482 document_default_radio.setEnabled(document_enabled);
483
484 boolean section_enabled = section_checkbox.isSelected();
485 section_default_radio.setEnabled(section_enabled);
486
487 // have we disabled our default??
488 if (!document_enabled && document_default_radio.isSelected()) {
489 section_default_radio.setSelected(true);
490 setDefaultLevel(StaticStrings.SECTION_STR);
491
492 } else if (!section_enabled && section_default_radio.isSelected()) {
493 document_default_radio.setSelected(true);
494 setDefaultLevel(StaticStrings.DOCUMENT_STR);
495 }
496
497
498 }
499
500 }
501
502}
Note: See TracBrowser for help on using the repository browser.