]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/impl/ModifyComboListenerImpl.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 / ModifyComboListenerImpl.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 import java.util.Map;
16
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;
28
29 abstract public class ModifyComboListenerImpl<T, S> implements ModifyListener, Widget {
30
31         private ISessionContext context;
32         private Object lastInput = null;
33         
34     final private Class<?> clazz;
35         
36         public ModifyComboListenerImpl() {
37         clazz = ReflectionUtils.getSingleParameterType(getClass());
38         }
39         
40         private Object getInputContents(Object input, Class<?> inputClass) {
41
42                 if(inputClass.isInstance(input)) return input;
43                 
44                 if(input instanceof ISelection) {
45                         return ISelectionUtils.filterSingleSelection((ISelection)input, inputClass);
46                 } else {
47                         return null;
48                 }
49                 
50         }
51         
52         @SuppressWarnings("unchecked")
53     @Override
54         public void modifyText(ModifyEvent e) {
55                 
56                 Combo combo = (Combo)e.widget.getData("org.simantics.browsing.ui.widgets.Combo");
57                 
58                 int selectionIndex = combo.getWidget().getSelectionIndex();
59                 if (selectionIndex == -1)
60                     return;
61
62                 String[] items = combo.getWidget().getItems();
63                 
64                 String key = items[selectionIndex];
65                 final Object input = lastInput;
66                 
67                 if(e.widget.getData() instanceof List) {
68
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;
72                         
73                         try {
74                                 context.getSession().syncRequest(new WriteRequest() {
75
76                                         @Override
77                                         public void perform(WriteGraph graph) throws DatabaseException {
78
79                                                 T single = (T)getInputContents(input, clazz);
80                                                 applySelection(graph, single, (S)o);
81                                                 
82                                         }
83                                         
84                                 });
85                         } catch (DatabaseException e1) {
86                                 e1.printStackTrace();
87                         }
88                         
89                 } else if (e.widget.getData() instanceof Map) {
90                         
91                         Map<String, Object> data = (Map<String, Object>) e.widget.getData();
92                         final Object o = data.get(key);
93                         
94                         try {
95                                 context.getSession().syncRequest(new WriteRequest() {
96
97                                         @Override
98                                         public void perform(WriteGraph graph) throws DatabaseException {
99
100                                                 T single = (T)getInputContents(input, clazz);
101                                                 applySelection(graph, single, (S)o);
102                                                 
103                                         }
104                                         
105                                 });
106                         } catch (DatabaseException e1) {
107                                 e1.printStackTrace();
108                         }
109                         
110                 }
111                 
112         }
113
114         @Override
115         public void setInput(ISessionContext context, Object parameter) {
116                 this.context = context;
117                 lastInput = parameter;
118         }
119         
120         abstract public void applySelection(WriteGraph graph, T input, S selection) throws DatabaseException;
121         
122 }
123