~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to substance/src/tools/java/tools/common/JImageComponent.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2005-2010 Substance Kirill Grouchnikov. All Rights Reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without 
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 * 
 
7
 *  o Redistributions of source code must retain the above copyright notice, 
 
8
 *    this list of conditions and the following disclaimer. 
 
9
 *     
 
10
 *  o Redistributions in binary form must reproduce the above copyright notice, 
 
11
 *    this list of conditions and the following disclaimer in the documentation 
 
12
 *    and/or other materials provided with the distribution. 
 
13
 *     
 
14
 *  o Neither the name of Substance Kirill Grouchnikov nor the names of 
 
15
 *    its contributors may be used to endorse or promote products derived 
 
16
 *    from this software without specific prior written permission. 
 
17
 *     
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 
19
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 
20
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 
21
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 
22
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 
23
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 
24
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 
25
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 
26
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 
27
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 
28
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 
29
 */
 
30
package tools.common;
 
31
 
 
32
import java.awt.*;
 
33
import java.awt.datatransfer.*;
 
34
import java.awt.event.*;
 
35
import java.awt.image.BufferedImage;
 
36
import java.io.File;
 
37
import java.net.URL;
 
38
 
 
39
import javax.imageio.ImageIO;
 
40
import javax.swing.*;
 
41
 
 
42
import org.pushingpixels.lafwidget.utils.RenderingUtils;
 
43
import org.pushingpixels.substance.internal.utils.border.SubstanceBorder;
 
44
 
 
45
public class JImageComponent extends JPanel {
 
46
        private BufferedImage image;
 
47
 
 
48
        private double leftX;
 
49
 
 
50
        private double topY;
 
51
 
 
52
        private double zoom;
 
53
 
 
54
        private boolean isDragging;
 
55
 
 
56
        private Point lastDragPoint;
 
57
 
 
58
        private Color selectedColor;
 
59
 
 
60
        private Color rolloverColor;
 
61
 
 
62
        private File originalFile;
 
63
 
 
64
        private String[] legend;
 
65
 
 
66
        public JImageComponent(boolean hasKeyboardZoom) {
 
67
                this.setTransferHandler(new TransferHandler() {
 
68
                        @Override
 
69
                        public boolean canImport(TransferSupport support) {
 
70
                                // can import a list of files
 
71
                                if (support
 
72
                                                .isDataFlavorSupported(DataFlavor.javaFileListFlavor))
 
73
                                        return true;
 
74
                                // an image
 
75
                                if (support.isDataFlavorSupported(DataFlavor.imageFlavor))
 
76
                                        return true;
 
77
                                for (DataFlavor df : support.getDataFlavors()) {
 
78
                                        // and a flavor represented by URL
 
79
                                        if (df.getRepresentationClass() == URL.class)
 
80
                                                return true;
 
81
                                }
 
82
                                return false;
 
83
                        }
 
84
 
 
85
                        @Override
 
86
                        public boolean importData(TransferSupport support) {
 
87
                                Transferable t = support.getTransferable();
 
88
 
 
89
                                try {
 
90
                                        if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
 
91
                                                // load the image
 
92
                                                Image data = (Image) t
 
93
                                                                .getTransferData(DataFlavor.imageFlavor);
 
94
                                                originalFile = null;
 
95
                                                BufferedImage old = image;
 
96
                                                image = new BufferedImage(data.getWidth(null), data
 
97
                                                                .getHeight(null), BufferedImage.TYPE_INT_ARGB);
 
98
                                                image.getGraphics().drawImage(data, 0, 0, null);
 
99
                                                reset();
 
100
                                                repaint();
 
101
 
 
102
                                                firePropertyChange("image", old, image);
 
103
 
 
104
                                                return true;
 
105
                                        }
 
106
 
 
107
                                        if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
 
108
                                                // load the image from the file
 
109
                                                java.util.List<File> l = (java.util.List<File>) t
 
110
                                                                .getTransferData(DataFlavor.javaFileListFlavor);
 
111
 
 
112
                                                if (l.size() == 1) {
 
113
                                                        BufferedImage oldImage = image;
 
114
                                                        File oldFile = originalFile;
 
115
                                                        originalFile = l.get(0);
 
116
                                                        image = ImageIO.read(originalFile);
 
117
                                                        reset();
 
118
                                                        repaint();
 
119
 
 
120
                                                        firePropertyChange("image", oldImage, image);
 
121
                                                        firePropertyChange("file", oldFile, originalFile);
 
122
                                                }
 
123
                                                return true;
 
124
                                        }
 
125
 
 
126
                                        for (DataFlavor df : support.getDataFlavors()) {
 
127
                                                if (df.getRepresentationClass() == URL.class) {
 
128
                                                        // load the image from the URL
 
129
                                                        URL url = (URL) t.getTransferData(df);
 
130
                                                        originalFile = null;
 
131
                                                        Image data = ImageIO.read(url);
 
132
                                                        if (data != null) {
 
133
                                                                BufferedImage old = image;
 
134
                                                                image = new BufferedImage(data.getWidth(null),
 
135
                                                                                data.getHeight(null),
 
136
                                                                                BufferedImage.TYPE_INT_ARGB);
 
137
                                                                image.getGraphics().drawImage(data, 0, 0, null);
 
138
                                                                reset();
 
139
                                                                repaint();
 
140
 
 
141
                                                                firePropertyChange("image", old, image);
 
142
                                                                return true;
 
143
                                                        }
 
144
                                                }
 
145
                                        }
 
146
                                        return true;
 
147
                                } catch (Throwable thr) {
 
148
                                        thr.printStackTrace();
 
149
                                        return false;
 
150
                                }
 
151
                        }
 
152
                });
 
