Merge branch 'release/4.6.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / group / authority / CdmAuthorityEditor.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.taxeditor.editor.group.authority;
10
11 import org.eclipse.core.runtime.IProgressMonitor;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.layout.FillLayout;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.ui.IEditorInput;
16 import org.eclipse.ui.IEditorSite;
17 import org.eclipse.ui.PartInitException;
18 import org.eclipse.ui.PlatformUI;
19 import org.eclipse.ui.part.EditorPart;
20
21 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
22 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
23 import eu.etaxonomy.cdm.model.common.Group;
24 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
25 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
26 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
27 import eu.etaxonomy.taxeditor.ui.group.grantedauthority.CdmAuthorityCompositeViewer;
28
29 /**
30 * Editor responsible for editing authorities related to a particular {@link Group} entity.
31 *
32 * @author cmathew
33 * @created Mar 28, 2013
34 *
35 */
36 public class CdmAuthorityEditor extends EditorPart implements IConversationEnabled, IDirtyMarkable {
37
38 public static final String ID = "eu.etaxonomy.taxeditor.editor.group.authority"; //$NON-NLS-1$
39
40 private boolean dirty;
41 private ConversationHolder conversation;
42 private CdmAuthorityCompositeViewer viewer;
43
44 public CdmAuthorityEditor() {
45
46 }
47
48 /**
49 * Create contents of the editor part.
50 * @param parent
51 */
52 @Override
53 public void createPartControl(Composite parent) {
54 Composite container = new Composite(parent, SWT.NONE);
55 container.setLayout(new FillLayout(SWT.HORIZONTAL));
56
57 this.viewer = new CdmAuthorityCompositeViewer(container, this,((CdmAuthorityEditorInput) getEditorInput()).getGroup());
58
59 conversation = ((CdmAuthorityEditorInput) getEditorInput()).getConversationHolder();
60 setPartName(getEditorInput().getName());
61
62 }
63
64 /* (non-Javadoc)
65 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
66 */
67 @Override
68 public void setFocus() {
69 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().setFocus();
70 ((CdmAuthorityEditorInput) getEditorInput()).bind();
71 }
72
73 /* (non-Javadoc)
74 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
75 */
76 @Override
77 public void doSave(IProgressMonitor monitor) {
78 try {
79 monitor.beginTask(Messages.CdmAuthorityEditor_SAVING_AUTHORITY_EDITOR, 1);
80 getConversationHolder().commit(true);
81 ((CdmAuthorityEditorInput) getEditorInput()).merge();
82 dirty = false;
83 firePropertyChange(PROP_DIRTY);
84 monitor.worked(1);
85 } finally {
86 monitor.done();
87 viewer.save();
88 }
89 }
90
91 /* (non-Javadoc)
92 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
93 */
94 @Override
95 public void doSaveAs() {
96 // Do the Save As operation
97 }
98
99 /* (non-Javadoc)
100 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
101 */
102 @Override
103 public void init(IEditorSite site, IEditorInput input)
104 throws PartInitException {
105 setSite(site);
106 setInput(input);
107 }
108
109 /* (non-Javadoc)
110 * @see org.eclipse.ui.part.EditorPart#isDirty()
111 */
112 @Override
113 public boolean isDirty() {
114 return dirty;
115 }
116
117 /* (non-Javadoc)
118 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
119 */
120 @Override
121 public boolean isSaveAsAllowed() {
122 return false;
123 }
124
125 /* (non-Javadoc)
126 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
127 */
128 @Override
129 public void update(CdmDataChangeMap changeEvents) {
130 // TODO Auto-generated method stub
131
132 }
133
134 /* (non-Javadoc)
135 * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#changed(java.lang.Object)
136 */
137 @Override
138 public void changed(Object element) {
139 //FIXME : should be optimised to split changes into adding / removing authorities
140 // vs updating authorites
141 dirty = viewer.isDirty();
142 firePropertyChange(PROP_DIRTY);
143 // if the change has happened on the group then refresh the table
144 if(element instanceof Group) {
145
146 //FIXME: activating (setting focus) on the editor happens on every change
147 // This should be changed to only when the drop is successful
148 getSite().getPage().activate(this);
149 }
150
151 }
152
153 /* (non-Javadoc)
154 * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
155 */
156 @Override
157 public void forceDirty() {
158 changed(null);
159 }
160
161 /* (non-Javadoc)
162 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
163 */
164 @Override
165 public ConversationHolder getConversationHolder() {
166 return conversation;
167 }
168
169 /*
170 * (non-Javadoc)
171 *
172 * @see org.eclipse.ui.forms.editor.FormEditor#dispose()
173 */
174 @Override
175 public void dispose() {
176 conversation.unregisterForDataStoreChanges(this);
177 conversation.close();
178 ((CdmAuthorityEditorInput) getEditorInput()).getCdmEntitySession().dispose();
179 super.dispose();
180 }
181 }