src/share/classes/com/sun/java/swing/plaf/gtk/GTKColorChooserPanel.java

Print this page


   1 /*
   2  * Copyright (c) 2002, 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.sun.java.swing.plaf.gtk;
  26 
  27 import java.awt.*;
  28 import java.awt.event.*;
  29 import java.awt.image.*;
  30 import javax.swing.*;
  31 import javax.swing.colorchooser.*;
  32 import javax.swing.event.*;
  33 import javax.swing.plaf.*;
  34 
  35 /**
  36  * A color chooser panel mimicking that of GTK's: a color wheel showing
  37  * hue and a triangle that varies saturation and brightness.
  38  *
  39  * @author Scott Violet
  40  */

  41 class GTKColorChooserPanel extends AbstractColorChooserPanel implements
  42               ChangeListener {
  43     private static final float PI_3 = (float)(Math.PI / 3);
  44 
  45     private ColorTriangle triangle;
  46     private JLabel lastLabel;
  47     private JLabel label;
  48 
  49     private JSpinner hueSpinner;
  50     private JSpinner saturationSpinner;
  51     private JSpinner valueSpinner;
  52 
  53     private JSpinner redSpinner;
  54     private JSpinner greenSpinner;
  55     private JSpinner blueSpinner;
  56 
  57     private JTextField colorNameTF;
  58 
  59     private boolean settingColor;
  60 


 518      * Indicates the triangle is being dragged.
 519      */
 520     private static final int FLAGS_DRAGGING_TRIANGLE = 1 << 2;
 521     /**
 522      * Indicates a color is being set and we should ignore setColor
 523      */
 524     private static final int FLAGS_SETTING_COLOR = 1 << 3;
 525     /**
 526      * Indicates the wheel has focus.
 527      */
 528     private static final int FLAGS_FOCUSED_WHEEL = 1 << 4;
 529     /**
 530      * Indicates the triangle has focus.
 531      */
 532     private static final int FLAGS_FOCUSED_TRIANGLE = 1 << 5;
 533 
 534 
 535     /**
 536      * Class responsible for rendering a color wheel and color triangle.
 537      */

 538     private class ColorTriangle extends JPanel {
 539         /**
 540          * Cached image of the wheel.
 541          */
 542         private Image wheelImage;
 543 
 544         /**
 545          * Cached image of the triangle.
 546          */
 547         private Image triangleImage;
 548 
 549         /**
 550          * Angle triangle is rotated by.
 551          */
 552         private double angle;
 553 
 554         /**
 555          * Boolean bitmask.
 556          */
 557         private int flags;


1212             if (positive) {
1213                 hue += 1.0f / 360.0f;
1214             }
1215             else {
1216                 hue -= 1.0f / 360.0f;
1217             }
1218             if (hue > 1) {
1219                 hue -= 1;
1220             }
1221             else if (hue < 0) {
1222                 hue += 1;
1223             }
1224             getGTKColorChooserPanel().setHue(hue, true);
1225         }
1226     }
1227 
1228 
1229     /**
1230      * Action class used for colors.
1231      */

