Project

General

Profile

Download (11.1 KB) Statistics
| Branch: | Tag: | Revision:
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.List;
13
import java.util.UUID;
14

    
15
import org.apache.commons.lang.StringUtils;
16
import org.eclipse.jface.util.IPropertyChangeListener;
17
import org.eclipse.jface.util.PropertyChangeEvent;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.events.SelectionEvent;
20
import org.eclipse.swt.events.SelectionListener;
21
import org.eclipse.swt.layout.GridData;
22
import org.eclipse.swt.layout.GridLayout;
23
import org.eclipse.swt.widgets.Button;
24
import org.eclipse.swt.widgets.Composite;
25
import org.eclipse.swt.widgets.Label;
26
import org.eclipse.swt.widgets.Text;
27

    
28
import eu.etaxonomy.cdm.api.service.ITermService;
29
import eu.etaxonomy.cdm.api.service.IVocabularyService;
30
import eu.etaxonomy.cdm.model.location.NamedArea;
31
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
32
import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
33
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
34
import eu.etaxonomy.cdm.model.metadata.PreferenceResolver;
35
import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
36
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
37
import eu.etaxonomy.cdm.persistence.dto.TermDto;
38
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
39
import eu.etaxonomy.taxeditor.l10n.Messages;
40
import eu.etaxonomy.taxeditor.model.ImageResources;
41
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
42
import eu.etaxonomy.taxeditor.preference.Resources;
43
import eu.etaxonomy.taxeditor.store.CdmStore;
44
import eu.etaxonomy.taxeditor.store.StoreUtil;
45
import eu.etaxonomy.taxeditor.ui.dialog.selection.SelectionDialogFactory;
46

    
47
/**
48
 * @author k.luther
49
 * @since 05.10.2018
50
 *
51
 */
52
public class AvailableDistributionPerAreaStatusPage extends AvailableDistributionStatusPage implements IPropertyChangeListener{
53

    
54
    List<CdmPreference> perAreaPreferences = new ArrayList();
55
    List<CdmPreference> perAreaPreferencesDeleteList = new ArrayList();
56
    List<TermVocabularyDto> vocs;
57
    NamedArea actualArea;
58
    CdmPreference actualPref;
59
    Text areaText;
60
    Button btnSelectArea;
61
    Button btnRemove;
62
    Label status;
63
    int countTerms;
64

    
65

    
66
    public AvailableDistributionPerAreaStatusPage(String pageName, List<CdmPreference> prefs, boolean localPref) {
67
        super(pageName, null, localPref);
68

    
69
        perAreaPreferences = prefs;
70
        if(PreferencesUtil.getPreferenceFromDB(PreferencePredicate.AvailableDistributionAreaVocabularies) == null && PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey()) == null){
71
            vocs = CdmStore.getService(IVocabularyService.class).findVocabularyDtoByTermType(type);
72

    
73
        }
74
        pref = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.AvailableDistributionStatus);
75
        if (pref == null){
76
            pref = CdmPreference.NewTaxEditorInstance(PreferencePredicate.AvailableDistributionStatus, null);
77
        }
78
        initialiseVocabularies();
79
        countTerms = CdmStore.getTermManager().getPreferredTerms(type).size();
80
    }
81

    
82
    @Override
83
    public void createControl(Composite parent) {
84
        setTitle(Messages.AvailableDistributionStatusPage_PAGE_TITLE);
85
        setDescription(Messages.AvailableDistributionStatusPage_PAGE_DESCRIPTION);
86
        createAreaSelectionCombo(parent);
87

    
88
        super.createControl(parent);
89

    
90
    }
