/* * Licensed Materials - Property of Perforce Software, Inc. * © Copyright Perforce Software, Inc. 2014, 2021 * © Copyright IBM Corp. 2009, 2014 * © Copyright ILOG 1996, 2009 * All Rights Reserved. * * Note to U.S. Government Users Restricted Rights: * The Software and Documentation were developed at private expense and * are "Commercial Items" as that term is defined at 48 CFR 2.101, * consisting of "Commercial Computer Software" and * "Commercial Computer Software Documentation", as such terms are * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4, * as applicable. */ import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.SystemColor; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.net.URL; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRootPane; import javax.swing.JTextField; import ilog.views.IlvDirection; import ilog.views.IlvGrapher; import ilog.views.IlvHandlesSelection; import ilog.views.IlvManagerView; import ilog.views.IlvTransformer; import ilog.views.animation.IlvManagerAnimator; import ilog.views.graphlayout.IlvGraphLayout; import ilog.views.graphlayout.IlvGraphLayoutException; import ilog.views.graphlayout.IlvGraphLayoutReport; import ilog.views.graphlayout.IlvGrapherAdapter; import ilog.views.graphlayout.random.IlvRandomLayout; import ilog.views.graphlayout.tree.IlvTreeLayout; import ilog.views.swing.IlvJScrollManagerView; import ilog.views.util.IlvProductUtil; import ilog.views.util.swing.IlvSwingUtil; /** * The main class SingleThreadedAnimationApp. This shows how to use the * animation framework in a single-threaded version with a Swing view (a * IlvJScrollManagerView). The single-threaded version flickers more than the * multithreaded version; therefore it is recommended to use multithreading for * animation. The app allows you to select various layouts. The position * changes between the layouts are animated smoothly. The animation rate (how * many animation steps are drawn to show the movement) and the animation delay * (the interval between animation steps) can be entered to control the behavior * of the app. */ public class SingleThreadedAnimationApp extends JRootPane { { // This sample uses JViews Diagrammer features. When deploying an // application that includes this code, you need to be in possession // of a Perforce JViews Diagrammer Deployment license. IlvProductUtil.DeploymentLicenseRequired(IlvProductUtil.JViews_Diagrammer_Deployment); } /** The grapher */ IlvGrapher grapher = new IlvGrapher(); /** The view of the grapher */ IlvManagerView mgrview = new IlvManagerView(grapher, null); /** The animator */ IlvManagerAnimator mgranim = new IlvManagerAnimator(grapher); /** An instance of the Tree Layout algorithm */ IlvTreeLayout treeLayout = new IlvTreeLayout(); /** An instance of the Random Layout algorithm */ IlvRandomLayout randomLayout = new IlvRandomLayout(); /** A text field to display messages */ JTextField msgLine = new JTextField(); /** Text fields for the animation parameters */ JTextField animationRate = new JTextField("" + 40 + " ", 4); JTextField accelerationRate = new JTextField("" + 20 + " ", 4); JTextField animationDelay = new JTextField("" + 20 + " ", 4); /** * Initializes the app. */ public void init() { showMessage("animation sample initialization ..."); // Set the default values of the animator mgranim.setAnimationRate(40); mgranim.setAnimationAccelerationRate(20); // attach the grapher to the layout instances IlvGrapherAdapter adapter = new IlvGrapherAdapter(grapher); treeLayout.attach(adapter); randomLayout.attach(adapter); // use manager coordinates treeLayout.setCoordinatesMode(IlvGraphLayout.MANAGER_COORDINATES); randomLayout.setCoordinatesMode(IlvGraphLayout.MANAGER_COORDINATES); // create the scroll manager view IlvJScrollManagerView scrollManView = new IlvJScrollManagerView(mgrview); // Some settings on the manager view and on the scroll manager view. // Keep antialiasing off, because if it's on, drawing is slower. mgrview.setAntialiasing(false); mgrview.setKeepingAspectRatio(true); mgrview.setBackground(Color.white); mgrview.setForeground(SystemColor.windowText); Color xc = SystemColor.windowText; xc = new Color(255 - xc.getRed(), 255 - xc.getGreen(), 255 - xc.getBlue()); mgrview.setDefaultXORColor(xc); mgrview.setDefaultGhostColor(SystemColor.windowText); // Settings parameters for selection handles IlvHandlesSelection.defaultHandleColor = Color.black; IlvHandlesSelection.defaultHandleBackgroundColor = Color.white; IlvHandlesSelection.defaultHandleShape = IlvHandlesSelection.SQUARE_SHAPE; // set the layout manager getContentPane().setLayout(new BorderLayout(0, 0)); // a panel at the top JPanel panel = new JPanel(); panel.setLayout(new FlowLayout(FlowLayout.LEFT)); // layout style selection JComboBox<String> chooser = new JComboBox<String>(); panel.add(new JLabel("Select layout:")); panel.add(chooser); chooser.addItem("random"); chooser.addItem("tree"); chooser.addItem("org chart"); chooser.addItem("tip over"); chooser.addItem("radial"); chooser.setSelectedIndex(0); chooser.setToolTipText("Select a style and start layout"); chooser.addItemListener(new ItemListener() { Override public void itemStateChanged(ItemEvent event) { if (event.getStateChange() == ItemEvent.DESELECTED) return; transferAnimationParameter(); performLayout((String) event.getItem()); } }); panel.add(new JLabel(" ")); panel.add(new JLabel("Animation rate:")); panel.add(animationRate); panel.add(new JLabel(" ")); panel.add(new JLabel("Acceleration rate:")); panel.add(accelerationRate); panel.add(new JLabel(" ")); panel.add(new JLabel("Animation delay:")); panel.add(animationDelay); animationRate.setToolTipText("Set the animation frame rate"); animationRate.addActionListener(new ActionListener() { Override public void actionPerformed(ActionEvent e) { // transfer parameters from the text fields to the manager animator transferAnimationParameter(); } }); accelerationRate.setToolTipText("Set the animation acceleration rate"); accelerationRate.addActionListener(new ActionListener() { Override public void actionPerformed(ActionEvent e) { // transfer parameters from the text fields to the manager animator transferAnimationParameter(); } }); animationDelay.setToolTipText("Set the delay between animation frames"); animationDelay.addActionListener(new ActionListener() { Override public void actionPerformed(ActionEvent e) { // transfer parameters from the text fields to the manager animator transferAnimationParameter(); } }); // the message line at the bottom of the frame msgLine.setEditable(false); // fit all together getContentPane().add("Center", scrollManView); getContentPane().add("North", panel); getContentPane().add("South", msgLine); // load a sample grapher try { grapher.read("data" + System.getProperty("file.separator") + "Sample.ivl"); grapher.reDraw(); } catch (Exception e) { // e.printStackTrace(); showMessage(e.getMessage()); } } /** * Allows you to run the demo as a standalone application. */ public static void main(String[] arg) { SingleThreadedAnimationApp app = new SingleThreadedAnimationApp(); app.init(); JFrame frame = new JFrame("Animated Layout Demo (Single Threaded)"); // EXIT_ON_CLOSE is not defined in jdk1.2, hence this way: frame.setDefaultCloseOperation(3 /* EXIT_ON_CLOSE, >= jdk1.2.2 */); frame.setSize(750, 600); frame.getContentPane().add(app); frame.setVisible(true); } /** * Displays a message. */ void showMessage(String message) { // the message is displayed in the message line msgLine.setText(message); msgLine.paintImmediately(0, 0, msgLine.getWidth(), msgLine.getHeight()); } /** * Transfers the animation parameters from the text fields of the app to * the manager animator. */ void transferAnimationParameter() { int value; try { // validate the animation rate value = Integer.valueOf(animationRate.getText().trim()).intValue(); if (value < 1) value = 1; animationRate.setText("" + value); mgranim.setAnimationRate(value); // validate the animation acceleration rate value = Integer.valueOf(accelerationRate.getText().trim()).intValue(); if (value < 0) value = 0; if (value > mgranim.getAnimationRate() / 2) value = mgranim.getAnimationRate() / 2; accelerationRate.setText("" + value); mgranim.setAnimationAccelerationRate(value); // validate the animation delay value = Integer.valueOf(animationDelay.getText().trim()).intValue(); if (value < 10) value = 10; animationDelay.setText("" + value); mgranim.setAnimationDelay(value); } catch (Exception ex) { showMessage("Illegal animation rate or delay: " + ex.getMessage()); } } /** * Prepares and performs the graph layout. The input string indicates what * kind of layout should be performed. */ void performLayout(String layoutType) { if (layoutType.equals("random")) { layout(randomLayout, grapher); } else if (layoutType.equals("tree")) { treeLayout.setLayoutMode(IlvTreeLayout.FREE); treeLayout.setGlobalAlignment(IlvTreeLayout.CENTER); treeLayout.setFlowDirection(IlvDirection.Bottom); treeLayout.setGlobalLinkStyle(IlvTreeLayout.STRAIGHT_LINE_STYLE); layout(treeLayout, grapher); } else if (layoutType.equals("org chart")) { treeLayout.setLayoutMode(IlvTreeLayout.FREE); treeLayout.setGlobalAlignment(IlvTreeLayout.CENTER); treeLayout.setFlowDirection(IlvDirection.Right); treeLayout.setGlobalLinkStyle(IlvTreeLayout.ORTHOGONAL_STYLE); layout(treeLayout, grapher); } else if (layoutType.equals("tip over")) { treeLayout.setLayoutMode(IlvTreeLayout.FREE); treeLayout.setGlobalAlignment(IlvTreeLayout.TIP_OVER); treeLayout.setFlowDirection(IlvDirection.Bottom); treeLayout.setGlobalLinkStyle(IlvTreeLayout.ORTHOGONAL_STYLE); layout(treeLayout, grapher); } else if (layoutType.equals("radial")) { treeLayout.setLayoutMode(IlvTreeLayout.RADIAL); treeLayout.setGlobalAlignment(IlvTreeLayout.CENTER); treeLayout.setFlowDirection(IlvDirection.Bottom); treeLayout.setGlobalLinkStyle(IlvTreeLayout.STRAIGHT_LINE_STYLE); treeLayout.setAspectRatio(1.0); layout(treeLayout, grapher); } } /** * Perform a layout. This now illustrates how to implement the animation. */ void layout(IlvGraphLayout layout, IlvGrapher grapher) { // record the start state for the animation mgranim.recordState(); // show a start message showMessage("layout started ... wait ..."); try { // to avoid influence by the transformer, set transformer to null before // layout mgrview.setTransformer(new IlvTransformer()); // perform the layout and get the layout report IlvGraphLayoutReport layoutReport = layout.performLayout(false, false); // fit all in view mgrview.fitTransformerToContent(); // print the code from the layout report showMessage(layoutReport.codeToString(layoutReport.getCode()) + " ... animation started ..."); // animate the transition from the recorded state to the current state mgranim.animate(); // final message showMessage("animation done"); } catch (IlvGraphLayoutException e) { // e.printStackTrace(); showMessage(e.getMessage()); } } }