Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » The problem about saving and deleting.
The problem about saving and deleting. [message #226792] Thu, 23 November 2006 11:32 Go to next message
Eclipse UserFriend
Originally posted by: brad_young.163.com

Hello, I'm developing a modeling tool using GEF+EMF. The problem is that
I can save the diagram using the saving dialog it gives when I close it,
but I can't save it using Ctrl + "s"?
Another problem is that I can't delete the diagram element.
How should I change the code? Thank you.

The code of the editor is below:

public class ProcessEditor extends GraphicalEditorWithPalette {

/**
* This class listens for command stack changes of the pages
* contained in this editor and decides if the editor is dirty or not.
*
* @author Gunnar Wagenknecht
*/
private class MultiPageCommandStackListener implements CommandStackListener
{

/** the observed command stacks */
private List commandStacks = new ArrayList(2);

/**
* Adds a <code>CommandStack</code> to observe.
* @param commandStack
*/
public void addCommandStack(CommandStack commandStack)
{
commandStacks.add(commandStack);
commandStack.addCommandStackListener(this);
}

/* (non-Javadoc)
* @see
org.eclipse.gef.commands.CommandStackListener#commandStackCh anged(java.util.EventObject)
*/
public void commandStackChanged(EventObject event)
{
if (((CommandStack) event.getSource()).isDirty())
{
// at least one command stack is dirty,
// so the multi page editor is dirty too
setDirty(true);
}
else
{
// probably a save, we have to check all command stacks
boolean oneIsDirty = false;
for (Iterator stacks = commandStacks.iterator();
stacks.hasNext();
)
{
CommandStack stack = (CommandStack) stacks.next();
if (stack.isDirty())
{
oneIsDirty = true;
break;
}
}
setDirty(oneIsDirty);
}
}

/**
* Disposed the listener
*/
public void dispose()
{
for (Iterator stacks = commandStacks.iterator(); stacks.hasNext();)
{
((CommandStack)
stacks.next()).removeCommandStackListener(this);
}
commandStacks.clear();
}

/**
* Marks every observed command stack beeing saved.
* This method should be called whenever the editor/model
* was saved.
*/
public void markSaveLocations()
{
for (Iterator stacks = commandStacks.iterator(); stacks.hasNext();)
{
CommandStack stack = (CommandStack) stacks.next();
stack.markSaveLocation();
}
}
}

/** the multi page editor's dirty state */
private boolean isDirty = false;
Process process;
private PaletteRoot root;
private KeyHandler sharedKeyHandler;

/** the list of action ids that are to EditPart actions */
private List editPartActionIDs = new ArrayList();

/** the list of action ids that are to CommandStack actions */
private List stackActionIDs = new ArrayList();

/** the <code>CommandStackListener</code> */
private MultiPageCommandStackListener multiPageCommandStackListener;

/** the list of action ids that are editor actions */
private List editorActionIDs = new ArrayList();

/** the delegating ZoomManager */
private DelegatingZoomManager delegatingZoomManager;

/**
* Returns the <code>DelegatingZoomManager</code> for this editor.
* @return the <code>DelegatingZoomManager</code>
*/
protected DelegatingZoomManager getDelegatingZoomManager()
{
if (null == delegatingZoomManager)
{
delegatingZoomManager = new DelegatingZoomManager();
delegatingZoomManager.setCurrentZoomManager(
getZoomManager(getGraphicalViewer()));
}

return delegatingZoomManager;
}

/**
* Returns the zoom manager of the specified viewer.
* @param viewer the viewer to get the zoom manager from
* @return the zoom manager
*/
private ZoomManager getZoomManager(GraphicalViewer viewer)
{
// get zoom manager from root edit part
RootEditPart rootEditPart = viewer.getRootEditPart();
ZoomManager zoomManager = null;
if (rootEditPart instanceof ScalableFreeformRootEditPart)
{
zoomManager =
((ScalableFreeformRootEditPart) rootEditPart).getZoomManager();
}
else if (rootEditPart instanceof ScalableRootEditPart)
{
zoomManager =
((ScalableRootEditPart) rootEditPart).getZoomManager();
}
return zoomManager;
}

public ProcessEditor() {
DefaultEditDomain defaultEditDomain = new DefaultEditDomain(this);
setEditDomain(defaultEditDomain);
}


/**
* Adds an <code>CommandStack</code> action to this editor.
*
* <p><code>CommandStack</code> actions are actions that depend
* and work on the <code>CommandStack</code>.
*
* @param action the <code>CommandStack</code> action
*/
protected void addStackAction(StackAction action)
{
getActionRegistry().registerAction(action);
stackActionIDs.add(action.getId());
}


/**
* Adds an <code>EditPart</code> action to this editor.
*
* <p><code>EditPart</code> actions are actions that depend
* and work on the selected <code>EditPart</code>s.
*
* @param action the <code>EditPart</code> action
*/
protected void addEditPartAction(SelectionAction action)
{
getActionRegistry().registerAction(action);
editPartActionIDs.add(action.getId());
}

/**
* Adds an action to this editor's <code>ActionRegistry</code>.
* (This is a helper method.)
*
* @param action the action to add.
*/
protected void addAction(IAction action)
{
getActionRegistry().registerAction(action);
}


/**
* Adds an editor action to this editor.
*
* <p><Editor actions are actions that depend
* and work on the editor.
*
* @param action the editor action
*/
protected void addEditorAction(EditorPartAction action)
{
getActionRegistry().registerAction(action);
editorActionIDs.add(action.getId());
}

/**
* @see org.eclipse.gef.ui.parts.GraphicalEditor#createActions()
*/
protected void createActions() {
addStackAction(new UndoAction(this));
addStackAction(new RedoAction(this));

addEditPartAction(new DeleteAction((IWorkbenchPart) this));

addEditPartAction(new AlignmentAction((IWorkbenchPart) this,
PositionConstants.LEFT));
addEditPartAction(new AlignmentAction((IWorkbenchPart) this,
PositionConstants.RIGHT));
addEditPartAction(new AlignmentAction((IWorkbenchPart) this,
PositionConstants.TOP));
addEditPartAction(new AlignmentAction((IWorkbenchPart) this,
PositionConstants.BOTTOM));
addEditPartAction(new AlignmentAction((IWorkbenchPart) this,
PositionConstants.CENTER));
addEditPartAction(new AlignmentAction((IWorkbenchPart) this,
PositionConstants.MIDDLE));

addEditorAction(new SaveAction(this));
}

/**
* Creates an appropriate output stream and writes the activity diagram out
to this stream.
* @param os the base output stream
* @throws IOException
*/
protected void createOutputStream(OutputStream os) throws IOException {
ObjectOutputStream out = new ObjectOutputStream(os);
out.writeObject(process);
out.close();
}

/**
* @see org.eclipse.gef.ui.parts.GraphicalEditor#configureGraphicalV iewer()
*/
protected void configureGraphicalViewer() {
super.configureGraphicalViewer();
getGraphicalViewer().setRootEditPart(new ScalableRootEditPart());
getGraphicalViewer().setEditPartFactory(new GraphicalEditPartsFactory());
getGraphicalViewer().setKeyHandler(new
GraphicalViewerKeyHandler(getGraphicalViewer())
.setParent(getCommonKeyHandler()));

ContextMenuProvider provider =
new ProcessContextMenuProvider(getGraphicalViewer(), getActionRegistry());
getGraphicalViewer().setContextMenu(provider);
getSite().registerContextMenu(
"org.eclipse.gef.examples.flow.editor.contextmenu", //$NON-NLS-1$
provider,
getGraphicalViewer());

}

/**
* @see org.eclipse.gef.ui.parts.GraphicalEditor#initializeGraphical Viewer()
*/
protected void initializeGraphicalViewer() {
getGraphicalViewer().setContents(process);
getGraphicalViewer().addDropTargetListener(
new TemplateTransferDropTargetListener(getGraphicalViewer()));

}

/**
* @see
org.eclipse.gef.ui.parts.GraphicalEditorWithPalette#initiali zePaletteViewer()
*/
protected void initializePaletteViewer() {
super.initializePaletteViewer();
getPaletteViewer().addDragSourceListener(
new TemplateTransferDragSourceListener(getPaletteViewer()));
}

protected KeyHandler getCommonKeyHandler() {
if (sharedKeyHandler == null) {
sharedKeyHandler = new KeyHandler();
sharedKeyHandler.put(
KeyStroke.getPressed(SWT.DEL, 127, 0),
getActionRegistry().getAction(ActionFactory.DELETE.getId())) ;
sharedKeyHandler.put(
KeyStroke.getPressed(SWT.F2, 0),
getActionRegistry().getAction(GEFActionConstants.DIRECT_EDIT ));
}
return sharedKeyHandler;
}

/**
* @see org.eclipse.gef.ui.parts.GraphicalEditorWithPalette#getPalet teRoot()
*/
protected PaletteRoot getPaletteRoot() {
if (root == null)
root = ProcessEditorPaletteFactory.createPalette();
return root;
}

public void gotoMarker(IMarker marker) { }

/**
* @see org.eclipse.ui.ISaveablePart#isSaveAsAllowed()
*/
public boolean isSaveAsAllowed() {
return true;
}

/**
* @see org.eclipse.ui.part.EditorPart#setInput(org.eclipse.ui.IEdit orInput)
*/
protected void setInput(IEditorInput input){
super.setInput(input);

try
{
// we expect IFileEditorInput here,
// ClassCassException is catched to force PartInitException
IFile file = ((IFileEditorInput) input).getFile();
process = create(file);

// validate process
if (null == getProcess())
throw new PartInitException("The specified input is not a valid
process.");
}
catch (CoreException e)
{
System.out.print("Error loading the process.");
}
catch (ClassCastException e)
{
System.out.print("The specified input is not a valid process.");
}
}

/** the model manager */
private ProcessModelManager modelManager;

/**
* Returns the process object from the specified file.
*
* @param file
* @return the process object from the specified file
*/
private Process create(IFile file) throws CoreException
{
Process process = null;
modelManager = new ProcessModelManager();

if (file.exists())
{
try
{
modelManager.load(file.getFullPath());
}
catch (Exception e)
{
modelManager.createProcess(file.getFullPath());
}

process = modelManager.getModel();
if (null == process)
{
throw new CoreException(
new Status(
IStatus.ERROR,
ProcessPlugin.PLUGIN_ID,
0,
"Error loading the process.",
null));
}
}
return process;
}

/**
* Returns the process used by this editor.
* @return the process
*/
public Process getProcess()
{
return process;
}

/* (non-Javadoc)
* @see
org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runti me.IProgressMonitor)
*/
public void doSave(IProgressMonitor monitor)
{
try
{
IFile file = ((IFileEditorInput) getEditorInput()).getFile();
if (file.exists()
|| MessageDialogWithToggle.openConfirm(
getSite().getShell(),
"Create File",
"The file '"
+ file.getName()
+ "' doesn't exist. Click OK to create it."))
{
save(file, monitor);
getMultiPageCommandStackListener().markSaveLocations();
}
}
catch (CoreException e)
{
ErrorDialog.openError(
getSite().getShell(),
"Error During Save",
"The current process model could not be saved.",
e.getStatus());
}
}

/* (non-Javadoc)
* @see org.eclipse.ui.ISaveablePart#doSaveAs()
*/
public void doSaveAs()
{
SaveAsDialog dialog = new SaveAsDialog(getSite().getShell());
dialog.setOriginalFile(((IFileEditorInput) getEditorInput()).getFile());
dialog.open();
IPath path = dialog.getResult();

if (path == null)
return;

ProgressMonitorDialog progressMonitorDialog =
new ProgressMonitorDialog(getSite().getShell());
IProgressMonitor progressMonitor =
progressMonitorDialog.getProgressMonitor();

try
{
save(
ResourcesPlugin.getWorkspace().getRoot().getFile(path),
progressMonitor);
getMultiPageCommandStackListener().markSaveLocations();
}
catch (CoreException e)
{
ErrorDialog.openError(
getSite().getShell(),
"Error During Save",
"The current process model could not be saved.",
e.getStatus());
}
}

/**
* Saves the process under the specified path.
*
* @param process
* @param path workspace relative path
* @param progressMonitor
*/
private void save(IFile file, IProgressMonitor progressMonitor)
throws CoreException
{

if (null == progressMonitor)
progressMonitor = new NullProgressMonitor();

progressMonitor.beginTask("Saving " + file, 2);

if (null == modelManager)
{
IStatus status =
new Status(
IStatus.ERROR,
ProcessPlugin.PLUGIN_ID,
0,
"No model manager found for saving the file.",
null);
throw new CoreException(status);
}

// save process to file
try
{
modelManager.save(file.getFullPath());

progressMonitor.worked(1);
file.refreshLocal(
IResource.DEPTH_ZERO,
new SubProgressMonitor(progressMonitor, 1));
progressMonitor.done();
}
catch (FileNotFoundException e)
{
IStatus status =
new Status(
IStatus.ERROR,
ProcessPlugin.PLUGIN_ID,
0,
"Error writing file.",
e);
throw new CoreException(status);
}
catch (IOException e)
{
IStatus status =
new Status(
IStatus.ERROR,
ProcessPlugin.PLUGIN_ID,
0,
"Error writing file.",
e);
throw new CoreException(status);
}
}

/**
* Returns the global command stack listener.
* @return the <code>CommandStackListener</code>
*/
protected MultiPageCommandStackListener getMultiPageCommandStackListener()
{
if (null == multiPageCommandStackListener)
multiPageCommandStackListener = new MultiPageCommandStackListener();
return multiPageCommandStackListener;
}

/**
* Changes the dirty state.
* @param dirty
*/
private void setDirty(boolean dirty)
{
if (isDirty != dirty)
{
isDirty = dirty;
firePropertyChange(IEditorPart.PROP_DIRTY);
}
}


/* (non-Javadoc)
* @see org.eclipse.ui.part.WorkbenchPart#firePropertyChange(int)
*/
protected void firePropertyChange(int propertyId)
{
super.firePropertyChange(propertyId);
updateActions(editorActionIDs);
}


/* (non-Javadoc)
* @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Cla ss)
*/
public Object getAdapter(Class type)
{
// else if (type == CommandStack.class)
// return getDelegatingCommandStack();
if (type == ActionRegistry.class)
return getActionRegistry();
else if (type == ZoomManager.class)
return getDelegatingZoomManager();

return super.getAdapter(type);
}

public void commandStackChanged(EventObject arg0) {
firePropertyChange(IEditorPart.PROP_DIRTY);
super.commandStackChanged(arg0);
}
}
Re: The problem about saving and deleting. [message #226903 is a reply to message #226792] Fri, 24 November 2006 21:57 Go to previous message
Anthony Hunter is currently offline Anthony HunterFriend
Messages: 446
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.

------=_NextPart_000_004F_01C70FE9.94E1A180
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi Brad,=20

1) If CTRL+S does work when you exit, then the save action is also not =
enabled, so you are likely missing firing the editor dirty event in your =
editor:

public void commandStackChanged(EventObject event) {
firePropertyChange(IEditorPart.PROP_DIRTY);
super.commandStackChanged(event);
}

2) Cannot delete a node on your diagram, look at the shapes or logic =
examples for the *DeleteCommand.

Cheers...
Anthony

Check out the=20
"Brad_Young" <brad_young@163.com> wrote in message =
news:ek40s9$323$1@utils.eclipse.org...
> Hello, I'm developing a modeling tool using GEF+EMF. The problem is =
that
> I can save the diagram using the saving dialog it gives when I close =
it,
> but I can't save it using Ctrl + "s"?
> Another problem is that I can't delete the diagram element.
> How should I change the code? Thank you.
>=20
> The code of the editor is below:
>=20
> [snip]
>
------=_NextPart_000_004F_01C70FE9.94E1A180
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2900.2995" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>Hi Brad, </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>1) If CTRL+S does work when you exit, =
then the save=20
action is also not enabled, so you are likely missing firing the editor =
dirty=20
event in your editor:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&nbsp;&nbsp;&nbsp; public void=20
commandStackChanged(EventObject event)=20
{<BR> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;=20
firePropertyChange(IEditorPart.PROP_DIRTY);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;=20
super.commandStackChanged(event);<BR>&nbsp;&nbsp;&nbsp; =
}<BR></FONT><FONT=20
face=3D"Courier New" size=3D2></DIV></FONT>
<DIV><FONT face=3DArial size=3D2>2) Cannot delete a node on your =
diagram, look at=20
the shapes or logic examples for the *DeleteCommand.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Cheers...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Anthony</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Check out the </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>"Brad_Young" &lt;</FONT><A=20
href=3D"mailto:brad_young@163.com"><FONT face=3DArial=20
size=3D2>brad_young@163.com</FONT></A><FONT face=3DArial size=3D2>&gt; =
wrote in=20
message </FONT><A href=3D"news:ek40s9$323$1@utils.eclipse.org"><FONT =
face=3DArial=20
size=3D2>news:ek40s9$323$1@utils.eclipse.org</FONT></A><FONT =
face=3DArial=20
size=3D2>...</FONT></DIV><FONT face=3DArial size=3D2>&gt; Hello, I'm =
developing a=20
modeling tool using GEF+EMF. The problem is that<BR>&gt; I can save the =
diagram=20
using the saving dialog it gives when I close it,<BR>&gt; but I can't =
save it=20
using Ctrl + "s"?<BR>&gt; Another problem is that I can't delete the =
diagram=20
element.<BR>&gt;&nbsp;How should I change the code? Thank you.<BR>&gt; =
<BR>&gt;=20
The code of the editor is below:<BR>&gt; <BR>&gt;=20
[snip]<BR>&gt;</FONT></BODY></HTML>

------=_NextPart_000_004F_01C70FE9.94E1A180--
Previous Topic:cannot capture mouse double click event
Next Topic:Draw2D PolylineConnection
Goto Forum:
  


Current Time: Tue Oct 19 00:37:15 GMT 2021

Powered by FUDForum. Page generated in 0.01844 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top