< prev index next >

src/java.desktop/share/classes/javax/swing/colorchooser/ColorPanel.java

Print this page


   1 /*
   2  * Copyright (c) 2008, 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 
  26 package javax.swing.colorchooser;
  27 
  28 import java.awt.Color;
  29 import java.awt.ContainerOrderFocusTraversalPolicy;
  30 import java.awt.GridBagConstraints;
  31 import java.awt.GridBagLayout;
  32 import java.awt.Insets;
  33 import java.awt.event.ActionEvent;
  34 import java.awt.event.ActionListener;
  35 import javax.swing.ButtonGroup;

  36 import javax.swing.JLabel;
  37 import javax.swing.JPanel;
  38 import javax.swing.JRadioButton;
  39 import javax.swing.border.EmptyBorder;
  40 import javax.swing.JSpinner.DefaultEditor;
  41 
  42 @SuppressWarnings("serial") // Superclass is not serializable across versions
  43 final class ColorPanel extends JPanel implements ActionListener {
  44 
  45     private final SlidingSpinner[] spinners = new SlidingSpinner[5];
  46     private final float[] values = new float[this.spinners.length];
  47 
  48     private final ColorModel model;
  49     private Color color;
  50     private int x = 1;
  51     private int y = 2;
  52     private int z;
  53 
  54     ColorPanel(ColorModel model) {
  55         super(new GridBagLayout());
  56 
  57         GridBagConstraints gbc = new GridBagConstraints();
  58         gbc.fill = GridBagConstraints.HORIZONTAL;
  59 
  60         gbc.gridx = 1;
  61         ButtonGroup group = new ButtonGroup();
  62         EmptyBorder border = null;
  63         for (int i = 0; i < this.spinners.length; i++) {
  64             if (i < 3) {
  65                 JRadioButton button = new JRadioButton();
  66                 if (i == 0) {
  67                     Insets insets = button.getInsets();
  68                     insets.left = button.getPreferredSize().width;
  69                     border = new EmptyBorder(insets);
  70                     button.setSelected(true);
  71                     gbc.insets.top = 5;
  72                 }
  73                 add(button, gbc);
  74                 group.add(button);
  75                 button.setActionCommand(Integer.toString(i));
  76                 button.addActionListener(this);
  77                 this.spinners[i] = new SlidingSpinner(this, button);
  78             }
  79             else {
  80                 JLabel label = new JLabel();
  81                 add(label, gbc);
  82                 label.setBorder(border);
  83                 label.setFocusable(false);
  84                 this.spinners[i] = new SlidingSpinner(this, label);
  85             }
  86         }
  87         gbc.gridx = 2;
  88         gbc.weightx = 1.0;
  89         gbc.insets.top = 0;
  90         gbc.insets.left = 5;
  91         for (SlidingSpinner spinner : this.spinners) {
  92             add(spinner.getSlider(), gbc);
  93             gbc.insets.top = 5;
  94         }
  95         gbc.gridx = 3;
  96         gbc.weightx = 0.0;
  97         gbc.insets.top = 0;
  98         for (SlidingSpinner spinner : this.spinners) {
  99             add(spinner.getSpinner(), gbc);
 100             gbc.insets.top = 5;
 101         }
 102         setFocusTraversalPolicy(new ContainerOrderFocusTraversalPolicy());
 103         setFocusTraversalPolicyProvider(true);
 104         setFocusable(false);
 105 
 106         this.model = model;
 107     }
 108 
 109     public void actionPerformed(ActionEvent event) {




 110         try {
 111             this.z = Integer.parseInt(event.getActionCommand());
 112             this.y = (this.z != 2) ? 2 : 1;
 113             this.x = (this.z != 0) ? 0 : 1;
 114             getParent().repaint();
 115         }
 116         catch (NumberFormatException exception) {
 117         }
 118     }


 119 
 120     void buildPanel() {
 121         int count = this.model.getCount();
 122         this.spinners[4].setVisible(count > 4);
 123         for (int i = 0; i < count; i++) {
 124             String text = this.model.getLabel(this, i);
 125             Object object = this.spinners[i].getLabel();
 126             if (object instanceof JRadioButton) {
 127                 JRadioButton button = (JRadioButton) object;
 128                 button.setText(text);
 129                 button.getAccessibleContext().setAccessibleDescription(text);
 130             }
 131             else if (object instanceof JLabel) {
 132                 JLabel label = (JLabel) object;
 133                 label.setText(text);
 134             }
 135             this.spinners[i].setRange(this.model.getMinimum(i), this.model.getMaximum(i));
 136             this.spinners[i].setValue(this.values[i]);
 137             this.spinners[i].getSlider().getAccessibleContext().setAccessibleName(text);
 138             this.spinners[i].getSpinner().getAccessibleContext().setAccessibleName(text);


   1 /*
   2  * Copyright (c) 2008, 2019, 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 
  26 package javax.swing.colorchooser;
  27 
  28 import java.awt.Color;
  29 import java.awt.ContainerOrderFocusTraversalPolicy;
  30 import java.awt.GridBagConstraints;
  31 import java.awt.GridBagLayout;
  32 import java.awt.Insets;
  33 import java.awt.event.ItemEvent;
  34 import java.awt.event.ItemListener;
  35 import javax.swing.ButtonGroup;
  36 import javax.swing.DefaultButtonModel;
  37 import javax.swing.JLabel;
  38 import javax.swing.JPanel;
  39 import javax.swing.JRadioButton;
  40 import javax.swing.border.EmptyBorder;
  41 import javax.swing.JSpinner.DefaultEditor;
  42 
  43 @SuppressWarnings("serial") // Superclass is not serializable across versions
  44 final class ColorPanel extends JPanel implements ItemListener {
  45 
  46     private final SlidingSpinner[] spinners = new SlidingSpinner[5];
  47     private final float[] values = new float[this.spinners.length];
  48 
  49     private final ColorModel model;
  50     private Color color;
  51     private int x = 1;
  52     private int y = 2;
  53     private int z;
  54 
  55     ColorPanel(ColorModel model) {
  56         super(new GridBagLayout());
  57 
  58         GridBagConstraints gbc = new GridBagConstraints();
  59         gbc.fill = GridBagConstraints.HORIZONTAL;
  60 
  61         gbc.gridx = 1;
  62         ButtonGroup group = new ButtonGroup();
  63         EmptyBorder border = null;
  64         for (int i = 0; i < this.spinners.length; i++) {
  65             if (i < 3) {
  66                 JRadioButton button = new JRadioButton();
  67                 if (i == 0) {
  68                     Insets insets = button.getInsets();
  69                     insets.left = button.getPreferredSize().width;
  70                     border = new EmptyBorder(insets);
  71                     button.setSelected(true);
  72                     gbc.insets.top = 5;
  73                 }
  74                 add(button, gbc);
  75                 group.add(button);
  76                 button.setActionCommand(Integer.toString(i));
  77                 button.getModel().addItemListener(this);
  78                 this.spinners[i] = new SlidingSpinner(this, button);
  79             }
  80             else {
  81                 JLabel label = new JLabel();
  82                 add(label, gbc);
  83                 label.setBorder(border);
  84                 label.setFocusable(false);
  85                 this.spinners[i] = new SlidingSpinner(this, label);
  86             }
  87         }
  88         gbc.gridx = 2;
  89         gbc.weightx = 1.0;
  90         gbc.insets.top = 0;
  91         gbc.insets.left = 5;
  92         for (SlidingSpinner spinner : this.spinners) {
  93             add(spinner.getSlider(), gbc);
  94             gbc.insets.top = 5;
  95         }
  96         gbc.gridx = 3;
  97         gbc.weightx = 0.0;
  98         gbc.insets.top = 0;
  99         for (SlidingSpinner spinner : this.spinners) {
 100             add(spinner.getSpinner(), gbc);
 101             gbc.insets.top = 5;
 102         }
 103         setFocusTraversalPolicy(new ContainerOrderFocusTraversalPolicy());
 104         setFocusTraversalPolicyProvider(true);
 105         setFocusable(false);
 106 
 107         this.model = model;
 108     }
 109 
 110     @Override
 111     public void itemStateChanged(ItemEvent e) {
 112         if(e.getStateChange() == ItemEvent.SELECTED) {
 113             if (e.getSource() instanceof DefaultButtonModel) {
 114                 DefaultButtonModel model = (DefaultButtonModel) e.getSource();
 115                 try {
 116                     this.z = Integer.parseInt(model.getActionCommand());
 117                     this.y = (this.z != 2) ? 2 : 1;
 118                     this.x = (this.z != 0) ? 0 : 1;
 119                     getParent().repaint();
 120                 }
 121                 catch (NumberFormatException exception) {
 122                 }
 123             }
 124         }
 125     }
 126 
 127     void buildPanel() {
 128         int count = this.model.getCount();
 129         this.spinners[4].setVisible(count > 4);
 130         for (int i = 0; i < count; i++) {
 131             String text = this.model.getLabel(this, i);
 132             Object object = this.spinners[i].getLabel();
 133             if (object instanceof JRadioButton) {
 134                 JRadioButton button = (JRadioButton) object;
 135                 button.setText(text);
 136                 button.getAccessibleContext().setAccessibleDescription(text);
 137             }
 138             else if (object instanceof JLabel) {
 139                 JLabel label = (JLabel) object;
 140                 label.setText(text);
 141             }
 142             this.spinners[i].setRange(this.model.getMinimum(i), this.model.getMaximum(i));
 143             this.spinners[i].setValue(this.values[i]);
 144             this.spinners[i].getSlider().getAccessibleContext().setAccessibleName(text);
 145             this.spinners[i].getSpinner().getAccessibleContext().setAccessibleName(text);


< prev index next >