⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netviewer.java

📁 Rakiura JFern是一个非常轻型的带有模拟器的Petri网络框架
💻 JAVA
字号:
// This is copyrighted source file, part of Rakiura JFern package. // See the file LICENSE for copyright information and the terms and conditions// for copying, distributing and modifications of Rakiura JFern package.// Copyright (C) 1999-2002 by Mariusz Nowostawski and others  [http://www.rakiura.org]package org.rakiura.cpn.gui;/**/import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import javax.swing.ButtonModel;import javax.swing.AbstractButton;import javax.swing.ButtonGroup;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JMenuItem;import javax.swing.JPopupMenu;import javax.swing.JRadioButtonMenuItem;import javax.swing.filechooser.FileFilter;import org.rakiura.cpn.Arc;import org.rakiura.cpn.Net;import org.rakiura.cpn.NetElement;import org.rakiura.cpn.Place;import org.rakiura.cpn.Transition;import org.rakiura.draw.Figure;import org.rakiura.draw.basic.BasicDrawingView;import org.rakiura.draw.basic.BasicEditor;import org.rakiura.draw.figure.ChopBoxConnector;import org.rakiura.draw.figure.ChopEllipseConnector;import org.rakiura.draw.figure.LineConnection;import org.rakiura.draw.figure.TextFigure;/** * Represents a view, a visualization of a single Net. *  *<br><br> * NetViewer.java<br> * Created: Mon Apr 15 14:43:46 2002<br> * *@author  <a href="mariusz@rakiura.org">Mariusz Nowostawski</a> *@version $Revision: 1.7 $ $Date: 2002/05/30 23:01:50 $ *@since 2.0 */public class NetViewer extends BasicDrawingView implements ActionListener {    /**/  private Net net;  /** map of places -> jplaces */  final Map map = new HashMap();  /** map name -> figure. */  final Map layout = new HashMap();  /** map name -> point. */  Map layoutHolder = new HashMap();  /** map filename -> layoutHolder */  Map layoutStore = new HashMap();  /**/  public NetViewer(final Net aNet) {    super(440, 550);    this.net = aNet;    final Iterator places = this.net.places().iterator();    while (places.hasNext()) {      final Place p = (Place) places.next();      final JPlace jp = new JPlace(p);      map.put(p, jp);      add(jp);    }    final Iterator trans = this.net.transitions().iterator();    while (trans.hasNext()) {      final Transition t = (Transition) trans.next();      final JTransition jt = new JTransition(t);      map.put(t, jt);      add(jt);      final Iterator inA = t.inputArcs().iterator();      while (inA.hasNext()) {        final JPlace jp = (JPlace) map.get(((Arc) inA.next()).place());        final LineConnection l1 = new LineConnection();        l1.connectStart(new ChopEllipseConnector(jp.getCircle()));        l1.connectEnd(new ChopBoxConnector(jt.getRectangle()));        l1.updateConnection();        add(l1);      }      final Iterator outA = t.outputArcs().iterator();      while (outA.hasNext()) {        final JPlace jp = (JPlace) map.get(((Arc) outA.next()).place());        final LineConnection l2 = new LineConnection();        l2.connectStart(new ChopBoxConnector(jt.getRectangle()));        l2.connectEnd(new ChopEllipseConnector(jp.getCircle()));        l2.updateConnection();        add(l2);      }    }    add(new TextFigure("Net name:  " + this.net.getName(), false));    prepareLayout();    createPopup();  }  public JFrame getFrame() {    final JFrame frame = new JFrame();    frame.getContentPane().add((javax.swing.JPanel) this);    frame.pack();    return frame;  }    private JPopupMenu popup;  private ButtonGroup group = new ButtonGroup();  void createPopup() {    popup = new JPopupMenu();    JMenuItem menuItem = new JMenuItem("Load Net layout");    menuItem.addActionListener(this);    menuItem.setActionCommand(LOAD_LAYOUT);    popup.add(menuItem);    menuItem = new JMenuItem("Save Net layout");    menuItem.addActionListener(this);    menuItem.setActionCommand(SAVE_LAYOUT);    popup.add(menuItem);    popup.addSeparator();    //Add listener to components that can bring up popup menus.    MouseListener popupListener = new MouseAdapter() {        public void mousePressed(MouseEvent e) {          maybeShowPopup(e);        }        public void mouseReleased(MouseEvent e) {          maybeShowPopup(e);        }        private void maybeShowPopup(MouseEvent e) {          if (e.isPopupTrigger()) {            popup.show(e.getComponent(), e.getX(), e.getY());          }        }      };    this.addMouseListener(popupListener);  }  void addGroupButton(final String fileName) {    final JRadioButtonMenuItem rbMenuItem = new JRadioButtonMenuItem(fileName);    this.group.add(rbMenuItem);    this.popup.add(rbMenuItem);    final ButtonModel m = this.group.getSelection();    if (m != null) { this.group.setSelected(m, false); }    this.group.setSelected(rbMenuItem.getModel(), true);    rbMenuItem.setActionCommand(SELECT_LAYOUT);    rbMenuItem.addActionListener(this);  }  private final static String SELECT_LAYOUT = "selectLayout";  private final static String LOAD_LAYOUT = "loadLayout";  private final static String SAVE_LAYOUT = "saveLayout";    /** Action listener handler. */  public void actionPerformed(ActionEvent e) {    final String command = e.getActionCommand();    if (command.equals(LOAD_LAYOUT)) {      loadLayout();    } else if (command.equals(SAVE_LAYOUT)) {      saveLayout();    } else if (command.equals(SELECT_LAYOUT)) {      Map m = (Map) this.layoutStore.get(((AbstractButton) e.getSource()).getText());      if (m != null)        this.layoutHolder = m;    }    updateLayout();  }  /**    * Loads previously saved layout and makes it active. */  private void loadLayout() {    final File file = chooseFile();    try {      final ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));      this.layoutHolder = (Map) in.readObject();      in.close();      updateLayout();      this.layoutStore.put(file.getName(), this.layoutHolder);      addGroupButton(file.getName());    } catch (IOException ioe) { ioe.printStackTrace();     } catch (ClassNotFoundException cnfe) { cnfe.printStackTrace(); }  }  /**   * Saves the current net layout to a file. */  private void saveLayout() {    final File file = chooseFile();    prepareLayout();    try {      final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));      out.writeObject(this.layoutHolder);      out.flush();      out.close();    } catch (IOException ioe) { ioe.printStackTrace(); }  }  /**   * Prepares the internal layout datastructures. */  private void prepareLayout() {    final Iterator iter = this.map.keySet().iterator();    while (iter.hasNext()) {      final NetElement netElement = (NetElement) iter.next();      final Figure figure = (Figure) this.map.get(netElement);      final String name = netElement.getName();      this.layout.put(name, figure);      this.layoutHolder.put(name, figure.center());    }  }  /**   * Updates the layout on the screen. */  private void updateLayout() {    final Iterator iter = this.layoutHolder.keySet().iterator();    while (iter.hasNext()) {      final String name = iter.next().toString();      final Point point = (Point) this.layoutHolder.get(name);      final Figure figure = (Figure) this.layout.get(name);      Point c = figure.center();      figure.moveBy(- c.x, - c.y);      figure.moveBy(point.x, point.y);    }  }  /** File filter for the layout files. */  private static final FileFilter FILTER =      new FileFilter(){        /**         * @param aFile file to be tested         * @return <code>true</code> when the file name ends with jar         */        public boolean accept(final File aFile) {          if (aFile.isDirectory() ||               aFile.getName().endsWith(".jlf")) {            return true;          } else {            return false;           }        }        /** @return filter description */        public String getDescription(){          return "JFern Layout files";        }      };  /**   * Shows the file chooser and    * allows the user to choose a file.   *@return the choosen file   */  private File chooseFile() {    JFileChooser chooser = new JFileChooser();    chooser.addChoosableFileFilter(FILTER);    chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));    int returnVal = chooser.showOpenDialog(this);    if (returnVal == JFileChooser.APPROVE_OPTION) {      return chooser.getSelectedFile();     } else {      return null;    }  }} // NetViewer//////////////////// end of file ////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -