]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/CompositeHintSynchronizer.java
Some enhancements to GraphLayer-related utilities for Diagram layers
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / synchronization / CompositeHintSynchronizer.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;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17
18 import org.simantics.utils.datastructures.hints.IHintObservable;
19 import org.simantics.utils.datastructures.hints.IHintContext.Key;
20
21 /**
22  * Synchronizer that is composed of several other synchronizers. It will consult
23  * the composed synchronizers in the order in which they were specified. Should
24  * a composed synchronizer report that it has handled the synchronization, i.e.
25  * returns <code>true</code>, the consultation will end there.
26  * 
27  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
28  * @author Tuukka Lehtonen
29  */
30 public class CompositeHintSynchronizer implements IHintSynchronizer {
31
32     private final List<IHintSynchronizer> synchronizers = new ArrayList<IHintSynchronizer>();
33
34     public CompositeHintSynchronizer() {
35     }
36
37     public CompositeHintSynchronizer(IHintSynchronizer... synchronizers) {
38         for (IHintSynchronizer s : synchronizers)
39             this.synchronizers.add(s);
40     }
41
42     public CompositeHintSynchronizer(Collection<IHintSynchronizer> synchronizers) {
43         this.synchronizers.addAll(synchronizers);
44     }
45
46     public CompositeHintSynchronizer add(IHintSynchronizer s) {
47         synchronizers.add(s);
48         return this;
49     }
50
51     @Override
52     public int synchronize(ISynchronizationContext context, IHintObservable observable) {
53         int count = 0;
54         for (IHintSynchronizer synchronizer : synchronizers) {
55             count += synchronizer.synchronize(context, observable);
56         }
57         return count;
58     }
59
60     @Override
61     public boolean hintChanged(ISynchronizationContext context, IHintObservable sender, Key key, Object oldValue,
62             Object newValue) {
63         for (IHintSynchronizer s : synchronizers) {
64             if (s.hintChanged(context, sender, key, oldValue, newValue))
65                 return true;
66         }
67         return false;
68     }
69
70     @Override
71     public boolean hintRemoved(ISynchronizationContext context, IHintObservable sender, Key key, Object oldValue) {
72         for (IHintSynchronizer s : synchronizers) {
73             if (s.hintRemoved(context, sender, key, oldValue))
74                 return true;
75         }
76         return false;
77     }
78
79 }