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

📄 beanboxdndcatcher.java

📁 openmap java写的开源数字地图程序. 用applet实现,可以像google map 那样放大缩小地图.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }        selectedBeanBox.showUnCut(selectedBean);        clearSelection();        if (Debug.debugging("beanbox"))            Debug.output("Exit> unCutSelectedBean");    }    private void clearSelection() {        cutBean = null;        selectedBean = null;        selectedBeanLocation = null;        selectedBeanBox = null;        selectedBeanLayer = null;        serBean = null;    }    /**     * This method is called when the user chooses to delete a bean by     * some means such by by pressing DEL. This method removes the     * selected bean from its beanbox. If no bean is selected this     * method is a no-op.     */    protected void deleteSelectedBean() {        if (Debug.debugging("beanbox"))            Debug.output("Enter> deleteSelectedBean");        if (selectedBean == null || selectedBeanLocation == null) {            if (Debug.debugging("beanbox"))                Debug.output("selectedBean=" + selectedBean);            if (Debug.debugging("beanbox"))                Debug.output("selectedBeanLocation=" + selectedBeanLocation);            return;        }        selectedBeanBox.removeBean(selectedBean);        cutBean = null;        if (Debug.debugging("beanbox"))            Debug.output("Exit> deleteSelectedBean");    }    /**     * The drag operation has terminated with a drop on this     * <code>DropTarget</code>. This method is responsible for     * undertaking the transfer of the data associated with the     * gesture. The <code>DropTargetDropEvent</code> provides a     * means to obtain a <code>Transferable</code> object that     * represents the data object(s) to be transfered.     * <P>     * <P>     *      * @param dtde the <code>DropTargetDropEvent</code>     */    public void drop(DropTargetDropEvent dtde) {        if (Debug.debugging("beanbox"))            Debug.output("Enter> drop");        dtde.acceptDrop(DnDConstants.ACTION_MOVE);        extractTransferData(dtde);        extractDropLocation(dtde);        if (transferData == null || dropLocation == null)            return;        Component parent = ((DropTarget) dtde.getSource()).getComponent();        dtde.dropComplete(true);        showPopUp(parent);        if (Debug.debugging("beanbox"))            Debug.output("Exit> drop");    }    private void showPopUp(Component parent) {        if (Debug.debugging("beanbox"))            Debug.output("Enter> showPopUp");        JPopupMenu popup = new JPopupMenu();        TitledBorder titledBorder = BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(),                "Available Drop Targets:");        titledBorder.setTitleColor(Color.gray);        popup.setBorder(titledBorder);        Border compoundborder = BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(),                BorderFactory.createEmptyBorder(2, 2, 2, 2));        Enumeration keys = layers.keys();        while (keys.hasMoreElements()) {            String layerName = keys.nextElement().toString();            Layer omlayer = (Layer) layers.get(layerName);            if (omlayer.isVisible()) {                JMenuItem menuItem = new JMenuItem(layerName);                menuItem.setHorizontalTextPosition(SwingConstants.CENTER);                menuItem.setBorder(compoundborder);                menuItem.addActionListener(this);                popup.add(menuItem);            }        }        popup.addSeparator();        JMenuItem menuItem = new JMenuItem("CANCEL");        menuItem.setForeground(Color.red);        menuItem.setHorizontalTextPosition(SwingConstants.CENTER);        menuItem.setBorder(compoundborder);        popup.add(menuItem);        popup.setPreferredSize(new Dimension(150, (popup.getComponentCount() + 1) * 25));        if (Debug.debugging("beanbox"))            Debug.output("showing popup");        popup.show(parent, dropLocation.x, dropLocation.y);        if (Debug.debugging("beanbox"))            Debug.output("Exit> showPopUp");    }    /**     * Displays a     * {@link com.bbn.openmap.tools.beanbox.GenericPropertySheet}if     * mouse click is on a bean in some layer. In case of overlapping     * beans, chooses the first bean found to be under the mouse,     * which is usually a bean in the top most visible layer.     */    public void mouseClicked(MouseEvent evt) {        if (Debug.debugging("beanbox"))            Debug.output("Enter> mouseClicked");        Point srcLocation = evt.getPoint();        Enumeration keys = layers.keys();        while (keys.hasMoreElements()) {            String layerName = keys.nextElement().toString();            selectedBeanLayer = (Layer) layers.get(layerName);            if (!selectedBeanLayer.isVisible())                continue;            selectedBeanBox = ((BeanBoxHandler) selectedBeanLayer).getBeanBox();            if (selectedBeanBox == null)                continue;            selectedBean = selectedBeanBox.getBeanAtLocation(srcLocation);            if (selectedBean != null) {                break;            }        }        if (selectedBean == null) {            clearSelection();            return;        }        selectedBeanLocation = srcLocation;        selectedBeanBox.showSelected(selectedBean);        if (Debug.debugging("beanbox"))            Debug.output("selectedBean=" + selectedBean);        if (evt.getModifiers() != InputEvent.BUTTON1_MASK) {            GenericPropertySheet propertySheet = new GenericPropertySheet(selectedBean, 575, 20, null, selectedBeanBox);            propertySheet.setVisible(true);        }        if (Debug.debugging("beanbox"))            Debug.output("Exit> mouseClicked");    }    /**     * This method is called whenever the user choose a layer to drop     * or move a bean to. This method adds the bean to the layer or     * moves the beans to or within the selected layer.     */    public void actionPerformed(ActionEvent evt) {        if (Debug.debugging("beanbox"))            Debug.output("Enter> actionPerformed");        Object source = evt.getSource();        if (!(source instanceof JMenuItem))            return;        JMenuItem mi = (JMenuItem) source;        String name = mi.getText();        Layer targetLayer = (Layer) layers.get(name);        if (targetLayer == null) {            System.out.println("ERROR> BBDnDC::actionPerformed: "                    + "no layer found with name " + name);            return;        }        BeanBox targetBeanBox = ((BeanBoxHandler) targetLayer).getBeanBox();        Object bean = transferData.get(0);        BeanInfo beanInfo = (BeanInfo) transferData.get(1);        Boolean wasBeanMoved = (Boolean) transferData.get(2);        if (wasBeanMoved.booleanValue()) {            String sourceLayerName = (String) transferData.get(3);            if (sourceLayerName.equals(targetLayer.getName())) {                targetBeanBox.relocateBean(bean, beanInfo, dropLocation);            } else {                Layer sourceLayer = (Layer) layers.get(sourceLayerName);                BeanBox sourceBeanBox = ((BeanBoxHandler) sourceLayer).getBeanBox();                sourceBeanBox.removeBean(bean);                Vector object = new Vector();                object.add(bean);                object.add(beanInfo);                object.add(dropLocation);                targetBeanBox.addBean(object);            }        } else {            Vector object = new Vector();            object.add(bean);            object.add(beanInfo);            object.add(dropLocation);            targetBeanBox.addBean(object);        }    }    /**     * Asscoiates a DropTarget with each layer. Also caches all layers     * that implement the BeanBoxHandler interface.     */    public void setLayers(Layer[] allLayers) {        layers.clear();        for (int i = 0; i < allLayers.length; i++) {            new DropTarget(allLayers[i], DnDConstants.ACTION_MOVE, this);            if (allLayers[i] instanceof BeanBoxHandler) {                Debug.message("DnDCatcher", "Layers changed");                layers.put(allLayers[i].getName(), allLayers[i]);            }        }    }    /**     * Invoked on dragGestureRecognized     */    public void startDragAction(DragGestureEvent dge, DragSourceListener dsl) {        if (Debug.debugging("beanbox"))            Debug.output("Enter> startDragAction");        Object selectedBean = null;        BeanBox selectedBeanBox = null;        Layer selectedLayer = null;        Point srcLocation = dge.getDragOrigin();        Enumeration keys = layers.keys();        while (keys.hasMoreElements()) {            String layerName = keys.nextElement().toString();            Layer omLayer = (Layer) layers.get(layerName);            BeanBox beanBox = ((BeanBoxHandler) omLayer).getBeanBox();            selectedBean = beanBox.getBeanAtLocation(srcLocation);            if (selectedBean != null) {                selectedBeanBox = beanBox;                selectedLayer = omLayer;                break;            }        }        if (Debug.debugging("beanbox"))            Debug.output("selectedBean=" + selectedBean);        if (selectedBean == null) {            if (Debug.debugging("beanbox"))                Debug.output("Exit> startDragAction");            return;        }        Image dragImage = selectedBeanBox.getDragImage(selectedBean);        super.setCursor(dragImage, DragSource.DefaultMoveDrop);        BeanInfo beanInfo = selectedBeanBox.getBeanInfoForBean(selectedBean.getClass()                .getName());        Vector beanTransferData = new Vector();        beanTransferData.add(selectedBean);        beanTransferData.add(beanInfo);        beanTransferData.add(new Boolean(true));        beanTransferData.add(selectedLayer.getName());        dragSource.startDrag(dge,                super.getCursor(DragSource.DefaultMoveDrop),                new DefaultTransferableObject(beanTransferData),                dsl);        if (Debug.debugging("beanbox"))            Debug.output("Exit> startDragAction");    }    private void extractTransferData(DropTargetDropEvent dtde) {        if (dtde == null) {            System.out.println("ERROR> BDnDC::getTransferData(): dropEvent is null");            return;        }        Transferable tr = dtde.getTransferable();        try {            transferData = (Vector) tr.getTransferData(DefaultTransferableObject.OBJECT_FLAVOR);            // cache beanInfos            if (transferData.size() >= 2) {                Object bean = transferData.get(0);                BeanInfo beanInfo = (BeanInfo) transferData.get(1);                beanInfoMap.put(bean.getClass().getName(), beanInfo);            }        } catch (Exception e) {            e.printStackTrace();        }    }    private void extractDropLocation(DropTargetDropEvent dtde) {        if (dtde == null) {            System.out.println("ERROR> BDnDC::getTransferData(): dropEvent is null");            return;        }        dropLocation = dtde.getLocation();    }    private static void setDefaultIcon() {        if (BeanPanel.defaultBeanIcon == null) {            URL url = BeanPanel.class.getResource("bluebean.gif");            if (url != null)                BeanPanel.defaultBeanIcon = new ImageIcon(url);        }    }}

⌨️ 快捷键说明

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