1232     private static class ColorAction extends AbstractAction {
1233         private int type;
1234 
1235         ColorAction(String name, int type) {
1236             super(name);
1237             this.type = type;
1238         }
1239 
1240         public void actionPerformed(ActionEvent e) {
1241             ColorTriangle triangle = (ColorTriangle)e.getSource();
1242 
1243             if (triangle.isWheelFocused()) {
1244                 float hue = triangle.getGTKColorChooserPanel().getHue();
1245 
1246                 switch (type) {
1247                 case 0:
1248                 case 2:
1249                     triangle.incrementHue(true);
1250                     break;
1251                 case 1:


1277                     // left
1278                     xDelta--;
1279                     break;
1280                 case 3:
1281                     // right
1282                     xDelta++;
1283                     break;
1284                 case 4:
1285                     compositeRequestFocus(triangle, true);
1286                     return;
1287                 case 5:
1288                     triangle.focusWheel();
1289                     return;
1290                 }
1291                 triangle.adjustSB(triangle.getColorX() + xDelta,
1292                                   triangle.getColorY() + yDelta, true);
1293             }
1294         }
1295     }
1296 
1297 
1298     private class OpaqueLabel extends JLabel {
1299         public boolean isOpaque() {
1300             return true;
1301         }
1302     }
1303 }
   1 /*
   2  * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.sun.java.swing.plaf.gtk;
  26 
  27 import java.awt.*;
  28 import java.awt.event.*;
  29 import java.awt.image.*;
  30 import javax.swing.*;
  31 import javax.swing.colorchooser.*;
  32 import javax.swing.event.*;
  33 import javax.swing.plaf.*;
  34 
  35 /**
  36  * A color chooser panel mimicking that of GTK's: a color wheel showing
  37  * hue and a triangle that varies saturation and brightness.
  38  *
  39  * @author Scott Violet
  40  */
  41 @SuppressWarnings("serial") // Superclass is not serializable across versions
  42 class GTKColorChooserPanel extends AbstractColorChooserPanel implements
  43               ChangeListener {
  44     private static final float PI_3 = (float)(Math.PI / 3);
  45 
  46     private ColorTriangle triangle;
  47     private JLabel lastLabel;
  48     private JLabel label;
  49 
  50     private JSpinner hueSpinner;
  51     private JSpinner saturationSpinner;
  52     private JSpinner valueSpinner;
  53 
  54     private JSpinner redSpinner;
  55     private JSpinner greenSpinner;
  56     private JSpinner blueSpinner;
  57 
  58     private JTextField colorNameTF;
  59 
  60     private boolean settingColor;
  61 


 519      * Indicates the triangle is being dragged.
 520      */
 521     private static final int FLAGS_DRAGGING_TRIANGLE = 1 << 2;
 522     /**
 523      * Indicates a color is being set and we should ignore setColor
 524      */
 525     private static final int FLAGS_SETTING_COLOR = 1 << 3;
 526     /**
 527      * Indicates the wheel has focus.
 528      */
 529     private static final int FLAGS_FOCUSED_WHEEL = 1 << 4;
 530     /**
 531      * Indicates the triangle has focus.
 532      */
 533     private static final int FLAGS_FOCUSED_TRIANGLE = 1 << 5;
 534 
 535 
 536     /**
 537      * Class responsible for rendering a color wheel and color triangle.
 538      */
 539     @SuppressWarnings("serial") // Superclass is not serializable across versions
 540     private class ColorTriangle extends JPanel {
 541         /**
 542          * Cached image of the wheel.
 543          */
 544         private Image wheelImage;
 545 
 546         /**
 547          * Cached image of the triangle.
 548          */
 549         private Image triangleImage;
 550 
 551         /**
 552          * Angle triangle is rotated by.
 553          */
 554         private double angle;
 555 
 556         /**
 557          * Boolean bitmask.
 558          */
 559         private int flags;


1214             if (positive) {
1215                 hue += 1.0f / 360.0f;
1216             }
1217             else {
1218                 hue -= 1.0f / 360.0f;
1219             }
1220             if (hue > 1) {
1221                 hue -= 1;
1222             }
1223             else if (hue < 0) {
1224                 hue += 1;
1225             }
1226             getGTKColorChooserPanel().setHue(hue, true);
1227         }
1228     }
1229 
1230 
1231     /**
1232      * Action class used for colors.
1233      */
1234     @SuppressWarnings("serial") // Superclass is not serializable across versions
1235     private static class ColorAction extends AbstractAction {
1236         private int type;
1237 
1238         ColorAction(String name, int type) {
1239             super(name);
1240             this.type = type;
1241         }
1242 
1243         public void actionPerformed(ActionEvent e) {
1244             ColorTriangle triangle = (ColorTriangle)e.getSource();
1245 
1246             if (triangle.isWheelFocused()) {
1247                 float hue = triangle.getGTKColorChooserPanel().getHue();
1248 
1249                 switch (type) {
1250                 case 0:
1251                 case 2:
1252                     triangle.incrementHue(true);
1253                     break;
1254                 case 1:


1280                     // left
1281                     xDelta--;
1282                     break;
1283                 case 3:
1284                     // right
1285                     xDelta++;
1286                     break;
1287                 case 4:
1288                     compositeRequestFocus(triangle, true);
1289                     return;
1290                 case 5:
1291                     triangle.focusWheel();
1292                     return;
1293                 }
1294                 triangle.adjustSB(triangle.getColorX() + xDelta,
1295                                   triangle.getColorY() + yDelta, true);
1296             }
1297         }
1298     }
1299 
1300     @SuppressWarnings("serial") // Superclass is not serializable across versions
1301     private class OpaqueLabel extends JLabel {
1302         public boolean isOpaque() {
1303             return true;
1304         }
1305     }
1306 }