jdk/src/share/classes/javax/swing/plaf/basic/BasicColorChooserUI.java
author darcy
Sun, 23 Mar 2014 13:49:48 -0700
changeset 23697 e556a715949f
parent 23010 6dadb192ad81
child 23715 54ae9dd9df73
permissions -rw-r--r--
8034169: Fix serial lint warnings in javax.swing Reviewed-by: alanb, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1282
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1282
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1282
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1282
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1282
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.colorchooser.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.swing.DefaultLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Provides the basic look and feel for a JColorChooser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Tom Santos
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Steve Wilson
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class BasicColorChooserUI extends ColorChooserUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * JColorChooser this BasicColorChooserUI is installed on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    protected JColorChooser chooser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    JTabbedPane tabbedPane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    JPanel singlePanel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    JPanel previewPanelHolder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    JComponent previewPanel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    boolean isMultiPanel = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static TransferHandler defaultTransferHandler = new ColorTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    protected AbstractColorChooserPanel[] defaultChoosers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    protected ChangeListener previewListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected PropertyChangeListener propertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private Handler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return new BasicColorChooserUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    protected AbstractColorChooserPanel[] createDefaultChoosers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        AbstractColorChooserPanel[] panels = ColorChooserComponentFactory.getDefaultChooserPanels();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        return panels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    protected void uninstallDefaultChoosers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        AbstractColorChooserPanel[] choosers = chooser.getChooserPanels();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        for( int i = 0 ; i < choosers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            chooser.removeChooserPanel( choosers[i] );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public void installUI( JComponent c ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        chooser = (JColorChooser)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        super.installUI( c );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        installDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        tabbedPane = new JTabbedPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        tabbedPane.setName("ColorChooser.tabPane");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        tabbedPane.setInheritsPopupMenu(true);
14658
67661687f722 4631925: JColor Chooser is not fully accessible
jviswana
parents: 9230
diff changeset
    97
        tabbedPane.getAccessibleContext().setAccessibleDescription(tabbedPane.getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        singlePanel = new JPanel(new CenterLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        singlePanel.setName("ColorChooser.panel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        singlePanel.setInheritsPopupMenu(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        chooser.setLayout( new BorderLayout() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        defaultChoosers = createDefaultChoosers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        chooser.setChooserPanels(defaultChoosers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        previewPanelHolder = new JPanel(new CenterLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        previewPanelHolder.setName("ColorChooser.previewPanelHolder");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (DefaultLookup.getBoolean(chooser, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                  "ColorChooser.showPreviewPanelText", true)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            String previewString = UIManager.getString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                "ColorChooser.previewText", chooser.getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            previewPanelHolder.setBorder(new TitledBorder(previewString));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        previewPanelHolder.setInheritsPopupMenu(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        installPreviewPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        chooser.applyComponentOrientation(c.getComponentOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public void uninstallUI( JComponent c ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        chooser.remove(tabbedPane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        chooser.remove(singlePanel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        chooser.remove(previewPanelHolder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        uninstallDefaultChoosers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        uninstallListeners();
6105
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   129
        uninstallPreviewPanel();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        uninstallDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        previewPanelHolder = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        previewPanel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        defaultChoosers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        chooser = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        tabbedPane = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    protected void installPreviewPanel() {
6105
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   142
        JComponent previewPanel = this.chooser.getPreviewPanel();
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   143
        if (previewPanel == null) {
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   144
            previewPanel = ColorChooserComponentFactory.getPreviewPanel();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
6512
ce0a2790e09f 6977726: JColorChooser.getPreviewPanel() returnes null starting from jdk7 b105.
malenkov
parents: 6105
diff changeset
   146
        else if (JPanel.class.equals(previewPanel.getClass()) && (0 == previewPanel.getComponentCount())) {
ce0a2790e09f 6977726: JColorChooser.getPreviewPanel() returnes null starting from jdk7 b105.
malenkov
parents: 6105
diff changeset
   147
            previewPanel = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
6105
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   149
        this.previewPanel = previewPanel;
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   150
        if (previewPanel != null) {
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   151
            chooser.add(previewPanelHolder, BorderLayout.SOUTH);
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   152
            previewPanel.setForeground(chooser.getColor());
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   153
            previewPanelHolder.add(previewPanel);
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   154
            previewPanel.addMouseListener(getHandler());
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   155
            previewPanel.setInheritsPopupMenu(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
6105
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   157
    }
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   158
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   159
    /**
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   160
     * Removes installed preview panel from the UI delegate.
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   161
     *
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   162
     * @since 1.7
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   163
     */
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   164
    protected void uninstallPreviewPanel() {
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   165
        if (this.previewPanel != null) {
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   166
            this.previewPanel.removeMouseListener(getHandler());
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   167
            this.previewPanelHolder.remove(this.previewPanel);
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   168
        }
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   169
        this.chooser.remove(this.previewPanelHolder);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    protected void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        LookAndFeel.installColorsAndFont(chooser, "ColorChooser.background",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                              "ColorChooser.foreground",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                              "ColorChooser.font");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        LookAndFeel.installProperty(chooser, "opaque", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        TransferHandler th = chooser.getTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (th == null || th instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            chooser.setTransferHandler(defaultTransferHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    protected void uninstallDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (chooser.getTransferHandler() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            chooser.setTransferHandler(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        propertyChangeListener = createPropertyChangeListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        chooser.addPropertyChangeListener( propertyChangeListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        previewListener = getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        chooser.getSelectionModel().addChangeListener(previewListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private Handler getHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    protected PropertyChangeListener createPropertyChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        chooser.removePropertyChangeListener( propertyChangeListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        chooser.getSelectionModel().removeChangeListener(previewListener);
465
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   212
        previewListener = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
465
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   215
    private void selectionChanged(ColorSelectionModel model) {
6512
ce0a2790e09f 6977726: JColorChooser.getPreviewPanel() returnes null starting from jdk7 b105.
malenkov
parents: 6105
diff changeset
   216
        JComponent previewPanel = this.chooser.getPreviewPanel();
ce0a2790e09f 6977726: JColorChooser.getPreviewPanel() returnes null starting from jdk7 b105.
malenkov
parents: 6105
diff changeset
   217
        if (previewPanel != null) {
ce0a2790e09f 6977726: JColorChooser.getPreviewPanel() returnes null starting from jdk7 b105.
malenkov
parents: 6105
diff changeset
   218
            previewPanel.setForeground(model.getSelectedColor());
ce0a2790e09f 6977726: JColorChooser.getPreviewPanel() returnes null starting from jdk7 b105.
malenkov
parents: 6105
diff changeset
   219
            previewPanel.repaint();
465
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   220
        }
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   221
        AbstractColorChooserPanel[] panels = this.chooser.getChooserPanels();
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   222
        if (panels != null) {
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   223
            for (AbstractColorChooserPanel panel : panels) {
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   224
                if (panel != null) {
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   225
                    panel.updateChooser();
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   226
                }
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   227
            }
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   228
        }
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   229
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    private class Handler implements ChangeListener, MouseListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        // ChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        public void stateChanged(ChangeEvent evt) {
465
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   237
            selectionChanged((ColorSelectionModel) evt.getSource());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // MouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        public void mousePressed(MouseEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (chooser.getDragEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                TransferHandler th = chooser.getTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                th.exportAsDrag(chooser, evt, TransferHandler.COPY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        public void mouseReleased(MouseEvent evt) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        public void mouseClicked(MouseEvent evt) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        public void mouseEntered(MouseEvent evt) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        public void mouseExited(MouseEvent evt) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        // PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        public void propertyChange(PropertyChangeEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            String prop = evt.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (prop == JColorChooser.CHOOSER_PANELS_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                AbstractColorChooserPanel[] oldPanels =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    (AbstractColorChooserPanel[])evt.getOldValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                AbstractColorChooserPanel[] newPanels =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    (AbstractColorChooserPanel[])evt.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                for (int i = 0; i < oldPanels.length; i++) {  // remove old panels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                   Container wrapper = oldPanels[i].getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    if (wrapper != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                      Container parent = wrapper.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                      if (parent != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                          parent.remove(wrapper);  // remove from hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                      oldPanels[i].uninstallChooserPanel(chooser); // uninstall
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                int numNewPanels = newPanels.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                if (numNewPanels == 0) {  // removed all panels and added none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    chooser.remove(tabbedPane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                else if (numNewPanels == 1) {  // one panel case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    chooser.remove(tabbedPane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    JPanel centerWrapper = new JPanel( new CenterLayout() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    centerWrapper.setInheritsPopupMenu(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    centerWrapper.add(newPanels[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    singlePanel.add(centerWrapper, BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    chooser.add(singlePanel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                else {   // multi-panel case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    if ( oldPanels.length < 2 ) {// moving from single to multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        chooser.remove(singlePanel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        chooser.add(tabbedPane, BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    for (int i = 0; i < newPanels.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                        JPanel centerWrapper = new JPanel( new CenterLayout() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                        centerWrapper.setInheritsPopupMenu(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                        String name = newPanels[i].getDisplayName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        int mnemonic = newPanels[i].getMnemonic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                        centerWrapper.add(newPanels[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        tabbedPane.addTab(name, centerWrapper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        if (mnemonic > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                            tabbedPane.setMnemonicAt(i, mnemonic);
1282
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents: 465
diff changeset
   303
                            int index = newPanels[i].getDisplayedMnemonicIndex();
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents: 465
diff changeset
   304
                            if (index >= 0) {
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents: 465
diff changeset
   305
                                tabbedPane.setDisplayedMnemonicIndexAt(i, index);
00a51b36c173 6552812: Add HSL tab to JColorChooser
malenkov
parents: 465
diff changeset
   306
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                chooser.applyComponentOrientation(chooser.getComponentOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                for (int i = 0; i < newPanels.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    newPanels[i].installChooserPanel(chooser);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
465
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   315
            else if (prop == JColorChooser.PREVIEW_PANEL_PROPERTY) {
6105
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   316
                uninstallPreviewPanel();
de4af6e215ad 6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
malenkov
parents: 5506
diff changeset
   317
                installPreviewPanel();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
465
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   319
            else if (prop == JColorChooser.SELECTION_MODEL_PROPERTY) {
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   320
                ColorSelectionModel oldModel = (ColorSelectionModel) evt.getOldValue();
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   321
                oldModel.removeChangeListener(previewListener);
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   322
                ColorSelectionModel newModel = (ColorSelectionModel) evt.getNewValue();
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   323
                newModel.addChangeListener(previewListener);
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   324
                selectionChanged(newModel);
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   325
            }
507ce3a1af17 6348456: BasicColorChooserUI ignores JColorChooser selection model changes
malenkov
parents: 2
diff changeset
   326
            else if (prop == "componentOrientation") {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                ComponentOrientation o =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    (ComponentOrientation)evt.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                JColorChooser cc = (JColorChooser)evt.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                if (o != (ComponentOrientation)evt.getOldValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    cc.applyComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    cc.updateUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * This class should be treated as a &quot;protected&quot; inner class.
9230
c5674561cb9e 6985329: 9 classes in swing.plaf contains words inappropriate for public spec - about some compiler bug
alexp
parents: 6512
diff changeset
   340
     * Instantiate it only within subclasses of {@code BasicColorChooserUI}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public class PropertyHandler implements PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            getHandler().propertyChange(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
   348
    @SuppressWarnings("serial") // JDK-implementation class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    static class ColorTransferHandler extends TransferHandler implements UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        ColorTransferHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            super("color");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
}