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;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.swt.events.ModifyEvent;
19 import org.eclipse.swt.events.ModifyListener;
20 import org.simantics.browsing.ui.swt.widgets.Combo;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.request.WriteRequest;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.management.ISessionContext;
25 import org.simantics.utils.ReflectionUtils;
26 import org.simantics.utils.datastructures.Pair;
27 import org.simantics.utils.ui.ISelectionUtils;
29 abstract public class ModifyComboListenerImpl<T, S> implements ModifyListener, Widget {
31 private ISessionContext context;
32 private Object lastInput = null;
34 final private Class<?> clazz;
36 public ModifyComboListenerImpl() {
37 clazz = ReflectionUtils.getSingleParameterType(getClass());
40 private Object getInputContents(Object input, Class<?> inputClass) {
42 if(inputClass.isInstance(input)) return input;
44 if(input instanceof ISelection) {
45 return ISelectionUtils.filterSingleSelection((ISelection)input, inputClass);
52 @SuppressWarnings("unchecked")
54 public void modifyText(ModifyEvent e) {
56 Combo combo = (Combo)e.widget.getData("org.simantics.browsing.ui.widgets.Combo");
58 int selectionIndex = combo.getWidget().getSelectionIndex();
59 if (selectionIndex == -1)
62 String[] items = combo.getWidget().getItems();
64 String key = items[selectionIndex];
65 final Object input = lastInput;
67 if(e.widget.getData() instanceof List) {
69 List<Pair<String, Object>> data = (List<Pair<String, Object>>) e.widget.getData();
70 Pair<String, Object> pair = data.get(selectionIndex);
71 final Object o = pair.second;
74 context.getSession().syncRequest(new WriteRequest() {
77 public void perform(WriteGraph graph) throws DatabaseException {
79 T single = (T)getInputContents(input, clazz);
80 applySelection(graph, single, (S)o);
85 } catch (DatabaseException e1) {
89 } else if (e.widget.getData() instanceof Map) {
91 Map<String, Object> data = (Map<String, Object>) e.widget.getData();
92 final Object o = data.get(key);
95 context.getSession().syncRequest(new WriteRequest() {
98 public void perform(WriteGraph graph) throws DatabaseException {
100 T single = (T)getInputContents(input, clazz);
101 applySelection(graph, single, (S)o);
106 } catch (DatabaseException e1) {
107 e1.printStackTrace();
115 public void setInput(ISessionContext context, Object parameter) {
116 this.context = context;
117 lastInput = parameter;
120 abstract public void applySelection(WriteGraph graph, T input, S selection) throws DatabaseException;