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 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;
24 public abstract class TextModifyListenerImpl<T> implements TextModifyListener, Widget {
26 protected ISessionContext context;
27 protected Object lastInput = null;
29 protected final Class<?> clazz;
31 public TextModifyListenerImpl() {
32 clazz = ReflectionUtils.getSingleParameterType(getClass());
35 private Object getInputContents(Object input, Class<?> inputClass) {
36 if (inputClass.isInstance(input))
38 if (input instanceof ISelection)
39 return ISelectionUtils.filterSingleSelection(input, inputClass);
44 public void modifyText(TrackedModifyEvent e) {
46 Text text = (Text)e.getWidget();
47 final String textValue = text.getText();
48 final Object input = lastInput;
51 context.getSession().syncRequest(new WriteRequest() {
53 @SuppressWarnings("unchecked")
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);
61 T single = (T)getInputContents(input, clazz);
63 applyText(graph, single, textValue);
64 Layer0Utils.addCommentMetadata(graph, "Modified " + single.toString() + " to " + textValue);
72 } catch (DatabaseException e1) {
78 public void setInput(ISessionContext context, Object parameter) {
79 this.context = context;
80 lastInput = parameter;
83 abstract public void applyText(WriteGraph graph, T input, String text) throws DatabaseException;