]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/impl/SelectionListenerImpl.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 / SelectionListenerImpl.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.SelectionEvent;
16 import org.eclipse.swt.events.SelectionListener;
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 SelectionListenerImpl<T> implements SelectionListener, Widget {
25
26         private ISessionContext context;
27         private Object lastInput = null;
28         
29     final private Class<?> clazz;
30         
31         public SelectionListenerImpl(ISessionContext context) {
32                 this.context = context;
33         clazz = ReflectionUtils.getSingleParameterType(getClass());
34         }
35         
36         @Override
37         public void widgetDefaultSelected(SelectionEvent e) {
38                 widgetSelected(e);
39         }
40         
41         @Override
42         public void widgetSelected(SelectionEvent e) {
43                 
44                 final Object input = lastInput;
45                 
46                 try {
47                     @SuppressWarnings("unchecked")
48             T single = (T) ISelectionUtils.filterSingleSelection((ISelection)input, clazz);
49                         beforeApply(single);
50                         
51                         context.getSession().syncRequest(new WriteRequest() {
52
53                                 @SuppressWarnings("unchecked")
54                 @Override
55                                 public void perform(WriteGraph graph) throws DatabaseException {
56                                     graph.markUndoPoint();
57                                         T single = (T) ISelectionUtils.filterSingleSelection((ISelection)input, clazz);
58                                         apply(graph, single);
59                                 }
60                                 
61                         });
62                 } catch (DatabaseException e1) {
63                         e1.printStackTrace();
64                 }
65         }
66
67         @Override
68         public void setInput(ISessionContext context, Object parameter) {
69                 this.context = context;
70                 lastInput = parameter;
71         }
72         
73         public void beforeApply() {
74                 
75         }
76         
77     public void beforeApply(T input) {
78         beforeApply();
79     }
80
81         abstract public void apply(WriteGraph graph, T input) throws DatabaseException;
82         
83 }
84