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.spreadsheet.ui;
14 import java.awt.geom.AffineTransform;
15 import java.util.Collections;
16 import java.util.List;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.simantics.databoard.Bindings;
20 import org.simantics.databoard.util.URIStringUtils;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.Session;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.request.Read;
26 import org.simantics.diagram.adapter.GraphToDiagramSynchronizer;
27 import org.simantics.diagram.participant.PopulateSelectionDropParticipant;
28 import org.simantics.g2d.dnd.ElementClassDragItem;
29 import org.simantics.g2d.element.ElementHints;
30 import org.simantics.layer0.Layer0;
31 import org.simantics.spreadsheet.resource.SpreadsheetResource;
32 import org.simantics.utils.ui.ISelectionUtils;
34 public class PopulateSheetDropParticipant extends PopulateSelectionDropParticipant {
36 public PopulateSheetDropParticipant(GraphToDiagramSynchronizer synchronizer) {
41 public List<ElementClassDragItem> getElements(Session session, IStructuredSelection selection) throws DatabaseException {
43 final Resource resource = ISelectionUtils.filterSingleSelection(selection, Resource.class);
44 if(resource == null) return Collections.emptyList();
46 return session.syncRequest(new Read<List<ElementClassDragItem>>() {
49 public List<ElementClassDragItem> perform(ReadGraph graph) throws DatabaseException {
51 SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
52 if(graph.isInstanceOf(resource, sr.Spreadsheet)) {
53 Layer0 L0 = Layer0.getInstance(graph);
54 ElementClassDragItem item = new ElementClassDragItem(SheetFactory.createSheetClass(sr.SpreadsheetElement));
55 item.getHintContext().setHint(SheetClass.KEY_SHEET, resource);
56 String rvi = "/" + URIStringUtils.escape((String)graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING));
57 item.getHintContext().setHint(SheetClass.KEY_RVI, rvi);
58 item.getHintContext().setHint(ElementHints.KEY_TRANSFORM, AffineTransform.getScaleInstance(1,1));
59 return Collections.singletonList(item);
61 return Collections.emptyList();