153
 
 
154
                this.addMouseListener(new MouseAdapter() {
 
155
                        @Override
 
156
                        public void mousePressed(MouseEvent e) {
 
157
                                if (image == null)
 
158
                                        return;
 
159
 
 
160
                                if (!e.isPopupTrigger()) {
 
161
                                        lastDragPoint = e.getPoint();
 
162
                                } else {
 
163
                                        processPopup(e);
 
164
                                }
 
165
                        }
 
166
 
 
167
                        @Override
 
168
                        public void mouseReleased(MouseEvent e) {
 
169
                                if (e.isPopupTrigger()) {
 
170
                                        processPopup(e);
 
171
                                } else {
 
172
                                        if (image == null)
 
173
                                                return;
 
174
 
 
175
                                        if (!isDragging) {
 
176
                                                int xRel = e.getX();
 
177
                                                int yRel = e.getY();
 
178
                                                int xAbs = (int) ((xRel / zoom) + leftX);
 
179
                                                int yAbs = (int) ((yRel / zoom) + topY);
 
180
 
 
181
                                                if ((xAbs >= 0) && (xAbs < image.getWidth())
 
182
                                                                && (yAbs >= 0) && (yAbs < image.getHeight())) {
 
183
                                                        selectedColor = new Color(image.getRGB(xAbs, yAbs));
 
184
                                                        firePropertyChange("selectedColor", null,
 
185
                                                                        selectedColor);
 
186
                                                }
 
187
                                        }
 
188
                                        isDragging = false;
 
189
                                }
 
190
                        }
 
191
 
 
192
                        private void processPopup(MouseEvent e) {
 
193
                                JPopupMenu editMenu = new JPopupMenu();
 
194
                                editMenu.add(new AbstractAction("paste from clipboard") {
 
195
                                        @Override
 
196
                                        public void actionPerformed(ActionEvent e) {
 
197
                                                try {
 
198
                                                        Clipboard clipboard = Toolkit.getDefaultToolkit()
 
199
                                                                        .getSystemClipboard();
 
200
                                                        DataFlavor[] flavors = clipboard
 
201
                                                                        .getAvailableDataFlavors();
 
202
                                                        if (flavors != null) {
 
203
                                                                for (DataFlavor flavor : flavors) {
 
204
                                                                        if (Image.class == flavor
 
205
                                                                                        .getRepresentationClass()) {
 
206
                                                                                Image data = (Image) clipboard
 
207
                                                                                                .getData(flavor);
 
208
 
 
209
                                                                                BufferedImage old = image;
 
210
                                                                                originalFile = null;
 
211
                                                                                image = new BufferedImage(data
 
212
                                                                                                .getWidth(null), data
 
213
                                                                                                .getHeight(null),
 
214
                                                                                                BufferedImage.TYPE_INT_ARGB);
 
215
                                                                                image.getGraphics().drawImage(data, 0,
 
216
                                                                                                0, null);
 
217
                                                                                reset();
 
218
                                                                                repaint();
 
219
                                                                                firePropertyChange("image", old, image);
 
220
                                                                                break;
 
221
                                                                        }
 
222
                                                                }
 
223
                                                        }
 
224
                                                        repaint();
 
225
                                                } catch (Throwable ignored) {
 
226
                                                }
 
227
                                        }
 
228
 
 
229
                                        @Override
 
230
                                        public boolean isEnabled() {
 
231
                                                Clipboard clipboard = Toolkit.getDefaultToolkit()
 
232
                                                                .getSystemClipboard();
 
233
                                                DataFlavor[] flavors = clipboard
 
234
                                                                .getAvailableDataFlavors();
 
235
                                                if (flavors != null) {
 
236
                                                        for (DataFlavor flavor : flavors) {
 
237
                                                                if (Image.class == flavor
 
238
                                                                                .getRepresentationClass()) {
 
239
                                                                        return true;
 
240
                                                                }
 
241
                                                        }
 
242
                                                }
 
243
                                                return false;
 
244
                                        }
 
245
                                });
 
246
                                Point pt = SwingUtilities.convertPoint(e.getComponent(), e
 
247
                                                .getPoint(), JImageComponent.this);
 
248
                                editMenu.show(JImageComponent.this, pt.x, pt.y);
 
249
                        }
 
250
 
 
251
                });
 
