]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/GraphEnumerationModifierBase.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / GraphEnumerationModifierBase.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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.graph.impl;
13
14 import org.simantics.browsing.ui.common.modifiers.AbstractEnumerationModifier;
15 import org.simantics.browsing.ui.common.modifiers.EnumeratedValue;
16 import org.simantics.browsing.ui.common.modifiers.Enumeration;
17 import org.simantics.db.Session;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.request.WriteRequest;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.utils.ObjectUtils;
22
23 /**
24  * A graph resource based EnumerationModifier utility implementation.
25  * 
26  * @author Tuukka Lehtonen
27  */
28 public abstract class GraphEnumerationModifierBase<T> extends AbstractEnumerationModifier<T> {
29
30     protected final Session  session;
31
32     public GraphEnumerationModifierBase(Session session, Enumeration<T> enumeration, T value) {
33         this(session, enumeration, enumeration.find(value));
34     }
35
36     public GraphEnumerationModifierBase(Session session, Enumeration<T> enumeration, EnumeratedValue<T> value) {
37         super(enumeration, value);
38         if (session == null)
39             throw new NullPointerException("null session");
40         this.session = session;
41     }
42
43     @Override
44     protected void modifyWithObject(final T oldEnumObject, final T enumObject) {
45         if (ObjectUtils.objectEquals(oldEnumObject, enumObject))
46             return;
47
48         try {
49             session.getSession().syncRequest(new WriteRequest() {
50                 @Override
51                 public void perform(WriteGraph graph) throws DatabaseException {
52 //                    System.out.println("old enum object: " + oldEnumObject);
53 //                    System.out.println("enum object: " + enumObject);
54                     modifyWithObject(graph, oldEnumObject, enumObject);
55                 }
56             });
57         } catch (DatabaseException e) {
58             handleException(e);
59         }
60     }
61
62     /**
63      * Implement this modifier to perform database modifications.
64      * 
65      * @param graph
66      * @param oldEnumObject
67      * @param enumObject
68      */
69     protected abstract void modifyWithObject(WriteGraph graph, T oldEnumObject, T enumObject) throws DatabaseException;
70
71     /**
72      * Client overridable handler for possible exceptions that may occur while
73      * performing the database modifications.
74      * 
75      * <p>
76      * This default implementation merely forwards the exception as a runtime
77      * exception.
78      * 
79      * @param e
80      */
81     protected void handleException(DatabaseException e) {
82         throw new RuntimeException(e);
83     }
84
85
86 }