/* * Copyright (C) 2003 Jens Kanschik, * mail : jensKanschik@users.sourceforge.net * * Part of , an open source project at sourceforge.net * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package hypergraph.applications.hexplorer; import hypergraph.graphApi.Graph; import hypergraph.graphApi.GraphSystem; import hypergraph.graphApi.GraphSystemFactory; import hypergraph.graphApi.Node; import hypergraph.graphApi.algorithms.GraphUtilities; import hypergraph.graphApi.io.GraphWriter; import hypergraph.graphApi.io.GraphXMLWriter; import hypergraph.graphApi.io.SAXReader; import hypergraph.visualnet.ArrowLineRenderer; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.URL; import javax.swing.JApplet; import javax.swing.JOptionPane; import org.xml.sax.SAXException; /** * @author Jens Kanschik * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class HExplorerApplet extends JApplet { /** Stores the graph that the applet shows. */ private GraphPanel graphPanel; public GraphPanel getGraphPanel() { return graphPanel; } /**@inheritDoc */ public void init() { String file = getParameter("file"); String value = getParameter("value"); GraphSystem graphSystem = null; try { graphSystem = GraphSystemFactory.createGraphSystem("hypergraph.graph.GraphSystemImpl", null); } catch (Exception e) { e.printStackTrace(); System.exit(8); } Graph graph = null; URL url = null; try { SAXReader reader = null ; if((null != file) && (!"".equals(file))) { //url = new URL(getCodeBase(), file); url = new URL("" + getCodeBase() + file) ; reader = new SAXReader(graphSystem, url); } else { reader = new SAXReader(graphSystem, value, new URL(getCodeBase(), "")); } ContentHandlerFactory ch = new ContentHandlerFactory(); ch.setBaseUrl(getCodeBase()); reader.setContentHandlerFactory(ch); graph = reader.parse(); } catch (FileNotFoundException fnfe) { JOptionPane.showMessageDialog(null, "Could not find file " + url.getFile() + ". \n" + "Start applet with default graph", "File not found", JOptionPane.ERROR_MESSAGE); System.out.println("Exception : " + fnfe); fnfe.printStackTrace(System.out); } catch (SAXException saxe) { JOptionPane.showMessageDialog(null, "Error while parsing file" + url.getFile() + ". \n" + "Exception : " + saxe + ". \n" + "Start applet with default graph", "Parsing error", JOptionPane.ERROR_MESSAGE); System.out.println("Exception : " + saxe); saxe.getException().printStackTrace(); saxe.printStackTrace(System.out); } catch (Exception e) { java.io.StringWriter sw = new java.io.StringWriter() ; java.io.PrintWriter pw = new java.io.PrintWriter(sw) ; e.printStackTrace(pw) ; String stackTrace = sw.getBuffer().toString() ; JOptionPane.showMessageDialog(null, "General error while reading file " + url + ". \n" + "Exception : " + e + ". \n" + stackTrace + "\n" + "Start applet with default graph", "General error", JOptionPane.ERROR_MESSAGE); System.out.println(url); System.out.println("Exception : " + e); e.printStackTrace(System.out); } if (graph == null) { graph = GraphUtilities.createTree(graphSystem, 2, 3); } graphPanel = new GraphPanel(graph, this); file = getParameter("properties"); if (file != null) try { url = new URL(getCodeBase(), file); graphPanel.loadProperties(url.openStream()); } catch (FileNotFoundException fnfe) { JOptionPane.showMessageDialog(null, "Could not find propertyfile " + url.getFile() + ". \n" + "Start applet with default properties", "File not found", JOptionPane.ERROR_MESSAGE); } catch (Exception e) { java.io.StringWriter sw = new java.io.StringWriter() ; java.io.PrintWriter pw = new java.io.PrintWriter(sw) ; e.printStackTrace(pw) ; String stackTrace = sw.getBuffer().toString() ; JOptionPane.showMessageDialog(null, "General error while reading file " + url.getFile() + ". \n" + "Exception : " + e + ". \n" + stackTrace + "\n" + "Start applet with default properties", "General error", JOptionPane.ERROR_MESSAGE); System.out.println(url); System.out.println("Exception : " + e); e.printStackTrace(System.out); } String center = getParameter("center"); if (center != null) { Node n = (Node) graph.getElement(center); if (n != null) graphPanel.centerNode(n); } graphPanel.setLineRenderer(new ArrowLineRenderer()); getContentPane().add(graphPanel); } public String getGraphXML() { try { OutputStream os = new ByteArrayOutputStream(); GraphWriter graphWriter = new GraphXMLWriter(new OutputStreamWriter(os)); graphWriter.write(getGraphPanel().getGraph()); return os.toString(); } catch (IOException ioe) { ioe.printStackTrace(); return ioe.toString(); } } }