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