Java tutorial
package prototype; /* * Copyright (c) 2003, the JUNG Project and the Regents of the University of * California All rights reserved. * * This software is open-source under the BSD license; see either "license.txt" * or http://jung.sourceforge.net/license.txt for a description. * */ import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.Shape; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.geom.Ellipse2D; import java.awt.geom.Point2D; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.text.SimpleDateFormat; import java.util.Collection; import java.util.Date; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.BorderFactory; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JToggleButton; import org.apache.commons.collections15.Factory; import org.apache.commons.collections15.Transformer; import org.apache.commons.collections15.functors.ConstantTransformer; import org.freehep.graphics2d.VectorGraphics; import org.freehep.graphicsio.svg.SVGGraphics2D; import edu.uci.ics.jung.algorithms.layout.PolarPoint; import edu.uci.ics.jung.algorithms.layout.RadialTreeLayout; import edu.uci.ics.jung.algorithms.layout.TreeLayout; import edu.uci.ics.jung.graph.DirectedGraph; import edu.uci.ics.jung.graph.DirectedSparseMultigraph; import edu.uci.ics.jung.graph.Forest; import edu.uci.ics.jung.graph.DelegateForest; import edu.uci.ics.jung.graph.DelegateTree; import edu.uci.ics.jung.graph.Tree; import edu.uci.ics.jung.visualization.GraphZoomScrollPane; import edu.uci.ics.jung.visualization.Layer; import edu.uci.ics.jung.visualization.VisualizationServer; import edu.uci.ics.jung.visualization.VisualizationViewer; import edu.uci.ics.jung.visualization.control.CrossoverScalingControl; import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse; import edu.uci.ics.jung.visualization.control.ModalGraphMouse; import edu.uci.ics.jung.visualization.control.ScalingControl; import edu.uci.ics.jung.visualization.decorators.EdgeShape; import edu.uci.ics.jung.visualization.decorators.ToStringLabeller; import edu.uci.ics.jung.visualization.layout.LayoutTransition; import edu.uci.ics.jung.visualization.util.Animator; import graphics.GeschaeftsfallViewer; import graphics.Link; /** * Demonsrates TreeLayout and RadialTreeLayout. * @author Tom Nelson * */ @SuppressWarnings("serial") public class TreeLayoutDemo extends JApplet { /** * the graph */ Forest<String, Integer> graph; int stringcutofflenght = 80; Factory<DirectedGraph<String, Integer>> graphFactory = new Factory<DirectedGraph<String, Integer>>() { public DirectedGraph<String, Integer> create() { return new DirectedSparseMultigraph<String, Integer>(); } }; Factory<Tree<String, Integer>> treeFactory = new Factory<Tree<String, Integer>>() { public Tree<String, Integer> create() { return new DelegateTree<String, Integer>(graphFactory); } }; Factory<Integer> edgeFactory = new Factory<Integer>() { int i = 0; public Integer create() { return i++; } }; Factory<String> vertexFactory = new Factory<String>() { int i = 0; public String create() { return "V" + i++; } }; Transformer<String, String> labler = new Transformer<String, String>() { public String transform(String s) { String out = htmlsplitstring(s); out = "<html><b>" + out + "</b></html>"; return out; } }; /** * the visual component and renderer for the graph */ VisualizationViewer<String, Integer> vv; VisualizationServer.Paintable rings; String root; TreeLayout<String, Integer> treeLayout; RadialTreeLayout<String, Integer> radialLayout; public TreeLayoutDemo() { // create a simple graph for the demo graph = new DelegateForest<String, Integer>(); createTree(); treeLayout = new TreeLayout<String, Integer>(graph); radialLayout = new RadialTreeLayout<String, Integer>(graph); vv = new VisualizationViewer<String, Integer>(treeLayout, new Dimension(5000, 5000)); vv.setBackground(Color.white); //vv.setPreferredSize(new Dimension(2000, 2000)); vv.setPreferredSize(new Dimension(5000, 5000)); vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line()); vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller()); // add a listener for ToolTips vv.setVertexToolTipTransformer(new ToStringLabeller()); vv.getRenderContext().setVertexLabelTransformer(labler); vv.getRenderContext().setArrowFillPaintTransformer(new ConstantTransformer(Color.lightGray)); rings = new Rings(); Container content = getContentPane(); final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv); content.add(panel); final DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse(); vv.setGraphMouse(graphMouse); JComboBox modeBox = graphMouse.getModeComboBox(); modeBox.addItemListener(graphMouse.getModeListener()); graphMouse.setMode(ModalGraphMouse.Mode.TRANSFORMING); final ScalingControl scaler = new CrossoverScalingControl(); JButton plus = new JButton("+"); plus.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { scaler.scale(vv, 1.1f, vv.getCenter()); } }); JButton minus = new JButton("-"); minus.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { scaler.scale(vv, 1 / 1.1f, vv.getCenter()); } }); JToggleButton radial = new JToggleButton("Radial"); JButton screenshot = new JButton("Screenshot"); screenshot.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Taking Screenshot"); //ExportDialog export = new ExportDialog(); //export.showExportDialog(vv, "Export view as ...", vv, "export"); Properties p = new Properties(); //setSize(5000,5000); p.setProperty("PageSize", "A1"); VectorGraphics g; try { Date dt = new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyy_MM_dd_HH_mm"); String filename = "C://Users//frisch//Desktop//mapexport//" + "Output" + df.format(dt) + ".svg"; System.out.println(panel.getHeight() + " / " + panel.getWidth()); g = new SVGGraphics2D(new File(filename), new Dimension(5000, 5000));//5000,5000 g.setBackground(Color.WHITE); g.setProperties(p); g.startExport(); vv.printAll(g); g.endExport(); } catch (IOException ex) { Logger.getLogger(GeschaeftsfallViewer.class.getName()).log(Level.SEVERE, null, ex); System.out.println("Exception : " + ex.getMessage()); } } }); radial.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { LayoutTransition<String, Integer> lt = new LayoutTransition<String, Integer>(vv, treeLayout, radialLayout); Animator animator = new Animator(lt); animator.start(); vv.getRenderContext().getMultiLayerTransformer().setToIdentity(); vv.addPreRenderPaintable(rings); } else { LayoutTransition<String, Integer> lt = new LayoutTransition<String, Integer>(vv, radialLayout, treeLayout); Animator animator = new Animator(lt); animator.start(); vv.getRenderContext().getMultiLayerTransformer().setToIdentity(); vv.removePreRenderPaintable(rings); } vv.repaint(); } }); JPanel scaleGrid = new JPanel(new GridLayout(1, 0)); scaleGrid.setBorder(BorderFactory.createTitledBorder("Zoom")); JPanel controls = new JPanel(); scaleGrid.add(plus); scaleGrid.add(minus); controls.add(radial); controls.add(scaleGrid); controls.add(modeBox); controls.add(screenshot); content.add(controls, BorderLayout.NORTH); } class Rings implements VisualizationServer.Paintable { Collection<Double> depths; public Rings() { depths = getDepths(); } private Collection<Double> getDepths() { Set<Double> depths = new HashSet<Double>(); Map<String, PolarPoint> polarLocations = radialLayout.getPolarLocations(); for (String v : graph.getVertices()) { PolarPoint pp = polarLocations.get(v); depths.add(pp.getRadius()); } return depths; } public void paint(Graphics g) { g.setColor(Color.lightGray); Graphics2D g2d = (Graphics2D) g; Point2D center = radialLayout.getCenter(); Ellipse2D ellipse = new Ellipse2D.Double(); for (double d : depths) { ellipse.setFrameFromDiagonal(center.getX() - d, center.getY() - d, center.getX() + d, center.getY() + d); Shape shape = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT) .transform(ellipse); g2d.draw(shape); } } public boolean useTransform() { return true; } } /** * */ private void createTree() { //ReadXLSStructure xlsread = new ReadXLSStructure(); // HashSet Nodes = xlsread.returnNodes0_Links1(0, "C:\\Users\\frisch\\Desktop\\CRR_Off_Balance_Structure.xls"); // HashSet Links = xlsread.returnNodes0_Links1(1, "C:\\Users\\frisch\\Desktop\\CRR_Off_Balance_Structure.xls"); HashSet Links = new HashSet(); HashSet Nodes = new HashSet(); addNodes(Nodes, "C:\\Users\\frisch\\Desktop\\GFViewerData\\GFNodes.txt"); addLinks(Links, "C:\\Users\\frisch\\Desktop\\GFViewerData\\GFLinks.txt"); Iterator it = Nodes.iterator(); while (it.hasNext()) { graph.addVertex((String) it.next()); } it = Links.iterator(); while (it.hasNext()) { String dummy = (String) it.next(); String[] x = dummy.split("///"); graph.addEdge(edgeFactory.create(), x[0], x[1]); } /// // // graph.addVertex("V0"); // graph.addEdge(edgeFactory.create(), "V0", "V1"); // graph.addEdge(edgeFactory.create(), "V0", "V2"); // graph.addEdge(edgeFactory.create(), "V1", "V4"); // graph.addEdge(edgeFactory.create(), "V2", "V3"); // graph.addEdge(edgeFactory.create(), "V2", "V5"); // graph.addEdge(edgeFactory.create(), "V4", "V6"); // graph.addEdge(edgeFactory.create(), "V4", "V7"); // graph.addEdge(edgeFactory.create(), "V3", "V8"); // graph.addEdge(edgeFactory.create(), "V6", "V9"); // graph.addEdge(edgeFactory.create(), "V4", "V10"); // // graph.addVertex("A0"); // graph.addEdge(edgeFactory.create(), "A0", "A1"); // graph.addEdge(edgeFactory.create(), "A0", "A2"); // graph.addEdge(edgeFactory.create(), "A0", "A3"); // // graph.addVertex("B0"); // graph.addEdge(edgeFactory.create(), "B0", "B1"); // graph.addEdge(edgeFactory.create(), "B0", "B2"); // graph.addEdge(edgeFactory.create(), "B1", "B4"); // graph.addEdge(edgeFactory.create(), "B2", "B3"); // graph.addEdge(edgeFactory.create(), "B2", "B5"); // graph.addEdge(edgeFactory.create(), "B4", "B6"); // graph.addEdge(edgeFactory.create(), "B4", "B7"); // graph.addEdge(edgeFactory.create(), "B3", "B8"); // graph.addEdge(edgeFactory.create(), "B6", "B9"); } /** * a driver for this demo */ String htmlsplitstring(String dummy) { String checkdummy = ""; if (dummy.contains("$") == false) { int steps = dummy.length() / stringcutofflenght; int i; for (i = 0; i < steps; i++) { checkdummy = checkdummy + "<br>" + dummy.substring(i * stringcutofflenght, (i + 1) * stringcutofflenght); } if ((i) * stringcutofflenght < dummy.length()) { checkdummy = checkdummy + "<br>" + dummy.substring((i) * stringcutofflenght, dummy.length()); } } else { //System.out.println("kdfskdksdklskdlsdklskdlskdlskdlskdlskd"); String[] lines = dummy.split("\\$"); for (int i = 0; i < lines.length; i++) { //System.out.println("check" + lines[i]); if (i > 0) { checkdummy = checkdummy + "<br>" + lines[i]; } else { checkdummy = lines[i]; } } } return checkdummy; } public static void main(String[] args) { JFrame frame = new JFrame(); Container content = frame.getContentPane(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); content.add(new TreeLayoutDemo()); frame.pack(); frame.setVisible(true); } public void addNodes(HashSet Nodes, String inFile) { int cnt = 1; try { // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream(inFile); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console //System.out.println (strLine); String x = strLine; // if (dummy.length() > String_cutoff_length){ // dummy.S // // } Nodes.add(x); cnt++; //System.out.println (type); } //Close the input stream in.close(); } catch (Exception e) {//Catch exception if any System.err.println("Error: " + e.getMessage()); } } public void addLinks(HashSet Links, String inFile) { try { // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream(inFile); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console //System.out.println (strLine); String x = strLine; Links.add(x); //if (AllNames.contains(x[0]) == true && AllNames.contains(x[1])== true) } //Close the input stream in.close(); } catch (Exception e) {//Catch exception if any System.err.println("Error: " + e.getMessage()); } } }