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