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

📄 jmtclipboard.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
字号:
/**    
  * Copyright (C) 2006, Laboratorio di Valutazione delle Prestazioni - Politecnico di Milano

  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.

  * This program 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 General Public License for more details.

  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
  
package jmt.gui.jmodel.controller;

import jmt.gui.common.routingStrategies.ProbabilityRouting;
import jmt.gui.common.routingStrategies.RoutingStrategy;
import jmt.gui.jmodel.JGraphMod.CellComponent;
import jmt.gui.jmodel.JGraphMod.CellFactory;
import jmt.gui.jmodel.JGraphMod.JmtCell;
import jmt.gui.jmodel.JGraphMod.JmtEdge;
import jmt.gui.jmodel.definitions.JmodelStationDefinition;
import org.jgraph.JGraph;

import java.awt.geom.Point2D;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

/**
 * <p>Title: Jmt Clipboard</p>
 * <p>Description: This class provides clipboard facility to copy and paste selected sections
 * of current model. It will operate both on data structure (jmodelStationDefinition) end
 * on graphic rapresentation (Jgraph through mediator calls).</p>
 * 
 * @author Bertoli Marco
 *         Date: 21-giu-2005
 *         Time: 11.49.57
 */
public class JmtClipboard {
    protected Mediator mediator;
    protected HashMap stations;
    protected HashMap stationpositions;
    protected Vector links;
    protected Point2D zero;

    /**
     * When trying to paste over existing cells, try to move of 'move' down and right
     */
    protected static final int move = 20;

    /**
     * Initializes a new JmtClipboard with given mediator
     * @param m Mediator to be referenced
     */
    public JmtClipboard(Mediator m) {
        this.mediator = m;
    }

    /**
     * Flushes this clipboard. This method will be called upon copy and can be called whenever
     * user wants to flush clipboard
     */
    public void flush() {
        stations = new HashMap();
        stationpositions = new HashMap();
        links = new Vector();
        zero = null;
    }

    /**
     * Copy current selected Stations and Edges into clipboard
     */
    public void copy() {
        flush();
        // Gets selected cells
        JmodelStationDefinition sd = mediator.getStationDefinition();
        JGraph graph = mediator.getGraph();
        Object cells[] = graph.getDescendants(graph.getSelectionCells());
        // Temp variables
        Object key; // Station key
        JmtEdge edgetmp;
        JmtCell celltmp;
        Point2D location; // position of a station
        // Saves into data structure selected stations (including position) and links
        for (int i=0; i<cells.length; i++)
            if (cells[i] instanceof JmtCell) {
                celltmp = (JmtCell) cells[i];
                key = ((CellComponent)celltmp.getUserObject()).getKey();
                stations.put(key, sd.serializeStation(key));
                location = mediator.getCellCoordinates(celltmp);
                stationpositions.put(key, location);
                // Initialize 'zero' as the upper-leftmost point of selected stations
                // Will be used as a bias while pasting
                if (zero == null)
                    zero = location;
                if (zero.getX() > location.getX())
                    zero = new Point2D.Double(location.getX(), zero.getY());
                if (zero.getY() > location.getY())
                    zero = new Point2D.Double(zero.getX(),  location.getY());
            }
            else if (cells[i] instanceof JmtEdge) {
                edgetmp = (JmtEdge) cells[i];
                links.add(new Connection(edgetmp.getSourceKey(), edgetmp.getTargetKey()));
            }
    }

    /**
     * Copy current selected Stations and Edges into clipboard and deletes them from current graph.
     * It simply calls copy() and then mediator.deleteSelected()
     */
    public void cut() {
        copy();
        mediator.deleteSelected();
    }

