001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Sergey Burlak
020: * @version $Revision$
021: */package javax.swing.plaf.basic;
022:
023: import java.awt.BorderLayout;
024: import java.beans.PropertyChangeEvent;
025: import java.beans.PropertyChangeListener;
026:
027: import javax.swing.BorderFactory;
028: import javax.swing.JColorChooser;
029: import javax.swing.JComponent;
030: import javax.swing.JPanel;
031: import javax.swing.JTabbedPane;
032: import javax.swing.LookAndFeel;
033: import javax.swing.UIManager;
034: import javax.swing.colorchooser.AbstractColorChooserPanel;
035: import javax.swing.colorchooser.ColorChooserComponentFactory;
036: import javax.swing.colorchooser.ColorSelectionModel;
037: import javax.swing.event.ChangeEvent;
038: import javax.swing.event.ChangeListener;
039: import javax.swing.plaf.ColorChooserUI;
040: import javax.swing.plaf.ComponentUI;
041:
042: import org.apache.harmony.x.swing.Utilities;
043:
044: public class BasicColorChooserUI extends ColorChooserUI {
045: public class PropertyHandler extends Object implements
046: PropertyChangeListener {
047: public void propertyChange(final PropertyChangeEvent event) {
048: String propertyName = event.getPropertyName();
049: if (JColorChooser.CHOOSER_PANELS_PROPERTY
050: .equals(propertyName)) {
051: AbstractColorChooserPanel[] newPanels = (AbstractColorChooserPanel[]) event
052: .getNewValue();
053: AbstractColorChooserPanel[] oldPanels = (AbstractColorChooserPanel[]) event
054: .getOldValue();
055: if (newPanels == null || newPanels.length == 0) {
056: return;
057: }
058:
059: for (int i = 0; i < oldPanels.length; i++) {
060: oldPanels[i].uninstallChooserPanel(chooser);
061: }
062: for (int i = 0; i < newPanels.length; i++) {
063: newPanels[i].installChooserPanel(chooser);
064: }
065:
066: if (newPanels.length == 1) {
067: centerPanel.removeAll();
068: centerPanel.add(newPanels[0]);
069: if (tabbedPane.getParent() != null) {
070: chooser.remove(tabbedPane);
071: }
072: if (centerPanel.getParent() == null) {
073: chooser.add(centerPanel, BorderLayout.CENTER);
074: }
075: } else {
076: for (int i = 0; i < tabbedPane.getTabCount(); i++) {
077: tabbedPane.removeTabAt(i);
078: }
079: for (int i = 0; i < newPanels.length; i++) {
080: tabbedPane.insertTab(newPanels[i]
081: .getDisplayName(), null, newPanels[i],
082: null, i);
083: tabbedPane.setMnemonicAt(i, newPanels[i]
084: .getMnemonic());
085: tabbedPane.setDisplayedMnemonicIndexAt(i,
086: newPanels[i]
087: .getDisplayedMnemonicIndex());
088: }
089: if (centerPanel.getParent() != null) {
090: chooser.remove(centerPanel);
091: }
092: if (tabbedPane.getParent() == null) {
093: chooser.add(tabbedPane, BorderLayout.CENTER);
094: }
095: }
096:
097: chooser.revalidate();
098: chooser.repaint();
099: } else if (JColorChooser.PREVIEW_PANEL_PROPERTY
100: .equals(propertyName)) {
101: installPreviewPanel();
102:
103: chooser.revalidate();
104: chooser.repaint();
105: } else if (JColorChooser.SELECTION_MODEL_PROPERTY
106: .equals(propertyName)) {
107: if (model != null) {
108: model.removeChangeListener(previewListener);
109: }
110: model = (ColorSelectionModel) event.getNewValue();
111: if (model != null) {
112: model.addChangeListener(previewListener);
113: }
114: }
115:
116: chooser.revalidate();
117: chooser.repaint();
118: }
119: }
120:
121: protected JColorChooser chooser;
122: protected AbstractColorChooserPanel[] defaultChoosers;
123: protected ChangeListener previewListener;
124: protected PropertyChangeListener propertyChangeListener;
125:
126: private JPanel centerPanel;
127: private JTabbedPane tabbedPane;
128: private ColorSelectionModel model;
129: private JPanel previewPanel;
130:
131: public static ComponentUI createUI(final JComponent c) {
132: return new BasicColorChooserUI();
133: }
134:
135: protected AbstractColorChooserPanel[] createDefaultChoosers() {
136: return ColorChooserComponentFactory.getDefaultChooserPanels();
137: }
138:
139: protected void uninstallDefaultChoosers() {
140: AbstractColorChooserPanel[] chooserPanels = chooser
141: .getChooserPanels();
142: for (int i = 0; i < chooserPanels.length; i++) {
143: chooser.removeChooserPanel(chooserPanels[i]);
144: }
145: }
146:
147: public void installUI(final JComponent c) {
148: chooser = (JColorChooser) c;
149: model = chooser.getSelectionModel();
150:
151: installDefaults();
152: installListeners();
153:
154: tabbedPane = new JTabbedPane();
155: centerPanel = new JPanel();
156: previewPanel = new JPanel();
157:
158: installPreviewPanel();
159: chooser.setChooserPanels(createDefaultChoosers());
160: }
161:
162: public void uninstallUI(final JComponent c) {
163: uninstallListeners();
164: uninstallDefaultChoosers();
165: uninstallDefaults();
166: }
167:
168: protected void installPreviewPanel() {
169: if (chooser.getPreviewPanel() == null) {
170: chooser.setPreviewPanel(ColorChooserComponentFactory
171: .getPreviewPanel());
172: chooser.getPreviewPanel().setForeground(
173: model.getSelectedColor());
174:
175: return;
176: }
177: previewPanel.removeAll();
178: previewPanel.add(chooser.getPreviewPanel());
179: previewPanel.setBorder(BorderFactory
180: .createTitledBorder(UIManager
181: .getString("ColorChooser.previewText")));
182:
183: if (previewPanel.getParent() == null) {
184: chooser.add(previewPanel, BorderLayout.SOUTH);
185: }
186: }
187:
188: protected void installDefaults() {
189: LookAndFeel.installColorsAndFont(chooser,
190: "ColorChooser.background", "ColorChooser.foreground",
191: "ColorChooser.font");
192: LookAndFeel.installProperty(chooser, "opaque", Boolean.FALSE);
193: }
194:
195: protected void uninstallDefaults() {
196: Utilities.uninstallColorsAndFont(chooser);
197: }
198:
199: protected void installListeners() {
200: propertyChangeListener = createPropertyChangeListener();
201: chooser.addPropertyChangeListener(propertyChangeListener);
202:
203: previewListener = new ChangeListener() {
204: public void stateChanged(ChangeEvent e) {
205: chooser.getPreviewPanel().setForeground(
206: model.getSelectedColor());
207: }
208: };
209: model.addChangeListener(previewListener);
210: }
211:
212: protected void uninstallListeners() {
213: chooser.removePropertyChangeListener(propertyChangeListener);
214: propertyChangeListener = null;
215:
216: model.removeChangeListener(previewListener);
217: previewListener = null;
218: }
219:
220: protected PropertyChangeListener createPropertyChangeListener() {
221: return new PropertyHandler();
222: }
223: }
|