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