1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.taxeditor.preference.menu;
|
12
|
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.HashMap;
|
15
|
import java.util.List;
|
16
|
|
17
|
import org.eclipse.core.commands.Command;
|
18
|
import org.eclipse.core.commands.IParameter;
|
19
|
import org.eclipse.core.commands.Parameterization;
|
20
|
import org.eclipse.core.commands.ParameterizedCommand;
|
21
|
import org.eclipse.core.commands.common.NotDefinedException;
|
22
|
import org.eclipse.core.runtime.IStatus;
|
23
|
import org.eclipse.jface.preference.PreferencePage;
|
24
|
import org.eclipse.jface.viewers.CheckStateChangedEvent;
|
25
|
import org.eclipse.jface.viewers.CheckboxTableViewer;
|
26
|
import org.eclipse.jface.viewers.ICheckStateListener;
|
27
|
import org.eclipse.jface.wizard.WizardDialog;
|
28
|
import org.eclipse.swt.SWT;
|
29
|
import org.eclipse.swt.events.SelectionAdapter;
|
30
|
import org.eclipse.swt.events.SelectionEvent;
|
31
|
import org.eclipse.swt.layout.GridData;
|
32
|
import org.eclipse.swt.layout.GridLayout;
|
33
|
import org.eclipse.swt.widgets.Button;
|
34
|
import org.eclipse.swt.widgets.Composite;
|
35
|
import org.eclipse.swt.widgets.Control;
|
36
|
import org.eclipse.ui.IWorkbench;
|
37
|
import org.eclipse.ui.IWorkbenchPreferencePage;
|
38
|
import org.eclipse.ui.commands.ICommandService;
|
39
|
import org.eclipse.ui.handlers.IHandlerService;
|
40
|
|
41
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
42
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
43
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
44
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
45
|
import eu.etaxonomy.taxeditor.editor.definedterm.DefinedTermEditor;
|
46
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
47
|
import eu.etaxonomy.taxeditor.preference.wizard.VocabularyTermWizard;
|
48
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
49
|
import eu.etaxonomy.taxeditor.store.StoreUtil;
|
50
|
import eu.etaxonomy.taxeditor.store.TermStore;
|
51
|
|
52
|
/**
|
53
|
* <p>Abstract AbstractMenuPreferences class.</p>
|
54
|
*
|
55
|
* @author n.hoffmann
|
56
|
* @created 12.06.2009
|
57
|
* @version 1.0
|
58
|
*/
|
59
|
public abstract class AbstractMenuPreferences<T extends DefinedTermBase> extends PreferencePage implements
|
60
|
IWorkbenchPreferencePage, IConversationEnabled{
|
61
|
|
62
|
private Button toggleButton;
|
63
|
protected HashMap<DefinedTermBase<T>, Button> menuButtons;
|
64
|
|
65
|
protected Button newButton;
|
66
|
protected Button editButton;
|
67
|
protected Button removeButton;
|
68
|
|
69
|
protected CheckboxTableViewer tableViewer;
|
70
|
|
71
|
private boolean state = true;
|
72
|
private boolean vocabularyIsEditable;
|
73
|
private ConversationHolder conversation;
|
74
|
|
75
|
/**
|
76
|
* Constructs a new menu preference page.
|
77
|
*
|
78
|
* @param initialVocabulary
|
79
|
* the initial vocabulary containing all possible entries for the menu
|
80
|
* @param title
|
81
|
* the title of the page
|
82
|
* @param description
|
83
|
* describing text for the preference page
|
84
|
* @param editable
|
85
|
* whether this vocabulary should be editable. Will render "New", "Edit", "Delete" buttons
|
86
|
* Handler for these buttons have to be overriding addEditButtonListeners()
|
87
|
* @param <T> a T object.
|
88
|
*/
|
89
|
public AbstractMenuPreferences(String title, String description, boolean editable) {
|
90
|
super(title);
|
91
|
vocabularyIsEditable = editable;
|
92
|
setDescription(description);
|
93
|
}
|
94
|
|
95
|
protected List<T> getTerms(){
|
96
|
return TermStore.getTerms(getTermClass());
|
97
|
}
|
98
|
|
99
|
/**
|
100
|
* {@inheritDoc}
|
101
|
*
|
102
|
* Create contents of the preference page
|
103
|
*/
|
104
|
@Override
|
105
|
public Control createContents(Composite parent) {
|
106
|
|
107
|
Composite container = new Composite(parent, SWT.NULL);
|
108
|
final GridLayout gridLayout = new GridLayout();
|
109
|
gridLayout.numColumns = 2;
|
110
|
container.setLayout(gridLayout);
|
111
|
|
112
|
tableViewer = CheckboxTableViewer.newCheckList(container, SWT.NULL);
|
113
|
GridData tableLayoutData = new GridData(SWT.FILL, SWT.FILL, true, false);
|
114
|
tableLayoutData.heightHint = 300;
|
115
|
tableViewer.getTable().setLayoutData(tableLayoutData);
|
116
|
|
117
|
tableViewer.setContentProvider(new DefinedTermBaseContentProvider());
|
118
|
tableViewer.setLabelProvider(new DefinedTermBaseLabelProvider());
|
119
|
|
120
|
refresh(getTerms());
|
121
|
|
122
|
tableViewer.addCheckStateListener(new ICheckStateListener() {
|
123
|
|
124
|
@Override
|
125
|
public void checkStateChanged(CheckStateChangedEvent arg0) {
|
126
|
checkNoneChecked();
|
127
|
}
|
128
|
});
|
129
|
|
130
|
Composite buttonContainer = new Composite(container, SWT.NULL);
|
131
|
GridData buttonContainerLayoutData = new GridData();
|
132
|
buttonContainerLayoutData.verticalAlignment = SWT.TOP;
|
133
|
buttonContainer.setLayoutData(buttonContainerLayoutData);
|
134
|
buttonContainer.setLayout(new GridLayout());
|
135
|
|
136
|
if(vocabularyIsEditable) createEditButtons(buttonContainer);
|
137
|
|
138
|
toggleButton = new Button(buttonContainer, SWT.PUSH);
|
139
|
toggleButton.setText("Toggle");
|
140
|
toggleButton.addSelectionListener(new SelectionAdapter(){
|
141
|
/* (non-Javadoc)
|
142
|
* @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
|
143
|
*/
|
144
|
@Override
|
145
|
public void widgetSelected(SelectionEvent e) {
|
146
|
state = state ? false : true;
|
147
|
tableViewer.setAllChecked(state);
|
148
|
checkNoneChecked();
|
149
|
}
|
150
|
});
|
151
|
|
152
|
createAdditionalContent(container);
|
153
|
|
154
|
return container;
|
155
|
}
|
156
|
|
157
|
|
158
|
/**
|
159
|
* <p>refresh</p>
|
160
|
*
|
161
|
* @param definedTerms a {@link java.util.List} object.
|
162
|
*/
|
163
|
protected void refresh(List<T> definedTerms) {
|
164
|
tableViewer.setInput(definedTerms);
|
165
|
|
166
|
List<T> preferedTerms = CdmStore.getTermManager().getFilteredTerms(definedTerms);
|
167
|
tableViewer.setCheckedElements(preferedTerms.toArray());
|
168
|
}
|
169
|
|
170
|
/**
|
171
|
* <p>createAdditionalContent</p>
|
172
|
*
|
173
|
* @param container a {@link org.eclipse.swt.widgets.Composite} object.
|
174
|
*/
|
175
|
protected void createAdditionalContent(Composite container) {
|
176
|
// implement where needed
|
177
|
}
|
178
|
|
179
|
/* (non-Javadoc)
|
180
|
* @see eu.etaxonomy.taxeditor.preference.AbstractMenuPreferences#createButtons(org.eclipse.swt.widgets.Composite)
|
181
|
*/
|
182
|
/**
|
183
|
* <p>createEditButtons</p>
|
184
|
*
|
185
|
* @param buttonContainer a {@link org.eclipse.swt.widgets.Composite} object.
|
186
|
*/
|
187
|
protected void createEditButtons(Composite buttonContainer) {
|
188
|
|
189
|
editButton = new Button(buttonContainer, SWT.PUSH);
|
190
|
editButton.setText("Open Term Editor");
|
191
|
editButton.setLayoutData(new GridData(SWT.FILL));
|
192
|
|
193
|
addEditButtonListeners();
|
194
|
}
|
195
|
|
196
|
/**
|
197
|
* Implement this method in MenuPreference Pages where the vocabulary should be editable, editable flag
|
198
|
* is set.
|
199
|
*/
|
200
|
protected void addNewButtonListeners() {
|
201
|
newButton.addSelectionListener(new SelectionAdapter(){
|
202
|
/* (non-Javadoc)
|
203
|
* @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
|
204
|
*/
|
205
|
@Override
|
206
|
public void widgetSelected(SelectionEvent e) {
|
207
|
VocabularyTermWizard<T> wizard = new VocabularyTermWizard<T>(getTermClass(), getConversationHolder());
|
208
|
|
209
|
WizardDialog dialog = new WizardDialog(StoreUtil.getShell(), wizard);
|
210
|
if(dialog.open() == IStatus.OK){
|
211
|
tableViewer.setInput(getTerms());
|
212
|
}
|
213
|
}
|
214
|
|
215
|
|
216
|
});
|
217
|
}
|
218
|
|
219
|
/**
|
220
|
* Implement this method in MenuPreference Pages where the vocabulary should be editable, editable flag
|
221
|
* is set.
|
222
|
*/
|
223
|
protected void addEditButtonListeners() {
|
224
|
editButton.addSelectionListener(new SelectionAdapter(){
|
225
|
/* (non-Javadoc)
|
226
|
* @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
|
227
|
*/
|
228
|
@Override
|
229
|
public void widgetSelected(SelectionEvent event) {
|
230
|
|
231
|
ICommandService commandService = (ICommandService) StoreUtil.getService(ICommandService.class);
|
232
|
|
233
|
Command command = commandService.getCommand(DefinedTermEditor.OPEN_COMMAND_ID);
|
234
|
|
235
|
IParameter parameter;
|
236
|
try {
|
237
|
parameter = command.getParameter("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.parameter");
|
238
|
|
239
|
Parameterization[] parameterizations = new Parameterization[]{
|
240
|
new Parameterization(parameter, getTermClass().getName())
|
241
|
};
|
242
|
|
243
|
ParameterizedCommand pCommand = new ParameterizedCommand(command, parameterizations);
|
244
|
|
245
|
if (command.isEnabled()) {
|
246
|
IHandlerService handlerService = (IHandlerService) StoreUtil.getService(IHandlerService.class);
|
247
|
try {
|
248
|
handlerService.executeCommand(pCommand, null);
|
249
|
} catch (NotDefinedException e) {
|
250
|
throw new RuntimeException("Could not find open command: "
|
251
|
+ DefinedTermEditor.OPEN_COMMAND_ID);
|
252
|
} catch (Exception e) {
|
253
|
StoreUtil
|
254
|
.error(getClass(),
|
255
|
"An exception occured while trying to open a selection",
|
256
|
e);
|
257
|
}
|
258
|
}
|
259
|
} catch (NotDefinedException e) {
|
260
|
StoreUtil
|
261
|
.error(getClass(),
|
262
|
"An exception occured while trying to open the Term Editor",
|
263
|
e);
|
264
|
}
|
265
|
|
266
|
}
|
267
|
});
|
268
|
}
|
269
|
|
270
|
/** {@inheritDoc} */
|
271
|
public void init(IWorkbench workbench) {
|
272
|
setPreferenceStore(PreferencesUtil.getPreferenceStore());
|
273
|
getConversationHolder().bind();
|
274
|
}
|
275
|
|
276
|
/*
|
277
|
* (non-Javadoc)
|
278
|
* @see org.eclipse.jface.preference.PreferencePage#performDefaults()
|
279
|
*/
|
280
|
/**
|
281
|
* <p>performDefaults</p>
|
282
|
*/
|
283
|
protected void performDefaults() {
|
284
|
tableViewer.setAllChecked(true);
|
285
|
}
|
286
|
|
287
|
/**
|
288
|
* <p>Getter for the field <code>tableViewer</code>.</p>
|
289
|
*
|
290
|
* @return the tableViewer
|
291
|
*/
|
292
|
public CheckboxTableViewer getTableViewer() {
|
293
|
return tableViewer;
|
294
|
}
|
295
|
|
296
|
/* (non-Javadoc)
|
297
|
* @see org.eclipse.jface.preference.PreferencePage#performOk()
|
298
|
*/
|
299
|
/** {@inheritDoc} */
|
300
|
@Override
|
301
|
public boolean performOk() {
|
302
|
if(checkNoneChecked()){
|
303
|
return false;
|
304
|
}
|
305
|
|
306
|
getConversationHolder().commit(true);
|
307
|
|
308
|
List<T> preferredTerms = new ArrayList<T>();
|
309
|
for (Object element : getTableViewer().getCheckedElements()){
|
310
|
preferredTerms.add((T) element);
|
311
|
}
|
312
|
|
313
|
CdmStore.getTermManager().setPreferredTerms(preferredTerms, getTerms());
|
314
|
|
315
|
PreferencesUtil.firePreferencesChanged(this.getClass());
|
316
|
|
317
|
return true;
|
318
|
}
|
319
|
|
320
|
private boolean checkNoneChecked(){
|
321
|
|
322
|
if(tableViewer.getCheckedElements().length == 0){
|
323
|
setMessage("Please check at least one item", WARNING);
|
324
|
return true;
|
325
|
}else{
|
326
|
setMessage(null);
|
327
|
return false;
|
328
|
}
|
329
|
}
|
330
|
|
331
|
/**
|
332
|
* <p>getTermClass</p>
|
333
|
*
|
334
|
* @return a {@link java.lang.Class} object.
|
335
|
*/
|
336
|
protected abstract Class<T> getTermClass();
|
337
|
|
338
|
/* (non-Javadoc)
|
339
|
* @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
|
340
|
*/
|
341
|
@Override
|
342
|
public ConversationHolder getConversationHolder() {
|
343
|
if(conversation == null){
|
344
|
conversation = CdmStore.createConversation();
|
345
|
}
|
346
|
return conversation;
|
347
|
}
|
348
|
|
349
|
/* (non-Javadoc)
|
350
|
* @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
|
351
|
*/
|
352
|
@Override
|
353
|
public void update(CdmDataChangeMap changeEvents) {
|
354
|
// implement where needed
|
355
|
}
|
356
|
}
|