]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/PopulateSheetDropParticipant.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.spreadsheet.ui / src / org / simantics / spreadsheet / ui / PopulateSheetDropParticipant.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.spreadsheet.ui;
13
14 import java.awt.geom.AffineTransform;
15 import java.util.Collections;
16 import java.util.List;
17
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;
33
34 public class PopulateSheetDropParticipant extends PopulateSelectionDropParticipant {
35
36     public PopulateSheetDropParticipant(GraphToDiagramSynchronizer synchronizer) {
37         super(synchronizer);
38     }
39
40     @Override
41     public List<ElementClassDragItem> getElements(Session session, IStructuredSelection selection) throws DatabaseException {
42
43         final Resource resource = ISelectionUtils.filterSingleSelection(selection, Resource.class);
44         if(resource == null) return Collections.emptyList();
45
46         return session.syncRequest(new Read<List<ElementClassDragItem>>() {
47
48             @Override
49             public List<ElementClassDragItem> perform(ReadGraph graph) throws DatabaseException {
50
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);
60                 } else {
61                     return Collections.emptyList();
62                 }
63
64             }
65
66         });
67
68     }
69
70 }