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

📄 mediator.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    }

    /**
     * Gets the background color of graph.
     * @return this component's background color; if this component does
     * 		not have a background color,
     *		the background color of its parent is returned
     */
    public Color getGraphBackground() {
        return graph.getBackground();
    }

    /**
     * Returns the current marquee color of the graph.
     */
    public Color getGraphMarqueeColor() {
        return graph.getMarqueeColor();
    }


    public void connect(Point2D start, Point2D current, PortView inPort, PortView outPort) {
        Point2D p = fromScreen(start);
        Point2D p2 = fromScreen(current);
        if (inPort != null && outPort != null) {
            ArrayList list = new ArrayList();
            list.add(p);
            list.add(p2);
            Map map = new Hashtable();
            GraphConstants.setPoints(map, list);
            GraphConstants.setRouting(map, GraphConstants.ROUTING_SIMPLE);
            GraphConstants.setRouting(map, JmtGraphConstants.ROUTING_JMT);
            GraphConstants.setEndFill(map, true);

            // 24/09/03 - Massimo Cattai //////////////////////////////////////////
            // Add a Line End Attribute
            GraphConstants.setLineEnd(map, GraphConstants.ARROW_CLASSIC);
            // 24/09/03 - end /////////////////////////////////////////////////////
            Map viewMap = new Hashtable();
            // ---- Adds connection into underlayng data structure -- BERTOLI MARCO
            Object sourceKey = ((CellComponent)((JmtCell)((OutputPort)(outPort.getCell())).getUserObject()).getUserObject()).getKey();
            Object targetKey = ((CellComponent)((JmtCell)((InputPort)(inPort.getCell())).getUserObject()).getUserObject()).getKey();
            JmtEdge connection = new JmtEdge(sourceKey, targetKey);
            viewMap.put(connection, map);
            Object[] insert = new Object[]{connection};
            ConnectionSet cs = new ConnectionSet();
            cs.connect(connection, outPort.getCell(), true);
            cs.connect(connection, inPort.getCell(), false);
            // Visualize connection only if it can be created into data structure
            if (model.setConnected(sourceKey, targetKey, true)) {
                graph.getModel().insert(insert, viewMap, cs, null, null);
            }
            // ---- End -- BERTOLI MARCO
        }

    }

    /**
     * Creates a connection between given source and target JmtCells
     * @param source source cell
     * @param target target cell
     * @return created component or null if connection between source and target cannot be created
     *
     * Author: Bertoli Marco
     */
    public JmtEdge connect (JmtCell source, JmtCell target) {
        return connect(source, target, false);
    }


    /**
     * Creates a connection between given source and target JmtCells
     * @param source source cell
     * @param target target cell
     * @param forced true if connection must be shown also if could not be created into data structure.
     * @return created component or null if connection between source and target cannot be created
     *
     * Author: Bertoli Marco
     */
    public JmtEdge connect (JmtCell source, JmtCell target, boolean forced) {
        // If one of parameter is null, returns null
        if (source == null || target == null)
            return null;
        // Retrives source and target keys to create connection
        Object sourceKey = ((CellComponent)source.getUserObject()).getKey();
        Object targetKey = ((CellComponent)target.getUserObject()).getKey();
        // Initializes correct layout for routing edges
        Map map = new Hashtable();
        GraphConstants.setRouting(map, GraphConstants.ROUTING_SIMPLE);
        GraphConstants.setRouting(map, JmtGraphConstants.ROUTING_JMT);
        GraphConstants.setLineEnd(map, GraphConstants.ARROW_CLASSIC);
        GraphConstants.setEndFill(map, true);
        Map viewMap = new Hashtable();
        JmtEdge connection = new JmtEdge(sourceKey, targetKey);
        viewMap.put(connection, map);
        Object[] insert = new Object[]{connection};
        ConnectionSet cs = new ConnectionSet();
        // Finds sourcePort
        Iterator it;
        it = source.getChildren().iterator();
        DefaultPort tmpPort, sourcePort, targetPort;
        sourcePort = null;
        while (it.hasNext()) {
            tmpPort = (DefaultPort)it.next();
            if (tmpPort instanceof OutputPort)
                sourcePort = tmpPort;
        }
        // Finds targetPort
        it = target.getChildren().iterator();
        targetPort = null;
        while (it.hasNext()) {
            tmpPort = (DefaultPort)it.next();
            if (tmpPort instanceof InputPort)
                targetPort = tmpPort;
        }
        if (sourcePort != null && targetPort != null) {
            cs.connect(connection, sourcePort, true);
            cs.connect(connection, targetPort, false);
            // Adds connection to the graph only if it can be created into data structure
            if (model.setConnected(sourceKey, targetKey, true) || forced) {
                graph.getModel().insert(insert, viewMap, cs, null, null);
                return connection;
            }
        }
        return null;
    }

    /**
     * repaints the graph component
     */
    public void graphRepaint() {
        graph.repaint();
    }

    /**
     * Returns the parent of <I>child</I> in the model.
     * <I>child</I> must be a node previously obtained from
     * this data source. This returns null if <i>child</i> is
     * a root in the model.
     *
     * @param   child  a node in the graph, obtained from this data source
     * @return  the parent of <I>child</I>
     */
    public Object getParent(Object child) {
        return graph.getModel().getParent(child);
    }

    /** gets the first portView of the input port of the cell at position
     *
     * @param x
     * @param y
     * @return portView of the input port
     */
    public PortView getInPortViewAt(int x, int y) {
        return (PortView) graph.getGraphLayoutCache().
                getMapping(((JmtJGraph) graph).getInPortAt(x, y), false);
    }

    /** gets the first portView of the output port of the cell at position
     *
     * @param x
     * @param y
     * @return portView of the output port
     */
    public PortView getOutPortViewAt(int x, int y) {
        return (PortView) graph.getGraphLayoutCache().
                getMapping(((JmtJGraph) graph).getOutPortAt(x, y), false);
    }

    /**
     * Returns if a given cell is visible on graph
     * @param cell
     * @return true iff cell is visible
     */
    public boolean isCellVisible(Object cell) {
        return ((JmtGraphUI) graph.getUI()).getGraphLayoutCache().isVisible(cell);
    }

    /**
     * Returns the views for the specified array of cells. Returned
     * array may contain null pointers if the respective cell is not
     * mapped in this view and <code>create</code> is <code>false</code>.
     */
    public CellView getViewOfCell(Object cell, boolean create) {
        return ((JmtGraphUI) graph.getUI()).getGraphLayoutCache().getMapping(cell, create);
    }

    /**
     * Selects the specified cell and initiates editing.
     * The edit-attempt fails if the <code>CellEditor</code>
     * does not allow
     * editing for the specified item.
     */
    public void startEditingAtCell(Object cell) {
        graph.startEditingAtCell(cell);
        if ((cell != null) && (cell instanceof JmtCell)) {
            StationParameterPanel stationPanel = new jmt.gui.common.panels.StationParameterPanel(model, model,
                            ((CellComponent)((JmtCell)cell).getUserObject()).getKey());
            // Adds on the top a panel to change station name
            stationPanel.add(
                    new StationNamePanel(model, ((CellComponent)((JmtCell)cell).getUserObject()).getKey()),
                    BorderLayout.NORTH);
            DialogFactory.getDialog(stationPanel,
                    "Editing " + ((JmtCell)cell).getUserObject().toString() + " Properties...");
        }
        // Blocking region editing
        else if ((cell != null) && (cell instanceof BlockingRegion)) {
            Object regionKey = ((BlockingRegion)cell).getKey();
            DialogFactory.getDialog(new BlockingRegionParameterPanel(model,
                    model,
                    regionKey),
                    "Editing " + model.getRegionName(regionKey) + " Properties...");
        }
    }

    /**
     * Cuts the selection of the graph.
     */
    public void cutSelection() {
        // Bertoli Marco
        clipboard.cut();
    }

    /**
     * Pastes the selection on the graph.
     */
    public void pasteSelection() {
        // Bertoli Marco
        clipboard.paste();

        // If more than one stations are present enables link button
        if (graph.getModel().getRootCount() > 1)
            mainWindow.getComponentBar().enableLink(true);
        // If one station is present show select button
        if (graph.getModel().getRootCount() >= 1) {
            mainWindow.getComponentBar().enableSelect(true);
            mainWindow.getComponentBar().setSelect();
        }
    }

    /**
     * Copies the selection of the graph.
     */
    public void copySelection() {
        // Bertoli Marco
        clipboard.copy();
    }

    /**
     * Displays an error message in the panel that is responable to make the
     * user understand why a certain operation is not valid.
     *
     * @param message error to be displayed.
     */
    public void displayGraphErrMsg(String message) {
        //per ora faccio printare nell'output, assolutamente provvisorio.
        System.out.println("message = " + message);
    }

    /**
     * Deletes all the vertex & edgees that are selected. it deletes also the
     * edges that are connected to the eliminated vertexes.

     * Bertoli Marco 03-06-2005
     */
    public void deleteSelected() {
        Object cells[] = graph.getSelectionCells();
        GraphModel graphmodel = graph.getModel();

        // If a cell is a blocking region avoid removing its edges and
        // select its element at the end of the removal process
        Set edges = new HashSet();
        Set select = new HashSet();

        // Set with all regions that can be deleted as its child were removed
        Set regions = new HashSet();
        // Set with all JmtCells that we are removing
        Set jmtCells = new HashSet();

        for (int i=0; i<cells.length;i++)
            if (!(cells[i] instanceof BlockingRegion)) {
                // Adds edge for removal
                edges.addAll(DefaultGraphModel.getEdges(graphmodel, new Object[]{cells[i]}));
                // Stores parents information and cell
                if (cells[i] instanceof JmtCell) {
                    if (((JmtCell)cells[i]).getParent() instanceof BlockingRegion)
                        regions.add(((JmtCell)cells[i]).getParent());
                    jmtCells.add(cells[i]);
                }
            }
            else {
                // Adds node for selection
                Object[] nodes = graph.getDescendants(new Object[]{cells[i]});
                for (int j=0; j<nodes.length; j++)
                    if (nodes[j] instanceof JmtCell || nodes[j] instanceof JmtEdge)
                    select.add(nodes[j]);
                // Removes blocking region from data structure
                model.deleteBlockingRegion(((BlockingRegion)cells[i]).getKey());
            }


        if (!edges.isEmpty()) {
            graphmodel.remove(edges.toArray());
        }
        //removes cells from graph
        graphmodel.remove(cells);

        // Checks if all children of a blocking region have been removed
        Iterator it = regions.iterator();
        while(it.hasNext()) {
            jmtCells.add(null);
            BlockingRegion region = (BlockingRegion) it.next();
            List child = region.getChildren();
            boolean empty = true;
            for (int i=0; i<child.size(); i++)
                if (child.get(i) instanceof JmtCell && !jmtCells.contains(child.get(i))) {
                    empty = false;
                    break;
                }
            if (empty) {
                model.deleteBlockingRegion(region.getKey());
                graphmodel.remove(new Object[] {region});
            }
        }

        // Removes cells from data structure
        for (int i=0; i<cells.length; i++)
            if (cells[i] instanceof JmtCell)
                model.deleteStation(((CellComponent)((JmtCell)cells[i]).getUserObject()).getKey());
            else if (cells[i] instanceof JmtEdge) {
                JmtEdge link = (JmtEdge)cells[i];
                model.setConnected(link.getSourceKey(), link.getTargetKey(), false);
            }

        // If no stations remains gray select and link buttons
        if (graph.getModel().getRootCount() == 0) {
            mainWindow.getComponentBar().enableLink(false);
            mainWindow.getComponentBar().unsetAll();
            mainWindow.getComponentBar().enableSelect(false);
        }

        // Selects components from removed blocking regions
        if (select.size() > 0) {
            graph.setSelectionCells(select.toArray());
            // Resets parent information of cells that changed parent
            it = select.iterator();
            while (it.hasNext()) {
                Object next = it.next();
                if (next instanceof JmtCell)
                    ((JmtCell)next).resetParent();
            }
        }
    }

    /**
     * Shows the panel that opens when the rigth button is clicked.
     *
     * @param p point where the right button is cliecked on the graph
     */
    public void showOPanel(Point p) {

⌨️ 快捷键说明

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