1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2009 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
|
package eu.etaxonomy.taxeditor.editor.definedterm;
|
11
|
|
12
|
import java.util.HashSet;
|
13
|
import java.util.List;
|
14
|
import java.util.Set;
|
15
|
|
16
|
import org.eclipse.core.runtime.IProgressMonitor;
|
17
|
import org.eclipse.jface.action.GroupMarker;
|
18
|
import org.eclipse.jface.action.MenuManager;
|
19
|
import org.eclipse.jface.viewers.ISelection;
|
20
|
import org.eclipse.jface.viewers.StructuredSelection;
|
21
|
import org.eclipse.jface.viewers.TreeViewer;
|
22
|
import org.eclipse.jface.viewers.ViewerSorter;
|
23
|
import org.eclipse.swt.SWT;
|
24
|
import org.eclipse.swt.dnd.DND;
|
25
|
import org.eclipse.swt.dnd.Transfer;
|
26
|
import org.eclipse.swt.layout.FillLayout;
|
27
|
import org.eclipse.swt.widgets.Composite;
|
28
|
import org.eclipse.swt.widgets.Control;
|
29
|
import org.eclipse.swt.widgets.Menu;
|
30
|
import org.eclipse.ui.IEditorInput;
|
31
|
import org.eclipse.ui.IEditorSite;
|
32
|
import org.eclipse.ui.ISelectionListener;
|
33
|
import org.eclipse.ui.ISelectionService;
|
34
|
import org.eclipse.ui.IWorkbenchActionConstants;
|
35
|
import org.eclipse.ui.IWorkbenchPart;
|
36
|
import org.eclipse.ui.PartInitException;
|
37
|
import org.eclipse.ui.part.EditorPart;
|
38
|
|
39
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
40
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
41
|
import eu.etaxonomy.cdm.api.service.IVocabularyService;
|
42
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
43
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
44
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
45
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
46
|
import eu.etaxonomy.taxeditor.editor.definedterm.input.AbstractDefinedTermEditorInput;
|
47
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider;
|
48
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
49
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
50
|
import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory;
|
51
|
|
52
|
/**
|
53
|
* @author l.morris
|
54
|
* @date 3 Jan 2012
|
55
|
*
|
56
|
*/
|
57
|
public class DefinedTermEditor extends EditorPart implements IConversationEnabled, ISelectionListener, IDirtyMarkableSelectionProvider, IPostOperationEnabled {
|
58
|
|
59
|
public static final String ID = "eu.etaxonomy.taxeditor.editor.definedTerm";
|
60
|
|
61
|
public static final String OPEN_COMMAND_ID = "eu.etaxonomy.taxeditor.store.openDefinedTermEditor";
|
62
|
|
63
|
protected TreeViewer viewer;
|
64
|
protected ConversationHolder conversation;
|
65
|
private ISelectionService selectionService;
|
66
|
private boolean dirty;
|
67
|
Set<TermVocabulary<? extends DefinedTermBase>> inMemoryVocabularies = new HashSet<TermVocabulary<? extends DefinedTermBase>>();
|
68
|
|
69
|
private int dndOperations = DND.DROP_COPY | DND.DROP_MOVE;
|
70
|
/**
|
71
|
*
|
72
|
*/
|
73
|
public DefinedTermEditor() {
|
74
|
super();
|
75
|
conversation = CdmStore.createConversation();
|
76
|
}
|
77
|
|
78
|
@Override
|
79
|
public void createPartControl(Composite parent) {
|
80
|
|
81
|
// parent.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE));
|
82
|
|
83
|
FillLayout layout = new FillLayout();
|
84
|
layout.marginHeight = 0;
|
85
|
layout.marginWidth = 0;
|
86
|
layout.type = SWT.VERTICAL;
|
87
|
|
88
|
parent.setLayout(layout);
|
89
|
viewer = new TreeViewer(parent);
|
90
|
viewer.getControl().setLayoutData(CdmFormFactory.FILL());
|
91
|
viewer.setContentProvider(new TermContentProvider());
|
92
|
viewer.setLabelProvider(new TermLabelProvider());
|
93
|
viewer.setSorter(new ViewerSorter());
|
94
|
|
95
|
Transfer[] transfers = new Transfer[] { TermTransfer.getInstance() };
|
96
|
viewer.addDragSupport(dndOperations, transfers, new DefinedTermDragListener(viewer));
|
97
|
viewer.addDropSupport(dndOperations, transfers, new DefinedTermDropAdapter(this));
|
98
|
|
99
|
|
100
|
initialiseVocabularies();
|
101
|
viewer.setInput(inMemoryVocabularies);
|
102
|
|
103
|
getSite().setSelectionProvider(viewer);
|
104
|
|
105
|
selectionService = getSite().getWorkbenchWindow().getSelectionService();
|
106
|
selectionService.addSelectionListener(this);
|
107
|
|
108
|
setPartName(getEditorInput().getName());
|
109
|
createMenu();
|
110
|
}
|
111
|
|
112
|
|
113
|
protected void initialiseVocabularies() {
|
114
|
inMemoryVocabularies.clear();
|
115
|
//List<TermVocabulary> vocabularies = CdmStore.getService(IVocabularyService.class).list(null, null, null, null, null);
|
116
|
|
117
|
// for (TermVocabulary vocabulary : vocabularies){
|
118
|
// if(vocabulary.getTerms().isEmpty()){
|
119
|
// inMemoryVocabularies.add(vocabulary);
|
120
|
// }
|
121
|
// }
|
122
|
|
123
|
|
124
|
List<TermVocabulary<? extends DefinedTermBase>> termVocabularies = CdmStore.getService(IVocabularyService.class).listByTermClass(getDefinedTermEditorInput().getTermClass(), true, true, null, null, null, null);
|
125
|
|
126
|
|
127
|
inMemoryVocabularies.addAll(termVocabularies);
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
*
|
132
|
*/
|
133
|
public AbstractDefinedTermEditorInput getDefinedTermEditorInput() {
|
134
|
return (AbstractDefinedTermEditorInput) getEditorInput();
|
135
|
}
|
136
|
|
137
|
/* (non-Javadoc)
|
138
|
* @see eu.etaxonomy.taxeditor.operation.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
|
139
|
*/
|
140
|
@Override
|
141
|
public boolean postOperation(CdmBase objectAffectedByOperation) {
|
142
|
|
143
|
if(objectAffectedByOperation instanceof DefinedTermBase){
|
144
|
viewer.refresh();
|
145
|
} else if (objectAffectedByOperation instanceof TermVocabulary){
|
146
|
// if (inMemoryVocabularies.contains(objectAffectedByOperation)){
|
147
|
// inMemoryVocabularies.remove((TermVocabulary) objectAffectedByOperation);
|
148
|
// }else{
|
149
|
// inMemoryVocabularies.add((TermVocabulary) objectAffectedByOperation);
|
150
|
// DetailsViewPart view = (DetailsViewPart) StoreUtil.getView(DetailsViewPart.ID, true);
|
151
|
// view.setFocus();
|
152
|
// }
|
153
|
|
154
|
// viewer.setInput(inMemoryVocabularies);
|
155
|
viewer.refresh();
|
156
|
|
157
|
}
|
158
|
|
159
|
|
160
|
|
161
|
if(objectAffectedByOperation != null){
|
162
|
viewer.setSelection(new StructuredSelection(objectAffectedByOperation));
|
163
|
}
|
164
|
setDirty(true);
|
165
|
|
166
|
return true;
|
167
|
}
|
168
|
|
169
|
private void createMenu() {
|
170
|
// register context menu
|
171
|
MenuManager menuManager = new MenuManager();
|
172
|
menuManager
|
173
|
.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
|
174
|
getSite().registerContextMenu(ID, menuManager, viewer);
|
175
|
|
176
|
Control control = viewer.getControl();
|
177
|
Menu menu = menuManager.createContextMenu(control);
|
178
|
control.setMenu(menu);
|
179
|
}
|
180
|
|
181
|
|
182
|
|
183
|
|
184
|
@Override
|
185
|
public void doSave(IProgressMonitor monitor) {
|
186
|
getConversationHolder().commit();
|
187
|
setDirty(false);
|
188
|
initialiseVocabularies();
|
189
|
}
|
190
|
|
191
|
|
192
|
@Override
|
193
|
public void doSaveAs() {}
|
194
|
|
195
|
@Override
|
196
|
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
|
197
|
setSite(site);
|
198
|
setInput(input);
|
199
|
|
200
|
}
|
201
|
|
202
|
@Override
|
203
|
public boolean isDirty() {
|
204
|
return dirty;
|
205
|
}
|
206
|
|
207
|
@Override
|
208
|
public boolean isSaveAsAllowed() {
|
209
|
return false;
|
210
|
}
|
211
|
|
212
|
@Override
|
213
|
public void setFocus() {
|
214
|
getConversationHolder().bind();
|
215
|
}
|
216
|
|
217
|
@Override
|
218
|
public void update(CdmDataChangeMap changeEvents) {
|
219
|
// TODO Auto-generated method stub
|
220
|
|
221
|
}
|
222
|
|
223
|
@Override
|
224
|
public ConversationHolder getConversationHolder() {
|
225
|
return conversation;
|
226
|
}
|
227
|
|
228
|
@Override
|
229
|
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
|
230
|
// TODO Auto-generated method stub
|
231
|
|
232
|
}
|
233
|
|
234
|
@Override
|
235
|
public void changed(Object element) {
|
236
|
dirty = true;
|
237
|
setDirty(true);
|
238
|
viewer.update(element, null);
|
239
|
}
|
240
|
|
241
|
/**
|
242
|
* @param dirty the dirty to set
|
243
|
*/
|
244
|
public void setDirty(boolean dirty) {
|
245
|
this.dirty = dirty;
|
246
|
firePropertyChange(PROP_DIRTY);
|
247
|
}
|
248
|
|
249
|
@Override
|
250
|
public boolean onComplete() {
|
251
|
// TODO Auto-generated method stub
|
252
|
return false;
|
253
|
}
|
254
|
|
255
|
/**
|
256
|
*
|
257
|
*/
|
258
|
public TreeViewer getViewer() {
|
259
|
return viewer;
|
260
|
}
|
261
|
|
262
|
/**
|
263
|
* @return
|
264
|
*
|
265
|
*/
|
266
|
public Set<TermVocabulary<? extends DefinedTermBase>> getInMemoryVocabularies() {
|
267
|
return inMemoryVocabularies;
|
268
|
}
|
269
|
|
270
|
}
|