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
|
|
10
|
package eu.etaxonomy.taxeditor.preference.menu;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.List;
|
15
|
|
16
|
import org.eclipse.core.commands.Command;
|
17
|
import org.eclipse.core.commands.IParameter;
|
18
|
import org.eclipse.core.commands.Parameterization;
|
19
|
import org.eclipse.core.commands.ParameterizedCommand;
|
20
|
import org.eclipse.core.commands.common.NotDefinedException;
|
21
|
import org.eclipse.jface.viewers.CheckStateChangedEvent;
|
22
|
import org.eclipse.jface.viewers.CheckboxTableViewer;
|
23
|
import org.eclipse.jface.viewers.ICheckStateListener;
|
24
|
import org.eclipse.swt.SWT;
|
25
|
import org.eclipse.swt.events.SelectionAdapter;
|
26
|
import org.eclipse.swt.events.SelectionEvent;
|
27
|
import org.eclipse.swt.layout.GridData;
|
28
|
import org.eclipse.swt.layout.GridLayout;
|
29
|
import org.eclipse.swt.widgets.Button;
|
30
|
import org.eclipse.swt.widgets.Composite;
|
31
|
import org.eclipse.swt.widgets.Control;
|
32
|
import org.eclipse.ui.commands.ICommandService;
|
33
|
import org.eclipse.ui.handlers.IHandlerService;
|
34
|
|
35
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
36
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
37
|
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
|
38
|
import eu.etaxonomy.cdm.model.term.TermType;
|
39
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
40
|
import eu.etaxonomy.taxeditor.editor.definedterm.e4.DefinedTermEditorE4;
|
41
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
42
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
43
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
44
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
45
|
import eu.etaxonomy.taxeditor.store.TermStore;
|
46
|
|
47
|
/**
|
48
|
* @author n.hoffmann
|
49
|
* @created 12.06.2009
|
50
|
* @version 1.0
|
51
|
*/
|
52
|
public abstract class AbstractMenuPreferences<T extends DefinedTermBase> extends CdmPreferencePage implements IConversationEnabled{
|
53
|
|
54
|
private Button toggleButton;
|
55
|
protected HashMap<DefinedTermBase<T>, Button> menuButtons;
|
56
|
|
57
|
protected Button editButton;
|
58
|
protected Button removeButton;
|
59
|
|
60
|
protected CheckboxTableViewer tableViewer;
|
61
|
|
62
|
private boolean state = true;
|
63
|
private final boolean vocabularyIsEditable;
|
64
|
private ConversationHolder conversation;
|
65
|
|
66
|
/**
|
67
|
* Constructs a new menu preference page.
|
68
|
*
|
69
|
* @param initialVocabulary
|
70
|
* the initial vocabulary containing all possible entries for the menu
|
71
|
* @param title
|
72
|
* the title of the page
|
73
|
* @param description
|
74
|
* describing text for the preference page
|
75
|
* @param editable
|
76
|
* whether this vocabulary should be editable. Will render "New", "Edit", "Delete" buttons
|
77
|
* Handler for these buttons have to be overriding addEditButtonListeners()
|
78
|
* @param <T> a T object.
|
79
|
*/
|
80
|
public AbstractMenuPreferences(String title, String description, boolean editable) {
|
81
|
super(title);
|
82
|
vocabularyIsEditable = editable;
|
83
|
setDescription(description);
|
84
|
init();
|
85
|
}
|
86
|
|
87
|
protected List<T> getTerms(){
|
88
|
if(CdmStore.isActive() && getTermType()!=null){
|
89
|
return TermStore.getTerms(getTermType(), null);
|
90
|
}
|
91
|
return new ArrayList<T>();
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* {@inheritDoc}
|
96
|
*
|
97
|
* Create contents of the preference page
|
98
|
*/
|
99
|
@Override
|
100
|
public Control createContents(Composite parent) {
|
101
|
|
102
|
Composite container = new Composite(parent, SWT.NULL);
|
103
|
final GridLayout gridLayout = new GridLayout();
|
104
|
gridLayout.numColumns = 2;
|
105
|
container.setLayout(gridLayout);
|
106
|
|
107
|
tableViewer = CheckboxTableViewer.newCheckList(container, SWT.NULL);
|
108
|
GridData tableLayoutData = new GridData(SWT.FILL, SWT.FILL, true, false);
|
109
|
tableLayoutData.heightHint = 300;
|
110
|
tableViewer.getTable().setLayoutData(tableLayoutData);
|
111
|
|
112
|
tableViewer.setContentProvider(new DefinedTermBaseContentProvider());
|
113
|
tableViewer.setLabelProvider(new DefinedTermBaseLabelProvider());
|
114
|
|
115
|
refresh(getTerms());
|
116
|
|
117
|
tableViewer.addCheckStateListener(new ICheckStateListener() {
|
118
|
|
119
|
@Override
|
120
|
public void checkStateChanged(CheckStateChangedEvent arg0) {
|
121
|
checkNoneChecked();
|
122
|
setApply(true);
|
123
|
}
|
124
|
});
|
125
|
|
126
|
|
127
|
Composite buttonContainer = new Composite(container, SWT.NULL);
|
128
|
GridData buttonContainerLayoutData = new GridData();
|
129
|
buttonContainerLayoutData.verticalAlignment = SWT.TOP;
|
130
|
buttonContainer.setLayoutData(buttonContainerLayoutData);
|
131
|
buttonContainer.setLayout(new GridLayout());
|
132
|
|
133
|
if(vocabularyIsEditable) {
|
134
|
createEditButtons(buttonContainer);
|
135
|
}
|
136
|
|
137
|
toggleButton = new Button(buttonContainer, SWT.PUSH);
|
138
|
toggleButton.setText("Toggle");
|
139
|
toggleButton.addSelectionListener(new SelectionAdapter(){
|
140
|
@Override
|
141
|
public void widgetSelected(SelectionEvent e) {
|
142
|
state = state ? false : true;
|
143
|
tableViewer.setAllChecked(state);
|
144
|
checkNoneChecked();
|
145
|
setApply(true);
|
146
|
}
|
147
|
});
|
148
|
|
149
|
createAdditionalContent(container);
|
150
|
|
151
|
return container;
|
152
|
}
|
153
|
|
154
|
protected void refresh(List<T> definedTerms) {
|
155
|
tableViewer.setInput(definedTerms);
|
156
|
|
157
|
List<T> preferedTerms = CdmStore.getTermManager().getFilteredTerms(definedTerms);
|
158
|
tableViewer.setCheckedElements(preferedTerms.toArray());
|
159
|
}
|
160
|
|
161
|
protected void createAdditionalContent(Composite container) {
|
162
|
// implement where needed
|
163
|
}
|
164
|
|
165
|
protected void createEditButtons(Composite buttonContainer) {
|
166
|
|
167
|
editButton = new Button(buttonContainer, SWT.PUSH);
|
168
|
editButton.setText("Open Term Editor");
|
169
|
editButton.setLayoutData(new GridData(SWT.FILL));
|
170
|
|
171
|
addEditButtonListeners();
|
172
|
}
|
173
|
|
174
|
/**
|
175
|
* Implement this method in MenuPreference Pages where the vocabulary should be editable, editable flag
|
176
|
* is set.
|
177
|
*/
|
178
|
protected void addEditButtonListeners() {
|
179
|
editButton.addSelectionListener(new SelectionAdapter(){
|
180
|
@Override
|
181
|
public void widgetSelected(SelectionEvent event) {
|
182
|
setApply(true);
|
183
|
ICommandService commandService = (ICommandService) AbstractUtility.getService(ICommandService.class);
|
184
|
|
185
|
Command command = commandService.getCommand(DefinedTermEditorE4.OPEN_COMMAND_ID);
|
186
|
|
187
|
IParameter parameter;
|
188
|
try {
|
189
|
parameter = command.getParameter("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid");
|
190
|
|
191
|
Parameterization[] parameterizations = new Parameterization[]{
|
192
|
new Parameterization(parameter, getTermType().getUuid().toString())
|
193
|
};
|
194
|
|
195
|
ParameterizedCommand pCommand = new ParameterizedCommand(command, parameterizations);
|
196
|
|
197
|
if (command.isEnabled()) {
|
198
|
IHandlerService handlerService = (IHandlerService) AbstractUtility.getService(IHandlerService.class);
|
199
|
try {
|
200
|
handlerService.executeCommand(pCommand, null);
|
201
|
} catch (NotDefinedException e) {
|
202
|
throw new RuntimeException("Could not find open command: "
|
203
|
+ DefinedTermEditorE4.OPEN_COMMAND_ID);
|
204
|
} catch (Exception e) {
|
205
|
MessagingUtils
|
206
|
.error(getClass(),
|
207
|
"An exception occurred while trying to open a selection",
|
208
|
e);
|
209
|
}
|
210
|
}
|
211
|
} catch (NotDefinedException e) {
|
212
|
MessagingUtils
|
213
|
.error(getClass(),
|
214
|
"An exception occurred while trying to open the Term Editor",
|
215
|
e);
|
216
|
}
|
217
|
|
218
|
}
|
219
|
});
|
220
|
}
|
221
|
|
222
|
/** {@inheritDoc} */
|
223
|
@Override
|
224
|
public void init() {
|
225
|
setPreferenceStore(PreferencesUtil.getPreferenceStore());
|
226
|
if(CdmStore.isActive()) {
|
227
|
getConversationHolder().bind();
|
228
|
}
|
229
|
}
|
230
|
|
231
|
/**
|
232
|
* <p>performDefaults</p>
|
233
|
*/
|
234
|
@Override
|
235
|
protected void performDefaults() {
|
236
|
tableViewer.setAllChecked(true);
|
237
|
}
|
238
|
|
239
|
/** {@inheritDoc} */
|
240
|
@Override
|
241
|
public boolean performOk() {
|
242
|
if(!CdmStore.isActive()) {
|
243
|
return true;
|
244
|
}
|
245
|
|
246
|
if(checkNoneChecked()){
|
247
|
return false;
|
248
|
}
|
249
|
|
250
|
getConversationHolder().commit(true);
|
251
|
|
252
|
if(tableViewer!=null){
|
253
|
List<T> preferredTerms = new ArrayList<T>();
|
254
|
for (Object element : tableViewer.getCheckedElements()){
|
255
|
preferredTerms.add((T) element);
|
256
|
}
|
257
|
|
258
|
CdmStore.getTermManager().setPreferredTerms(preferredTerms, getTerms());
|
259
|
|
260
|
CdmStore.getTermManager().clearTermMapForTermType(getTermType());
|
261
|
refresh(getTerms());
|
262
|
|
263
|
PreferencesUtil.firePreferencesChanged(this.getClass());
|
264
|
}
|
265
|
|
266
|
return super.performOk();
|
267
|
}
|
268
|
|
269
|
protected boolean checkNoneChecked(){
|
270
|
|
271
|
if(tableViewer!=null && tableViewer.getCheckedElements().length == 0){
|
272
|
setMessage("Please check at least one item", WARNING);
|
273
|
return true;
|
274
|
}else{
|
275
|
setMessage(null);
|
276
|
return false;
|
277
|
}
|
278
|
}
|
279
|
|
280
|
protected abstract TermType getTermType();
|
281
|
|
282
|
@Override
|
283
|
public ConversationHolder getConversationHolder() {
|
284
|
if(conversation == null){
|
285
|
conversation = CdmStore.createConversation();
|
286
|
}
|
287
|
return conversation;
|
288
|
}
|
289
|
|
290
|
@Override
|
291
|
public void update(CdmDataChangeMap changeEvents) {
|
292
|
// implement where needed
|
293
|
}
|
294
|
}
|