Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ActionSaveDiagramToClipboard |
|
| 3.5555555555555554;3.556 | ||||
ImageSelection |
|
| 3.5555555555555554;3.556 |
1 | /* $Id: ActionSaveDiagramToClipboard.java 17865 2010-01-12 20:45:26Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
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 | * tfmorris | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2007 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.uml.diagram.ui; | |
40 | ||
41 | import java.awt.Color; | |
42 | import java.awt.Graphics; | |
43 | import java.awt.Graphics2D; | |
44 | import java.awt.Image; | |
45 | import java.awt.Rectangle; | |
46 | import java.awt.Toolkit; | |
47 | import java.awt.datatransfer.Clipboard; | |
48 | import java.awt.datatransfer.ClipboardOwner; | |
49 | import java.awt.datatransfer.DataFlavor; | |
50 | import java.awt.datatransfer.Transferable; | |
51 | import java.awt.datatransfer.UnsupportedFlavorException; | |
52 | import java.awt.event.ActionEvent; | |
53 | ||
54 | import javax.swing.AbstractAction; | |
55 | ||
56 | import org.argouml.application.helpers.ResourceLoaderWrapper; | |
57 | import org.argouml.configuration.Configuration; | |
58 | import org.argouml.i18n.Translator; | |
59 | import org.argouml.uml.ui.SaveGraphicsManager; | |
60 | import org.tigris.gef.base.SaveGIFAction; | |
61 | import org.tigris.gef.base.Editor; | |
62 | import org.tigris.gef.base.Globals; | |
63 | import org.tigris.gef.base.Layer; | |
64 | ||
65 | /** | |
66 | * This class copies a diagram to the system clipboard, this functionality will | |
67 | * only work with Java1.4, but it will compile with 1.3. It can be put into GEF | |
68 | * as it is rather generic. | |
69 | * | |
70 | * @see <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/dnd.html"> | |
71 | * Swing Drag and Drop </a> | |
72 | * @author alexb | |
73 | * @since argoUML version 0.15.2, Created on 19 October 2003, 08:36 | |
74 | */ | |
75 | public class ActionSaveDiagramToClipboard extends AbstractAction implements | |
76 | ClipboardOwner { | |
77 | ||
78 | /** | |
79 | * The constructor. | |
80 | */ | |
81 | public ActionSaveDiagramToClipboard() { | |
82 | 900 | super(Translator.localize("menu.popup.copy-diagram-to-clip"), |
83 | ResourceLoaderWrapper.lookupIcon("action.copy")); | |
84 | 900 | } |
85 | ||
86 | /* | |
87 | * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) | |
88 | */ | |
89 | public void actionPerformed(ActionEvent actionEvent) { | |
90 | ||
91 | 0 | Image diagramGifImage = getImage(); |
92 | ||
93 | 0 | if (diagramGifImage == null) { |
94 | 0 | return; |
95 | } | |
96 | ||
97 | // copy the gif image to the clipboard | |
98 | 0 | Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); |
99 | 0 | clipboard.setContents(new ImageSelection(diagramGifImage), this); |
100 | 0 | } |
101 | ||
102 | /** | |
103 | * Get image from gef. | |
104 | * | |
105 | * @return An Image. | |
106 | */ | |
107 | private Image getImage() { | |
108 | ||
109 | 0 | int scale = |
110 | Configuration.getInteger( | |
111 | SaveGraphicsManager.KEY_GRAPHICS_RESOLUTION, 1); | |
112 | ||
113 | 0 | Editor ce = Globals.curEditor(); |
114 | 0 | Rectangle drawingArea = |
115 | ce.getLayerManager().getActiveLayer() | |
116 | .calcDrawingArea(); | |
117 | ||
118 | // avoid GEF calcDrawingArea bug when nothing in a diagram. | |
119 | 0 | if (drawingArea.x < 0 || drawingArea.y < 0 || drawingArea.width <= 0 |
120 | || drawingArea.height <= 0) { | |
121 | 0 | return null; |
122 | } | |
123 | ||
124 | 0 | boolean isGridHidden = ce.getGridHidden(); |
125 | 0 | ce.setGridHidden(true); // hide grid, otherwise can't see anything |
126 | 0 | Image diagramGifImage = |
127 | ce.createImage(drawingArea.width * scale, | |
128 | drawingArea.height * scale); | |
129 | 0 | Graphics g = diagramGifImage.getGraphics(); |
130 | 0 | if (g instanceof Graphics2D) { |
131 | 0 | ((Graphics2D) g).scale(scale, scale); |
132 | } | |
133 | ||
134 | // background color. | |
135 | 0 | g.setColor(new Color(SaveGIFAction.TRANSPARENT_BG_COLOR)); |
136 | 0 | g.fillRect(0, 0, drawingArea.width * scale, drawingArea.height * scale); |
137 | 0 | g.translate(-drawingArea.x, -drawingArea.y); |
138 | 0 | ce.print(g); |
139 | 0 | ce.setGridHidden(isGridHidden); |
140 | ||
141 | 0 | return diagramGifImage; |
142 | } | |
143 | ||
144 | /* | |
145 | * @see java.awt.datatransfer.ClipboardOwner#lostOwnership( | |
146 | * java.awt.datatransfer.Clipboard, java.awt.datatransfer.Transferable) | |
147 | */ | |
148 | public void lostOwnership(Clipboard clipboard, Transferable transferable) { | |
149 | // do nothing | |
150 | 0 | } |
151 | ||
152 | /* | |
153 | * @see javax.swing.AbstractAction#isEnabled() | |
154 | */ | |
155 | public boolean isEnabled() { | |
156 | 0 | Editor ce = Globals.curEditor(); |
157 | 0 | if (ce == null || ce.getLayerManager() == null |
158 | || ce.getLayerManager().getActiveLayer() == null) { | |
159 | 0 | return false; |
160 | } | |
161 | 0 | Layer layer = ce.getLayerManager().getActiveLayer(); |
162 | 0 | if (layer == null) { |
163 | 0 | return false; |
164 | } | |
165 | 0 | Rectangle drawingArea = layer.calcDrawingArea(); |
166 | ||
167 | // avoid GEF calcDrawingArea bug when nothing in a diagram. | |
168 | 0 | if (drawingArea.x < 0 || drawingArea.y < 0 || drawingArea.width <= 0 |
169 | || drawingArea.height <= 0) { | |
170 | 0 | return false; |
171 | } | |
172 | 0 | return super.isEnabled(); |
173 | } | |
174 | ||
175 | ||
176 | /** | |
177 | * The UID. | |
178 | */ | |
179 | private static final long serialVersionUID = 4916652432210626558L; | |
180 | } | |
181 | ||
182 | /** | |
183 | * Encapsulates an awt Image for Data Transfer to/from the clipboard. | |
184 | */ | |
185 | class ImageSelection implements Transferable { | |
186 | ||
187 | 0 | private DataFlavor[] supportedFlavors = { |
188 | DataFlavor.imageFlavor, | |
189 | }; | |
190 | ||
191 | // the diagram image data | |
192 | private Image diagramImage; | |
193 | ||
194 | /** | |
195 | * Constructor. | |
196 | * | |
197 | * @param newDiagramImage The image. | |
198 | */ | |
199 | 0 | public ImageSelection(Image newDiagramImage) { |
200 | ||
201 | 0 | diagramImage = newDiagramImage; |
202 | 0 | } |
203 | ||
204 | /* | |
205 | * @see java.awt.datatransfer.Transferable#getTransferDataFlavors() | |
206 | */ | |
207 | public synchronized DataFlavor[] getTransferDataFlavors() { | |
208 | 0 | return (supportedFlavors); |
209 | } | |
210 | ||
211 | /* | |
212 | * @see java.awt.datatransfer.Transferable#isDataFlavorSupported( | |
213 | * java.awt.datatransfer.DataFlavor) | |
214 | */ | |
215 | public boolean isDataFlavorSupported(DataFlavor parFlavor) { | |
216 | ||
217 | // hack in order to be able to compile in java1.3 | |
218 | 0 | return (parFlavor.getMimeType().equals( |
219 | DataFlavor.imageFlavor.getMimeType()) && parFlavor | |
220 | .getHumanPresentableName().equals( | |
221 | DataFlavor.imageFlavor.getHumanPresentableName())); | |
222 | } | |
223 | ||
224 | /* | |
225 | * @see java.awt.datatransfer.Transferable#getTransferData( | |
226 | * java.awt.datatransfer.DataFlavor) | |
227 | */ | |
228 | public synchronized Object getTransferData(DataFlavor parFlavor) | |
229 | throws UnsupportedFlavorException { | |
230 | ||
231 | 0 | if (isDataFlavorSupported(parFlavor)) { |
232 | 0 | return (diagramImage); |
233 | } | |
234 | 0 | throw new UnsupportedFlavorException(DataFlavor.imageFlavor); |
235 | ||
236 | } | |
237 | } |