1 package org.simantics.jfreechart.chart.properties;
\r
3 import org.eclipse.jface.viewers.ISelection;
\r
4 import org.eclipse.swt.widgets.Text;
\r
5 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
\r
6 import org.simantics.db.WriteGraph;
\r
7 import org.simantics.db.common.request.WriteRequest;
\r
8 import org.simantics.db.exception.DatabaseException;
\r
9 import org.simantics.db.management.ISessionContext;
\r
10 import org.simantics.utils.ReflectionUtils;
\r
11 import org.simantics.utils.ui.ISelectionUtils;
\r
13 public abstract class StringChooserModifyListenerImpl<T,T2> implements StringChooserModifyListener<T2>, Widget {
\r
14 protected ISessionContext context;
\r
15 protected T lastInput = null;
\r
17 protected final Class<?> clazz;
\r
19 public StringChooserModifyListenerImpl() {
\r
20 clazz = ReflectionUtils.getSingleParameterType(getClass());
\r
23 private Object getInputContents(Object input, Class<?> inputClass) {
\r
24 if (inputClass.isInstance(input))
\r
26 if (input instanceof ISelection)
\r
27 return ISelectionUtils.filterSingleSelection(input, inputClass);
\r
32 public void modifySelection(StringChooserModifyEvent<T2> e) {
\r
34 Text text = (Text)e.getWidget();
\r
35 final T2 object = e.getObject();
\r
36 final String textValue = text.getText();
\r
37 final T input = lastInput;
\r
40 context.getSession().syncRequest(new WriteRequest() {
\r
42 @SuppressWarnings("unchecked")
\r
44 public void perform(WriteGraph graph) throws DatabaseException {
\r
46 if(clazz.isInstance(input)) {
\r
47 applyObject(graph, (T)input, object, textValue);
\r
49 T single = (T)getInputContents(input, clazz);
\r
51 applyObject(graph, single, object, textValue);
\r
58 } catch (DatabaseException e1) {
\r
59 e1.printStackTrace();
\r
64 public void setInput(ISessionContext context, Object parameter) {
\r
65 this.context = context;
\r
66 lastInput = (T)parameter;
\r
69 abstract public void applyObject(WriteGraph graph, T input, T2 object, String text) throws DatabaseException;
\r