]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/impl/TextModifyListenerImpl.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 / TextModifyListenerImpl.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 org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.swt.widgets.Text;
16 import org.simantics.db.WriteGraph;
17 import org.simantics.db.common.request.WriteRequest;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.layer0.util.Layer0Utils;
20 import org.simantics.db.management.ISessionContext;
21 import org.simantics.utils.ReflectionUtils;
22 import org.simantics.utils.ui.ISelectionUtils;
23
24 public abstract class TextModifyListenerImpl<T> implements TextModifyListener, Widget {
25
26         protected ISessionContext context;
27         protected Object lastInput = null;
28
29         protected final Class<?> clazz;
30
31         public TextModifyListenerImpl() {
32                 clazz = ReflectionUtils.getSingleParameterType(getClass());
33         }
34
35         private Object getInputContents(Object input, Class<?> inputClass) {
36                 if (inputClass.isInstance(input))
37                         return input;
38                 if (input instanceof ISelection)
39                         return ISelectionUtils.filterSingleSelection(input, inputClass);
40                 return null;
41         }
42
43         @Override
44         public void modifyText(TrackedModifyEvent e) {
45
46                 Text text = (Text)e.getWidget();
47                 final String textValue = text.getText();
48                 final Object input = lastInput;
49
50                 try {
51                         context.getSession().syncRequest(new WriteRequest() {
52
53                                 @SuppressWarnings("unchecked")
54                                 @Override
55                                 public void perform(WriteGraph graph) throws DatabaseException {
56                                         graph.markUndoPoint();
57                                         if(clazz.isInstance(input)) {
58                                                 applyText(graph, (T)input, textValue);
59                                                 Layer0Utils.addCommentMetadata(graph, "Modified " + input.toString() + " to " + textValue);
60                                         } else {
61                                                 T single = (T)getInputContents(input, clazz);
62                                                 if(single != null) {
63                                                         applyText(graph, single, textValue);
64                                                         Layer0Utils.addCommentMetadata(graph, "Modified " + single.toString() + " to " + textValue);
65                                                 }
66                                         }
67                                         
68                                         
69                                 }
70                                 
71                         });
72                 } catch (DatabaseException e1) {
73                         e1.printStackTrace();
74                 }
75         }
76
77         @Override
78         public void setInput(ISessionContext context, Object parameter) {
79                 this.context = context;
80                 lastInput = parameter;
81         }
82
83         abstract public void applyText(WriteGraph graph, T input, String text) throws DatabaseException;
84
85 }
86