Project

General

Profile

Download (9.59 KB) Statistics
| Branch: | Tag: | Revision:
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.model.common.DefinedTermBase;
38
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
39
import eu.etaxonomy.taxeditor.preference.wizard.VocabularyTermWizard;
40
import eu.etaxonomy.taxeditor.store.CdmStore;
41
import eu.etaxonomy.taxeditor.store.StoreUtil;
42
import eu.etaxonomy.taxeditor.store.TermStore;
43

    
44
/**
45
 * <p>Abstract AbstractMenuPreferences class.</p>
46
 *
47
 * @author n.hoffmann
48
 * @created 12.06.2009
49
 * @version 1.0
50
 */
51
public abstract class AbstractMenuPreferences<T extends DefinedTermBase> extends PreferencePage implements
52
		IWorkbenchPreferencePage{
53

    
54
	private Button toggleButton;
55
	protected HashMap<DefinedTermBase<T>, Button> menuButtons;
56
	
57
	protected Button newButton;
58
	protected Button editButton;
59
	protected Button removeButton;
60
	
61
	protected List<T> terms;
62
	
63
	protected CheckboxTableViewer tableViewer;
64
	
65
	private boolean state = true;
66
	private boolean vocabularyIsEditable;
67
	
68
	/**
69
	 * Constructs a new menu preference page.
70
	 *
71
	 * @param initialVocabulary
72
	 * 			the initial vocabulary containing all possible entries for the menu
73
	 * @param title
74
	 * 			the title of the page
75
	 * @param description
76
	 * 			describing text for the preference page
77
	 * @param editable
78
	 * 			whether this vocabulary should be editable. Will render "New", "Edit", "Delete" buttons
79
	 * 			Handler for these buttons have to be overriding addEditButtonListeners()
80
	 * @param <T> a T object.
81
	 */
82
	public AbstractMenuPreferences(List<T> initialVocabulary, String title, String description, boolean editable) {
83
		super(title);
84
		terms = initialVocabulary;
85
		vocabularyIsEditable = editable;
86
		setDescription(description);
87
	}
88

    
89
	/**
90
	 * {@inheritDoc}
91
	 *
92
	 * Create contents of the preference page
93
	 */
94
	@Override
95
	public Control createContents(Composite parent) {
96
		
97
		Composite container = new Composite(parent, SWT.NULL);
98
		final GridLayout gridLayout = new GridLayout();
99
		gridLayout.numColumns = 2;
100
		container.setLayout(gridLayout);
101
	
102
		tableViewer = CheckboxTableViewer.newCheckList(container, SWT.NULL);
103
		GridData tableLayoutData = new GridData(SWT.FILL, SWT.FILL, true, false);
104
		tableLayoutData.heightHint = 300;
105
		tableViewer.getTable().setLayoutData(tableLayoutData);
106
		
107
		tableViewer.setContentProvider(new DefinedTermBaseContentProvider());
108
		tableViewer.setLabelProvider(new DefinedTermBaseLabelProvider());
109

    
110
		refresh(terms);
111
		
112
		tableViewer.addCheckStateListener(new ICheckStateListener() {
113
			
114
			@Override
115
			public void checkStateChanged(CheckStateChangedEvent arg0) {
116
				checkNoneChecked();
117
			}
118
		});
119
		
120
		Composite buttonContainer = new Composite(container, SWT.NULL);
121
		GridData buttonContainerLayoutData = new GridData();
122
		buttonContainerLayoutData.verticalAlignment = SWT.TOP;
123
		buttonContainer.setLayoutData(buttonContainerLayoutData);
124
		buttonContainer.setLayout(new GridLayout());
125
		
126
		if(vocabularyIsEditable) createEditButtons(buttonContainer);
127
		
128
		toggleButton = new Button(buttonContainer, SWT.PUSH);
129
		toggleButton.setText("Toggle");
130
		toggleButton.addSelectionListener(new SelectionAdapter(){
131
			/* (non-Javadoc)
132
			 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
133
			 */
134
			@Override
135
			public void widgetSelected(SelectionEvent e) {
136
				state = state ? false : true;
137
				tableViewer.setAllChecked(state);
138
				checkNoneChecked();
139
			}
140
		});
141
		
142
		createAdditionalContent(container);
143
		
144
		return container;
145
	}
146

    
147

    
148
	/**
149
	 * <p>refresh</p>
150
	 *
151
	 * @param definedTerms a {@link java.util.List} object.
152
	 */
153
	protected void refresh(List<T> definedTerms) {
154
		tableViewer.setInput(definedTerms);
155

    
156
		List<T> preferedTerms = PreferencesUtil.getPreferredTerms(definedTerms);
157
		tableViewer.setCheckedElements(preferedTerms.toArray());
158
	}
159

    
160
	/**
161
	 * <p>createAdditionalContent</p>
162
	 *
163
	 * @param container a {@link org.eclipse.swt.widgets.Composite} object.
164
	 */
165
	protected void createAdditionalContent(Composite container) {
166
		// implement where needed		
167
	}
168

    
169
	/* (non-Javadoc)
170
	 * @see eu.etaxonomy.taxeditor.preference.AbstractMenuPreferences#createButtons(org.eclipse.swt.widgets.Composite)
171
	 */
172
	/**
173
	 * <p>createEditButtons</p>
174
	 *
175
	 * @param buttonContainer a {@link org.eclipse.swt.widgets.Composite} object.
176
	 */
177
	protected void createEditButtons(Composite buttonContainer) {
178
		newButton = new Button(buttonContainer, SWT.PUSH);
179
		newButton.setText("New");
180
		newButton.setLayoutData(new GridData(SWT.FILL));
181

    
182
		
183
		editButton = new Button(buttonContainer, SWT.PUSH);
184
		editButton.setText("Edit");
185
		editButton.setLayoutData(new GridData(SWT.FILL));
186

    
187
		
188
		removeButton = new Button(buttonContainer, SWT.PUSH);
189
		removeButton.setText("Remove");
190
		removeButton.setLayoutData(new GridData(SWT.FILL));
191

    
192
		
193
		addNewButtonListeners();
194
		addEditButtonListeners();
195
		addDeleteButtonListeners();
196
	}
197

    
198
	/**
199
	 * Implement this method in MenuPreference Pages where the vocabulary should be editable, editable flag
200
	 * is set.
201
	 */
202
	protected void addNewButtonListeners() {
203
		newButton.addSelectionListener(new SelectionAdapter(){
204
			/* (non-Javadoc)
205
			 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
206
			 */
207
			@Override
208
			public void widgetSelected(SelectionEvent e) {
209
				VocabularyTermWizard<T> wizard = new VocabularyTermWizard<T>(getTermClass()); 
210
				
211
				WizardDialog dialog = new WizardDialog(StoreUtil.getShell(), wizard);
212
			    if(dialog.open() == IStatus.OK){
213
			    	tableViewer.setInput(TermStore.getFeatures());
214
			    }
215
			}
216

    
217
			
218
		});
219
	}
220
	
221
	/**
222
	 * Implement this method in MenuPreference Pages where the vocabulary should be editable, editable flag
223
	 * is set.
224
	 */
225
	protected void addEditButtonListeners() {
226
		editButton.addSelectionListener(new SelectionAdapter(){
227
			/* (non-Javadoc)
228
			 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
229
			 */
230
			@Override
231
			public void widgetSelected(SelectionEvent e) {
232
				T selection = (T) ((StructuredSelection) tableViewer.getSelection()).getFirstElement();
233
				
234
				VocabularyTermWizard<T> wizard = new VocabularyTermWizard<T>(getTermClass(), selection); 
235
				
236
				WizardDialog dialog = new WizardDialog(StoreUtil.getShell(), wizard);
237
			    dialog.open();
238
			    
239
			    tableViewer.setInput(TermStore.getFeatures());
240
			    tableViewer.reveal(selection);
241
			}
242
		});
243
	}
244
	
245
	/**
246
	 * Implement this method in MenuPreference Pages where the vocabulary should be editable, editable flag
247
	 * is set.
248
	 */
249
	protected void addDeleteButtonListeners() {
250
		removeButton.addSelectionListener(new SelectionAdapter(){
251
			/* (non-Javadoc)
252
			 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
253
			 */
254
			@Override
255
			public void widgetSelected(SelectionEvent e) {
256
				boolean confirmation = StoreUtil.confirmDialog("Confirm deletion", "Do you really want to delete the selected terms?");
257
				
258
				if(confirmation){
259
					IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
260
					
261
					Iterator<T> selectionIterator = selection.iterator();
262
					
263
					while(selectionIterator.hasNext()){
264
						CdmStore.getTermService().delete(selectionIterator.next());
265
					}
266
				}
267
			}
268
		});
269
	}
270

    
271
	/** {@inheritDoc} */
272
	public void init(IWorkbench workbench) {
273
		setPreferenceStore(PreferencesUtil.getPreferenceStore());
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
		List<T> preferredTerms = new ArrayList<T>();
307
		for (Object element : getTableViewer().getCheckedElements()){
308
			preferredTerms.add((T) element);
309
		}
310
		
311
		PreferencesUtil.setPreferredTerms(preferredTerms, this.terms);
312
		
313
		PreferencesUtil.firePreferencesChanged(this.getClass());
314
		
315
		return true;
316
	}
317
	
318
	private boolean checkNoneChecked(){
319
		
320
		if(tableViewer.getCheckedElements().length == 0){
321
			setMessage("Please check at least one item", WARNING);
322
			return true;
323
		}else{
324
			setMessage(null);
325
			return false;
326
		}
327
	}
328
	
329
	/**
330
	 * <p>getTermClass</p>
331
	 *
332
	 * @return a {@link java.lang.Class} object.
333
	 */
334
	protected abstract Class<T> getTermClass();
335
}
(1-1/18)