]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.project/src/org/simantics/project/features/AbstractProjectFeature.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / features / AbstractProjectFeature.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.project.features;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.RequestProcessor;
19 import org.simantics.db.Session;
20 import org.simantics.db.common.processor.MergingGraphRequestProcessor;
21 import org.simantics.db.common.request.ReadRequest;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.project.IProject;
24 import org.simantics.project.exception.ProjectException;
25 import org.simantics.utils.datastructures.hints.IHintContext.Key;
26
27
28 /**
29  * Implement {@link #configure()} and {@link #deconfigure()} to customize
30  * how a feature configures the project in question.
31  * 
32  * @author Tuukka Lehtonen
33  */
34 public abstract class AbstractProjectFeature implements IProjectFeature {
35
36     private IProject project;
37
38     public AbstractProjectFeature() {
39     }
40
41     protected void assertProject() {
42         if (project == null)
43             throw new IllegalStateException("project element is null");
44     }
45
46     @Override
47     public IProject getProjectElement() {
48         return project;
49     }
50
51     public IProject getProject() {
52         assertProject();
53         return project;
54     }
55
56     public IProject peekProject() {
57         return project;
58     }
59
60     protected Session getSession() {
61         IProject p = getProject();
62         Session s = p.getSession();
63         if (s == null)
64             throw new IllegalStateException("project not attached to a database session");
65         return s;
66     }
67
68     protected Session peekSession() {
69         IProject p = peekProject();
70         if (p == null)
71             return null;
72         return p.getSession();
73     }
74
75     protected RequestProcessor getGraphRequestProcessor() {
76         IProject p = peekProject();
77         if (p == null)
78             return null;
79         Session s = p.getSession();
80         //MergingGraphRequestProcessor mgrp = s.getService(MergingGraphRequestProcessor.class);
81         MergingGraphRequestProcessor mgrp = null;
82         return mgrp != null ? mgrp : s;
83     }
84
85     @Override
86     public void setProjectElement(IProject project) {
87         this.project = project;
88     }
89
90     public void onActivated(final ReadGraph graph, final IProject project) throws DatabaseException {
91     }
92
93     @Override
94     public void configure() throws ProjectException {
95         try {
96             getSession().syncRequest(new ReadRequest() {
97                 @Override
98                 public void run(ReadGraph graph) throws DatabaseException {
99                     onActivated(graph, getProject());
100                 }
101             });
102         } catch (DatabaseException e) {
103             throw new ProjectException(e);
104         }
105     }
106
107     @Override
108     public void deconfigure() throws ProjectException {
109     }
110
111     protected <T> void addToCollectionHint(Key key, T... ts) {
112         Collection<T> c = getProjectElement().getHint(key);
113         if (c == null) {
114             c = new ArrayList<T>();
115             getProjectElement().setHint(key, c);
116         }
117         for (T t : ts)
118             c.add(t);
119     }
120
121 }