]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/canvas/impl/MouseCaptureHandle.java
Automatically import also SCLMain modules of dependent index roots
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / canvas / impl / MouseCaptureHandle.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.canvas.impl;
13
14 import org.simantics.g2d.canvas.IMouseCaptureHandle;
15 import org.simantics.g2d.canvas.IMouseCaptureHandleListener;
16 import org.simantics.utils.datastructures.ListenerList;
17
18 /**
19  * @author Toni Kalajainen
20  */
21 public class MouseCaptureHandle implements IMouseCaptureHandle {
22
23     boolean released = false;
24     final int mouseId;
25     ListenerList<IMouseCaptureHandleListener> listeners;
26
27     public MouseCaptureHandle(int mouseId) 
28     {
29         this.mouseId = mouseId;
30     }
31     
32     @Override
33     public int mouseId() {
34         return mouseId;
35     }
36
37     @Override
38     public void release() {
39         synchronized(this) {
40             if (released) return;
41             released = true;
42         }
43         fireOnReleased();
44     }
45
46     void fireOnReleased() {
47         ListenerList<IMouseCaptureHandleListener> lis;
48         synchronized(this) {
49             lis = listeners;
50             if (lis==null) return;
51         }
52         
53         for (IMouseCaptureHandleListener l : lis.getListeners())
54             l.onCaptureReleased(this);
55     }
56
57     @Override
58     public void removeMouseCaptureHandleListener(IMouseCaptureHandleListener listener) {    
59         if (listeners == null) return;
60         listeners.remove(listener);
61     }
62
63     @Override
64     public synchronized void addMouseCaptureHandleListener(IMouseCaptureHandleListener listener) {
65         if (listeners == null) {
66             listeners = new ListenerList<IMouseCaptureHandleListener>(IMouseCaptureHandleListener.class);
67         }
68         listeners.add(listener);
69     }
70     
71 }