001: /*
002: * @(#)QuaquaColorChooserUI.java 1.1 2005-12-18
003: *
004: * Copyright (c) 2004 Werner Randelshofer
005: * Staldenmattweg 2, Immensee, CH-6405, Switzerland.
006: * All rights reserved.
007: *
008: * This software is the confidential and proprietary information of
009: * Werner Randelshofer. ("Confidential Information"). You shall not
010: * disclose such Confidential Information and shall use it only in
011: * accordance with the terms of the license agreement you entered into
012: * with Werner Randelshofer.
013: */
014:
015: package contrib.ch.randelshofer.quaqua;
016:
017: import java.awt.*;
018: import java.awt.event.*;
019: import java.beans.*;
020: import java.util.*;
021: import javax.swing.*;
022: import javax.swing.colorchooser.*;
023: import javax.swing.event.*;
024: import javax.swing.plaf.*;
025: import javax.swing.plaf.basic.*;
026:
027: import contrib.ch.randelshofer.quaqua.colorchooser.*;
028:
029: /**
030: * QuaquaColorChooserUI with enhancements for Java 1.4.
031: *
032: * @author Werner Randelshofer
033: * @version 1.1 2005-12-18 Gracefully handle instantiation failures of
034: * color chooser panels.
035: * <br>1.0 29 March 2005 Created.
036: */
037: public class Quaqua14ColorChooserUI extends Quaqua13ColorChooserUI {
038: private static TransferHandler defaultTransferHandler = new ColorTransferHandler();
039: private MouseListener previewMouseListener;
040:
041: public static ComponentUI createUI(JComponent c) {
042: return new Quaqua14ColorChooserUI();
043: }
044:
045: public void installUI(JComponent c) {
046: super .installUI(c);
047: chooser.applyComponentOrientation(c.getComponentOrientation());
048: }
049:
050: protected void installDefaults() {
051: super .installDefaults();
052: TransferHandler th = chooser.getTransferHandler();
053: if (th == null || th instanceof UIResource) {
054: chooser.setTransferHandler(defaultTransferHandler);
055: }
056: }
057:
058: protected void uninstallDefaults() {
059: if (chooser.getTransferHandler() instanceof UIResource) {
060: chooser.setTransferHandler(null);
061: }
062: }
063:
064: protected void installListeners() {
065: super .installListeners();
066:
067: previewMouseListener = new MouseAdapter() {
068: public void mousePressed(MouseEvent e) {
069: if (chooser.getDragEnabled()) {
070: TransferHandler th = chooser.getTransferHandler();
071: th.exportAsDrag(chooser, e, TransferHandler.COPY);
072: }
073: }
074: };
075:
076: }
077:
078: protected void uninstallListeners() {
079: super .uninstallListeners();
080: previewPanel.removeMouseListener(previewMouseListener);
081: }
082:
083: protected void installPreviewPanel() {
084: if (previewPanel != null) {
085: previewPanel.removeMouseListener(previewMouseListener);
086: }
087: super .installPreviewPanel();
088: previewPanel.addMouseListener(previewMouseListener);
089: }
090:
091: protected PropertyChangeListener createPropertyChangeListener() {
092: return new PropertyHandler();
093: }
094:
095: public class PropertyHandler implements PropertyChangeListener {
096:
097: public void propertyChange(PropertyChangeEvent e) {
098:
099: if (e.getPropertyName().equals(
100: JColorChooser.CHOOSER_PANELS_PROPERTY)) {
101: AbstractColorChooserPanel[] oldPanels = (AbstractColorChooserPanel[]) e
102: .getOldValue();
103: AbstractColorChooserPanel[] newPanels = (AbstractColorChooserPanel[]) e
104: .getNewValue();
105:
106: for (int i = 0; i < oldPanels.length; i++) { // remove old panels
107: if (oldPanels[i] != null) {
108: Container wrapper = oldPanels[i].getParent();
109: if (wrapper != null) {
110: Container parent = wrapper.getParent();
111: if (parent != null)
112: parent.remove(wrapper); // remove from hierarchy
113: oldPanels[i].uninstallChooserPanel(chooser); // uninstall
114: }
115: }
116: }
117:
118: mainPanel.removeAllColorChooserPanels();
119: for (int i = 0; i < newPanels.length; i++) {
120: if (newPanels[i] != null) {
121: mainPanel.addColorChooserPanel(newPanels[i]);
122: }
123: }
124:
125: chooser.applyComponentOrientation(chooser
126: .getComponentOrientation());
127: for (int i = 0; i < newPanels.length; i++) {
128: if (newPanels[i] != null) {
129: newPanels[i].installChooserPanel(chooser);
130: }
131: }
132: }
133: if (e.getPropertyName().equals(
134: JColorChooser.PREVIEW_PANEL_PROPERTY)) {
135: if (e.getNewValue() != previewPanel) {
136: installPreviewPanel();
137: }
138: }
139: if (e.getPropertyName().equals("componentOrientation")) {
140: ComponentOrientation o = (ComponentOrientation) e
141: .getNewValue();
142: JColorChooser cc = (JColorChooser) e.getSource();
143: if (o != (ComponentOrientation) e.getOldValue()) {
144: cc.applyComponentOrientation(o);
145: cc.updateUI();
146: }
147: }
148: }
149: }
150:
151: static class ColorTransferHandler extends TransferHandler implements
152: UIResource {
153:
154: ColorTransferHandler() {
155: super ("color");
156: }
157: }
158: }
|