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 java.util.List;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.events.SelectionListener;
19 import org.eclipse.swt.widgets.Table;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.request.WriteRequest;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.management.ISessionContext;
24 import org.simantics.utils.ReflectionUtils;
25 import org.simantics.utils.datastructures.Pair;
26 import org.simantics.utils.ui.ISelectionUtils;
28 abstract public class TableSelectionListenerImpl<T, T2> implements SelectionListener, Widget {
30 private ISessionContext context;
31 private Object lastInput = null;
33 final private Class<?> clazz;
35 public TableSelectionListenerImpl() {
36 clazz = ReflectionUtils.getSingleParameterType(getClass());
40 public void widgetDefaultSelected(SelectionEvent e) {
45 public void widgetSelected(SelectionEvent e) {
47 Table table = (Table)e.getSource();
48 @SuppressWarnings("unchecked")
49 List<Pair<String, Object>> items = (List<Pair<String, Object>>)table.getData();
50 Pair<String, Object> pair = (Pair<String, Object>)items.get(table.getSelectionIndex());
51 final Object selection = pair.second;
52 final Object input = lastInput;
55 context.getSession().syncRequest(new WriteRequest() {
57 @SuppressWarnings("unchecked")
59 public void perform(WriteGraph graph) throws DatabaseException {
61 T single = (T) ISelectionUtils.filterSingleSelection((ISelection)input, clazz);
62 applySelection(graph, single, (T2)selection);
67 } catch (DatabaseException e1) {
74 public void setInput(ISessionContext context, Object parameter) {
75 this.context = context;
76 lastInput = parameter;
79 abstract public void applySelection(WriteGraph graph, T input, T2 selection) throws DatabaseException;