/*
* @(#)DelegationSelectionTool.java
*
* Copyright (c) 1996-2010 by the original authors of JHotDraw and all its
* contributors. All rights reserved.
*
* You may not use, copy or modify this file, except in compliance with the
* license agreement you entered into with the copyright holders. For details
* see accompanying license terms.
*/
package org.jhotdraw.draw.tool;
import edu.umd.cs.findbugs.annotations.Nullable;
import org.jhotdraw.draw.*;
import org.jhotdraw.draw.handle.Handle;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import org.jhotdraw.app.action.ActionUtil;
/**
* A SelectionTool, which recognizes double clicks and popup menu triggers.
* If a double click or popup trigger is encountered a hook method is called,
* which handles the event. This methods can be overriden in subclasse to
* provide customized behaviour.
*
* By default, this Tool delegates mouse events to a specific Tool if
* the figure which has been double clicked, provides a specialized tool.
*
* @author Werner Randelshofer
* @version $Id: DelegationSelectionTool.java 718 2010-11-21 17:49:53Z rawcoder $
*/
public class DelegationSelectionTool extends SelectionTool {
/**
* Set this to true to turn on debugging output on System.out.
*/
private final static boolean DEBUG = false;
/**
* A set of actions which is applied to the drawing.
*/
private Collection drawingActions;
/**
* A set of actions which is applied to a selection of figures.
*/
private Collection selectionActions;
/**
* We use this timer, to show a popup menu, when the user presses the
* mouse key for a second without moving the mouse.
*/
@Nullable private javax.swing.Timer popupTimer;
/**
* When the popup menu is visible, we do not track mouse movements.
*/
@Nullable private JPopupMenu popupMenu;
/**
* We store the last mouse click here, to support multi-click behavior,
* that is, a behavior that is invoked, when the user clicks multiple on
* the same spot, but in a longer interval than needed for a double click.
*/
private MouseEvent lastClickEvent;
/**
* This variable is set to true, if a mouse pressed event is a popup trigger.
*/
private boolean isMousePressedPopupTrigger;
/** Creates a new instance. */
public DelegationSelectionTool() {
this(new LinkedList(), new LinkedList());
}
/** Creates a new instance. */
public DelegationSelectionTool(Collection drawingActions, Collection selectionActions) {
this.drawingActions = drawingActions;
this.selectionActions = selectionActions;
}
public void setDrawingActions(Collection drawingActions) {
this.drawingActions = drawingActions;
}
public void setFigureActions(Collection selectionActions) {
this.selectionActions = selectionActions;
}
/**
* MouseListener method for mousePressed events. If the popup trigger has
* been activated, then the appropriate hook method is called.
*/
@Override
public void mousePressed(final MouseEvent evt) {
if (popupTimer != null) {
popupTimer.stop();
popupTimer = null;
}
// XXX - When we want to support multiple views, we have to
// implement this:
//setView((DrawingView)e.getSource());
isMousePressedPopupTrigger = evt.isPopupTrigger();
if (isMousePressedPopupTrigger) {
getView().requestFocus();
handlePopupMenu(evt);
} else {
super.mousePressed(evt);
popupTimer = new javax.swing.Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent aevt) {
handlePopupMenu(evt);
popupTimer = null;
}
});
popupTimer.setRepeats(false);
popupTimer.start();
}
}
/**
* MouseListener method for mouseReleased events. If the popup trigger has
* been activated, then the appropriate hook method is called.
*/
@Override
public void mouseReleased(MouseEvent evt) {
if (popupTimer != null) {
popupTimer.stop();
popupTimer = null;
}
if (isMousePressedPopupTrigger) {
isMousePressedPopupTrigger = false;
} else {
if (evt.isPopupTrigger()) {
handlePopupMenu(evt);
} else {
super.mouseReleased(evt);
}
}
}
@Override
public void mouseDragged(MouseEvent evt) {
if (popupTimer != null) {
popupTimer.stop();
popupTimer = null;
}
if (popupMenu == null || !popupMenu.isVisible()) {
super.mouseDragged(evt);
}
}
@Override
public void mouseClicked(MouseEvent evt) {
if (DEBUG) {
System.out.println("DelegationSelectionTool.mouseClicked " + evt);
}
super.mouseClicked(evt);
if (!evt.isConsumed()) {
if (evt.getClickCount() >= 2 && //
evt.getButton() == MouseEvent.BUTTON1) {
handleDoubleClick(evt);
} else if (evt.getClickCount() == 1 &&
evt.getModifiersEx() == 0 &&
lastClickEvent != null &&
lastClickEvent.getClickCount() == 1 &&
lastClickEvent.getModifiersEx() == 0 &&
lastClickEvent.getX() == evt.getX() &&
lastClickEvent.getY() == evt.getY()) {
handleMultiClick(evt);
}
}
lastClickEvent = evt;
}
/**
* Hook method which can be overriden by subclasses to provide
* specialised behaviour in the event of a popup trigger.
*/
protected void handlePopupMenu(MouseEvent evt) {
Point p = new Point(evt.getX(), evt.getY());
Figure figure = getView().findFigure(p);
if (figure != null || drawingActions.size() > 0) {
showPopupMenu(figure, p, evt.getComponent());
} else {
popupMenu = null;
}
}
protected void showPopupMenu(@Nullable Figure figure, Point p, Component c) {
if (DEBUG) {
System.out.println("DelegationSelectionTool.showPopupMenu " + figure);
}
JPopupMenu menu = new JPopupMenu();
popupMenu = menu;
JMenu submenu = null;
String submenuName = null;
LinkedList popupActions = new LinkedList();
if (figure != null) {
LinkedList figureActions = new LinkedList(
figure.getActions(viewToDrawing(p)));
if (popupActions.size() != 0 && figureActions.size() != 0) {
popupActions.add(null);
}
popupActions.addAll(figureActions);
if (popupActions.size() != 0 && selectionActions.size() != 0) {
popupActions.add(null);
}
popupActions.addAll(selectionActions);
}
if (popupActions.size() != 0 && drawingActions.size() != 0) {
popupActions.add(null);
}
popupActions.addAll(drawingActions);
HashMap
Ads help maintain this website.