1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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.diagram.synchronization.graph;
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;
24 * One should implement
25 * {@link #hintChanged(ISynchronizationContext, IModificationQueue, Resource, IHintObservable, Key, Object, Object)}
27 * {@link #getSynchronizedHints()}.
29 * @author Tuukka Lehtonen
31 public abstract class ResourceSynchronizer implements IHintSynchronizer {
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);
43 public abstract Key[] getSynchronizedHints();
45 public int synchronize(ISynchronizationContext context, IModificationQueue queue, Resource object, IHintObservable observable) {
47 for (Key key : getSynchronizedHints()) {
48 count += synchronizeHint(context, queue, object, observable, key);
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;
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);
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);
77 public abstract boolean hintChanged(ISynchronizationContext context, IModificationQueue queue, Resource object, IHintObservable sender, Key key, Object oldValue, Object newValue);
79 public boolean hintRemoved(ISynchronizationContext context, IModificationQueue queue, Resource object, IHintObservable sender, Key key, Object oldValue) {