Edition lifecycle.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / viewers / AbstractPageViewer.java
index 1445fed33a82caf7186c9b77f8a238ba5e5809c9..60db89978d9f1ba3493fc582edbce77bffdb1fe6 100644 (file)
@@ -13,9 +13,9 @@ import javax.security.auth.Subject;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.argeo.cms.CmsException;
 import org.argeo.cms.ui.CmsEditable;
 import org.argeo.cms.ui.widgets.ScrolledPage;
+import org.argeo.jcr.JcrException;
 import org.eclipse.jface.viewers.ContentViewer;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.StructuredSelection;
@@ -74,8 +74,8 @@ public abstract class AbstractPageViewer extends ContentViewer implements Observ
                                        initModel(node);
                                        node.getSession().save();
                                }
-               } catch (Exception e) {
-                       throw new CmsException("Cannot initialize model", e);
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot initialize model", e);
                }
        }
 
@@ -120,6 +120,16 @@ public abstract class AbstractPageViewer extends ContentViewer implements Observ
                }
        }
 
+       public void layoutPage() {
+               if (page != null)
+                       page.layout(true, true);
+       }
+
+       protected void showControl(Control control) {
+               if (page != null && (page instanceof ScrolledPage))
+                       ((ScrolledPage) page).showControl(control);
+       }
+
        @Override
        public void update(Observable o, Object arg) {
                if (o == cmsEditable)
@@ -153,9 +163,10 @@ public abstract class AbstractPageViewer extends ContentViewer implements Observ
                                else
                                        mouseListener = null;
                                refresh(getControl());
-                               layout(getControl());
+                               // layout(getControl());
+                               layoutPage();
                        } catch (RepositoryException e) {
-                               throw new CmsException("Cannot refresh", e);
+                               throw new JcrException("Cannot refresh", e);
                        }
                        return null;
                });
@@ -187,16 +198,30 @@ public abstract class AbstractPageViewer extends ContentViewer implements Observ
                        }
 
                        part.startEditing();
+                       edited = part;
                        updateContent(part);
                        prepare(part, caretPosition);
-                       edited = part;
+                       edited.getControl().addFocusListener(new FocusListener() {
+                               private static final long serialVersionUID = 6883521812717097017L;
+
+                               @Override
+                               public void focusLost(FocusEvent event) {
+                                       stopEditing(true);
+                               }
+
+                               @Override
+                               public void focusGained(FocusEvent event) {
+                               }
+                       });
+
                        layout(part.getControl());
+                       showControl(part.getControl());
                } catch (RepositoryException e) {
-                       throw new CmsException("Cannot edit " + part, e);
+                       throw new JcrException("Cannot edit " + part, e);
                }
        }
 
-       private void stopEditing(Boolean save) throws RepositoryException {
+       protected void stopEditing(Boolean save) {
                if (edited instanceof Widget && ((Widget) edited).isDisposed()) {
                        edited = null;
                        return;
@@ -209,32 +234,33 @@ public abstract class AbstractPageViewer extends ContentViewer implements Observ
                        return;
                }
 
-               if (save)
-                       save(edited);
+               try {
+                       if (save)
+                               save(edited);
 
-               edited.stopEditing();
-               updateContent(edited);
-               layout(((EditablePart) edited).getControl());
-               edited = null;
+                       edited.stopEditing();
+                       EditablePart editablePart = edited;
+                       Control control = ((EditablePart) edited).getControl();
+                       edited = null;
+                       // TODO make edited state management more robust
+                       updateContent(editablePart);
+                       layout(control);
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot stop editing", e);
+               } finally {
+                       edited = null;
+               }
        }
 
        // METHODS AVAILABLE TO EXTENDING CLASSES
        protected void saveEdit() {
-               try {
-                       if (edited != null)
-                               stopEditing(true);
-               } catch (RepositoryException e) {
-                       throw new CmsException("Cannot stop editing", e);
-               }
+               if (edited != null)
+                       stopEditing(true);
        }
 
        protected void cancelEdit() {
-               try {
-                       if (edited != null)
-                               stopEditing(false);
-               } catch (RepositoryException e) {
-                       throw new CmsException("Cannot cancel editing", e);
-               }
+               if (edited != null)
+                       stopEditing(false);
        }
 
        /** Layout this controls from the related base page. */
@@ -252,14 +278,14 @@ public abstract class AbstractPageViewer extends ContentViewer implements Observ
                if (parent.getParent() != null)
                        return findDataParent(parent.getParent());
                else
-                       throw new CmsException("No data parent found");
+                       throw new IllegalStateException("No data parent found");
        }
 
        // UTILITIES
        /** Check whether the edited part is in a proper state */
        protected void checkEdited() {
                if (edited == null || (edited instanceof Widget) && ((Widget) edited).isDisposed())
-                       throw new CmsException("Edited should not be null or disposed at this stage");
+                       throw new IllegalStateException("Edited should not be null or disposed at this stage");
        }
 
        /** Persist all changes. */
@@ -292,7 +318,7 @@ public abstract class AbstractPageViewer extends ContentViewer implements Observ
                        res = Subject.getSubject(accessControlContext);
                }
                if (res == null)
-                       throw new CmsException("No subject associated with this viewer");
+                       throw new IllegalStateException("No subject associated with this viewer");
                return res;
        }