]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
0e47ca9b128999477111e2ad89b484c300e71962
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.elements;\r
13 \r
14 import org.simantics.databoard.Bindings;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.Session;\r
17 import org.simantics.db.exception.DatabaseException;\r
18 import org.simantics.diagram.synchronization.IModificationQueue;\r
19 import org.simantics.diagram.synchronization.ISynchronizationContext;\r
20 import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;\r
21 import org.simantics.diagram.synchronization.graph.RelatedPropertyModification;\r
22 import org.simantics.diagram.synchronization.graph.ResourceSynchronizer;\r
23 import org.simantics.g2d.element.ElementHints;\r
24 import org.simantics.layer0.Layer0;\r
25 import org.simantics.modeling.ModelingResources;\r
26 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
27 import org.simantics.utils.datastructures.hints.IHintObservable;\r
28 \r
29 /**\r
30  * @author Tuukka Lehtonen\r
31  */\r
32 public class ComponentNameSynchronizer extends ResourceSynchronizer {\r
33 \r
34     public static final ComponentNameSynchronizer INSTANCE = new ComponentNameSynchronizer();\r
35 \r
36     private static final Key[] SYNCHRONIZED_HINTS = {\r
37         ElementHints.KEY_TEXT\r
38     };\r
39 \r
40     @Override\r
41     public Key[] getSynchronizedHints() {\r
42         return SYNCHRONIZED_HINTS;\r
43     }\r
44 \r
45     @Override\r
46     public boolean hintChanged(ISynchronizationContext context, IModificationQueue queue, Resource object, IHintObservable sender, Key key, Object oldValue, Object newValue) {\r
47         if (ElementHints.KEY_TEXT.equals(key)) {\r
48             Session session = context.get(GraphSynchronizationHints.SESSION);\r
49             Layer0 l0;\r
50                         try {\r
51                                 l0 = Layer0.getInstance(session);\r
52                     ModelingResources mr = session.getService(ModelingResources.class);\r
53                     return queue.offer(new RelatedPropertyModification(object, mr.ElementToComponent, l0.HasName, l0.String, newValue, Bindings.STRING), null);\r
54                         } catch (DatabaseException e) {\r
55                                 e.printStackTrace();\r
56                         }\r
57         }\r
58         return false;\r
59     }\r
60 \r
61 }\r