    /**
     * Pastes previously copied elements into graph, beginning from the point indicated by where
     * and constructing new data structures.
     * @param where upper-left point of the region where pasted components will be put.
     */
    public void paste(Point2D where) {
        HashMap tempkey = new HashMap(); // Used as a translator from old key to new one to paste correct links
        HashMap newstations = new HashMap(); // Used to store newly created stations
        Vector select = new Vector(); // Elements to be selected after paste
        // Temp variables
        JmtCell newcell; //New created cell
        JmtEdge newEdge; //New created edge (link)
        Object newkey; // New station key
        Object oldkey; // Old station key
        Iterator keys; // All keys in Hashmap stations
        Point2D oldpos; // Old station position
        Point2D newpos; // New station position
        if (stations == null || stations.size() == 0)
            return;

        JmodelStationDefinition sd = mediator.getStationDefinition();

        // Recreates all stations and position them
        keys = stations.keySet().iterator();
        while (keys.hasNext()) {
            oldkey = keys.next();
            // Creates a new station with parameters got from previous copy operation
            newkey = sd.deserializeStation(stations.get(oldkey));
            newcell = CellFactory.createCell(sd.getStationType(newkey)+"Cell", 
                    new CellComponent(newkey, sd));
            tempkey.put(oldkey, newkey);
            // Calculates where this station should be put
            oldpos = (Point2D) stationpositions.get(oldkey);
            newpos = new Point2D.Double(where.getX()+oldpos.getX()-zero.getX(),
                    where.getY()+oldpos.getY()-zero.getY());
            // Insert created station into JGraph. Finds the first empty position going
            // down and right with respect of original position
            while(mediator.overlapCells(newpos, newcell))
                newpos.setLocation(newpos.getX()+move,newpos.getY()+move);
            mediator.InsertCell(newpos, newcell);
            newstations.put(newkey, newcell);
            select.add(newcell);
        }

        Object sourceKey;
        Object targetKey;

        // Creates new links and show them on graph
        for (int i=0; i<links.size(); i++) {
            // Translates old key values in new ones
            sourceKey = tempkey.get(((Connection)links.get(i)).sourceKey);
            targetKey = tempkey.get(((Connection)links.get(i)).targetKey);
            newEdge = mediator.connect((JmtCell)newstations.get(sourceKey),
                        (JmtCell)newstations.get(targetKey));
            if (newEdge != null)
                select.add(newEdge);
        }

        // Now adjusts Empirical Routing (if any) - Now named Probability Routing
        keys = tempkey.keySet().iterator();
        Object[] classes;
        RoutingStrategy rs;
        HashMap oldRouting, newRouting;
        // Search in every new inserted station
        while (keys.hasNext()) {
            newkey = tempkey.get(keys.next());
            classes = mediator.getClassDefinition().getClassKeys().toArray();
            for (int i=0; i<classes.length; i++) {
                rs = (RoutingStrategy) mediator.getStationDefinition().getRoutingStrategy(newkey, classes[i]);
                if (rs instanceof ProbabilityRouting) {
                    oldRouting = rs.getValues();
                    newRouting = new HashMap();
                    // For each old destination, set new one if it was copied with
                    Object[] oldDest = oldRouting.keySet().toArray();
                    for (int j=0; j<oldDest.length; j++) {
                        // Now checks if target station was copied with source one and a link connecting them exists
                        if (tempkey.containsKey(oldDest[j]) &&
                                mediator.getStationDefinition().areConnected(newkey, tempkey.get(oldDest[j])))
                            newRouting.put(tempkey.get(oldDest[j]), oldRouting.get(oldDest[j]));
                    }
                    rs.getValues().clear();
                    rs.getValues().putAll(newRouting);
                }
            }
        }

        // Selects every inserted element
        mediator.getGraph().setSelectionCells(select.toArray());
    }

    /**
     * Pastes previously copied elements into graph, constructing new data structures.
     * Elements are copied near original ones.
     */
    public void paste() {
        paste(zero);
    }

    /**
     * Inner class used to store pairs of sourceKey/targetKey objects.
     */
    protected class Connection {
        protected Object sourceKey;
        protected Object targetKey;
        public Connection(Object sourceKey, Object targetKey) {
            this.sourceKey = sourceKey;
            this.targetKey = targetKey;
        }
    }
}

⌨️ 快捷键说明

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