X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.project%2Fsrc%2Forg%2Fsimantics%2Fproject%2Ffeatures%2FAbstractProjectFeature.java;fp=bundles%2Forg.simantics.project%2Fsrc%2Forg%2Fsimantics%2Fproject%2Ffeatures%2FAbstractProjectFeature.java;h=90bea675304627a70d35f3ff4e87d0711dbbf171;hp=390668b97fd51af51666a70ed5db5da342867bdf;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.project/src/org/simantics/project/features/AbstractProjectFeature.java b/bundles/org.simantics.project/src/org/simantics/project/features/AbstractProjectFeature.java index 390668b97..90bea6753 100644 --- a/bundles/org.simantics.project/src/org/simantics/project/features/AbstractProjectFeature.java +++ b/bundles/org.simantics.project/src/org/simantics/project/features/AbstractProjectFeature.java @@ -1,121 +1,121 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.project.features; - -import java.util.ArrayList; -import java.util.Collection; - -import org.simantics.db.ReadGraph; -import org.simantics.db.RequestProcessor; -import org.simantics.db.Session; -import org.simantics.db.common.processor.MergingGraphRequestProcessor; -import org.simantics.db.common.request.ReadRequest; -import org.simantics.db.exception.DatabaseException; -import org.simantics.project.IProject; -import org.simantics.project.exception.ProjectException; -import org.simantics.utils.datastructures.hints.IHintContext.Key; - - -/** - * Implement {@link #configure()} and {@link #deconfigure()} to customize - * how a feature configures the project in question. - * - * @author Tuukka Lehtonen - */ -public abstract class AbstractProjectFeature implements IProjectFeature { - - private IProject project; - - public AbstractProjectFeature() { - } - - protected void assertProject() { - if (project == null) - throw new IllegalStateException("project element is null"); - } - - @Override - public IProject getProjectElement() { - return project; - } - - public IProject getProject() { - assertProject(); - return project; - } - - public IProject peekProject() { - return project; - } - - protected Session getSession() { - IProject p = getProject(); - Session s = p.getSession(); - if (s == null) - throw new IllegalStateException("project not attached to a database session"); - return s; - } - - protected Session peekSession() { - IProject p = peekProject(); - if (p == null) - return null; - return p.getSession(); - } - - protected RequestProcessor getGraphRequestProcessor() { - IProject p = peekProject(); - if (p == null) - return null; - Session s = p.getSession(); - //MergingGraphRequestProcessor mgrp = s.getService(MergingGraphRequestProcessor.class); - MergingGraphRequestProcessor mgrp = null; - return mgrp != null ? mgrp : s; - } - - @Override - public void setProjectElement(IProject project) { - this.project = project; - } - - public void onActivated(final ReadGraph graph, final IProject project) throws DatabaseException { - } - - @Override - public void configure() throws ProjectException { - try { - getSession().syncRequest(new ReadRequest() { - @Override - public void run(ReadGraph graph) throws DatabaseException { - onActivated(graph, getProject()); - } - }); - } catch (DatabaseException e) { - throw new ProjectException(e); - } - } - - @Override - public void deconfigure() throws ProjectException { - } - - protected void addToCollectionHint(Key key, T... ts) { - Collection c = getProjectElement().getHint(key); - if (c == null) { - c = new ArrayList(); - getProjectElement().setHint(key, c); - } - for (T t : ts) - c.add(t); - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.project.features; + +import java.util.ArrayList; +import java.util.Collection; + +import org.simantics.db.ReadGraph; +import org.simantics.db.RequestProcessor; +import org.simantics.db.Session; +import org.simantics.db.common.processor.MergingGraphRequestProcessor; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.project.IProject; +import org.simantics.project.exception.ProjectException; +import org.simantics.utils.datastructures.hints.IHintContext.Key; + + +/** + * Implement {@link #configure()} and {@link #deconfigure()} to customize + * how a feature configures the project in question. + * + * @author Tuukka Lehtonen + */ +public abstract class AbstractProjectFeature implements IProjectFeature { + + private IProject project; + + public AbstractProjectFeature() { + } + + protected void assertProject() { + if (project == null) + throw new IllegalStateException("project element is null"); + } + + @Override + public IProject getProjectElement() { + return project; + } + + public IProject getProject() { + assertProject(); + return project; + } + + public IProject peekProject() { + return project; + } + + protected Session getSession() { + IProject p = getProject(); + Session s = p.getSession(); + if (s == null) + throw new IllegalStateException("project not attached to a database session"); + return s; + } + + protected Session peekSession() { + IProject p = peekProject(); + if (p == null) + return null; + return p.getSession(); + } + + protected RequestProcessor getGraphRequestProcessor() { + IProject p = peekProject(); + if (p == null) + return null; + Session s = p.getSession(); + //MergingGraphRequestProcessor mgrp = s.getService(MergingGraphRequestProcessor.class); + MergingGraphRequestProcessor mgrp = null; + return mgrp != null ? mgrp : s; + } + + @Override + public void setProjectElement(IProject project) { + this.project = project; + } + + public void onActivated(final ReadGraph graph, final IProject project) throws DatabaseException { + } + + @Override + public void configure() throws ProjectException { + try { + getSession().syncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + onActivated(graph, getProject()); + } + }); + } catch (DatabaseException e) { + throw new ProjectException(e); + } + } + + @Override + public void deconfigure() throws ProjectException { + } + + protected void addToCollectionHint(Key key, T... ts) { + Collection c = getProjectElement().getHint(key); + if (c == null) { + c = new ArrayList(); + getProjectElement().setHint(key, c); + } + for (T t : ts) + c.add(t); + } + +}