91

    
92

    
93
    protected void createAreaSelectionCombo(Composite parent) {
94

    
95
        Composite toolbar = new Composite(parent, SWT.NONE);
96
        toolbar.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
97
        GridLayout gl_toolbar = new GridLayout(4, false);
98
        gl_toolbar.horizontalSpacing = 4;
99
        gl_toolbar.marginHeight = 0;
100
        gl_toolbar.verticalSpacing = 0;
101
        toolbar.setLayout(gl_toolbar);
102

    
103
        Label areaLabel = new Label(toolbar, SWT.NONE);
104
        areaLabel.setText("Select area for area specific status");
105

    
106
        GridData textData = new GridData();
107
        textData.minimumWidth = 200;
108
        textData.widthHint = 200;
109
        areaText = new Text(toolbar,  SWT.NONE);
110
        areaText.setEditable(false);
111
        areaText.setBackground(StoreUtil
112
                .getColor(Resources.COLOR_TEXT_DISABLED_BACKGROUND));
113
        areaText.setLayoutData(textData);
114
        btnSelectArea = new Button(toolbar, SWT.PUSH | SWT.FLAT );
115
        btnSelectArea.setImage(ImageResources
116
                .getImage(ImageResources.BROWSE_ICON));
117
        btnSelectArea.setToolTipText("Browse Area");
118

    
119
        btnSelectArea.addSelectionListener(new SelectionListener(){
120

    
121
            @Override
122
            public void widgetSelected(SelectionEvent e) {
123
                refreshActualPrefernce();
124

    
125
                NamedArea selection = SelectionDialogFactory.getSelectionFromDialog(NamedArea.class, getShell(), //getConversationHolder(),
126
                        actualArea, null);
127
                actualArea = selection;
128
                PrefKey key;
129
                if (actualArea != null){
130
                    areaText.setText(actualArea.getLabel());
131
                    key = newAreaSpecificStatusPreference();
132
                    actualPref = PreferenceResolver.resolve(perAreaPreferences, key);
133
                    if (actualPref == null){
134
                        actualPref = CdmPreference.NewInstance(key, null);
135
                        perAreaPreferences.add(actualPref);
136
                    }
137
                }else{
138
                    actualPref = null;
139
                }
140

    
141
                status.setVisible(false);
142
                rememberCheckedValues(getCheckedValuesFromPreferences());
143

    
144
            }
145

    
146
            @Override
147
            public void widgetDefaultSelected(SelectionEvent e) {
148
                // TODO Auto-generated method stub
149

    
150
            }
151

    
152
        });
153
//        btnRemove = new Button(toolbar, SWT.NONE);
154
//        btnRemove.addSelectionListener(new SelectionListener(){
155
//
156
//            @Override
157
//            public void widgetSelected(SelectionEvent e) {
158
//
159
//                PrefKey key = newAreaSpecificStatusPreference();
160
//                actualPref = PreferenceResolver.resolve(perAreaPreferences, key);
161
//                actualPref = CdmPreference.NewInstance(key, null);
162
//                if (actualPref.getKey().equals(pref.getKey())){
163
//                    pref = actualPref;
164
//                }
165
//
166
//                perAreaPreferences.add(actualPref);
167
//                if (actualArea != null){
168
//                    status.setText("Preference for area " + actualArea.getLabel() + " removed. Now the status defined for all areas are displayed");
169
//                }else{
170
//                    status.setText("Preference for all areas is removed. Now all status will be displayed");
171
//                }
172
//                status.setVisible(true);
173
//
174
//                actualArea = null;
175
//
176
//                areaText.setText("");
177
//                rememberCheckedValues(getCheckedValuesFromPreferences());
178
//
179
//            }
180
//
181
//            @Override
182
//            public void widgetDefaultSelected(SelectionEvent e) {
183
//                // TODO Auto-generated method stub
184
//
185
//            }
186
//
187
//        });
188
//        btnRemove.setImage(ImageResources.getImage(ImageResources.ACTIVE_DELETE_ICON));
189
//        btnRemove.setToolTipText("");
190
        status = new Label(toolbar, SWT.NONE);
191
        status.setVisible(false);
192
    }
193

    
194

    
195
    @Override
