]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/symbollibrary/SymbolManagerFeature.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / symbollibrary / SymbolManagerFeature.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in 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.diagram.symbollibrary;\r
13 \r
14 import java.util.Collection;\r
15 import java.util.Collections;\r
16 import java.util.HashSet;\r
17 \r
18 import org.simantics.db.exception.DatabaseException;\r
19 import org.simantics.diagram.synchronization.graph.BasicResources;\r
20 import org.simantics.project.IProject;\r
21 import org.simantics.project.exception.ProjectException;\r
22 import org.simantics.project.features.AbstractProjectFeature;\r
23 import org.simantics.utils.datastructures.disposable.AbstractDisposable;\r
24 import org.simantics.utils.datastructures.disposable.IDisposable;\r
25 \r
26 class SymbolManager extends AbstractDisposable implements ISymbolManager, IDisposable {\r
27 \r
28     IProject project;\r
29 \r
30     public SymbolManager(IProject project) {\r
31         this.project = project;\r
32         project.setHint(ISymbolManager.KEY_SYMBOL_GROUPS, Collections.emptySet());\r
33     }\r
34 \r
35     @Override\r
36     protected void doDispose() {\r
37         project.setHint(KEY_SYMBOL_GROUPS, Collections.emptySet());\r
38     }\r
39 \r
40     @Override\r
41     public void addEntryPoints(Collection<ISymbolGroup> eps) {\r
42         Collection<ISymbolGroup> groups = project.getHint(KEY_SYMBOL_GROUPS);\r
43         Collection<ISymbolGroup> newGroups = new HashSet<ISymbolGroup>(groups);\r
44         newGroups.addAll(eps);\r
45         project.setHint(KEY_SYMBOL_GROUPS, Collections.unmodifiableCollection(newGroups));\r
46     }\r
47 \r
48     @Override\r
49     public void removeEntryPoints(Collection<ISymbolGroup> eps) {\r
50         Collection<ISymbolGroup> groups = project.getHint(KEY_SYMBOL_GROUPS);\r
51         Collection<ISymbolGroup> newGroups = new HashSet<ISymbolGroup>(groups);\r
52         newGroups.removeAll(eps);\r
53         project.setHint(KEY_SYMBOL_GROUPS, Collections.unmodifiableCollection(newGroups));\r
54     }\r
55 \r
56 }\r
57 \r
58 public class SymbolManagerFeature extends AbstractProjectFeature {\r
59 \r
60     SymbolManager sm;\r
61 \r
62     @Override\r
63     public void configure() throws ProjectException {\r
64         // IMPORTANT: This is vital for making sure that certain basic\r
65         // resource classes are loaded into the database session as its\r
66         // services (see org.simantics.db.ServiceLocator).\r
67         try {\r
68             BasicResources.getInstance(getSession());\r
69         } catch (DatabaseException e) {\r
70             throw new ProjectException(e);\r
71         }\r
72 \r
73         sm = new SymbolManager(getProject());\r
74         getProjectElement().setHint(ISymbolManager.KEY_SYMBOL_MANAGER, sm);\r
75     }\r
76 \r
77     @Override\r
78     public void deconfigure() throws ProjectException {\r
79         if (getProjectElement().getHint(ISymbolManager.KEY_SYMBOL_MANAGER) == sm)\r
80             getProjectElement().removeHint(ISymbolManager.KEY_SYMBOL_MANAGER);\r
81         sm.dispose();\r
82         sm = null;\r
83     }\r
84 \r
85 }