252
 
 
253
                this.addMouseMotionListener(new MouseMotionAdapter() {
 
254
                        @Override
 
255
                        public void mouseDragged(MouseEvent e) {
 
256
                                if (image == null)
 
257
                                        return;
 
258
 
 
259
                                isDragging = true;
 
260
 
 
261
                                Point currDragPoint = e.getPoint();
 
262
                                double dx = ((currDragPoint.x - lastDragPoint.x) / zoom);
 
263
                                double dy = ((currDragPoint.y - lastDragPoint.y) / zoom);
 
264
                                leftX -= dx;
 
265
                                topY -= dy;
 
266
 
 
267
                                lastDragPoint = currDragPoint;
 
268
                                repaint();
 
269
                        }
 
270
 
 
271
                        @Override
 
272
                        public void mouseMoved(MouseEvent e) {
 
273
                                if (image == null)
 
274
                                        return;
 
275
 
 
276
                                int xRel = e.getX();
 
277
                                int yRel = e.getY();
 
278
                                int xAbs = (int) ((xRel / zoom) + leftX);
 
279
                                int yAbs = (int) ((yRel / zoom) + topY);
 
280
 
 
281
                                // System.out.println(xRel + ":" + yRel + "->" + xAbs + ":"
 
282
                                // + yAbs);
 
283
 
 
284
                                if ((xAbs >= 0) && (xAbs < image.getWidth()) && (yAbs >= 0)
 
285
                                                && (yAbs < image.getHeight())) {
 
286
                                        Color old = rolloverColor;
 
287
                                        rolloverColor = new Color(image.getRGB(xAbs, yAbs));
 
288
                                        firePropertyChange("rolloverColor", old, rolloverColor);
 
289
                                }
 
290
                        }
 
291
                });
 
