1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.swt.widgets.impl;
\r
14 import org.eclipse.jface.viewers.ISelection;
\r
15 import org.eclipse.swt.events.SelectionEvent;
\r
16 import org.eclipse.swt.events.SelectionListener;
\r
17 import org.simantics.db.WriteGraph;
\r
18 import org.simantics.db.common.request.WriteRequest;
\r
19 import org.simantics.db.exception.DatabaseException;
\r
20 import org.simantics.db.management.ISessionContext;
\r
21 import org.simantics.utils.ReflectionUtils;
\r
22 import org.simantics.utils.ui.ISelectionUtils;
\r
24 abstract public class SelectionListenerImpl<T> implements SelectionListener, Widget {
\r
26 private ISessionContext context;
\r
27 private Object lastInput = null;
\r
29 final private Class<?> clazz;
\r
31 public SelectionListenerImpl(ISessionContext context) {
\r
32 this.context = context;
\r
33 clazz = ReflectionUtils.getSingleParameterType(getClass());
\r
37 public void widgetDefaultSelected(SelectionEvent e) {
\r
42 public void widgetSelected(SelectionEvent e) {
\r
44 final Object input = lastInput;
\r
47 @SuppressWarnings("unchecked")
\r
48 T single = (T) ISelectionUtils.filterSingleSelection((ISelection)input, clazz);
\r
49 beforeApply(single);
\r
51 context.getSession().syncRequest(new WriteRequest() {
\r
53 @SuppressWarnings("unchecked")
\r
55 public void perform(WriteGraph graph) throws DatabaseException {
\r
56 graph.markUndoPoint();
\r
57 T single = (T) ISelectionUtils.filterSingleSelection((ISelection)input, clazz);
\r
58 apply(graph, single);
\r
62 } catch (DatabaseException e1) {
\r
63 e1.printStackTrace();
\r
68 public void setInput(ISessionContext context, Object parameter) {
\r
69 this.context = context;
\r
70 lastInput = parameter;
\r
73 public void beforeApply() {
\r
77 public void beforeApply(T input) {
\r
81 abstract public void apply(WriteGraph graph, T input) throws DatabaseException;
\r