196
    public void propertyChange(PropertyChangeEvent event) {
197
        if(event == null){
198
            return;
199
        }
200
        Object eventSource = event.getSource();
201
        if(eventSource != this && btnSelectArea.equals(eventSource)){
202
            if (actualArea != null){
203
                PrefKey key = newAreaSpecificStatusPreference();
204
                actualPref = PreferenceResolver.resolve(perAreaPreferences, key);
205
                areaText.setText(actualArea.getLabel());
206
            }else{
207
                actualPref = null;
208
            }
209

    
210
        }
211
    }
212

    
213
    @Override
214
    protected String getCheckedValuesFromPreferences() {
215
       String checkedValues = null;
216
       if (actualArea == null){
217
           checkedValues = pref.getValue();
218
       }else{
219
           if (actualPref == null){
220
               PrefKey key = newAreaSpecificStatusPreference();
221
               actualPref = PreferenceResolver.resolve(perAreaPreferences, key);
222

    
223
               if (actualPref == null){
224
                   actualPref = CdmPreference.NewInstance(key, null);
225
                   checkedValues = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionStatus.getKey(), true);
226

    
227
               }
228
           }
229
           checkedValues = actualPref.getValue();
230
       }
231

    
232
       return checkedValues;
233
    }
234

    
235
    protected void refreshActualPrefernce(){
236
        String saveCheckedElements;
237
        getViewer().expandAll();
238

    
239

    
240
        if (countTerms == getViewer().getCheckedElements().length){
241
            saveCheckedElements = null;
242
        }else{
243
            Object[] checkedElements = getViewer().getCheckedElements();
244
            List<UUID> listUIIDChecked = new ArrayList<>();
245
            List<DefinedTermBase> preferredTerms = new ArrayList<>();
246
            for (Object o : checkedElements) {
247
                 if(o instanceof TermDto){
248
                     TermDto termDto = (TermDto) o;
249
                     listUIIDChecked.add(termDto.getUuid());
250
                     preferredTerms.add(CdmStore.getService(ITermService.class).load(termDto.getUuid()));
251
                 }
252
             }
253
             saveCheckedElements = StringUtils.join(listUIIDChecked, ";"); //$NON-NLS-1$
254
             if (StringUtils.isBlank(saveCheckedElements)){
255
                 saveCheckedElements = null;
256
         }
257
        }
258
        if (actualArea == null){
259
            CdmPreference savePref = CdmPreference.NewTaxEditorInstance(PreferencePredicate.AvailableDistributionStatus, saveCheckedElements);
260
            savePref.setAllowOverride(pref.isAllowOverride());
261
            pref = savePref;
262
        }else{
263
            PrefKey key = newAreaSpecificStatusPreference();
264
            actualPref = PreferenceResolver.resolve(perAreaPreferences, key);
265
            CdmPreference savePref = CdmPreference.NewInstance(PreferenceSubject.fromKey(key), PreferencePredicate.AvailableDistributionStatus, saveCheckedElements);
266
            if (actualPref != null){
267
               perAreaPreferences.remove(actualPref);
268
            }
269
            perAreaPreferences.add(savePref);
270
            actualPref = savePref;
271

    
272
        }
273
    }
274

    
275
    @Override
276
    protected  List<TermVocabularyDto> getVocabulariesFromPreference(){
277
     return null;
278
    }
279

    
280
    private PrefKey newAreaSpecificStatusPreference(){
281
        PrefKey prefKey;
282
        if (actualArea != null){
283
            prefKey  = CdmPreference.NewKey(PreferenceSubject.NewInstance(PreferenceSubject.ROOT+"NamedArea["+actualArea.getUuid().toString()+"]"+PreferenceSubject.SEP), PreferencePredicate.AvailableDistributionStatus);
284
        }else{
285
            prefKey = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AvailableDistributionStatus);
286
        }
287
        return prefKey;
288
    }
289

    
290

    
291
}
(6-6/16)