📄 defaultdndcatcher.java
字号:
//**********************************************************************////<copyright>////BBN Technologies//10 Moulton Street//Cambridge, MA 02138//(617) 873-8000////Copyright (C) BBNT Solutions LLC. All rights reserved.////</copyright>//**********************************************************************////$Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/tools/dnd/DefaultDnDCatcher.java,v $//$RCSfile: DefaultDnDCatcher.java,v $//$Revision: 1.4.2.2 $//$Date: 2005/08/09 21:17:57 $//$Author: dietrick $////**********************************************************************package com.bbn.openmap.tools.dnd;import java.awt.Color;import java.awt.Component;import java.awt.Dimension;import java.awt.Point;import java.awt.datatransfer.Transferable;import java.awt.dnd.DnDConstants;import java.awt.dnd.DragGestureEvent;import java.awt.dnd.DragGestureListener;import java.awt.dnd.DragSource;import java.awt.dnd.DragSourceListener;import java.awt.dnd.DropTarget;import java.awt.dnd.DropTargetDropEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.beans.PropertyChangeListener;import java.beans.PropertyChangeSupport;import java.beans.PropertyVetoException;import java.beans.VetoableChangeListener;import java.beans.beancontext.BeanContext;import java.beans.beancontext.BeanContextChild;import java.beans.beancontext.BeanContextChildSupport;import java.beans.beancontext.BeanContextMembershipEvent;import java.beans.beancontext.BeanContextMembershipListener;import java.io.Serializable;import java.util.Enumeration;import java.util.Hashtable;import java.util.Iterator;import javax.swing.BorderFactory;import javax.swing.JMenuItem;import javax.swing.JPopupMenu;import javax.swing.SwingConstants;import javax.swing.border.Border;import javax.swing.border.TitledBorder;import com.bbn.openmap.Layer;import com.bbn.openmap.LayerHandler;import com.bbn.openmap.MapBean;import com.bbn.openmap.MouseDelegator;import com.bbn.openmap.event.LayerEvent;import com.bbn.openmap.event.LayerListener;import com.bbn.openmap.event.ProjectionEvent;import com.bbn.openmap.event.ProjectionListener;import com.bbn.openmap.event.SelectMouseMode;import com.bbn.openmap.layer.OMGraphicHandlerLayer;import com.bbn.openmap.layer.location.Location;import com.bbn.openmap.omGraphics.OMAction;import com.bbn.openmap.omGraphics.OMGraphic;import com.bbn.openmap.omGraphics.SinkGraphic;import com.bbn.openmap.proj.Projection;import com.bbn.openmap.util.Debug;/** * DefaultDnDCatcher manages Drag and Drop events on the map. * * Drag: When a mouseDragged event occurs, DropListenerSupport * forwards it to the DefaultDnDCatcher (consume() method). If it's * the first mouseDragged event, dragGestureRecognized is fired and * drag starts. * * Drop: Each layer in the LayerHandler listens to the drop events. * When a drop occurs, a list of potential targets (layers) is shown * in the popup menu. * * DefaultDnDCatcher recognizes Location as the droppable object. * * DefaultDnDCatcher recognizes OMGraphicHandlerLayer layers as * potential drop targets. */public class DefaultDnDCatcher extends DnDListener implements BeanContextChild, BeanContextMembershipListener, PropertyChangeListener, Serializable, ProjectionListener, LayerListener, ActionListener { /** * PropertyChangeSupport for handling listeners. */ protected PropertyChangeSupport pcSupport = new PropertyChangeSupport(this); /** * BeanContextChildSupport object provides helper functions for * BeanContextChild interface. */ protected BeanContextChildSupport beanContextChildSupport = new BeanContextChildSupport(this); /** * Hashtable for keeping references to potential drop targets */ protected Hashtable layers = new Hashtable(); protected DragSource dragSource; // a reference to the MouseDelegator object in the MapHandler protected transient MouseDelegator md; // a copy of current projection protected transient Projection proj; // object that is being passed in transferable protected Object transferData; protected Point dropLocation; /** * Constructs a new DefaultDnDCatcher. */ public DefaultDnDCatcher() { this(new DragSource()); } /** * Constructs a new DefaultDnDCatcher given the DragSource for the * Component. * * @param ds the DragSource for the Component */ public DefaultDnDCatcher(DragSource ds) { this(ds, null); } /** * Construct a new DefaultDnDCatcher 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 DefaultDnDCatcher(DragSource ds, Component c) { this(ds, c, DnDConstants.ACTION_NONE); } public DefaultDnDCatcher(DragSource ds, Component c, int act) { this(ds, c, act, null); } public DefaultDnDCatcher(DragSource ds, Component c, int act, DragGestureListener dgl) { super(ds, c, act, dgl); dragSource = getDragSource(); dragGestureListener = new ComponentDragGestureListener(this, this); setSourceActions(DnDConstants.ACTION_MOVE); } /** * Invoked when an action from the popup menu occurs. */ public void actionPerformed(java.awt.event.ActionEvent e) { Object source = e.getSource(); if (!(source instanceof JMenuItem)) return; JMenuItem mi = (JMenuItem) source; String name = mi.getText(); OMGraphicHandlerLayer targetLayer = (OMGraphicHandlerLayer) layers.get(name); if (targetLayer == null) { Debug.message("defaultdndcatcher", "ERROR> DefaultDnDCatcher::actionPerformed: " + "no layer found with name " + name); return; } targetLayer.doAction((OMGraphic) transferData, new OMAction(OMAction.UPDATE_GRAPHIC_MASK)); } public void addPropertyChangeListener(PropertyChangeListener listener) { pcSupport.addPropertyChangeListener(listener); } /** Method for BeanContextChild interface. */ public void addPropertyChangeListener(String propertyName, PropertyChangeListener in_pcl) { pcSupport.addPropertyChangeListener(propertyName, in_pcl); } /** Method for BeanContextChild interface. */ public void addVetoableChangeListener(String propertyName, VetoableChangeListener in_vcl) { beanContextChildSupport.addVetoableChangeListener(propertyName, in_vcl); } /** * BeanContextMembershipListener method. Called when new objects * are added to the parent BeanContext. * * @param bcme event that contains an iterator that can be used to * go through the new objects. */ public void childrenAdded(BeanContextMembershipEvent bcme) { findAndInit(bcme.iterator()); } /** * BeanContextMembershipListener method. Called when objects have * been removed from the parent BeanContext. The DefaultDnDCatcher * looks for the MapBean it is managing DnD and MouseEvents for, * and any layers that may be removed. * * @param bcme event that contains an iterator that can be used to * go through the removed objects. */ public void childrenRemoved(BeanContextMembershipEvent bcme) { Iterator it = bcme.iterator(); while (it.hasNext()) { findAndUndo(it.next()); } } /** * The method is invoked on mousePressed, mouseReleased, and * mouseDragged events that come from the MapBean through * DropListenerSupport. * * @return boolean * @param e java.awt.event.MouseEvent */ public boolean consume(MouseEvent e) { if (e.getID() == MouseEvent.MOUSE_PRESSED) { mousePressed(e); } else if (e.getID() == MouseEvent.MOUSE_RELEASED) { mouseReleased(e); } else if (e.getID() == MouseEvent.MOUSE_DRAGGED) { mouseDragged(e); } return false; } /** * 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> * From this method, the <code>DropTargetListener</code> shall * accept or reject the drop via the acceptDrop(int dropAction) or * rejectDrop() methods of the <code>DropTargetDropEvent</code> * parameter. * <P> * Subsequent to acceptDrop(), but not before, * <code>DropTargetDropEvent</code>'s getTransferable() method * may be invoked, and data transfer may be performed via the * returned <code>Transferable</code>'s getTransferData() * method. * <P> * At the completion of a drop, an implementation of this method * is required to signal the success/failure of the drop by * passing an appropriate <code>boolean</code> to the * <code>DropTargetDropEvent</code>'s dropComplete(boolean * success) method. * <P> * Note: The actual processing of the data transfer is not * required to finish before this method returns. It may be * deferred until later. * <P> * * @param dtde the <code>DropTargetDropEvent</code> */ public void drop(java.awt.dnd.DropTargetDropEvent dtde) { // // Accept the drop and get transferable object. // dtde.acceptDrop(DnDConstants.ACTION_MOVE); transferData = extractTransferData(dtde); dropLocation = extractDropLocation(dtde); dtde.dropComplete(true); if (transferData == null || dropLocation == null) return; 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)); // // Check whether the dropped object is of type Location // (has exact x and y coordinates). // if (transferData instanceof Location) { ((Location) transferData).setLocation(dropLocation.x, dropLocation.y, proj); OMGraphicHandlerLayer omlayer = null; String layer_name; Enumeration keys = layers.keys();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -