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

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/internal/contrib/randelshofer/quaqua/colorchooser/Quaqua15ColorPicker.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
 * @(#)Quaqua15ColorPicker.java  1.2  2006-04-23
 
3
 *
 
4
 * Copyright (c) 2005-2006 Werner Randelshofer
 
5
 * Staldenmattweg 2, Immensee, CH-6405, Switzerland.
 
6
 * All rights reserved.
 
7
 *
 
8
 * This software is the confidential and proprietary information of
 
9
 * Werner Randelshofer. ("Confidential Information").  You shall not
 
10
 * disclose such Confidential Information and shall use it only in
 
11
 * accordance with the terms of the license agreement you entered into
 
12
 * with Werner Randelshofer.
 
13
 */
 
14
 
 
15
package org.pushingpixels.substance.internal.contrib.randelshofer.quaqua.colorchooser;
 
16
 
 
17
import java.awt.*;
 
18
import java.awt.event.*;
 
19
import java.awt.image.BufferedImage;
 
20
import java.security.AccessControlException;
 
21
 
 
22
import javax.swing.*;
 
23
import javax.swing.colorchooser.AbstractColorChooserPanel;
 
24
import javax.swing.plaf.IconUIResource;
 
25
 
 
26
import org.pushingpixels.substance.api.SubstanceColorScheme;
 
27
import org.pushingpixels.substance.internal.utils.SubstanceImageCreator;
 
28
import org.pushingpixels.substance.internal.utils.icon.TransitionAwareIcon;
 
29
 
 
30
/**
 
31
 * Quaqua15ColorPicker.
 
32
 * 
 
33
 * 
 
34
 * 
 
35
 * @author Werner Randelshofer
 
36
 * @version 1.2 2006-04-23 Retrieve labels from UIManager. <br>
 
37
 *          1.1.1 2006-03-15 Forgot to create robot instance. <br>
 
38
 *          1.1 2006-03-06 Abort picker when the user presses the Escape-Key. <br>
 
39
 *          1.0 December 18, 2005 Created.
 
40
 */
 
41
public class Quaqua15ColorPicker extends AbstractColorChooserPanel {
 
42
        /**
 
43
         * This frame is constantly moved to the current location of the mouse. This
 
44
         * ensures that we can trap mouse clicks while the picker cursor is showing.
 
45
         * Also, by tying the picker cursor to this frame, we ensure that the picker
 
46
         * cursor (the magnifying glass) is shown.
 
47
         */
 
48
        private Dialog pickerFrame;
 
49
 
 
50
        private Timer pickerTimer;
 
51
 
 
52
        /**
 
53
         * Holds the image of the picker cursor.
 
54
         */
 
55
        private BufferedImage cursorImage;
 
56
        /**
 
57
         * Graphics object used for drawing on the cursorImage.
 
58
         */
 
59
        private Graphics2D cursorGraphics;
 
60
 
 
61
        /**
 
62
         * The picker cursor.
 
63
         */
 
64
        private Cursor pickerCursor;
 
65
 
 
66
        /**
 
67
         * The hot spot of the cursor.
 
68
         */
 
69
        private Point hotSpot;
 
70
 
 
71
        /**
 
72
         * Offset from the hot spot of the pickerCursor to the pixel that we want to
 
73
         * pick. We can't pick the color at the hotSpot of the cursor, because this
 
74
         * point is obscured by the pickerFrame.
 
75
         */
 
76
        private Point pickOffset;
 
77
 
 
78
        /**
 
79
         * The magnifying glass image.
 
80
         */
 
81
        private BufferedImage magnifierImage;
 
82
 
 
83
        /**
 
84
         * The robot is used for creating screen captures.
 
85
         */
 
86
        private Robot robot;
 
87
 
 
88
        private Color previousColor = Color.white;
 
89
        private Point previousLoc = new Point();
 
90
        private Point pickLoc = new Point();
 
91
        private Point captureOffset = new Point();
 
92
        private Rectangle captureRect;
 
93
        private final static Color transparentColor = new Color(0, true);
 
94
        private Rectangle zoomRect;
 
95
        private Rectangle glassRect;
 
96
 
 
97
        /**
 
98
         * Creates a new instance.
 
99
         */
 
100
        public Quaqua15ColorPicker() {
 
101
                // Try immediately to create a screen capture in order to fail quickly,
 
102
                // when
 
103
                // we can't provide a color picker functionality.
 
104
                try {
 
105
                        robot = new Robot();
 
106
                        robot.createScreenCapture(new Rectangle(0, 0, 1, 1));
 
107
                } catch (AWTException e) {
 
108
                        throw new AccessControlException("Unable to capture screen");
 
109
                }
 
110
 
 
111
        }
 
112
 
 
113
        /**
 
114
         * Gets the picker frame. If the frame does not yet exist, it is created
 
115
         * along with all the other objects that are needed to make the picker work.
 
116
         */
 
117
        private Dialog getPickerFrame() {
 
118
                if (pickerFrame == null) {
 
119
                        Window owner = SwingUtilities.getWindowAncestor(this);
 
120
                        if (owner instanceof Dialog) {
 
121
                                pickerFrame = new Dialog((Dialog) owner);
 
122
                        } else if (owner instanceof Frame) {
 
123
                                pickerFrame = new Dialog((Frame) owner);
 
124
                        } else {
 
125
                                pickerFrame = new Dialog(new JFrame());
 
126
                        }
 
127
 
 
128
                        pickerFrame.addMouseListener(new MouseAdapter() {
 
129
                                @Override
 
130
                                public void mousePressed(MouseEvent evt) {
 
131
                                        pickFinish();
 
132
                                }
 
133
 
 
134
                                @Override
 
135
                                public void mouseExited(MouseEvent evt) {
 
136
                                        updatePicker();
 
137
                                }
 
138
                        });
 
139
 
 
140
                        pickerFrame.addMouseMotionListener(new MouseMotionAdapter() {
 
141
                                @Override
 
142
                                public void mouseMoved(MouseEvent evt) {
 
143
                                        updatePicker();
 
144
                                }
 
145
                        });
 
146
                        pickerFrame.setSize(3, 3);
 
147
                        pickerFrame.setUndecorated(true);
 
148
                        pickerFrame.setAlwaysOnTop(true);
 
149
 
 
150
                        pickerFrame.addKeyListener(new KeyAdapter() {
 
151
                                @Override
 
152
                                public void keyPressed(KeyEvent e) {
 
153
                                        switch (e.getKeyCode()) {
 
154
                                        case KeyEvent.VK_ESCAPE:
 
155
                                                pickCancel();
 
156
                                                break;
 
157
                                        case KeyEvent.VK_ENTER:
 
158
                                                pickFinish();
 
159
                                                break;
 
160
                                        }
 
161
                                }
 
162
                        });
 
163
 
 
164
                        magnifierImage = (BufferedImage) UIManager
 
165
                                        .get("ColorChooser.colorPickerMagnifier");
 
166
                        glassRect = (Rectangle) UIManager
 
167
                                        .get("ColorChooser.colorPickerGlassRect");
 
168
                        zoomRect = (Rectangle) UIManager
 
169
                                        .get("ColorChooser.colorPickerZoomRect");
 
170
                        hotSpot = (Point) UIManager.get("ColorChooser.colorPickerHotSpot");// new
 
171
                        // Point(29,
 
172
                        // 29);
 
173
                        captureRect = new Rectangle((Rectangle) UIManager
 
174
                                        .get("ColorChooser.colorPickerCaptureRect"));
 
175
                        pickOffset = (Point) UIManager
 
176
                                        .get("ColorChooser.colorPickerPickOffset");
 
177
                        captureOffset = new Point(captureRect.x, captureRect.y);
 
178
                        cursorImage = getGraphicsConfiguration().createCompatibleImage(
 
179
                                        magnifierImage.getWidth(), magnifierImage.getHeight(),
 
180
                                        Transparency.TRANSLUCENT);
 
181
                        cursorGraphics = cursorImage.createGraphics();
 
182
                        cursorGraphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
 
183
                                        RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
 
184
 
 
185
                        pickerTimer = new Timer(5, new ActionListener() {
 
186
                                @Override
 
187
                public void actionPerformed(ActionEvent evt) {
 
188
                                        updatePicker();
 
189
                                }
 
190
                        });
 
191
                }
 
192
                return pickerFrame;
 
193
        }
 
194
 
 
195
        /**
 
196
         * Updates the color picker.
 
197
         */
 
198
        protected void updatePicker() {
 
199
                if (pickerFrame != null && pickerFrame.isShowing()) {
 
200
                        PointerInfo info = MouseInfo.getPointerInfo();
 
201
            if (info == null) return;  //pointer not on a graphics device
 
202
                        Point mouseLoc = info.getLocation();
 
203
                        pickerFrame.setLocation(mouseLoc.x - pickerFrame.getWidth() / 2,
 
204
                                        mouseLoc.y - pickerFrame.getHeight() / 2);
 
205
 
 
206
                        pickLoc.x = mouseLoc.x + pickOffset.x;
 
207
                        pickLoc.y = mouseLoc.y + pickOffset.y;
 
208
 
 
209
                        if (pickLoc.x >= 0 && pickLoc.y >= 0) {
 
210
                                Color c = robot.getPixelColor(pickLoc.x, pickLoc.y);
 
211
                                if (!c.equals(previousColor) || !mouseLoc.equals(previousLoc)) {
 
212
                                        previousColor = c;
 
213
                                        previousLoc = mouseLoc;
 
214
 
 
215
                                        captureRect.setLocation(mouseLoc.x + captureOffset.x,
 
216
                                                        mouseLoc.y + captureOffset.y);
 
217
                                        if (captureRect.x >= 0 && captureRect.y >= 0) {
 
218
                                                BufferedImage capture = robot
 
219
                                                                .createScreenCapture(captureRect);
 
220
 
 
221
                                                // Clear the cursor graphics
 
222
                                                cursorGraphics.setComposite(AlphaComposite.Src);
 
223
                                                cursorGraphics.setColor(transparentColor);
 
224
                                                cursorGraphics.fillRect(0, 0, cursorImage.getWidth(),
 
225
                                                                cursorImage.getHeight());
 
226
 
 
227
                                                // Fill the area for the zoomed screen capture with a
 
228
                                                // non-transparent color (any non-transparent color does
 
229
                                                // the job).
 
230
                                                cursorGraphics.setColor(Color.red);
 
231
                                                cursorGraphics.fillOval(glassRect.x, glassRect.y,
 
232
                                                                glassRect.width, glassRect.height);
 
233
 
 
234
                                                // Paint the screen capture with a zoom factor of 5
 
235
                                                cursorGraphics.setComposite(AlphaComposite.SrcIn);
 
236
                                                cursorGraphics.drawImage(capture, zoomRect.x,
 
237
                                                                zoomRect.y, zoomRect.width, zoomRect.height,
 
238
                                                                this);
 
239
 
 
240
                                                // Draw the magnifying glass image
 
241
                                                cursorGraphics.setComposite(AlphaComposite.SrcOver);
 
242
                                                cursorGraphics.drawImage(magnifierImage, 0, 0, this);
 
243
 
 
244
                                                // We need to create a new subImage. This forces that
 
245
                                                // the color picker uses the new imagery.
 
246
                                                BufferedImage subImage = cursorImage
 
247
                                                                .getSubimage(0, 0, cursorImage.getWidth(),
 
248
                                                                                cursorImage.getHeight());
 
249
                                                pickerFrame.setCursor(getToolkit().createCustomCursor(
 
250
                                                                cursorImage, hotSpot, "ColorPicker"));
 
251
                                        }
 
252
                                }
 
253
                        }
 
254
                }
 
255
        }
 
256
 
 
257
        /**
 
258
         * This method is called from within the constructor to initialize the form.
 
259
         * WARNING: Do NOT modify this code. The content of this method is always
 
260
         * regenerated by the Form Editor.
 
261
         */
 
262
        private void initComponents() {// GEN-BEGIN:initComponents
 
263
                pickerButton = new javax.swing.JButton();
 
264
 
 
265
                setLayout(new java.awt.BorderLayout());
 
266
 
 
267
                // pickerButton.setBorderPainted(false);
 
268
                pickerButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
 
269
                pickerButton.addActionListener(new java.awt.event.ActionListener() {
 
270
                        @Override
 
271
            public void actionPerformed(java.awt.event.ActionEvent evt) {
 
272
                                pickBegin(evt);
 
273
                        }
 
274
                });
 
275
 
 
276
                add(pickerButton, java.awt.BorderLayout.CENTER);
 
277
 
 
278
        }// GEN-END:initComponents
 
279
 
 
280
        private void pickBegin(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_pickBegin
 
281
                getPickerFrame();
 
282
                pickerTimer.start();
 
283
                getPickerFrame().setVisible(true);
 
284
        }// GEN-LAST:event_pickBegin
 
285
 
 
286
        protected void pickFinish() {
 
287
                pickerTimer.stop();
 
288
                pickerFrame.setVisible(false);
 
289
                PointerInfo info = MouseInfo.getPointerInfo();
 
290
        if (info == null) return;  //pointer not on a graphics device
 
291
                Point loc = info.getLocation();
 
292
                Color c = robot.getPixelColor(loc.x + pickOffset.x, loc.y
 
293
                                + pickOffset.y);
 
294
                getColorSelectionModel().setSelectedColor(c);
 
295
        }
 
296
 
 
297
        protected void pickCancel() {
 
298
                pickerTimer.stop();
 
299
                pickerFrame.setVisible(false);
 
300
        }
 
301
 
 
302
        @Override
 
303
        protected void buildChooser() {
 
304
                initComponents();
 
305
                pickerButton.setIcon(new TransitionAwareIcon(pickerButton,
 
306
                                new TransitionAwareIcon.Delegate() {
 
307
                                        @Override
 
308
                                        public Icon getColorSchemeIcon(SubstanceColorScheme scheme) {
 
309
                                                return new IconUIResource(SubstanceImageCreator
 
310
                                                                .getSearchIcon(15, scheme, pickerButton
 
311
                                                                                .getComponentOrientation()
 
312
                                                                                .isLeftToRight()));
 
313
                                        }
 
314
                                }, "ColorChooser.colorPickerIcon"));
 
315
        }
 
316
 
 
317
        @Override
 
318
        public String getDisplayName() {
 
319
                return UIManager.getString("ColorChooser.colorPicker");
 
320
        }
 
321
 
 
322
        @Override
 
323
        public Icon getLargeDisplayIcon() {
 
324
                return UIManager.getIcon("ColorChooser.colorPickerIcon");
 
325
        }
 
326
 
 
327
        @Override
 
328
        public Icon getSmallDisplayIcon() {
 
329
                return getLargeDisplayIcon();
 
330
        }
 
331
 
 
332
        @Override
 
333
        public void updateChooser() {
 
334
        }
 
335
 
 
336
        // Variables declaration - do not modify//GEN-BEGIN:variables
 
337
        private javax.swing.JButton pickerButton;
 
338
        // End of variables declaration//GEN-END:variables
 
339
 
 
340
}