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