]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/handler/PasteOperation.java
Merge "Add missing javax.servlet-api bundle requirement for jersey bundles"
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / handler / PasteOperation.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.diagram.handler;
13
14 import java.awt.geom.Point2D;
15 import java.util.Collections;
16 import java.util.Map;
17
18 import org.simantics.db.Resource;
19 import org.simantics.g2d.canvas.ICanvasContext;
20 import org.simantics.g2d.diagram.IDiagram;
21 import org.simantics.scenegraph.g2d.events.command.Command;
22
23 /**
24  * 
25  * @author Tuukka Lehtonen
26  */
27 public final class PasteOperation {
28
29     /**
30      * Optional specifications for the paste operation that can be given through.
31      */
32     public static interface PasteOption {}
33
34     /**
35      * For forcing pasting of diagram reference elements even when the parent
36      * element of the reference element is not included in the copied material.
37      */
38     public static enum ForceCopyReferences implements PasteOption { INSTANCE }
39
40     // Input
41     public final Command                 command;
42     public final ICanvasContext          ctx;
43     public final Resource                sourceDiagram;
44     public final Resource                targetDiagram;
45     public final IDiagram                target;
46     public final ElementObjectAssortment ea;
47     public final boolean                 cut;
48     public final Point2D                 offset;
49
50     public Map<Resource, Resource>       initialNodeMap;
51
52     public PasteOption[]                 options;
53
54     // Output
55     public final Map<Object, Object>     copyMap;
56
57     public PasteOperation(
58             Command command,
59             ICanvasContext ctx,
60             Resource sourceDiagram,
61             Resource targetDiagram,
62             IDiagram target,
63             ElementObjectAssortment ea,
64             boolean cut,
65             Point2D offset,
66             Map<Resource, Resource> initialNodeMap,
67             Map<Object, Object> copyMap
68     ) {
69         if (sourceDiagram == null)
70             throw new IllegalArgumentException("null source diagram");
71         if (targetDiagram == null)
72             throw new IllegalArgumentException("null target diagram");
73
74         this.command = command;
75         this.ctx = ctx;
76         this.sourceDiagram = sourceDiagram;
77         this.targetDiagram = targetDiagram;
78         this.target = target;
79         this.ea = ea;
80         this.cut = cut;
81         this.offset = offset;
82         this.copyMap = copyMap;
83         this.initialNodeMap = initialNodeMap;
84     }
85
86     public PasteOperation(
87             Command command,
88             ICanvasContext ctx,
89             Resource sourceDiagram,
90             Resource targetDiagram,
91             IDiagram target,
92             ElementObjectAssortment ea,
93             boolean cut,
94             Point2D offset,
95             Map<Resource, Resource> initialNodeMap
96     ) {
97         this(command, ctx, sourceDiagram, targetDiagram, target, ea, cut, offset, initialNodeMap, null);
98     }
99
100     public PasteOperation(
101             Command command,
102             ICanvasContext ctx,
103             Resource sourceDiagram,
104             Resource targetDiagram,
105             IDiagram target,
106             ElementObjectAssortment ea,
107             boolean cut,
108             Point2D offset
109     ) {
110         this(command, ctx, sourceDiagram, targetDiagram, target, ea, cut, offset, Collections.<Resource, Resource>emptyMap());
111     }
112
113     public PasteOperation options(PasteOption... options) {
114         this.options = options;
115         return this;
116     }
117
118     public boolean sameDiagram() {
119         return sourceDiagram.equals(targetDiagram);
120     }
121
122     @SuppressWarnings("unchecked")
123     public <T extends PasteOption> T getOption(Class<T> clazz) {
124         if (options == null)
125             return null;
126         for (PasteOption option : options) {
127             if (clazz.isInstance(option))
128                 return (T) option;
129         }
130         return null;
131     }
132
133     public <T extends PasteOption> boolean hasOption(Class<T> clazz) {
134         return getOption(clazz) != null;
135     }
136
137 }