2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.preference
.menu
;
12 import java
.util
.ArrayList
;
13 import java
.util
.HashMap
;
14 import java
.util
.List
;
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
.IWorkbench
;
33 import org
.eclipse
.ui
.IWorkbenchPreferencePage
;
34 import org
.eclipse
.ui
.commands
.ICommandService
;
35 import org
.eclipse
.ui
.handlers
.IHandlerService
;
37 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
38 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
39 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
40 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
41 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
42 import eu
.etaxonomy
.taxeditor
.editor
.definedterm
.DefinedTermEditor
;
43 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
44 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
45 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
46 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
47 import eu
.etaxonomy
.taxeditor
.store
.TermStore
;
54 public abstract class AbstractMenuPreferences
<T
extends DefinedTermBase
> extends CdmPreferencePage
implements
55 IWorkbenchPreferencePage
, IConversationEnabled
{
57 private Button toggleButton
;
58 protected HashMap
<DefinedTermBase
<T
>, Button
> menuButtons
;
60 protected Button editButton
;
61 protected Button removeButton
;
63 protected CheckboxTableViewer tableViewer
;
65 private boolean state
= true;
66 private final boolean vocabularyIsEditable
;
67 private ConversationHolder conversation
;
70 * Constructs a new menu preference page.
72 * @param initialVocabulary
73 * the initial vocabulary containing all possible entries for the menu
75 * the title of the page
77 * describing text for the preference page
79 * whether this vocabulary should be editable. Will render "New", "Edit", "Delete" buttons
80 * Handler for these buttons have to be overriding addEditButtonListeners()
81 * @param <T> a T object.
83 public AbstractMenuPreferences(String title
, String description
, boolean editable
) {
85 vocabularyIsEditable
= editable
;
86 setDescription(description
);
89 protected List
<T
> getTerms(){
90 if(CdmStore
.isActive() && getTermType()!=null){
91 return TermStore
.getTerms(getTermType(), null);
93 return new ArrayList
<T
>();
99 * Create contents of the preference page
102 public Control
createContents(Composite parent
) {
104 Composite container
= new Composite(parent
, SWT
.NULL
);
105 final GridLayout gridLayout
= new GridLayout();
106 gridLayout
.numColumns
= 2;
107 container
.setLayout(gridLayout
);
109 tableViewer
= CheckboxTableViewer
.newCheckList(container
, SWT
.NULL
);
110 GridData tableLayoutData
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false);
111 tableLayoutData
.heightHint
= 300;
112 tableViewer
.getTable().setLayoutData(tableLayoutData
);
114 tableViewer
.setContentProvider(new DefinedTermBaseContentProvider());
115 tableViewer
.setLabelProvider(new DefinedTermBaseLabelProvider());
119 tableViewer
.addCheckStateListener(new ICheckStateListener() {
122 public void checkStateChanged(CheckStateChangedEvent arg0
) {
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());
133 if(vocabularyIsEditable
) {
134 createEditButtons(buttonContainer
);
137 toggleButton
= new Button(buttonContainer
, SWT
.PUSH
);
138 toggleButton
.setText("Toggle");
139 toggleButton
.addSelectionListener(new SelectionAdapter(){
141 public void widgetSelected(SelectionEvent e
) {
142 state
= state ?
false : true;
143 tableViewer
.setAllChecked(state
);
148 createAdditionalContent(container
);
153 protected void refresh(List
<T
> definedTerms
) {
154 tableViewer
.setInput(definedTerms
);
156 List
<T
> preferedTerms
= CdmStore
.getTermManager().getFilteredTerms(definedTerms
);
157 tableViewer
.setCheckedElements(preferedTerms
.toArray());
160 protected void createAdditionalContent(Composite container
) {
161 // implement where needed
164 protected void createEditButtons(Composite buttonContainer
) {
166 editButton
= new Button(buttonContainer
, SWT
.PUSH
);
167 editButton
.setText("Open Term Editor");
168 editButton
.setLayoutData(new GridData(SWT
.FILL
));
170 addEditButtonListeners();
174 * Implement this method in MenuPreference Pages where the vocabulary should be editable, editable flag
177 protected void addEditButtonListeners() {
178 editButton
.addSelectionListener(new SelectionAdapter(){
180 public void widgetSelected(SelectionEvent event
) {
182 ICommandService commandService
= (ICommandService
) AbstractUtility
.getService(ICommandService
.class);
184 Command command
= commandService
.getCommand(DefinedTermEditor
.OPEN_COMMAND_ID
);
186 IParameter parameter
;
188 parameter
= command
.getParameter("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid");
190 Parameterization
[] parameterizations
= new Parameterization
[]{
191 new Parameterization(parameter
, getTermType().getUuid().toString())
194 ParameterizedCommand pCommand
= new ParameterizedCommand(command
, parameterizations
);
196 if (command
.isEnabled()) {
197 IHandlerService handlerService
= (IHandlerService
) AbstractUtility
.getService(IHandlerService
.class);
199 handlerService
.executeCommand(pCommand
, null);
200 } catch (NotDefinedException e
) {
201 throw new RuntimeException("Could not find open command: "
202 + DefinedTermEditor
.OPEN_COMMAND_ID
);
203 } catch (Exception e
) {
206 "An exception occured while trying to open a selection",
210 } catch (NotDefinedException e
) {
213 "An exception occured while trying to open the Term Editor",
223 public void init(IWorkbench workbench
) {
224 setPreferenceStore(PreferencesUtil
.getPreferenceStore());
225 if(!CdmStore
.isActive()) {
226 MessagingUtils
.noDataSourceWarningDialog(null);
229 getConversationHolder().bind();
234 * <p>performDefaults</p>
237 protected void performDefaults() {
238 tableViewer
.setAllChecked(true);
241 public CheckboxTableViewer
getTableViewer() {
247 public boolean performOk() {
248 if(!CdmStore
.isActive()) {
252 if(checkNoneChecked()){
256 getConversationHolder().commit(true);
258 List
<T
> preferredTerms
= new ArrayList
<T
>();
259 for (Object element
: getTableViewer().getCheckedElements()){
260 preferredTerms
.add((T
) element
);
263 CdmStore
.getTermManager().setPreferredTerms(preferredTerms
, getTerms());
267 PreferencesUtil
.firePreferencesChanged(this.getClass());
270 return super.performOk();
273 private boolean checkNoneChecked(){
275 if(tableViewer
.getCheckedElements().length
== 0){
276 setMessage("Please check at least one item", WARNING
);
284 protected abstract TermType
getTermType();
287 public ConversationHolder
getConversationHolder() {
288 if(conversation
== null){
289 conversation
= CdmStore
.createConversation();
295 public void update(CdmDataChangeMap changeEvents
) {
296 // implement where needed