📄 beanboxdndcatcher.java
字号:
/* ********************************************************************** * * Use, duplication, or disclosure by the Government is subject to * restricted rights as set forth in the DFARS. * * BBNT Solutions LLC * A Part of Verizon * 10 Moulton Street * Cambridge, MA 02138 * (617) 873-3000 * * Copyright (C) 2002 by BBNT Solutions, LLC * All Rights Reserved. * ********************************************************************** */package com.bbn.openmap.tools.beanbox;import java.awt.dnd.*;import java.awt.datatransfer.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.util.*;import java.net.*;import java.beans.*;import java.beans.beancontext.*;import javax.swing.*;import javax.swing.border.*;import com.bbn.openmap.tools.dnd.DefaultDnDCatcher;import com.bbn.openmap.tools.dnd.ComponentDragGestureListener;import com.bbn.openmap.tools.dnd.DefaultTransferableObject;import com.bbn.openmap.MapBean;import com.bbn.openmap.SoloMapComponent;import com.bbn.openmap.Layer;import com.bbn.openmap.event.LayerListener;import com.bbn.openmap.event.ProjectionListener;import com.bbn.openmap.util.Debug;/** * The BeanBoxDnDCatcher class manages all Java Drag-and-Drop events * associated with openmap layers that implement the * {@link com.bbn.openmap.tools.beanbox.BeanBoxHandler}interface. */public class BeanBoxDnDCatcher extends DefaultDnDCatcher implements SoloMapComponent, BeanContextChild, BeanContextMembershipListener, PropertyChangeListener, Serializable, ProjectionListener, LayerListener, ActionListener { static { setDefaultIcon(); } private Vector transferData; private Point dropLocation; /** holds the currently selected bean */ protected Object selectedBean = null; /** holds the serialized version of currently selected bean */ protected ByteArrayOutputStream serBean = null; /** holds the map location of the currently selected bean */ protected Point selectedBeanLocation = null; /** * holds the {@link com.bbn.openmap.tools.beanbox.BeanBox}that * manages the currently selected bean */ protected BeanBox selectedBeanBox = null; /** * holds the openmap layer that contains the currently selected * bean */ protected Layer selectedBeanLayer = null; /** holds the currently cut bean, if any */ Object cutBean = null; /** * contains BeanInfo objects hashed by the class names of the * associated bean classes */ protected HashMap beanInfoMap = null; /** * Constructs a new {@link com.bbn.openmap.tools.dnd.DnDListener} * object. */ public BeanBoxDnDCatcher() { this(new DragSource()); } /** * Constructs a new MouseDragGestureRecognizer given the * DragSource for the Component. * * @param ds the DragSource for the Component */ public BeanBoxDnDCatcher(DragSource ds) { this(ds, null); } /** * Construct a new MouseDragGestureRecognizer given the DragSource * for the Component c, and the Component to observe. * * @param ds the DragSource for the Component c * @param c the Component to observe */ public BeanBoxDnDCatcher(DragSource ds, Component c) { this(ds, c, DnDConstants.ACTION_MOVE); } /** * Construct a new MouseDragGestureRecognizer given the DragSource * for the Component c, and the Component to observe and the * drag-and-drop action. * * @param ds the DragSource for the Component c * @param c the Component to observe * @param act the drag-and-drop action */ public BeanBoxDnDCatcher(DragSource ds, Component c, int act) { this(ds, c, act, null); } /** * Construct a new MouseDragGestureRecognizer given the DragSource * for the Component c, and the Component to observe. the * drag-and-drop action and a DragGestureListener * * @param ds the DragSource for the Component c * @param c the Component to observe * @param act the drag-and-drop action * @param dgl the DragGestureListener */ public BeanBoxDnDCatcher(DragSource ds, Component c, int act, DragGestureListener dgl) { super(ds, c, act, dgl); dragSource = getDragSource(); dragGestureListener = new ComponentDragGestureListener(this, this); setSourceActions(DnDConstants.ACTION_MOVE); beanInfoMap = new HashMap(); } /** * Calls superclass method and then adds the KeyListener to * someObj if someObj is of type OpenMapFrame. */ public void findAndInit(Object someObj) { super.findAndInit(someObj); if (someObj instanceof MapBean) { ((MapBean) someObj).addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent evt) { if (evt.getModifiers() == InputEvent.CTRL_MASK && evt.getKeyCode() == KeyEvent.VK_C) copySelectedBean(); else if (evt.getModifiers() == InputEvent.CTRL_MASK && evt.getKeyCode() == KeyEvent.VK_V) pasteSelectedBean(); else if (evt.getModifiers() == InputEvent.CTRL_MASK && evt.getKeyCode() == KeyEvent.VK_X) cutSelectedBean(); else if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) unCutSelectedBean(); else if (evt.getKeyCode() == KeyEvent.VK_DELETE) deleteSelectedBean(); } }); } } /** * This method is called when the user chooses to copy a bean by * some means such by by pressing Ctrl-C. This method tries to * serialize the selected bean. If no bean is selected or the bean * is not serializable, this method is a no-op. */ protected void copySelectedBean() { if (Debug.debugging("beanbox")) Debug.output("Enter> copySelectedBean"); if (selectedBean == null || selectedBeanLocation == null) { clearSelection(); if (Debug.debugging("beanbox")) Debug.output("selectedBean=" + selectedBean); if (Debug.debugging("beanbox")) Debug.output("selectedBeanLocation=" + selectedBeanLocation); return; } try { serBean = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(serBean); oos.writeObject(selectedBean); } catch (Exception e) { e.printStackTrace(); clearSelection(); ; if (Debug.debugging("beanbox")) Debug.output("Exit> copySelectedBean"); return; } cutBean = null; if (Debug.debugging("beanbox")) Debug.output("Exit> copySelectedBean"); } /** * This method is called when the user chooses to paste by some * means (such by pressing Ctrl-V) a previously copied or cut * bean. This method tries to deserialize the previously * serialized bean. If the bean in question was cut, this method * also removes it from from the source beanbox. The paste * operation is treated the same as a drop operation. If no bean * was previously copied or cut or if an error occurs during * deserialization, this method is a no-op. */ protected void pasteSelectedBean() { if (Debug.debugging("beanbox")) Debug.output("Enter> pasteSelectedBean"); if (serBean == null) { clearSelection(); if (Debug.debugging("beanbox")) Debug.output("Exit> pasteSelectedBean"); return; } BeanInfo beanInfo = (BeanInfo) beanInfoMap.get(selectedBean.getClass() .getName()); if (beanInfo == null) { System.out.println("ERROR> BBDnDC::pasteSelectedBean: " + "no cached BeanInfo found for bean " + selectedBean); clearSelection(); return; } // if bean was cut, remove it from its present location if (cutBean != null) { selectedBeanBox.removeBean(selectedBean); } Object deserBean = null; try { ByteArrayInputStream bais = new ByteArrayInputStream(serBean.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); deserBean = ois.readObject(); } catch (Exception e) { e.printStackTrace(); clearSelection(); ; if (Debug.debugging("beanbox")) Debug.output("Exit> pasteSelectedBean"); return; } // construct a transferData object transferData = new Vector(); transferData.add(deserBean); transferData.add(beanInfo); transferData.add(new Boolean(false)); // bean not being moved // let dropLocation be selectedBeanLocation dropLocation = selectedBeanLocation; showPopUp(selectedBeanLayer); cutBean = null; if (Debug.debugging("beanbox")) Debug.output("Exit> pasteSelectedBean"); } /** * This method is called when the user chooses to cut a bean by * some means such by by pressing Ctrl-X. This method tries to * serialize the selected bean. If no bean is selected or the bean * is not serializable, this method is a no-op. */ protected void cutSelectedBean() { if (Debug.debugging("beanbox")) Debug.output("Enter> cutSelectedBean"); if (selectedBean == null || selectedBeanLocation == null) { if (Debug.debugging("beanbox")) Debug.output("selectedBean=" + selectedBean); if (Debug.debugging("beanbox")) Debug.output("selectedBeanLocation=" + selectedBeanLocation); clearSelection(); return; } try { serBean = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(serBean); oos.writeObject(selectedBean); } catch (Exception e) { e.printStackTrace(); clearSelection(); ; if (Debug.debugging("beanbox")) Debug.output("Exit> copySelectedBean"); return; } cutBean = selectedBean; selectedBeanBox.showCut(selectedBean); if (Debug.debugging("beanbox")) Debug.output("Exit> cutSelectedBean"); } /** * This method is called when the user chooses to cancel a cut * operation on a bean by some means such by by pressing ESC. If * no bean was marked for cutting this method is a no-op. */ protected void unCutSelectedBean() { if (Debug.debugging("beanbox")) Debug.output("Enter> unCutSelectedBean"); if (selectedBean == null || selectedBeanLocation == null) { if (Debug.debugging("beanbox")) Debug.output("selectedBean=" + selectedBean); if (Debug.debugging("beanbox")) Debug.output("selectedBeanLocation=" + selectedBeanLocation); clearSelection(); return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -