p2izing the editor
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / preference / FeaturePreferences.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.preference;
10
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14
15 import org.apache.log4j.Logger;
16 import org.eclipse.jface.preference.PreferencePage;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.ui.IWorkbench;
23 import org.eclipse.ui.IWorkbenchPreferencePage;
24
25 import eu.etaxonomy.cdm.model.description.Feature;
26 import eu.etaxonomy.taxeditor.controller.PreferencesController;
27 import eu.etaxonomy.taxeditor.model.CdmSessionDataRepository;
28
29 /**
30 * @author p.ciardelli
31 * @created 17.09.2008
32 * @version 1.0
33 */
34 public class FeaturePreferences extends PreferencePage implements
35 IWorkbenchPreferencePage {
36 private static final Logger logger = Logger
37 .getLogger(FeaturePreferences.class);
38
39 public static final String PLUGIN_ID = "eu.etaxonomy.taxeditor.preferences.feature";
40
41 private Map<Feature, Button> featureButtons;
42
43 private Composite container;
44
45 public FeaturePreferences() {
46 super();
47 setDescription("Choose which features you would like to use for descriptive elements.");
48 }
49
50
51 /**
52 * Create contents of the preference page
53 * @param parent
54 */
55 @Override
56 public Control createContents(Composite parent) {
57
58 featureButtons = new HashMap<Feature, Button>();
59
60 container = new Composite(parent, SWT.NULL);
61 final GridLayout gridLayout = new GridLayout();
62 gridLayout.numColumns = 3;
63 container.setLayout(gridLayout);
64
65 List<Feature> features =
66 CdmSessionDataRepository.getDefault().getFeatures();
67
68 // Purpose of this code block:
69 // fill first column, then second, then third
70 Object[] array = features.toArray();
71 int count = features.size();
72 int colheight = count / 3;
73 if (count % 3 > 0) {
74 colheight++;
75 }
76 for (int i = 0; i < colheight; i++) {
77 createButton((Feature) array[i]);
78 if (i + colheight < count) {
79 createButton((Feature) array[i + colheight]);
80 }
81 if (i + (colheight * 2) < count) {
82 createButton((Feature) array[i + (colheight * 2)]);
83 }
84 }
85 return container;
86 }
87
88
89 /**
90 * @param object
91 */
92 private void createButton(Feature feature) {
93 Button button = new Button(container, SWT.CHECK);
94 button.setText(feature.getLabel());
95 featureButtons.put(feature, button);
96
97 if (PreferencesController.getFeaturePreference(feature)) {
98 button.setSelection(true);
99 }
100 }
101
102
103 /* (non-Javadoc)
104 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
105 */
106 public void init(IWorkbench workbench) {
107 setPreferenceStore(PreferencesController.getPrefStore());
108 }
109
110 /* (non-Javadoc)
111 * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
112 */
113 protected void performDefaults() {
114
115 // Set all buttons to show feature
116 for (Button button : featureButtons.values()) {
117 button.setSelection(true);
118 }
119 }
120
121 /* (non-Javadoc)
122 * @see org.eclipse.jface.preference.PreferencePage#performOk()
123 */
124 public boolean performOk() {
125
126 for (Feature feature : featureButtons.keySet()) {
127 Button button = featureButtons.get(feature);
128 PreferencesController.setFeaturePreference(feature, button.getSelection());
129 }
130
131 return true;
132 }
133 }