[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [gef-dev] ClassNotFoundException when using XMLEncoder to save model under doSaveAs method
|
You can add the following expression:
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
before using the nofouned class
----- Original Message -----
From: <agape85@xxxxxxxxxxxxxx>
To: <gef-dev@xxxxxxxxxxx>
Sent: Friday, March 04, 2005 9:45 AM
Subject: [gef-dev] ClassNotFoundException when using XMLEncoder to save model under doSaveAs method
> Hi all,
>
> I have been trying to use XMLEncoder to save my model/diagram view. But no matter what i did i kept hitting ClassNotFound Exception. Below is the codes written for doSaveAs method.
>
> -----------------------------------------------------------
> public void doSaveAs() {
> try {
> System.out.println("Got here with object "+diagram);
> XMLHandler handleXML = new XMLHandler();
> handleXML.WriteToXML("Testing.xml",diagram);
> } catch(Throwable th) {
> th.printStackTrace();
> }
> System.out.println("Exiting here with object "+diagram);
> }
> -----------------------------------------------------------
>
> And below is the codes written for WriteToXML
> -----------------------------------------------------------
> public boolean WriteToXML(String fileName, Object object)
> {
> boolean result = false;
> try
> {
> System.out.println("Step 1");
> XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(fileName)));
> System.out.println("Step 2");
> encoder.writeObject(object);
> System.out.println("Step 3");
> encoder.close();
> System.out.println("Step 4");
> result = true;
> }
> catch (FileNotFoundException e)
> {
> e.printStackTrace();
> }
> return result;
> }
> -----------------------------------------------------------
>
> And below is the error message that i encountered.
> -----------------------------------------------------------
> Got here with object com.bonevich.simplegef.model.view.DiagramView@1c1c92b
> Step 1
> Step 2
> java.lang.ClassNotFoundException: com.bonevich.simplegef.model.view.DiagramView
> Continuing ...
> java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject(DiagramView);
> Continuing ...
> Step 3
> Step 4
> Exiting here with object com.bonevich.simplegef.model.view.DiagramView@1c1c92b
> -----------------------------------------------------------
>
> Hopes that someone might be able to tell me if i am doing this wrong or is there something that i am missing which leads to the error.
>
> Attached is .java of XMLHandler and SimpleGefEditor.
>
> By the way i am editing on someone else's code which can be found here http://eclipsewiki.editme.com/GefExamplesForEclipse3
>
> Agape,
> Joshua
--------------------------------------------------------------------------------
>
> /*
> * Created on Feb 18, 2005
> *
> * TODO To change the template for this generated file go to
> * Window - Preferences - Java - Code Style - Code Templates
> */
> package com.bonevich.simplegef;
> import java.beans.*;
> import java.io.*;
>
>
> public class XMLHandler {
>
> public boolean WriteToXML(String fileName, Object object)
> {
> boolean result = false;
> try
> {
> System.out.println("Step 1");
> XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(fileName)));
> System.out.println("Step 2");
> encoder.writeObject(object);
> System.out.println("Step 3");
> encoder.close();
> System.out.println("Step 4");
> result = true;
> }
> catch (FileNotFoundException e)
> {
> e.printStackTrace();
> }
> return result;
> }
>
> public Object ReadFromXML(String fileName)
> {
> Object decoded = null;
> try
> {
> XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(fileName)));
> decoded = decoder.readObject();
> decoder.close();
> }
> catch (FileNotFoundException e)
> {
> e.printStackTrace();
> }
> return decoded;
> }
> }
--------------------------------------------------------------------------------
>
> /*
> * Created on Jun 20, 2004
> */
> package com.bonevich.simplegef.editors;
>
> import java.beans.XMLEncoder;
> import java.io.BufferedOutputStream;
> import java.io.ByteArrayInputStream;
> import java.io.ByteArrayOutputStream;
> import java.io.FileNotFoundException;
> import java.io.FileOutputStream;
>
> import org.eclipse.core.resources.IFile;
> import org.eclipse.core.resources.IWorkspace;
> import org.eclipse.core.resources.ResourcesPlugin;
> import org.eclipse.core.runtime.CoreException;
> import org.eclipse.core.runtime.IPath;
> import org.eclipse.core.runtime.IProgressMonitor;
> import org.eclipse.draw2d.FigureCanvas;
> import org.eclipse.gef.ContextMenuProvider;
> import org.eclipse.gef.DefaultEditDomain;
> import org.eclipse.gef.EditPart;
> import org.eclipse.gef.EditPartViewer;
> import org.eclipse.gef.GraphicalViewer;
> import org.eclipse.gef.KeyHandler;
> import org.eclipse.gef.KeyStroke;
> import org.eclipse.gef.Tool;
> import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
> import org.eclipse.gef.palette.ConnectionCreationToolEntry;
> import org.eclipse.gef.palette.CreationToolEntry;
> import org.eclipse.gef.palette.MarqueeToolEntry;
> import org.eclipse.gef.palette.PaletteEntry;
> import org.eclipse.gef.palette.PaletteGroup;
> import org.eclipse.gef.palette.PaletteRoot;
> import org.eclipse.gef.palette.PanningSelectionToolEntry;
> import org.eclipse.gef.palette.ToolEntry;
> import org.eclipse.gef.requests.SimpleFactory;
> import org.eclipse.gef.tools.ConnectionCreationTool;
> import org.eclipse.gef.ui.actions.ActionRegistry;
> import org.eclipse.gef.ui.actions.DirectEditAction;
> import org.eclipse.gef.ui.actions.GEFActionConstants;
> import org.eclipse.gef.ui.parts.GraphicalEditorWithPalette;
> import org.eclipse.gef.ui.parts.GraphicalViewerKeyHandler;
> import org.eclipse.jface.action.IAction;
> import org.eclipse.jface.dialogs.ProgressMonitorDialog;
> import org.eclipse.swt.SWT;
> import org.eclipse.ui.IEditorInput;
> import org.eclipse.ui.IFileEditorInput;
> import org.eclipse.ui.IWorkbenchPart;
> import org.eclipse.ui.actions.ActionFactory;
> import org.eclipse.ui.actions.WorkspaceModifyOperation;
> import org.eclipse.ui.dialogs.SaveAsDialog;
> import org.eclipse.ui.part.FileEditorInput;
>
> import com.bonevich.simplegef.SimpleGefPlugin;
> import com.bonevich.simplegef.XMLHandler;
> import com.bonevich.simplegef.actions.PropertiesAction;
> import com.bonevich.simplegef.model.impl.DiagramImpl;
> import com.bonevich.simplegef.model.view.ConnectionView;
> import com.bonevich.simplegef.model.view.DiagramView;
> import com.bonevich.simplegef.model.view.PartView;
> import com.bonevich.simplegef.parts.SimpleGefEditPartFactory;
>
> /**
> * @author jbonevic
> * @version $Id: SimpleGefEditor.java,v 1.6 2004/09/30 22:51:45 jbonevic Exp $
> */
> public class SimpleGefEditor extends GraphicalEditorWithPalette {
> DiagramView diagram;
> private KeyHandler sharedKeyHandler;
>
> public SimpleGefEditor() {
> super();
> setEditDomain(new DefaultEditDomain(this));
> }
>
> /**
> * @see org.eclipse.gef.ui.parts.GraphicalEditorWithPalette#getPaletteRoot()
> */
> protected PaletteRoot getPaletteRoot() {
> PaletteRoot root = new PaletteRoot();
> PaletteGroup group = new PaletteGroup("Tools");
> root.add(group);
>
> ToolEntry tool = new PanningSelectionToolEntry();
> group.add(tool);
> root.setDefaultEntry(tool);
>
> tool = new MarqueeToolEntry();
> group.add(tool);
>
> PaletteEntry entry = new CreationToolEntry(
> "Part",
> "Add a new part to the diagram",
> new SimpleFactory(PartView.class),
> SimpleGefPlugin.getDefault().getImageDescriptor("icons/part.gif"),
> SimpleGefPlugin.getDefault().getImageDescriptor("icons/part.gif")
> );
> group.add(entry);
>
> entry = new ConnectionCreationToolEntry(
> "Connection",
> "Add a new connection between parts",
> new SimpleFactory(ConnectionView.class),
> SimpleGefPlugin.getDefault().getImageDescriptor("icons/connection.gif"),
> SimpleGefPlugin.getDefault().getImageDescriptor("icons/connection.gif")
> )
> {
> /**
> * @see org.eclipse.gef.palette.ConnectionCreationToolEntry#createTool()
> */
> public Tool createTool() {
> ConnectionCreationTool connectionTool = (ConnectionCreationTool) super.createTool();
> connectionTool.setUnloadWhenFinished(true);
> return connectionTool;
> }
> };
> group.add(entry);
>
> return root;
> }
>
> /**
> * @see org.eclipse.gef.ui.parts.GraphicalEditor#initializeGraphicalViewer()
> */
> protected void initializeGraphicalViewer() {
> EditPartViewer viewer = getGraphicalViewer();
> diagram = new DiagramView();
> viewer.setContents(diagram);
> }
>
> /**
> * @see org.eclipse.gef.ui.parts.GraphicalEditor#configureGraphicalViewer()
> */
> protected void configureGraphicalViewer() {
> super.configureGraphicalViewer();
> GraphicalViewer viewer = getGraphicalViewer();
>
> ScalableFreeformRootEditPart rootPart = new ScalableFreeformRootEditPart();
> viewer.setRootEditPart(rootPart);
> viewer.setEditPartFactory(new SimpleGefEditPartFactory());
>
> ((FigureCanvas) viewer.getControl()).setScrollBarVisibility(FigureCanvas.ALWAYS);
>
> getGraphicalViewer().setKeyHandler(
> new GraphicalViewerKeyHandler(viewer).setParent(getCommonKeyHandler())
> );
> ContextMenuProvider provider =
> new SimpleGefContextMenuProvider(viewer, getActionRegistry());
> viewer.setContextMenu(provider);
> getSite().registerContextMenu(
> "com.bonevich.simplegef.editors.contextmenu", //$NON-NLS-1$
> provider,
> viewer
> );
> }
>
> /**
> * @see org.eclipse.gef.ui.parts.GraphicalEditor#createActions()
> */
> protected void createActions() {
> super.createActions();
> ActionRegistry registry = getActionRegistry();
> IAction action = new DirectEditAction((IWorkbenchPart) this);
> registry.registerAction(action);
> getSelectionActions().add(action.getId());
>
> action = new PropertiesAction(this);
> registry.registerAction(action);
> getSelectionActions().add(action.getId());
> }
>
> /**
> * @return
> */
> protected KeyHandler getCommonKeyHandler() {
> if (sharedKeyHandler == null) {
> sharedKeyHandler = new KeyHandler();
> sharedKeyHandler.put(
> KeyStroke.getPressed(SWT.DEL, 127, 0),
> getActionRegistry().getAction(ActionFactory.DELETE)
> );
> sharedKeyHandler.put(
> KeyStroke.getPressed(SWT.F2, 0),
> getActionRegistry().getAction(GEFActionConstants.DIRECT_EDIT)
> );
> }
> return sharedKeyHandler;
> }
>
> /**
> * @see org.eclipse.ui.ISaveablePart#doSave(org.eclipse.core.runtime.IProgressMonitor)
> */
> public void doSave(IProgressMonitor monitor) {
>
> }
> /**
> * @see org.eclipse.ui.ISaveablePart#doSaveAs()
> */
> public void doSaveAs() {
> try {
> System.out.println("Got here with object "+diagram);
> XMLHandler handleXML = new XMLHandler();
> handleXML.WriteToXML("Testing.xml",diagram);
> } catch(Throwable th) {
> th.printStackTrace();
> }
> System.out.println("Exiting here with object "+diagram);
> }
>
> /**
> * @see org.eclipse.ui.ISaveablePart#isDirty()
> */
> public boolean isDirty() {
> return getCommandStack().isDirty();
> }
>
> /**
> * @see org.eclipse.ui.ISaveablePart#isSaveAsAllowed()
> */
> public boolean isSaveAsAllowed() {
> return true;
> }
>
> /*
> protected void setInput(IEditorInput input)
> {
> System.out.println("hmm....");
> }
> */
> }
>