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
.ui
.group
.grantedauthority
;
12 import java
.util
.ArrayList
;
13 import java
.util
.EnumSet
;
14 import java
.util
.Iterator
;
15 import java
.util
.List
;
18 import org
.eclipse
.jface
.viewers
.ContentViewer
;
19 import org
.eclipse
.jface
.viewers
.ISelection
;
20 import org
.eclipse
.swt
.SWT
;
21 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
22 import org
.eclipse
.swt
.layout
.GridData
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
24 import org
.eclipse
.swt
.widgets
.Control
;
25 import org
.springframework
.security
.core
.GrantedAuthority
;
27 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
28 import eu
.etaxonomy
.cdm
.model
.common
.GrantedAuthorityImpl
;
29 import eu
.etaxonomy
.cdm
.model
.common
.Group
;
30 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.CRUD
;
31 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.CdmAuthority
;
32 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkableSelectionProvider
;
33 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
38 * Viewer class for the {@link CdmAuthorityComposite} used in the editing of CDM Authorities.
41 * @created Mar 28, 2013
46 //FIXME:move warning dialogs to the table class
47 public class CdmAuthorityCompositeViewer
extends ContentViewer
{
49 private IDirtyMarkableSelectionProvider dirtyMarkerEditor
;
50 private CdmAuthorityComposite cdmAuthorityComposite
;
52 private boolean isDirty
= false;
54 * List which contains already existing authority objects.
56 private List
<GrantedAuthorityImpl
> cdmAuthorities
;
58 * List which contains newly added (unsaved) authority objects.
59 * Used to make sure that new authority objects are always added
60 * to the top of the table.
62 private List
<GrantedAuthorityImpl
> newCdmAuthorities
;
66 * Creates a viewer with a {@link CdmAuthorityComposite} table, {@link Group} object as input
68 * @param composite parent of generated {@link CdmAuthorityComposite}
69 * @param dirtyMarkerEditor provider to mark as dirty
70 * @param group input data object
72 public CdmAuthorityCompositeViewer(Composite composite
,
73 IDirtyMarkableSelectionProvider dirtyMarkerEditor
,
75 this.dirtyMarkerEditor
= dirtyMarkerEditor
;
77 newCdmAuthorities
= new ArrayList
<GrantedAuthorityImpl
>();
80 ScrolledComposite sc
= new ScrolledComposite(composite
, SWT
.H_SCROLL
| SWT
.V_SCROLL
);
81 this.cdmAuthorityComposite
= new CdmAuthorityComposite(sc
, SWT
.H_SCROLL
| SWT
.V_SCROLL
, this);
82 sc
.setContent(cdmAuthorityComposite
);
83 sc
.setExpandHorizontal(true);
84 sc
.setExpandVertical(true);
90 * Gets the {@link CdmAuthorityComposite}
92 * @return {@link CdmAuthorityComposite} generated by this viewer
94 public CdmAuthorityComposite
getCdmAuthorityComposite() {
95 return cdmAuthorityComposite
;
100 * Retrieves the {@link GrantedAuthorityImpl} objects of the group attached to this viewer
101 * creates a sub-list of only {@link CdmAuthority} objects.
103 * @return list of {@link CdmAuthority} objects belonging to the input group
105 public List
<GrantedAuthorityImpl
> updateAuthorities() {
106 // get all granted authorities
107 Set
<GrantedAuthority
> grantedAuthorities
= group
.getGrantedAuthorities();
108 cdmAuthorities
= new ArrayList
<GrantedAuthorityImpl
>();
110 Iterator
<GrantedAuthority
> itr
= grantedAuthorities
.iterator();
111 while (itr
.hasNext()) {
112 GrantedAuthority grantedAuthority
= itr
.next();
115 if(grantedAuthority
!= null && grantedAuthority
instanceof GrantedAuthorityImpl
) {
116 // create a cdm authority from a granted authority
117 // this could fail in which case an exception is thrown
118 CdmAuthority cdmAuthority
= CdmAuthority
.fromGrantedAuthority(grantedAuthority
);
119 cdmAuthorities
.add((GrantedAuthorityImpl
)grantedAuthority
);
121 } catch (Exception e
) {
122 // not a CdmAuthority
123 //e.printStackTrace();
126 return cdmAuthorities
;
129 public boolean isDirty() {
133 * Adds a new {@link CdmAuthority} to the group attached to this viewer
134 * based on the input {@link CdmBase} object.
136 * @param cb {@link CdmBase} object used to construct the cdm authority
138 public void addCdmAuthority(CdmBase cb
) {
139 // Default permission is to READ
140 EnumSet
<CRUD
> defaultOperation
= EnumSet
.noneOf(CRUD
.class);
141 defaultOperation
.add(CRUD
.READ
);
142 CdmAuthority cdma
= new CdmAuthority(cb
, defaultOperation
, cb
.getUuid());
143 GrantedAuthorityImpl gai
;
146 // create a granted authrity from a cdm authority
147 gai
= cdma
.asNewGrantedAuthority();
148 } catch (Exception e
) {
149 MessagingUtils
.warningDialog("Parsing Error", this, "Could not parse authority string");
152 //FIXME : this contains call will allow users to add authorities which differ only in CRUD operation choice.
153 // need to have a comparator which only checks permission class and uuid
154 if(cdmAuthorities
.contains(gai
) || newCdmAuthorities
.contains(gai
)) {
155 MessagingUtils
.warningDialog("Duplicate CDM Authority", this, "Chosen CDM Authority is already attached to current group");
157 group
.addGrantedAuthority(gai
);
158 newCdmAuthorities
.add(gai
);
160 dirtyMarkerEditor
.changed(group
);
161 cdmAuthorityComposite
.refresh();
166 * Removes the given {@link GrantedAuthorityImpl} object from the list of
167 * granted authorities belonging to the group attached to this viewer.
169 * @param gai {@link GrantedAuthorityImpl} object to remove
171 public void removeCdmAuthority(GrantedAuthorityImpl gai
) {
173 if(cdmAuthorities
.contains(gai
)) {
174 cdmAuthorities
.remove(gai
);
177 newCdmAuthorities
.remove(gai
);
178 if(newCdmAuthorities
.isEmpty() && !isDirty
) {
182 group
.removeGrantedAuthority(gai
);
183 dirtyMarkerEditor
.changed(group
);
184 cdmAuthorityComposite
.refresh();
188 * Updates an existing {@link GrantedAuthorityImpl} object using a {@link CdmAuthority} object.
190 * @param grantedAuthorityI to update.
191 * @param cdmAuthority to use in updating the granted authority object.
193 public GrantedAuthorityImpl
updateGrantedAuthority(GrantedAuthorityImpl grantedAuthorityI
, CdmAuthority cdmAuthority
) {
194 if(grantedAuthorityI
.getAuthority().equals(cdmAuthority
.getAuthority())) {
198 // since granted authority is just a wrapper object around a string
199 // we can't really 'update' the granted authority.
200 // so we first add the new authority (hope no exception) is thrown
201 // and then remove the old one.
202 GrantedAuthorityImpl gai
= cdmAuthority
.asNewGrantedAuthority();
203 group
.addGrantedAuthority(gai
);
204 group
.removeGrantedAuthority(grantedAuthorityI
);
206 dirtyMarkerEditor
.changed(grantedAuthorityI
);
208 } catch(Exception e
) {
209 // Not a CDM Authority
210 //e.printStackTrace();
213 return grantedAuthorityI
;
217 * @return list of existing cdm authority objects
219 public List
<GrantedAuthorityImpl
> getCdmAuthorities() {
220 return cdmAuthorities
;
224 * @return list of newly added cdm authority objects
226 public List
<GrantedAuthorityImpl
> getNewCdmAuthorities() {
227 return newCdmAuthorities
;
232 * Empty the list of newly create autrity objects
234 public void clearNewCdmAuthorities() {
235 newCdmAuthorities
.clear();
244 clearNewCdmAuthorities();
250 * @see org.eclipse.jface.viewers.Viewer#getControl()
253 public Control
getControl() {
254 return cdmAuthorityComposite
;
258 * @see org.eclipse.jface.viewers.Viewer#getSelection()
261 public ISelection
getSelection() {
262 // TODO Auto-generated method stub
267 * @see org.eclipse.jface.viewers.Viewer#refresh()
270 public void refresh() {
272 cdmAuthorityComposite
.refresh();
277 * @see org.eclipse.jface.viewers.Viewer#setSelection(org.eclipse.jface.viewers.ISelection, boolean)
280 public void setSelection(ISelection selection
, boolean reveal
) {
281 // TODO Auto-generated method stub