/******************************************************************************* * Copyright (c) 2007 VTT Technical Research Centre of Finland and others. * 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.processeditor.views; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.views.contentoutline.IContentOutlinePage; import org.simantics.db.Graph; import org.simantics.db.Resource; import org.simantics.db.management.ISessionContext; import org.simantics.layer0.utils.IEntity; import org.simantics.processeditor.ProcessResource; import org.simantics.processeditor.actions.ConfigureFloorAction; import org.simantics.processeditor.actions.InsertComponentAction; import org.simantics.processeditor.actions.InsertEquipmentAction; import org.simantics.processeditor.actions.InsertNozzleAction; import org.simantics.processeditor.actions.RoutePipeAction; import org.simantics.processeditor.adapters.ProcessEditorAdapter; import org.simantics.processeditor.adapters.ProcessEditorSelectionAdapter; import org.simantics.processeditor.common.ControlPointTools; import org.simantics.processeditor.stubs.Plant; import org.simantics.processeditor.tools.PlantEditContribution; import org.simantics.processeditor.tools.PlantVisualizationContribution; import org.simantics.proconf.g3d.base.JmeRenderingComponent; import org.simantics.proconf.g3d.base.ScenegraphAdapter; import org.simantics.proconf.g3d.base.SelectionAdapter; import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase; import org.simantics.proconf.g3d.common.StructuredResourceSelection; import org.simantics.proconf.g3d.shapes.FloorShape; import org.simantics.proconf.g3d.stubs.G3DNode; import org.simantics.utils.ui.jface.MenuTools; import com.jme.math.Vector3f; import com.jme.scene.Geometry; public class ProcessEditor extends ThreeDimensionalEditorBase { private Resource plantResource = null; private ConfigureFloorAction configureFloorAction = null; private Geometry floorShape = null; public ProcessEditor(ISessionContext session) { super(session); addEditorContribution(new PlantEditContribution(this)); addEditorContribution(new PlantVisualizationContribution(this)); } public ProcessEditor(ISessionContext session,JmeRenderingComponent component) { super(session,component); addEditorContribution(new PlantEditContribution(this)); addEditorContribution(new PlantVisualizationContribution(this)); } @Override protected ScenegraphAdapter createScenegraphAdapter() { return new ProcessEditorAdapter(this,session,getRenderingComponent()); } @Override public void createControl(Graph graph,Composite parent) { super.createControl(graph,parent); floorShape = FloorShape.getShape(getRenderingComponent().getDisplaySystem().getRenderer(), 100.f,0.2f); getRenderingComponent().getNoCastRoot().attachChild(floorShape); floorShape.setLocalTranslation(new Vector3f(0.f,-0.01f,0.f)); configureFloorAction.setFloorShape(floorShape); } @Override protected void makeActions(Graph graph) { super.makeActions(graph); //actions.add(new ShowTrendsAction(this)); configureFloorAction = new ConfigureFloorAction(this); // ContextActionFactory extended[] = ContextActionRegistry.getActions("fi.vtt.proconf.shapeeditor.processeditorview"); // for (ContextActionFactory c : extended) { // actions.add(c.createAction(this)); // } } protected void fillLocalPullDown() { super.fillLocalPullDown(); MenuTools.getOrCreate(getMenuID(),"Advanced", menuManager).add(configureFloorAction); } @Override protected void pageSelectionChanged(IWorkbenchPart part, ISelection selection) { if (!(selection instanceof StructuredResourceSelection)) { return; } StructuredResourceSelection s = (StructuredResourceSelection) selection; selectionAdapter.setCurrentSelection(s); viewChanged = true; //if (s.getRootSelection() == null) { if (!(part instanceof ProcessEditor)) { //System.out.println("ShapeEditorView.pageSelectionChanged() no root selection"); ((ProcessEditorSelectionAdapter)selectionAdapter).setEditorSelection(true); return; } //if (!s.getRootSelection().getResource().getId().equals(plant.getResource().getId())) { ProcessEditor sender = (ProcessEditor)part; if (!sender.getPlantResource().equals(plantResource)) { // System.out.println("ShapeEditorView.pageSelectionChanged() not right group " // + s.getRootSelection().getResource().getId() + " != " + model.getResource().getId()); selectionAdapter.setCurrentSelection(new StructuredResourceSelection()); ((ProcessEditorSelectionAdapter)selectionAdapter).setEditorSelection(false); return; } selectionAdapter.setEditorSelection(); } @Override protected void reloadFrom(IEntity thing) { if (plantResource != null) { throw new UnsupportedOperationException("Reloading instantiated viewer not supported"); } if (thing.isInstanceOf(ProcessResource.plant3Dresource.Plant)) { plantResource = thing.getResource(); G3DNode plant = new G3DNode(thing); adapter.setRootNode(plant); //adapter.addOutbound(plant); ControlPointTools.reloadCache(thing.getGraph(),plant.getResource()); } else { throw new IllegalArgumentException("Resource is not a plant"); } } public Resource getPlantResource() { return plantResource; } public Plant getPlant(Graph g) { return new Plant(g, plantResource); } @Override protected SelectionAdapter createSelectionAdapter() { return new ProcessEditorSelectionAdapter(adapter); } @Override protected void hookDragAndDrop() { super.hookDragAndDrop(); dropTarget.addDropListener(new InsertEquipmentAction(this)); dropTarget.addDropListener(new InsertNozzleAction(this)); dropTarget.addDropListener(new InsertComponentAction(this)); dropTarget.addDropListener(new RoutePipeAction(this)); } @Override public Object getAdapter(Class adapter) { if (adapter == IContentOutlinePage.class) { if (getPlantResource() == null) return null; final PlantStructureOutlinePage page = new PlantStructureOutlinePage(sessionContext,getPlantResource()); getSelectionAdapter().addSelectionChangedListener(new ISelectionChangedListener() { @Override public void selectionChanged(SelectionChangedEvent event) { page.setSelection(event.getSelection()); } }); parent.getDisplay().asyncExec(new Runnable() { @Override public void run() { page.addSelectionChangedListener(new ISelectionChangedListener() { @Override public void selectionChanged(SelectionChangedEvent event) { selectionAdapter.setSelection(SelectionAdapter.transformSelection(event.getSelection())); } }); } }); return page; } return null; } }