Permalink
Browse files

fixed several minor bugs in the access control editor, default dirty/…

…save behavior now works
  • Loading branch information...
gregjan committed Apr 23, 2012
1 parent 3d7f2ce commit b86bf6044a76659826f27153f11486e981eb21fc
View
@@ -10,8 +10,14 @@
<arguments>
</arguments>
</buildCommand>
+ <buildCommand>
+ <name>org.eclipse.m2e.core.maven2Builder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
</buildSpec>
<natures>
+ <nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
</natures>
</projectDescription>
@@ -15,21 +15,124 @@
*/
package unc.lib.cdr.workbench.acl;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.EventObject;
+import java.util.HashMap;
+import java.util.Map;
+
+import edu.unc.lib.schemas.acl.AccessControlType;
+import gov.loc.mods.mods.presentation.MODSEditor;
+import gov.loc.mods.mods.presentation.ModsEditorPlugin;
+import gov.loc.mods.mods.presentation.URIFragmentEditorInput;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IResourceChangeEvent;
+import org.eclipse.core.resources.IResourceChangeListener;
+import org.eclipse.core.resources.IResourceDelta;
+import org.eclipse.core.resources.IResourceDeltaVisitor;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.emf.common.command.BasicCommandStack;
+import org.eclipse.emf.common.command.CommandStack;
+import org.eclipse.emf.common.command.CommandStackListener;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
+import org.eclipse.emf.edit.domain.EditingDomain;
+import org.eclipse.emf.edit.provider.AdapterFactoryItemDelegator;
+import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
+import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;
+import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IEditorSite;
+import org.eclipse.ui.IPartListener;
+import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.widgets.FormToolkit;
+import org.eclipse.ui.views.contentoutline.ContentOutline;
+import org.eclipse.ui.views.properties.PropertySheet;
+
+import unc.lib.cdr.workbench.project.MetsProjectNature;
+import unc.lib.cdr.workbench.rcp.Activator;
/**
* @author Gregory Jansen
*
*/
public class AccessControlFormEditor extends FormEditor {
+ protected AccessControlType model = null;
+ protected AccessControlFormPage page = null;
+ protected AdapterFactoryEditingDomain editingDomain;
+ protected ComposedAdapterFactory adapterFactory;
+ protected AdapterFactoryItemDelegator itemDelegator;
+ protected AdapterFactoryLabelProvider labelProvider;
+
+ private CommandStackListener commandStackListener = new CommandStackListener() {
+ @Override
+ public void commandStackChanged(final EventObject event) {
+ getContainer().getDisplay().asyncExec(new Runnable() {
+ public void run() {
+ editorDirtyStateChanged();
+ }
+ });
+ }
+ };
+
+ @Override
+ public void init(IEditorSite site, IEditorInput input) throws PartInitException {
+ super.init(site, input);
+ setPartName(input.getName());
+ initializeEditingDomainAndModel();
+ //ResourcesPlugin.getWorkspace()
+ // .addResourceChangeListener(resourceChangeListener, IResourceChangeEvent.POST_CHANGE);
+ }
+
public AccessControlFormEditor() {
}
+ protected void initializeEditingDomainAndModel() {
+ IProject project = null;
+ if(this.getEditorInput() instanceof URIFragmentEditorInput) {
+ URIFragmentEditorInput in = (URIFragmentEditorInput)this.getEditorInput();
+ project = ResourcesPlugin.getWorkspace().getRoot().getProject(in.getProjectName());
+ this.adapterFactory = MetsProjectNature.getAdapterFactory();
+ this.editingDomain = MetsProjectNature.get(project).getEditingDomain();
+ EObject eobj = MetsProjectNature.getModel(in);
+ this.model = (AccessControlType)eobj;
+ }
+
+ // command stack that will notify this editor as commands are executed
+ CommandStack commandStack = this.editingDomain.getCommandStack();
+
+ // Add a listener to set the editor dirty of commands have been executed
+ commandStack.addCommandStackListener(commandStackListener);
+ // These provide access to the model items, their property source and label
+ this.itemDelegator = new AdapterFactoryItemDelegator(adapterFactory);
+ this.labelProvider = new AdapterFactoryLabelProvider(adapterFactory);
+ }
+
+ @Override
+ public void dispose() {
+ this.editingDomain.getCommandStack().removeCommandStackListener(commandStackListener);
+ super.dispose();
+ }
+
@Override
protected FormToolkit createToolkit(Display display) {
// Create a toolkit that shares colors between editors.
@@ -44,18 +147,15 @@ protected FormToolkit createToolkit(Display display) {
@Override
protected void addPages() {
try {
- addPage(new AccessControlFormPage(this));
+ this.page = new AccessControlFormPage(this);
+ addPage(this.page);
} catch (PartInitException e) {
}
}
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime. IProgressMonitor)
- */
@Override
- public void doSave(IProgressMonitor monitor) {
+ public boolean isDirty() {
+ return ((BasicCommandStack) editingDomain.getCommandStack()).isSaveNeeded();
}
/*
@@ -76,5 +176,41 @@ public void doSaveAs() {
public boolean isSaveAsAllowed() {
return false;
}
-
+
+ /**
+ * This is for implementing {@link IEditorPart} and simply saves the model file. <!-- begin-user-doc --> <!--
+ * end-user-doc -->
+ *
+ * @generated
+ */
+ @Override
+ public void doSave(IProgressMonitor progressMonitor) {
+ // Save only resources that have actually changed.
+ final Map<Object, Object> saveOptions = new HashMap<Object, Object>();
+ saveOptions.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED, Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER);
+ // Do the work within an operation because this is a long running activity that modifies the workbench.
+ WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
+ // This is the method that gets invoked when the operation runs.
+ //
+ @Override
+ public void execute(IProgressMonitor monitor) {
+ // Save the resources to the file system.
+ try {
+ MetsProjectNature.getNatureForMetsObject(model).save();
+ } catch (CoreException e) {
+ e.printStackTrace();
+ }
+ }
+ };
+ try {
+ // This runs the options, and shows progress.
+ new ProgressMonitorDialog(getSite().getShell()).run(true, false, operation);
+ // Refresh the necessary state.
+ ((BasicCommandStack) editingDomain.getCommandStack()).saveIsDone();
+ firePropertyChange(IEditorPart.PROP_DIRTY);
+ } catch (Exception exception) {
+ ModsEditorPlugin.INSTANCE.log(exception);
+ }
+ }
+
}
@@ -22,41 +22,47 @@
import javax.xml.datatype.XMLGregorianCalendar;
+import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.xml.type.XMLTypeFactory;
+import org.eclipse.emf.edit.command.SetCommand;
+import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.ui.forms.IManagedForm;
-import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;
+import unc.lib.cdr.workbench.project.MetsProjectNature;
import edu.unc.lib.schemas.acl.AccessControlType;
+import edu.unc.lib.schemas.acl.AclPackage;
/**
* @author Gregory Jansen
*
*/
public class AccessControlFormPage extends FormPage {
private ScrolledPropertiesBlock block;
- private AccessControlType model;
+ protected AccessControlType model;
private Button inheritFlag;
private Button discoverableFlag;
private Button embargoFlag;
private DateTime untilDate;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+ private AccessControlFormEditor acleditor = null;
- public AccessControlFormPage(FormEditor editor) {
+ public AccessControlFormPage(AccessControlFormEditor editor) {
super(editor, "fourth", "Access Controls");
block = new ScrolledPropertiesBlock(this);
- model = ((ACLEditorInput) editor.getEditorInput()).getModel();
+ model = editor.model;
+ this.acleditor = editor;
}
@Override
@@ -93,8 +99,6 @@ private void createEmbargoContent(Composite parent, FormToolkit toolkit) {
s1.setDescription("You may embargo this object (and its contents) until a specified date."); //$NON-NLS-1$
s1.marginWidth = 10;
s1.marginHeight = 5;
- System.out.println("parent layout: " + parent.getLayout());
- System.out.println("s1 layout: " + s1.getLayout());
s1.setLayoutData(gd);
Composite client = toolkit.createComposite(s1);
@@ -114,7 +118,7 @@ public void widgetSelected(SelectionEvent e) {
});
embargoFlag.setLayoutData(twd);
untilDate = new DateTime(client, SWT.DATE | SWT.BORDER);
- if (model.isSetEmbargoUntil()) {
+ if (model.isSetEmbargoUntil() && model.getEmbargoUntil() != null) {
XMLGregorianCalendar cal = model.getEmbargoUntil();
untilDate.setYear(cal.getYear());
untilDate.setMonth(cal.getMonth() - 1);
@@ -142,12 +146,16 @@ public void widgetSelected(SelectionEvent e) {
*
*/
protected void updateEmbargo() {
+ Command cmd = null;
if (embargoFlag.getSelection()) {
Date dt = new Date(untilDate.getYear() - 1900, untilDate.getMonth(), untilDate.getDay());
XMLGregorianCalendar fromDate = XMLTypeFactory.eINSTANCE.createDate(sdf.format(dt));
- model.setEmbargoUntil(fromDate);
+ cmd = SetCommand.create(MetsProjectNature.getEditingDomain(model), model, AclPackage.eINSTANCE.getAccessControlType_EmbargoUntil(), fromDate);
} else { // no embargo
- model.unsetEmbargoUntil();
+ cmd = SetCommand.create(MetsProjectNature.getEditingDomain(model), model, AclPackage.eINSTANCE.getAccessControlType_EmbargoUntil(), null);
+ }
+ if(cmd.canExecute()) {
+ MetsProjectNature.getNatureForMetsObject(model).getCommandStack().execute(cmd);
}
}
@@ -162,17 +170,22 @@ private void createInheritContent(Composite parent, FormToolkit toolkit) {
s1.setDescription("Roles may have been granted to groups above this object. Do you want those groups to retain roles inherited in this way?"); //$NON-NLS-1$
s1.marginWidth = 10;
s1.marginHeight = 5;
- System.out.println("parent layout: " + parent.getLayout());
- System.out.println("s1 layout: " + s1.getLayout());
s1.setLayoutData(gd);
inheritFlag = toolkit.createButton(s1, "Yes, groups may retain roles granted above this level.", SWT.CHECK); //$NON-NLS-1$
+ for(IItemPropertyDescriptor d : acleditor.itemDelegator.getPropertyDescriptors(model)) {
+ System.out.println(d);
+ }
inheritFlag.setSelection(model.isInherit());
inheritFlag.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
- if (model != null)
- model.setInherit(inheritFlag.getSelection());
+ if (model != null) {
+ Command cmd = SetCommand.create(MetsProjectNature.getEditingDomain(model), model, AclPackage.eINSTANCE.getAccessControlType_Inherit(), inheritFlag.getSelection());
+ if(cmd.canExecute()) {
+ MetsProjectNature.getNatureForMetsObject(model).getCommandStack().execute(cmd);
+ }
+ }
}
});
gd = new TableWrapData(TableWrapData.FILL, TableWrapData.MIDDLE);
@@ -192,16 +205,18 @@ private void createDiscoverableContent(Composite parent, FormToolkit toolkit) {
s1.setDescription("Do you want this object to be discovered by repository search and browse functions?"); //$NON-NLS-1$
s1.marginWidth = 10;
s1.marginHeight = 5;
- System.out.println("parent layout: " + parent.getLayout());
- System.out.println("s1 layout: " + s1.getLayout());
s1.setLayoutData(gd);
discoverableFlag = toolkit.createButton(s1, "Yes, allow discovery via search and browse.", SWT.CHECK); //$NON-NLS-1$
discoverableFlag.setSelection(model.isDiscoverable());
discoverableFlag.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
- if (model != null)
- model.setDiscoverable(discoverableFlag.getSelection());
+ if (model != null) {
+ Command cmd = SetCommand.create(MetsProjectNature.getEditingDomain(model), model, AclPackage.eINSTANCE.getAccessControlType_Discoverable(), discoverableFlag.getSelection());
+ if(cmd.canExecute()) {
+ MetsProjectNature.getNatureForMetsObject(model).getCommandStack().execute(cmd);
+ }
+ }
}
});
gd = new TableWrapData(TableWrapData.FILL, TableWrapData.MIDDLE);
@@ -28,13 +28,15 @@
import gov.loc.mets.XmlDataType1;
import gov.loc.mets.util.METSConstants;
import gov.loc.mets.util.METSUtils;
+import gov.loc.mods.mods.presentation.URIFragmentEditorInput;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.ecore.xml.type.internal.XMLCalendar;
+import org.eclipse.emf.edit.ui.util.EditUIUtil;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
@@ -104,7 +106,8 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
} catch (CoreException e) {
throw new ExecutionException("There were unexpected problems opening the MODS Editor", e);
}
- ACLEditorInput input = new ACLEditorInput("Access Controls for '" + d.getLABEL1() + "'", acl);
+ String uriFrag = acl.eResource().getURIFragment(acl);
+ URIFragmentEditorInput input = new URIFragmentEditorInput(n.getProject().getName(), uriFrag, "Access Controls for '" + d.getLABEL1() + "'", acl);
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
try {
Oops, something went wrong.

0 comments on commit b86bf60

Please sign in to comment.