]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/impl/MouseListenerImpl.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / widgets / impl / MouseListenerImpl.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 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.browsing.ui.swt.widgets.impl;
13
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.swt.events.MouseEvent;
16 import org.eclipse.swt.events.MouseListener;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.common.request.WriteRequest;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.management.ISessionContext;
21 import org.simantics.utils.ReflectionUtils;
22 import org.simantics.utils.ui.ISelectionUtils;
23
24 abstract public class MouseListenerImpl<T> implements MouseListener, Widget {
25
26         private ISessionContext context;
27         private Object lastInput = null;
28         protected T single;     
29     final private Class<?> clazz;
30         
31         public MouseListenerImpl(ISessionContext context) {
32                 this.context = context;
33         clazz = ReflectionUtils.getSingleParameterType(getClass());
34         }
35         
36         @Override
37         public void mouseDown(MouseEvent e) {
38         }
39         
40         @Override
41         public void mouseUp(MouseEvent e) {
42         }
43         
44         @Override
45         public void mouseDoubleClick(MouseEvent e) {
46                 
47                 final Object input = lastInput;
48                 
49                 
50                 try {
51
52                         single = (T) ISelectionUtils.filterSingleSelection((ISelection)input, clazz);
53                         
54                         beforeApply(single);
55                         
56                         context.getSession().syncRequest(new WriteRequest() {
57
58                                 @SuppressWarnings("unchecked")
59                 @Override
60                                 public void perform(WriteGraph graph) throws DatabaseException {
61
62 //                                      T single = (T) ISelectionUtils.filterSingleSelection((ISelection)input, clazz);
63                                         doubleClick(graph, single);
64                                         
65                                 }
66                                 
67                         });
68                 } catch (DatabaseException e1) {
69                         e1.printStackTrace();
70                 }
71         }
72
73         @Override
74         public void setInput(ISessionContext context, Object parameter) {
75                 this.context = context;
76                 lastInput = parameter;
77         }
78         
79         public void beforeApply(T input) {
80                 
81         }
82         
83         abstract public void doubleClick(WriteGraph graph, T input) throws DatabaseException;
84         
85 }
86