]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/ResourceSynchronizer.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / synchronization / graph / ResourceSynchronizer.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.diagram.synchronization.graph;
13
14 import org.simantics.db.Resource;
15 import org.simantics.diagram.synchronization.IHintSynchronizer;
16 import org.simantics.diagram.synchronization.IModificationQueue;
17 import org.simantics.diagram.synchronization.ISynchronizationContext;
18 import org.simantics.diagram.synchronization.SynchronizationHints;
19 import org.simantics.g2d.element.ElementHints;
20 import org.simantics.utils.datastructures.hints.IHintObservable;
21 import org.simantics.utils.datastructures.hints.IHintContext.Key;
22
23 /**
24  * One should implement
25  * {@link #hintChanged(ISynchronizationContext, IModificationQueue, Resource, IHintObservable, Key, Object, Object)}
26  * and
27  * {@link #getSynchronizedHints()}.
28  * 
29  * @author Tuukka Lehtonen
30  */
31 public abstract class ResourceSynchronizer implements IHintSynchronizer {
32
33     @Override
34     public final int synchronize(ISynchronizationContext context, IHintObservable observable) {
35         Object o = observable.getHint(ElementHints.KEY_OBJECT);
36         if (o instanceof Resource) {
37             IModificationQueue queue = context.get(SynchronizationHints.MODIFICATION_QUEUE);
38             return synchronize(context, queue, (Resource) o, observable);
39         }
40         return 0;
41     }
42
43     public abstract Key[] getSynchronizedHints();
44
45     public int synchronize(ISynchronizationContext context, IModificationQueue queue, Resource object, IHintObservable observable) {
46         int count = 0;
47         for (Key key : getSynchronizedHints()) {
48             count += synchronizeHint(context, queue, object, observable, key);
49         }
50         return count;
51     }
52
53     protected final int synchronizeHint(ISynchronizationContext context, IModificationQueue queue, Resource object, IHintObservable observable, Key key) {
54         return hintChanged(context, queue, object, observable, key, null, observable.getHint(key)) ? 1 : 0;
55     }
56
57     @Override
58     public final boolean hintChanged(ISynchronizationContext context, IHintObservable sender, Key key, Object oldValue, Object newValue) {
59         Object o = sender.getHint(ElementHints.KEY_OBJECT);
60         if (o instanceof Resource) {
61             IModificationQueue queue = context.get(SynchronizationHints.MODIFICATION_QUEUE);
62             return hintChanged(context, queue, (Resource) o, sender, key, oldValue, newValue);
63         }
64         return false;
65     }
66
67     @Override
68     public final boolean hintRemoved(ISynchronizationContext context, IHintObservable sender, Key key, Object oldValue) {
69         Object o = sender.getHint(ElementHints.KEY_OBJECT);
70         if (o instanceof Resource) {
71             IModificationQueue queue = context.get(SynchronizationHints.MODIFICATION_QUEUE);
72             return hintRemoved(context, queue, (Resource) o, sender, key, oldValue);
73         }
74         return false;
75     }
76
77     public abstract boolean hintChanged(ISynchronizationContext context, IModificationQueue queue, Resource object, IHintObservable sender, Key key, Object oldValue, Object newValue);
78
79     public boolean hintRemoved(ISynchronizationContext context, IModificationQueue queue, Resource object, IHintObservable sender, Key key, Object oldValue) {
80         return false;
81     }
82
83 }