1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.swt.widgets.impl;
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;
24 abstract public class SelectionListenerImpl<T> implements SelectionListener, Widget {
26 private ISessionContext context;
27 private Object lastInput = null;
29 final private Class<?> clazz;
31 public SelectionListenerImpl(ISessionContext context) {
32 this.context = context;
33 clazz = ReflectionUtils.getSingleParameterType(getClass());
37 public void widgetDefaultSelected(SelectionEvent e) {
42 public void widgetSelected(SelectionEvent e) {
44 final Object input = lastInput;
47 @SuppressWarnings("unchecked")
48 T single = (T) ISelectionUtils.filterSingleSelection((ISelection)input, clazz);
51 context.getSession().syncRequest(new WriteRequest() {
53 @SuppressWarnings("unchecked")
55 public void perform(WriteGraph graph) throws DatabaseException {
56 graph.markUndoPoint();
57 T single = (T) ISelectionUtils.filterSingleSelection((ISelection)input, clazz);
62 } catch (DatabaseException e1) {
68 public void setInput(ISessionContext context, Object parameter) {
69 this.context = context;
70 lastInput = parameter;
73 public void beforeApply() {
77 public void beforeApply(T input) {
81 abstract public void apply(WriteGraph graph, T input) throws DatabaseException;