fix selection dialog after last changes
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / group / grantedauthority / CdmAuthorityCompositeViewer.java
index cb910b32c9960407c9c3ae5f7c7b6c3c5b05f61a..643544def754a7899665816b38b3976e635f77ff 100644 (file)
@@ -1,8 +1,8 @@
 /**
 * Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy 
+* European Distributed Institute of Taxonomy
 * http://www.e-taxonomy.eu
-* 
+*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * See LICENSE.TXT at the top of this package for the full license terms.
 */
@@ -18,6 +18,7 @@ import java.util.Set;
 import org.eclipse.jface.viewers.ContentViewer;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.ScrolledComposite;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
 import org.springframework.security.core.GrantedAuthority;
@@ -27,159 +28,168 @@ import eu.etaxonomy.cdm.model.common.GrantedAuthorityImpl;
 import eu.etaxonomy.cdm.model.common.Group;
 import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
 import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmAuthority;
-import eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider;
-import eu.etaxonomy.taxeditor.store.StoreUtil;
+import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
 
 
 
 /**
  * Viewer class for the {@link CdmAuthorityComposite} used in the editing of CDM Authorities.
- * 
+ *
  * @author cmathew
  * @created Mar 28, 2013
  *
  */
 
 
-/**
- * @author cmathew
- * @date Apr 5, 2013
- *
- */
-
 //FIXME:move warning dialogs to the table class
 public class CdmAuthorityCompositeViewer extends ContentViewer {
-       
-       private IDirtyMarkableSelectionProvider dirtyMarkerEditor;      
+
+       private IDirtyMarkable dirtyMarkerEditor;
        private CdmAuthorityComposite cdmAuthorityComposite;
        private Group group;
+       private boolean isDirty = false;
        /**
         * List which contains already existing authority objects.
         */
        private List<GrantedAuthorityImpl> cdmAuthorities;
        /**
         * List which contains newly added (unsaved) authority objects.
-        * Used to make sure that new authority objects are always added 
+        * Used to make sure that new authority objects are always added
         * to the top of the table.
         */
        private List<GrantedAuthorityImpl> newCdmAuthorities;
-       
-       
+
+
        /**
-        * Creates a viewer with a {@link CdmAuthorityComposite} table, {@link Group} object as input 
-        * 
+        * Creates a viewer with a {@link CdmAuthorityComposite} table, {@link Group} object as input
+        *
         * @param composite parent of generated {@link CdmAuthorityComposite}
         * @param dirtyMarkerEditor provider to mark as dirty
         * @param group input data object
         */
        public CdmAuthorityCompositeViewer(Composite composite,
-                       IDirtyMarkableSelectionProvider dirtyMarkerEditor, 
+                       IDirtyMarkable dirtyMarkerEditor,
                        Group group) {
                this.dirtyMarkerEditor = dirtyMarkerEditor;
                this.group = group;
-               newCdmAuthorities = new ArrayList<GrantedAuthorityImpl>();              
+               newCdmAuthorities = new ArrayList<GrantedAuthorityImpl>();
                updateAuthorities();
-               this.cdmAuthorityComposite = new CdmAuthorityComposite(composite, SWT.NONE, this);
+
+               ScrolledComposite sc = new ScrolledComposite(composite, SWT.H_SCROLL | SWT.V_SCROLL);
+               this.cdmAuthorityComposite = new CdmAuthorityComposite(sc, SWT.H_SCROLL | SWT.V_SCROLL, this);
+               sc.setContent(cdmAuthorityComposite);
+               sc.setExpandHorizontal(true);
+               sc.setExpandVertical(true);
        }
 
        /**
         * Gets the {@link CdmAuthorityComposite}
-        * 
+        *
         * @return {@link CdmAuthorityComposite} generated by this viewer
         */
        public CdmAuthorityComposite getCdmAuthorityComposite() {
                return cdmAuthorityComposite;
        }
-       
-       
+
+
        /**
         * Retrieves the {@link GrantedAuthorityImpl} objects of the group attached to this viewer
         * creates a sub-list of only {@link CdmAuthority} objects.
-        * 
+        *
         * @return list of {@link CdmAuthority} objects belonging to the input group
         */
        public List<GrantedAuthorityImpl> updateAuthorities()  {
                // get all granted authorities
                Set<GrantedAuthority> grantedAuthorities = group.getGrantedAuthorities();
-               cdmAuthorities = new ArrayList<GrantedAuthorityImpl>(); 
-               
-               Iterator<GrantedAuthority> itr = grantedAuthorities.iterator();         
+               cdmAuthorities = new ArrayList<GrantedAuthorityImpl>();
+
+               Iterator<GrantedAuthority> itr = grantedAuthorities.iterator();
                while (itr.hasNext()) {
                        GrantedAuthority grantedAuthority = itr.next();
                        try {
-                               
-                               if(grantedAuthority != null && grantedAuthority instanceof GrantedAuthorityImpl) {                      
+
+                               if(grantedAuthority != null && grantedAuthority instanceof GrantedAuthorityImpl) {
                                        // create a cdm authority from a granted authority
                                        // this could fail in which case an exception is thrown
                                        CdmAuthority cdmAuthority = CdmAuthority.fromGrantedAuthority(grantedAuthority);
-                                       cdmAuthorities.add((GrantedAuthorityImpl)grantedAuthority);                             
+                                       cdmAuthorities.add((GrantedAuthorityImpl)grantedAuthority);
                                }
                        } catch (Exception e) {
-                               // not a CdmAuthority                           
+                               // not a CdmAuthority
                                //e.printStackTrace();
-                       }                       
+                       }
                }
                return cdmAuthorities;
        }
-       
+
+       public boolean isDirty() {
+               return isDirty;
+       }
        /**
         * Adds a new {@link CdmAuthority} to the group attached to this viewer
         * based on the input {@link CdmBase} object.
-        * 
+        *
         * @param cb {@link CdmBase} object used to construct the cdm authority
         */
        public void addCdmAuthority(CdmBase cb) {
                // Default permission is to READ
                EnumSet<CRUD> defaultOperation = EnumSet.noneOf(CRUD.class);
                defaultOperation.add(CRUD.READ);
-               CdmAuthority cdma = new CdmAuthority(cb, defaultOperation, cb.getUuid());
+               CdmAuthority cdma = new CdmAuthority(cb, defaultOperation);
                GrantedAuthorityImpl gai;
+
                try {
-                       // create a granted authrity from a cdm authority 
+                       // create a granted authority from a cdm authority
                        gai = cdma.asNewGrantedAuthority();
-               } catch (Exception e) {                 
-                       StoreUtil.warningDialog("Parsing Error", this, "Could not parse authority string");
+               } catch (Exception e) {
+                       MessagingUtils.warningDialog("Parsing Error", this, "Could not parse authority string");
                        return;
                }
                //FIXME : this contains call will allow users to add authorities which differ only in CRUD operation choice.
                //        need to have a comparator which only checks permission class and uuid
-               if(cdmAuthorities.contains(gai)) {
-                       StoreUtil.warningDialog("Duplicate CDM Authority", this, "Chosen CDM Authority is already attached to current group");
-               } else {                                                                
+               if(cdmAuthorities.contains(gai) || newCdmAuthorities.contains(gai)) {
+                       MessagingUtils.warningDialog("Duplicate CDM Authority", this, "Chosen CDM Authority is already attached to current group");
+               } else {
                        group.addGrantedAuthority(gai);
-                       newCdmAuthorities.add(gai);                             
-                       dirtyMarkerEditor.changed(group);               
+                       newCdmAuthorities.add(gai);
+                       isDirty = true;
+                       dirtyMarkerEditor.changed(group);
                        cdmAuthorityComposite.refresh();
                }
        }
-       
+
        /**
         * Removes the given {@link GrantedAuthorityImpl} object from the list of
         * granted authorities belonging to the group attached to this viewer.
-        * 
+        *
         * @param gai {@link GrantedAuthorityImpl} object to remove
         */
        public void removeCdmAuthority(GrantedAuthorityImpl gai) {
+
                if(cdmAuthorities.contains(gai)) {
-                       group.removeGrantedAuthority(gai);
-                       newCdmAuthorities.remove(gai);
                        cdmAuthorities.remove(gai);
-                       dirtyMarkerEditor.changed(group);       
-                       cdmAuthorityComposite.refresh();
+                       isDirty = true;
                } else {
-                       StoreUtil.warningDialog("Unknown CDM Authority", this, "Chosen CDM Authority does not exist in  current group");
+                       newCdmAuthorities.remove(gai);
+                       if(newCdmAuthorities.isEmpty() && !isDirty) {
+                               isDirty = false;
+                       }
                }
+               group.removeGrantedAuthority(gai);
+               dirtyMarkerEditor.changed(group);
+               cdmAuthorityComposite.refresh();
        }
-       
+
        /**
         * Updates an existing {@link GrantedAuthorityImpl} object using a {@link CdmAuthority} object.
-        * 
+        *
         * @param grantedAuthorityI to update.
         * @param cdmAuthority to use in updating the granted authority object.
         */
        public GrantedAuthorityImpl updateGrantedAuthority(GrantedAuthorityImpl grantedAuthorityI, CdmAuthority cdmAuthority) {
                if(grantedAuthorityI.getAuthority().equals(cdmAuthority.getAuthority())) {
-                       
+
                } else {
                        try {
                                // since granted authority is just a wrapper object around a string
@@ -189,6 +199,7 @@ public class CdmAuthorityCompositeViewer extends ContentViewer {
                                GrantedAuthorityImpl gai = cdmAuthority.asNewGrantedAuthority();
                                group.addGrantedAuthority(gai);
                                group.removeGrantedAuthority(grantedAuthorityI);
+                               isDirty = true;
                                dirtyMarkerEditor.changed(grantedAuthorityI);
                                return gai;
                        } catch(Exception e) {
@@ -198,21 +209,21 @@ public class CdmAuthorityCompositeViewer extends ContentViewer {
                }
                return grantedAuthorityI;
        }
-       
+
        /**
         * @return list of existing cdm authority objects
         */
        public List<GrantedAuthorityImpl> getCdmAuthorities() {
                return cdmAuthorities;
        }
-       
+
        /**
         * @return list of newly added cdm authority objects
         */
        public List<GrantedAuthorityImpl> getNewCdmAuthorities() {
                return newCdmAuthorities;
        }
-       
+
 
        /**
         * Empty the list of newly create autrity objects
@@ -221,17 +232,17 @@ public class CdmAuthorityCompositeViewer extends ContentViewer {
                newCdmAuthorities.clear();
        }
 
-       
+
        /**
-        * 
-        * 
+        *
+        *
         */
        public void save() {
-               clearNewCdmAuthorities();               
+               clearNewCdmAuthorities();
                refresh();
-               
-       }       
-       
+
+       }
+
        /* (non-Javadoc)
         * @see org.eclipse.jface.viewers.Viewer#getControl()
         */
@@ -256,7 +267,7 @@ public class CdmAuthorityCompositeViewer extends ContentViewer {
        public void refresh() {
                updateAuthorities();
                cdmAuthorityComposite.refresh();
-               
+
        }
 
        /* (non-Javadoc)
@@ -265,6 +276,6 @@ public class CdmAuthorityCompositeViewer extends ContentViewer {
        @Override
        public void setSelection(ISelection selection, boolean reveal) {
                // TODO Auto-generated method stub
-               
+
        }
 }