1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 package org.openoffice.accessibility.awb.canvas; 23 24 import java.awt.Dimension; 25 import java.awt.event.InputEvent; 26 import java.awt.event.MouseEvent; 27 import java.awt.event.MouseListener; 28 import java.awt.event.MouseMotionListener; 29 import java.util.Iterator; 30 import javax.swing.tree.TreePath; 31 32 33 /** Observe the mouse and highlight shapes of the canvas when clicked. 34 */ 35 public class MouseObserver 36 implements MouseListener, 37 MouseMotionListener 38 { MouseObserver(Canvas aCanvas)39 public MouseObserver (Canvas aCanvas) 40 { 41 maCanvas = aCanvas; 42 maCanvas.addMouseListener (this); 43 maCanvas.addMouseMotionListener (this); 44 } 45 46 SetTree(javax.swing.JTree aTree)47 public void SetTree (javax.swing.JTree aTree) 48 { 49 maTree = aTree; 50 } 51 mouseClicked(MouseEvent e)52 public void mouseClicked (MouseEvent e) 53 {} 54 mousePressed(MouseEvent e)55 public void mousePressed (MouseEvent e) 56 { 57 CanvasShape aObjectUnderMouse = FindCanvasShapeUnderMouse (e); 58 maTree.clearSelection(); 59 if (aObjectUnderMouse != null) 60 { 61 TreePath aPath = aObjectUnderMouse.getNodePath(); 62 if ((e.getModifiers() & InputEvent.CTRL_MASK) != 0) 63 maTree.expandPath (aPath); 64 // Selecting the entry will eventually highlight the shape. 65 maTree.setSelectionPath (aPath); 66 maTree.makeVisible (aPath); 67 } 68 } 69 mouseReleased(MouseEvent e)70 public void mouseReleased (MouseEvent e) 71 {} 72 mouseEntered(MouseEvent e)73 public void mouseEntered (MouseEvent e) 74 {} 75 mouseExited(MouseEvent e)76 public void mouseExited (MouseEvent e) 77 {} 78 mouseDragged(MouseEvent e)79 public void mouseDragged (MouseEvent e) 80 { 81 } 82 mouseMoved(MouseEvent e)83 public void mouseMoved (MouseEvent e) 84 { 85 if ((e.getModifiers() & InputEvent.SHIFT_MASK) != 0) 86 maCanvas.HighlightObject (FindCanvasShapeUnderMouse (e)); 87 } 88 89 90 /** Search for the smallest shape that contains the mouse position. 91 */ FindCanvasShapeUnderMouse(MouseEvent e)92 protected CanvasShape FindCanvasShapeUnderMouse (MouseEvent e) 93 { 94 Dimension aSmallestSize = null; 95 Iterator maShapeIterator = maCanvas.GetShapeIterator(); 96 CanvasShape aShapeUnderMouse = null; 97 while (maShapeIterator.hasNext()) 98 { 99 CanvasShape aShape = (CanvasShape)maShapeIterator.next(); 100 if (aShape != null) 101 if (aShape.Contains (e.getX(),e.getY())) 102 { 103 if (aShapeUnderMouse == null) 104 { 105 aSmallestSize = aShape.GetSize(); 106 aShapeUnderMouse = aShape; 107 } 108 else 109 { 110 Dimension aSize = aShape.GetSize(); 111 if (aSize.getWidth()<aSmallestSize.getWidth() 112 || aSize.getHeight()<aSmallestSize.getHeight()) 113 { 114 aSmallestSize = aSize; 115 aShapeUnderMouse = aShape; 116 } 117 } 118 } 119 } 120 return aShapeUnderMouse; 121 } 122 123 private Canvas maCanvas; 124 private javax.swing.JTree maTree; 125 } 126