editableompoly.java

来自「OpenMap是一个基于JavaBeansTM的开发工具包。利用OpenMap你」· Java 代码 · 共 1,371 行 · 第 1/4 页

JAVA
1,371
字号
     */    public boolean generate(Projection proj) {        Debug.message("eomg", "EditableOMPoly.generate()");        if (poly != null) {            poly.generate(proj);        }        // Generate all the grab points        Iterator gps = polyGrabPoints.iterator();        while (gps.hasNext()) {            GrabPoint gb = (GrabPoint) gps.next();            if (gb != null)                gb.generate(proj);        }        if (gpo != null) {            gpo.generate(proj);            gpo.updateOffsets();        }        ;        return true;    }    /**     * Given a new projection, the grab points may need to be repositioned off     * the current position of the graphic. Called when the projection changes.     */    public void regenerate(Projection proj) {        Debug.message("eomg", "EditableOMPoly.regenerate()");        if (poly != null) {            poly.generate(proj);            setGrabPoints(poly);        }        // Generate all the grab points        Iterator gps = polyGrabPoints.iterator();        while (gps.hasNext()) {            GrabPoint gb = (GrabPoint) gps.next();            if (gb != null) {                gb.generate(proj);            }        }        if (gpo != null) {            gpo.generate(proj);            gpo.updateOffsets();        }    }    /**     * Draw the EditableOMPoly parts into the java.awt.Graphics object. The grab     * points are only rendered if the poly machine state is     * PolySelectedState.POLY_SELECTED.     *      * @param graphics java.awt.Graphics.     */    public void render(java.awt.Graphics graphics) {        Debug.message("eomg", "EditableOMPoly.render()");        State state = getStateMachine().getState();        if (poly != null && !(state instanceof PolyUndefinedState)) {            poly.setVisible(true);            poly.render(graphics);            poly.setVisible(false);        } else {            Debug.message("eomg",                    "EditableOMPoly.render: null or undefined poly.");            return;        }        // Render the points actually on the polygon        if (state instanceof GraphicSelectedState                || state instanceof PolyAddNodeState                || state instanceof PolyDeleteNodeState) {            Iterator gps = polyGrabPoints.iterator();            while (gps.hasNext()) {                GrabPoint gb = (GrabPoint) gps.next();                if (gb != null) {                    gb.setVisible(true);                    poly.render(graphics);                    gb.render(graphics);                    gb.setVisible(false);                }            }        }        // In certain condiditions, render the offset grab point.        if (state instanceof GraphicSelectedState                || state instanceof GraphicEditState /*                                                         * || state instanceof                                                         * PolySetOffsetState                                                         */) {            if (gpo != null                    && poly.getRenderType() == OMGraphic.RENDERTYPE_OFFSET) {                gpo.setVisible(true);                gpo.render(graphics);                gpo.setVisible(false);            }        }    }    // ///////////// Special Grab Point functions    // /////////////////////    // Since the GrabPoints only refer to the points actually on the    // polygon, we have to make sure that the generic    // EditableOMGraphic grab point methods handle that. The    // OffsetGrabPointIndex is -1, so we have to look out for that and    // use the gpo when appropriate.    // /////////////////////////////////////////////////////////////////    /**     * Set the grab point objects within the EditableOMGraphic array. For the     * EditableOMPoly, with its variable number of GrabPoints, this just sets up     * a new list of all the grab points to look at. It's different than the     * polyGrabPoints, which are the grab points just on the polygon. This list     * includes the offset grab point. This method should be called when a new     * point gets added to the polygon, and should take an array of all the     * GrabPoints created. It will add the offsetGrabPoint to the end of the     * array.     *      * @param points a GrabPoint[] for the points on the polygon.     * @return true if the grab point array was exactly what the     *         EditableOMGraphic was expecting, in terms of length of the     *         GrabPoint array length. The method copies the array values that     *         fit into the resident array.     */    public boolean setGrabPoints(GrabPoint[] points) {        gPoints = new GrabPoint[points.length + 1];        System.arraycopy(gPoints, 0, points, 0, points.length);        gPoints[points.length] = gpo;        return true;    }    /**     * Get the array of grab points used for the EditableOMGraphic. Creates the     * array by copying all the grab points out of the ArrayList, and tacking     * the offset grab point to the end.     */    public GrabPoint[] getGrabPoints() {        int size = polyGrabPoints.size();        // The second half of the test is the fix to the bug that caused        // OMEditablePolys to be unresponsive when the colors changed. Thanks,        // Stephane!        if (gPoints.length != size + 1                || ((size > 0) && (!gPoints[0].equals(polyGrabPoints.get(0))))) {            Debug.message("eomg",                    "EditableOMPoly.getGrabPoints(): recreating grab points");            gPoints = new GrabPoint[size + 1];            int counter = 0;            Iterator obj = polyGrabPoints.iterator();            while (obj.hasNext()) {                gPoints[counter++] = (GrabPoint) obj.next();            }            gPoints[counter] = gpo;        }        return gPoints;    }    /**     * Set the GrabPoint at a particule index of the array. This can be used to     * tie two different grab points together. This used to work with the     * gPoints array declared in EditableOMGraphic - no longer. If the index is     * -1, the offset grab point is set, and any other index refers to the     * concurrent polygon point.     *      * @param gb GrabPoint to assign within array.     * @param index the index of the array to put the GrabPoint. This index     *        should be -1 for the offset grab point, or the index of the corner     *        of the poly, in order starting from 0.     * @return If the grab point or array is null, or if the index is outside     *         the range of the array, false is returned. If everything goes OK,     *         then true is returned.     */    public boolean setGrabPoint(GrabPoint gb, int index) {        // We might have to take care of the offset grab point        // connections here...        if (index == OFFSET_POINT_INDEX) {            gpo = (OffsetGrabPoint) gb;            return true;        } else {            return super.setGrabPoint(gb, index);        }    }    /**     * Given a grab point, return its index into the polygon array. If its not     * in the array, the next available index is returned.     */    public int whichGrabPoint(GrabPoint gp) {        GrabPoint[] points = getGrabPoints();        for (int i = 0; i < points.length; i++) {            if (gp == points[i]) {                if (gp == gpo) {                    return OFFSET_POINT_INDEX;                } else {                    return i;                }            }        }        return points.length;    }    /**     * Return a particular GrabPoint at a particular point in the array. The     * EditableOMGraphic should describe which indexes refer to which grab     * points in the EOMG GrabPoint array. If the index is OFFSET_POINT_INDEX,     * the offset point is returned. If the index is otherwise outside the range     * of the array, null is returned.     */    public GrabPoint getGrabPoint(int index) {        if (index == OFFSET_POINT_INDEX) {            return gpo;        } else {            return super.getGrabPoint(index);        }    }    /**     * Adds widgets to modify polygon.     *      * @param graphicAttributes the GraphicAttributes to use to get the GUI     *        widget from to control those parameters for this EOMG.     * @return Component to use to control parameters for this EOMG.     */    public Component getGUI(GraphicAttributes graphicAttributes) {        Debug.message("eomg", "EditableOMPoly.getGUI");        if (graphicAttributes != null) {            JMenu ahm = getArrowHeadMenu();            graphicAttributes.setLineMenuAdditions(new JMenu[] { ahm });            Component gaGUI = graphicAttributes.getGUI();            ((JComponent) gaGUI).add(getPolyGUI());            return gaGUI;        } else {            return getPolyGUI();        }    }    JToggleButton polygonButton = null;    JButton addButton = null;    JButton deleteButton = null;    public void enablePolygonButton(boolean enable) {        if (polygonButton != null) {            polygonButton.setEnabled(enable);        }    }    public void enablePolygonEditButtons(boolean enable) {        if (addButton != null) {            addButton.setEnabled(enable);        }        if (deleteButton != null) {            deleteButton.setEnabled(enable);        }    }    public JToolBar getPolyGUI() {        return getPolyGUI(true, true, true);    }    public JToolBar getPolyGUI(boolean includeEnclose, boolean includeAdd,                               boolean includeDelete) {        JToolBar buttonBox = new GridBagToolBar();        URL url;        ImageIcon imageIcon;        if (polygonButton == null) {            url = getImageURL("enclosepoly.gif");            imageIcon = new ImageIcon(url);            polygonButton = new JToggleButton(imageIcon);            polygonButton.setToolTipText(i18n.get(EditableOMPoly.class,                    "polygonButton.tooltip",                    "Automatically link first and last nodes"));        }        polygonButton.setSelected(isEnclosed());        polygonButton.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                if (getStateMachine().getState() instanceof GraphicSelectedState) {                    enclose(((JToggleButton) e.getSource()).isSelected());                } else {                    setEnclosed(((JToggleButton) e.getSource()).isSelected());                }            }        });        if (includeEnclose) {            buttonBox.add(polygonButton);        }        if (addButton == null) {            url = getImageURL("addpoint.gif");            imageIcon = new ImageIcon(url);            addButton = new JButton(imageIcon);            addButton.setToolTipText(i18n.get(EditableOMPoly.class,                    "addButton.tooltip",                    "Add a node to the polygon"));        }        addButton.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                ((PolyStateMachine) stateMachine).setAddNode();                enablePolygonEditButtons(false);            }        });        addButton.setEnabled(false);        if (includeAdd) {            buttonBox.add(addButton);        }        if (deleteButton == null) {            url = getImageURL("deletepoint.gif");            imageIcon = new ImageIcon(url);            deleteButton = new JButton(imageIcon);            deleteButton.setToolTipText(i18n.get(EditableOMPoly.class,                    "deleteButton.tooltip",                    "Delete a node from the polygon"));        }        deleteButton.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                ((PolyStateMachine) stateMachine).setDeleteNode();                enablePolygonEditButtons(false);            }        });        deleteButton.setEnabled(false);        if (includeDelete) {            buttonBox.add(deleteButton);        }        return buttonBox;    }    public java.net.URL getImageURL(String imageName) {        return EditableOMPoly.class.getResource(imageName);    }}

⌨️ 快捷键说明

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