]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/contentassist/GraphContentAssistModifier.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / contentassist / GraphContentAssistModifier.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.contentassist;
13
14 import org.simantics.browsing.ui.common.modifiers.Enumeration;
15 import org.simantics.db.Session;
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.utils.ObjectUtils;
20
21 /**
22  * @author Tuukka Lehtonen
23  */
24 public abstract class GraphContentAssistModifier<T> extends AbstractContentAssistModifier<T> {
25
26     protected final Session session;
27
28     public GraphContentAssistModifier(Session session, Enumeration<T> enumeration, T value) {
29         super(enumeration, value);
30         if (session == null)
31             throw new NullPointerException("null session");
32         this.session = session;
33     }
34
35     @Override
36     protected void modifyWithValue(final T oldEnumObject, final String enumObject) {
37         if (ObjectUtils.objectEquals(oldEnumObject, enumObject))
38             return;
39
40         try {
41             session.getSession().syncRequest(new WriteRequest() {
42                 @Override
43                 public void perform(WriteGraph graph) throws DatabaseException {
44 //                    System.out.println("old enum object: " + oldEnumObject);
45 //                    System.out.println("enum object: " + enumObject);
46                     modifyWithObject(graph, oldEnumObject, enumObject);
47                 }
48             });
49         } catch (DatabaseException e) {
50             handleException(e);
51         }
52     }
53
54     /**
55      * Implement this modifier to perform database modifications.
56      * 
57      * @param graph
58      * @param oldEnumObject
59      * @param enumObject
60      */
61     protected abstract void modifyWithObject(WriteGraph graph, T oldEnumObject, String enumObject) throws DatabaseException;
62
63     /**
64      * Client overridable handler for possible exceptions that may occur while
65      * performing the database modifications.
66      * 
67      * <p>
68      * This default implementation merely forwards the exception as a runtime
69      * exception.
70      * 
71      * @param e
72      */
73     protected void handleException(DatabaseException e) {
74         throw new RuntimeException(e);
75     }
76
77 }