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.project.features;
14 import java.util.ArrayList;
15 import java.util.Collection;
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;
29 * Implement {@link #configure()} and {@link #deconfigure()} to customize
30 * how a feature configures the project in question.
32 * @author Tuukka Lehtonen
34 public abstract class AbstractProjectFeature implements IProjectFeature {
36 private IProject project;
38 public AbstractProjectFeature() {
41 protected void assertProject() {
43 throw new IllegalStateException("project element is null");
47 public IProject getProjectElement() {
51 public IProject getProject() {
56 public IProject peekProject() {
60 protected Session getSession() {
61 IProject p = getProject();
62 Session s = p.getSession();
64 throw new IllegalStateException("project not attached to a database session");
68 protected Session peekSession() {
69 IProject p = peekProject();
72 return p.getSession();
75 protected RequestProcessor getGraphRequestProcessor() {
76 IProject p = peekProject();
79 Session s = p.getSession();
80 //MergingGraphRequestProcessor mgrp = s.getService(MergingGraphRequestProcessor.class);
81 MergingGraphRequestProcessor mgrp = null;
82 return mgrp != null ? mgrp : s;
86 public void setProjectElement(IProject project) {
87 this.project = project;
90 public void onActivated(final ReadGraph graph, final IProject project) throws DatabaseException {
94 public void configure() throws ProjectException {
96 getSession().syncRequest(new ReadRequest() {
98 public void run(ReadGraph graph) throws DatabaseException {
99 onActivated(graph, getProject());
102 } catch (DatabaseException e) {
103 throw new ProjectException(e);
108 public void deconfigure() throws ProjectException {
111 protected <T> void addToCollectionHint(Key key, T... ts) {
112 Collection<T> c = getProjectElement().getHint(key);
114 c = new ArrayList<T>();
115 getProjectElement().setHint(key, c);