KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtriver > open > vectorvisuals > ui > VVMouseListener


1 /*
2  * VVMouseListener.java
3  *
4  * Created on 12 June 2003, 12:34
5  */

6
7 package com.thoughtriver.open.vectorvisuals.ui;
8
9 import java.awt.*;
10 import java.awt.event.*;
11
12 import com.thoughtriver.open.vectorvisuals.*;
13
14 /**
15  * This is the base class for all mouse event listeners supported by the Vector
16  * Visuals system. It combines three standard mouse listeners into a single
17  * implementation, and provides support for some useful behaviors, such as
18  * zooming the viewport with the mouse wheel. If custom behaviors are desired,
19  * this class should be extended and the appropriate methods overridden.
20  *
21  * @author Brandon Franklin
22  * @version $Date: 2006/11/25 09:00:37 $
23  */

24 public class VVMouseListener extends MouseAdapter implements MouseWheelListener {
25
26     /** The <CODE>SelectionManager</CODE> that this listener uses */
27     private SelectionManager selectionManager = null;
28
29     /** The <CODE>VVDisplay</CODE> for which events are being handled */
30     private VVDisplay vvDisplay = null;
31
32     /** Whether or not the viewport itself can be zoomed with the mouse wheel */
33     private boolean viewportZoomable = false;
34
35     /**
36      * Creates a new instance of <CODE>VVMouseListener</CODE> associated with
37      * the supplied <CODE>VVDisplay</CODE>.
38      *
39      * @param vvDisplay the <CODE>VVDisplay</CODE> for which events are being
40      * handled
41      */

42     public VVMouseListener(final VVDisplay vvDisplay) {
43         this(vvDisplay, SelectionManager.getSharedInstance());
44     }
45
46     /**
47      * Creates a new instance of <CODE>VVMouseListener</CODE> associated with
48      * the supplied <CODE>VVDisplay</CODE> and <CODE>SelectionManager</CODE>.
49      *
50      * @param vvDisplay the <CODE>VVDisplay</CODE> for which events are being
51      * handled
52      * @param selectionManager the <CODE>SelectionManager</CODE> that will be
53      * communicated with regarding selection activities
54      */

55     public VVMouseListener(final VVDisplay vvDisplay, final SelectionManager selectionManager) {
56         this.vvDisplay = vvDisplay;
57         this.selectionManager = selectionManager;
58
59         // Sets the default cursor
60
vvDisplay.getViewPane().setCursor(Cursor.getDefaultCursor());
61     }
62
63     /**
64      * Returns a reference to the <CODE>VVDisplay</CODE> that this listener is
65      * associated with.
66      *
67      * @return a reference to the <CODE>VVDisplay</CODE> that this listener is
68      * associated with
69      */

70     public VVDisplay getVVDisplay() {
71         return vvDisplay;
72     }
73
74     /**
75      * Returns whether or not the viewport itself can be zoomed using the mouse
76      * wheel.
77      *
78      * @return whether or not the viewport itself can be zoomed using the mouse
79      * wheel
80      */

81     public boolean isViewportZoomable() {
82         return viewportZoomable;
83     }
84
85     /**
86      * Sets whether or not the viewport itself can be zoomed using the mouse
87      * wheel.
88      *
89      * @param viewportZoomable whether or not the viewport itself can be zoomed
90      * using the mouse wheel
91      */

92     public void setViewportZoomable(final boolean viewportZoomable) {
93         this.viewportZoomable = viewportZoomable;
94     }
95
96     /**
97      * Invoked when the mouse button has been clicked (pressed and released) on
98      * a component.
99      *
100      * @param e the event to be processed
101      */

102     @Override JavaDoc
103     public void mouseClicked(final MouseEvent e) {
104         VisualObject clickedOn = getVVDisplay().getVisualObjectAt(e.getX(), e.getY());
105         selectionManager.setSelectedObject(clickedOn);
106     }
107
108     /**
109      * Invoked when the mouse wheel is rotated.
110      *
111      * @param e the event to be processed
112      * @see MouseWheelEvent
113      */

114     @Override JavaDoc
115     public void mouseWheelMoved(final MouseWheelEvent e) {
116         if (isViewportZoomable()) {
117             int rotation = e.getWheelRotation();
118             double scale = getVVDisplay().getWorldViewScale();
119             scale += ((scale * 0.15) * rotation);
120
121             getVVDisplay().setWorldViewScale(scale);
122         }
123     }
124
125 }
126
Popular Tags