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