Project

General

Profile

Download (6.6 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2019 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.ArrayList;
12
import java.util.List;
13

    
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.custom.CLabel;
16
import org.eclipse.swt.events.SelectionAdapter;
17
import org.eclipse.swt.events.SelectionEvent;
18
import org.eclipse.swt.events.SelectionListener;
19
import org.eclipse.swt.widgets.Button;
20
import org.eclipse.swt.widgets.Combo;
21
import org.eclipse.swt.widgets.Composite;
22
import org.eclipse.swt.widgets.Control;
23

    
24
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
25
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
26
import eu.etaxonomy.cdm.model.metadata.PublishEnum;
27
import eu.etaxonomy.taxeditor.l10n.Messages;
28
import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
29

    
30
/**
31
 * @author k.luther
32
 * @since 08.01.2019
33
 *
34
 */
35
public class PublishFlagLocalPreference extends CdmPreferencePage {
36

    
37
    protected PublishEnum publishBehaviour;
38

    
39
    protected Combo publishFlagBehaviour;
40
    protected CdmPreference pref = null;
41
    Button allowOverrideButton;
42

    
43
    protected boolean allowOverride;
44
    private boolean override = true;
45

    
46

    
47
   @Override
48
   public void init() {
49
       super.init();
50

    
51

    
52
   }
53
    /**
54
     * {@inheritDoc}
55
     */
56
    @Override
57
    protected Control createContents(Composite parent) {
58
        getValues();
59
        pref = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.DefaultBehaviourForPublishFlag);
60
        if (pref == null){
61
            pref = CdmPreference.NewTaxEditorInstance(PreferencePredicate.DefaultBehaviourForPublishFlag, PreferencePredicate.DefaultBehaviourForPublishFlag.getDefaultValue().toString());
62
        }
63
        allowOverride = pref.isAllowOverride();
64
        Composite composite = createComposite(parent);
65

    
66
        if (!allowOverride && !isAdminPreference){
67
            final CLabel description = new CLabel(composite, SWT.NULL);
68
            description.setText(Messages.PublishFlagPreference_description_not_allowed);
69
            description.setLayoutData(createTextGridData());
70
            return composite;
71
        }
72

    
73

    
74
//        description.setText(Messages.PublishFlagPreference_description);
75
//        description.setLayoutData(createTextGridData());
76
//        publishFlagBehaviour = new Combo(composite, SWT.READ_ONLY);
77
        List<String> enumValues = new ArrayList();
78

    
79
        publishFlagBehaviour = createCombo(composite, PublishEnum.values(), PreferencePredicate.DefaultBehaviourForPublishFlag, Messages.PublishFlagPreference_description, isAdminPreference);
80

    
81
//        publishFlagBehaviour.setFont(parent.getFont());
82
//        for(PublishEnum display: PublishEnum.values()){
83
//            if (pref.getValue().equals(display.getKey())){
84
//                publishFlagBehaviour.add(display.getLabel() + " (Default)");
85
//            }
86
//            else
87
//        }
88

    
89
        publishFlagBehaviour.addSelectionListener(new SelectionListener() {
90

    
91
            @Override
92
            public void widgetSelected(SelectionEvent e) {
93
                setApply(true);
94
                if (!publishFlagBehaviour.getText().startsWith(Messages.Preference_Use_Default)){
95

    
96
                    publishBehaviour = (PublishEnum)publishFlagBehaviour.getData(publishFlagBehaviour.getText());
97
                }
98

    
99
            }
100

    
101
            @Override
102
            public void widgetDefaultSelected(SelectionEvent e) {
103
                // TODO Auto-generated method stub
104

    
105
            }
106
        });
107
        if (this.isAdminPreference){
108
            allowOverrideButton = createAllowOverrideButton(composite);
109
            allowOverrideButton.setText(Messages.GeneralPreference_override);
110
            allowOverrideButton.setSelection(allowOverride);
111
            allowOverrideButton.addSelectionListener(new SelectionAdapter(){
112
                @Override
113
                public void widgetSelected(SelectionEvent e) {
114
                    allowOverride = !allowOverride;
115
                    setApply(true);
116
                    }
117
            });
118
        }
119
        int index = 0;
120

    
121
        if(publishBehaviour==null){
122
            publishFlagBehaviour.select(0);
123
        }
124
        else{
125
            for (String itemLabel : publishFlagBehaviour.getItems()){
126
                if (itemLabel.startsWith(publishBehaviour.getLabel())){
127
                    publishFlagBehaviour.select(index);
128
                    break;
129
                }
130
                index++;
131
            }
132
        }
133

    
134
        return composite;
135

    
136
    }
137

    
138

    
139
    @Override
140
    public boolean performOk() {
141
        if (publishBehaviour != null){
142
            String text = publishFlagBehaviour.getText();
143
            for (PublishEnum display: PublishEnum.values()){
144
                if (text.startsWith(display.getLabel())){
145
                    text = display.getKey();
146
                    break;
147
                }
148
            }
149
            PreferencesUtil.setStringValue(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey(), text);
150
            if (pref == null || !pref.getValue().equals(text)){
151
                PreferencesUtil.setBooleanValue(PreferencesUtil.prefOverrideKey(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey()), true);
152
            }
153
        }else{
154
            PreferencesUtil.setBooleanValue(PreferencesUtil.prefOverrideKey(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey()), false);
155
        }
156
        return true;
157
    }
158

    
159
    @Override
160
    public void getValues(){
161
        publishBehaviour = null;
162
        try{
163
            String publishString = PreferencesUtil.getStringValue(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey(), true);
164

    
165
            if (publishString != null){
166
                publishBehaviour = PublishEnum.valueOf(publishString);
167
            }
168
        }catch (IllegalArgumentException e){
169
            publishBehaviour = PublishEnum.InheritFromParent;
170
        }
171

    
172
        override = PreferencesUtil.getBooleanValue(PreferencesUtil.prefOverrideKey(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey()));
173
        if (!override){
174
            publishBehaviour = null;
175
        }
176
    }
177

    
178
    @Override
179
    protected void performDefaults() {
180
        publishBehaviour = null;
181
        allowOverride = true;
182
        int index = 0;
183

    
184
        for (String itemLabel : publishFlagBehaviour.getItems()){
185
            if (itemLabel.startsWith(Messages.Preference_Use_Default)){
186
                publishFlagBehaviour.select(index);
187
                break;
188
            }
189
            index++;
190
        }
191
        allowOverrideButton.setSelection(allowOverride);
192
        super.performDefaults();
193
    }
194

    
195

    
196
}
(38-38/52)