ref #7920: add handlers and wizards for area specific status preference selection
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / wizard / AvailableDistributionPerAreaStatusPage.java
1 /**
2 * Copyright (C) 2018 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.wizard;
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.UUID;
16
17 import org.apache.commons.lang.StringUtils;
18 import org.eclipse.jface.util.IPropertyChangeListener;
19 import org.eclipse.jface.util.PropertyChangeEvent;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.events.SelectionListener;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Text;
29
30 import eu.etaxonomy.cdm.api.service.ITermService;
31 import eu.etaxonomy.cdm.api.service.IVocabularyService;
32 import eu.etaxonomy.cdm.model.common.CdmBase;
33 import eu.etaxonomy.cdm.model.location.NamedArea;
34 import eu.etaxonomy.cdm.model.metadata.CdmPreference;
35 import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
36 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
37 import eu.etaxonomy.cdm.model.metadata.PreferenceResolver;
38 import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
39 import eu.etaxonomy.cdm.model.term.DefinedTermBase;
40 import eu.etaxonomy.cdm.persistence.dto.TermDto;
41 import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
42 import eu.etaxonomy.taxeditor.l10n.Messages;
43 import eu.etaxonomy.taxeditor.model.ImageResources;
44 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
45 import eu.etaxonomy.taxeditor.preference.Resources;
46 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
47 import eu.etaxonomy.taxeditor.store.CdmStore;
48 import eu.etaxonomy.taxeditor.store.StoreUtil;
49 import eu.etaxonomy.taxeditor.ui.dialog.selection.SelectionDialogFactory;
50
51 /**
52 * @author k.luther
53 * @since 05.10.2018
54 *
55 */
56 public class AvailableDistributionPerAreaStatusPage extends AvailableDistributionStatusPage implements IPropertyChangeListener{
57
58 List<CdmPreference> perAreaPreferences = new ArrayList();
59 List<TermVocabularyDto> vocs;
60 NamedArea actualArea;
61 CdmPreference actualPref;
62 Text areaText;
63 Button btnSelectArea;
64 Button btnRemove;
65 Label status;
66 int countTerms;
67
68
69 public AvailableDistributionPerAreaStatusPage(String pageName, List<CdmPreference> prefs, boolean localPref) {
70 super(pageName, null, localPref);
71
72 perAreaPreferences = prefs;
73 if(PreferencesUtil.getPreferenceFromDB(PreferencePredicate.AvailableDistributionAreaVocabularies) == null && PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey()) == null){
74 vocs = CdmStore.getService(IVocabularyService.class).findVocabularyDtoByTermType(type);
75
76 }
77 pref = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.AvailableDistributionStatus);
78 if (pref == null){
79 pref = CdmPreference.NewTaxEditorInstance(PreferencePredicate.AvailableDistributionStatus, null);
80 }
81 initialiseVocabularies();
82 countTerms = CdmStore.getTermManager().getPreferredTerms(type).size();
83 }
84
85 @Override
86 public void createControl(Composite parent) {
87 setTitle(Messages.AvailableDistributionStatusPage_PAGE_TITLE);
88 setDescription(Messages.AvailableDistributionStatusPage_PAGE_DESCRIPTION);
89 createAreaSelectionCombo(parent);
90
91 super.createControl(parent);
92
93 }
94
95
96 protected void createAreaSelectionCombo(Composite parent) {
97
98 Composite toolbar = new Composite(parent, SWT.NONE);
99 toolbar.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
100 GridLayout gl_toolbar = new GridLayout(4, false);
101 gl_toolbar.horizontalSpacing = 4;
102 gl_toolbar.marginHeight = 0;
103 gl_toolbar.verticalSpacing = 0;
104 toolbar.setLayout(gl_toolbar);
105
106 Label areaLabel = new Label(toolbar, SWT.NONE);
107 areaLabel.setText("Select area for area specific status");
108
109 GridData textData = new GridData();
110 textData.minimumWidth = 200;
111 textData.widthHint = 200;
112 areaText = new Text(toolbar, SWT.NONE);
113 areaText.setEditable(false);
114 areaText.setBackground(StoreUtil
115 .getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND));
116 areaText.setLayoutData(textData);
117 btnSelectArea = new Button(toolbar, SWT.PUSH | SWT.FLAT );
118 btnSelectArea.setImage(ImageResources
119 .getImage(ImageResources.BROWSE_ICON));
120 btnSelectArea.setToolTipText("Browse Area");
121
122 btnSelectArea.addSelectionListener(new SelectionListener(){
123
124 @Override
125 public void widgetSelected(SelectionEvent e) {
126 refreshActualPrefernce();
127
128 NamedArea selection = SelectionDialogFactory.getSelectionFromDialog(NamedArea.class, getShell(), //getConversationHolder(),
129 actualArea, null);
130 actualArea = selection;
131 PrefKey key;
132 if (actualArea != null){
133 areaText.setText(actualArea.getLabel());
134 key = CdmPreference.NewKey(PreferenceSubject.NewInstance(PreferenceSubject.NewDistributionEditorInstance()+"area/"+actualArea.getUuid().toString()), PreferencePredicate.AvailableDistributionPerAreaStatus);
135 actualPref = PreferenceResolver.resolve(perAreaPreferences, key);
136 if (actualPref == null){
137 actualPref = CdmPreference.NewInstance(key, null);
138 perAreaPreferences.add(actualPref);
139 }
140 }else{
141 actualPref = null;
142 }
143
144 status.setVisible(false);
145 rememberCheckedValues(getCheckedValuesFromPreferences());
146
147 }
148
149 @Override
150 public void widgetDefaultSelected(SelectionEvent e) {
151 // TODO Auto-generated method stub
152
153 }
154
155 });
156 btnRemove = new Button(toolbar, SWT.NONE);
157 btnRemove.addSelectionListener(new SelectionListener(){
158
159 @Override
160 public void widgetSelected(SelectionEvent e) {
161
162
163 PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewInstance(PreferenceSubject.NewDistributionEditorInstance()+"area/"+actualArea.getUuid().toString()), PreferencePredicate.AvailableDistributionPerAreaStatus);
164 actualPref = PreferenceResolver.resolve(perAreaPreferences, key);
165 actualPref = CdmPreference.NewInstance(key, null);
166
167 perAreaPreferences.add(actualPref);
168 status.setText("Preference for area " + actualArea.getLabel() + " removed.");
169 status.setVisible(true);
170 actualArea = null;
171 areaText.setText("");
172 rememberCheckedValues(getCheckedValuesFromPreferences());
173
174 }
175
176 @Override
177 public void widgetDefaultSelected(SelectionEvent e) {
178 // TODO Auto-generated method stub
179
180 }
181
182 });
183 btnRemove.setImage(ImageResources.getImage(ImageResources.ACTIVE_DELETE_ICON));
184 btnRemove.setToolTipText("");
185 status = new Label(toolbar, SWT.NONE);
186 status.setVisible(false);
187 }
188
189
190 @Override
191 public void propertyChange(PropertyChangeEvent event) {
192 if(event == null){
193 return;
194 }
195 Object eventSource = event.getSource();
196 if(eventSource != this && btnSelectArea.equals(eventSource)){
197 if (actualArea != null){
198 PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewInstance(PreferenceSubject.DISTR_EDITOR+"/area/"+actualArea.getUuid().toString()), PreferencePredicate.AvailableDistributionPerAreaStatus);
199 actualPref = PreferenceResolver.resolve(perAreaPreferences, key);
200 areaText.setText(actualArea.getLabel());
201 }else{
202 actualPref = null;
203 }
204
205 }
206 }
207
208 @Override
209 protected String getCheckedValuesFromPreferences() {
210 String checkedValues = null;
211 if (actualArea == null){
212 checkedValues = pref.getValue();
213 }else{
214 if (actualPref == null){
215 PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewInstance(PreferenceSubject.DISTR_EDITOR+"/area/"+actualArea.getUuid().toString()), PreferencePredicate.AvailableDistributionPerAreaStatus);
216 actualPref = PreferenceResolver.resolve(perAreaPreferences, key);
217
218 if (actualPref == null){
219 actualPref = CdmPreference.NewInstance(key, null);
220 checkedValues = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionStatus.getKey(), true);
221
222 }
223 }
224 checkedValues = actualPref.getValue();
225 }
226
227 return checkedValues;
228 }
229
230 protected void refreshActualPrefernce(){
231 String saveCheckedElements;
232 getViewer().expandAll();
233
234
235 if (countTerms == getViewer().getCheckedElements().length){
236 saveCheckedElements = null;
237 }else{
238 Object[] checkedElements = getViewer().getCheckedElements();
239 List<UUID> listUIIDChecked = new ArrayList<>();
240 List<DefinedTermBase> preferredTerms = new ArrayList<>();
241 for (Object o : checkedElements) {
242 if(o instanceof TermDto){
243 TermDto termDto = (TermDto) o;
244 listUIIDChecked.add(termDto.getUuid());
245 preferredTerms.add(CdmStore.getService(ITermService.class).load(termDto.getUuid()));
246 }
247 }
248 saveCheckedElements = StringUtils.join(listUIIDChecked, ";"); //$NON-NLS-1$
249 if (StringUtils.isBlank(saveCheckedElements)){
250 saveCheckedElements = null;
251 }
252 }
253 if (actualArea == null){
254 CdmPreference savePref = CdmPreference.NewTaxEditorInstance(PreferencePredicate.AvailableDistributionStatus, saveCheckedElements);
255 savePref.setAllowOverride(pref.isAllowOverride());
256 pref = savePref;
257 }else{
258 PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewInstance(PreferenceSubject.NewDistributionEditorInstance()+"area/"+actualArea.getUuid().toString()), PreferencePredicate.AvailableDistributionPerAreaStatus);
259 actualPref = PreferenceResolver.resolve(perAreaPreferences, key);
260 CdmPreference savePref = CdmPreference.NewInstance(PreferenceSubject.fromKey(key), PreferencePredicate.AvailableDistributionPerAreaStatus, saveCheckedElements);
261 if (actualPref != null){
262 perAreaPreferences.remove(actualPref);
263 }
264 perAreaPreferences.add(savePref);
265 actualPref = savePref;
266
267 }
268 }
269
270 @Override
271 public ICdmEntitySession getCdmEntitySession() {
272 return null;
273 }
274
275 @Override
276 public <T extends CdmBase> Collection<T> getRootEntities() {
277 return null;
278 }
279
280 @Override
281 public Map<Object, List<String>> getPropertyPathsMap() {
282 return null;
283 }
284
285 @Override
286 protected List<TermVocabularyDto> getVocabulariesFromPreference(){
287 return null;
288 }
289
290 }