292
 
 
293
                this.addMouseWheelListener(new MouseWheelListener() {
 
294
                        @Override
 
295
                        public void mouseWheelMoved(MouseWheelEvent e) {
 
296
                                zoom += e.getScrollAmount() * e.getWheelRotation() / 10.0;
 
297
                                zoom = Math.max(1.0, zoom);
 
298
                                repaint();
 
299
                        }
 
300
                });
 
301
 
 
302
                if (hasKeyboardZoom) {
 
303
                        Action zoomInAction = new AbstractAction("zoomin") {
 
304
                                @Override
 
305
                                public void actionPerformed(ActionEvent e) {
 
306
                                        zoom += 0.1;
 
307
                                        repaint();
 
308
                                }
 
309
                        };
 
310
                        Action zoomOutAction = new AbstractAction("zoomout") {
 
311
                                @Override
 
312
                                public void actionPerformed(ActionEvent e) {
 
313
                                        zoom -= 0.1;
 
314
                                        zoom = Math.max(1.0, zoom);
 
315
                                        repaint();
 
316
                                }
 
317
                        };
 
318
 
 
319
                        // create the key input maps to handle the zooming
 
320
                        // with I and O
 
321
                        InputMap inputMap = new ComponentInputMap(this);
 
322
                        inputMap.put(KeyStroke.getKeyStroke("I"), "zoomin");
 
323
                        inputMap.put(KeyStroke.getKeyStroke("O"), "zoomout");
 
324
 
 
325
                        ActionMap actionMap = new ActionMap();
 
326
                        actionMap.put("zoomin", zoomInAction);
 
327
                        actionMap.put("zoomout", zoomOutAction);
 
328
 
 
329
                        // and register the maps
 
330
                        this.setInputMap(WHEN_IN_FOCUSED_WINDOW, inputMap);
 
331
                        this.setActionMap(actionMap);
 
332
                }
 
333
 
 
334
                this.setBorder(new SubstanceBorder());
 
335
 
 
336
                this.zoom = 1.0;
 
337
        }
 
338
 
 
339
        public void setLegend(String[] legend) {
 
340
                this.legend = legend;
 
341
        }
 
342
 
 
343
        @Override
 
344
        protected void paintComponent(Graphics g) {
 
345
                Graphics2D g2d = (Graphics2D) g.create();
 
346
                g2d.setColor(Color.white);
 
347
                g2d.fillRect(0, 0, getWidth(), getHeight());
 
348
 
 
349
                if (this.image != null) {
 
350
                        // zoom from the visible top-left pixel
 
351
                        g2d.scale(zoom, zoom);
 
352
                        g2d.translate(-this.leftX, -this.topY);
 
353
                        g2d.drawImage(this.image, 0, 0, null);
 
354
                } else {
 
355
                        RenderingUtils.installDesktopHints(g2d, this);
 
356
                        g2d.setFont(UIManager.getFont("Label.font"));
 
357
                        g2d.setColor(Color.black);
 
358
 
 
359
                        int fh = g2d.getFontMetrics().getHeight();
 
360
                        if (this.legend != null) {
 
361
                                for (int i = 0; i < this.legend.length; i++) {
 
362
                                        g2d.drawString(this.legend[i], 10, 20 + i * fh);
 
363
                                }
 
364
                        }
 
365
                }
 
366
 
 
367
                g2d.dispose();
 
368
        }
 
369
 
 
370
        private void reset() {
 
371
                leftX = 0;
 
372
                topY = 0;
 
373
                zoom = 1.0;
 
374
        }
 
375
 
 
376
        public BufferedImage getImage() {
 
377
                return image;
 
378
        }
 
379
 
 
380
        public File getOriginalFile() {
 
381
                return originalFile;
 
382
        }
 
383
 
 
384
        public Point toOriginalImageCoords(int x, int y) {
 
385
                int xAbs = (int) ((x / zoom) + leftX);
 
386
                int yAbs = (int) ((y / zoom) + topY);
 
387
                return new Point(xAbs, yAbs);
 
388
        }
 
389
}
 
 
b'\\ No newline at end of file'