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 Anton Avtamonov
020: * @version $Revision$
021: */package javax.swing.plaf.metal;
022:
023: import java.awt.Component;
024: import java.awt.Container;
025: import java.awt.Dimension;
026: import java.awt.Graphics;
027: import java.awt.Insets;
028: import java.awt.LayoutManager;
029: import java.awt.Rectangle;
030: import java.awt.event.MouseEvent;
031: import java.beans.PropertyChangeEvent;
032: import java.beans.PropertyChangeListener;
033:
034: import javax.swing.ComboBoxEditor;
035: import javax.swing.JButton;
036: import javax.swing.JComboBox;
037: import javax.swing.JComponent;
038: import javax.swing.plaf.BorderUIResource;
039: import javax.swing.plaf.ComponentUI;
040: import javax.swing.plaf.basic.BasicBorders;
041: import javax.swing.plaf.basic.BasicComboBoxUI;
042: import javax.swing.plaf.basic.BasicComboPopup;
043: import javax.swing.plaf.basic.ComboPopup;
044:
045: import org.apache.harmony.x.swing.StringConstants;
046: import org.apache.harmony.x.swing.Utilities;
047:
048: public class MetalComboBoxUI extends BasicComboBoxUI {
049: /**
050: * @deprecated Do nothing and should not be used.
051: */
052: public class MetalComboPopup extends BasicComboPopup {
053: public MetalComboPopup(final JComboBox comboBox) {
054: super (comboBox);
055: }
056:
057: public void delegateFocus(final MouseEvent e) {
058: super .delegateFocus(e);
059: }
060: }
061:
062: public class MetalComboBoxLayoutManager extends
063: ComboBoxLayoutManager {
064: public void layoutContainer(final Container parent) {
065: layoutComboBox(parent, this );
066: }
067:
068: public void super Layout(final Container parent) {
069: super .layoutContainer(parent);
070: }
071: }
072:
073: public class MetalPropertyChangeListener extends
074: PropertyChangeHandler {
075: public void propertyChange(final PropertyChangeEvent event) {
076: if (StringConstants.EDITABLE_PROPERTY_CHANGED.equals(event
077: .getPropertyName())) {
078: if (arrowButton instanceof MetalComboBoxButton) {
079: ((MetalComboBoxButton) arrowButton)
080: .setIconOnly(((Boolean) event.getNewValue())
081: .booleanValue());
082: }
083: }
084: super .propertyChange(event);
085: }
086: }
087:
088: public static ComponentUI createUI(final JComponent c) {
089: return new MetalComboBoxUI();
090: }
091:
092: public void paint(final Graphics g, final JComponent c) {
093:
094: }
095:
096: public void paintCurrentValue(final Graphics g,
097: final Rectangle bounds, final boolean hasFocus) {
098: super .paintCurrentValue(g, bounds, hasFocus);
099: }
100:
101: public void paintCurrentValueBackground(final Graphics g,
102: final Rectangle bounds, final boolean hasFocus) {
103: super .paintCurrentValueBackground(g, bounds, hasFocus);
104: }
105:
106: public PropertyChangeListener createPropertyChangeListener() {
107: return new MetalPropertyChangeListener();
108: }
109:
110: public void configureEditor() {
111: super .configureEditor();
112: }
113:
114: public void unconfigureEditor() {
115: super .unconfigureEditor();
116: }
117:
118: public void layoutComboBox(final Container parent,
119: final MetalComboBoxLayoutManager manager) {
120: if (comboBox.isEditable()) {
121: manager.super Layout(parent);
122: } else if (arrowButton != null) {
123: Rectangle bounds = comboBox.getBounds();
124: Insets insets = getInsets();
125: arrowButton.setBounds(insets.left, insets.top, bounds.width
126: - insets.left - insets.right, bounds.height
127: - insets.top - insets.bottom);
128: }
129: }
130:
131: public Dimension getMinimumSize(final JComponent c) {
132: if (!isMinimumSizeDirty) {
133: return cachedMinimumSize;
134: }
135:
136: Dimension result;
137: if (arrowButton instanceof MetalComboBoxButton) {
138: Dimension displaySize = getDisplaySize();
139: int arrowIconWidth = ((MetalComboBoxButton) arrowButton)
140: .getComboIcon().getIconWidth();
141: Insets arrowButtonInsets = arrowButton.getInsets();
142:
143: result = new Dimension();
144: result.height = displaySize.height + arrowButtonInsets.top
145: + arrowButtonInsets.bottom;
146: result.width = displaySize.width + arrowIconWidth
147: + arrowButtonInsets.left + arrowButtonInsets.right
148: + arrowButtonInsets.right;
149:
150: Utilities.addInsets(result, comboBox.getInsets());
151:
152: cachedMinimumSize.setSize(result);
153: isMinimumSizeDirty = false;
154: } else {
155: result = super .getMinimumSize(c);
156: }
157:
158: return result;
159: }
160:
161: /**
162: * @deprecated
163: */
164: protected void removeListeners() {
165:
166: }
167:
168: /**
169: * @deprecated
170: */
171: protected void editablePropertyChanged(final PropertyChangeEvent e) {
172:
173: }
174:
175: protected ComboBoxEditor createEditor() {
176: return new MetalComboBoxEditor.UIResource();
177: }
178:
179: protected ComboPopup createPopup() {
180: return new MetalComboPopup(comboBox);
181: }
182:
183: protected JButton createArrowButton() {
184: JButton result = new MetalComboBoxButton(comboBox,
185: new MetalComboBoxIcon(), false, currentValuePane,
186: listBox);
187:
188: //Looks like a workarround!
189: result.setBorder(new BorderUIResource.CompoundBorderUIResource(
190: new MetalBorders.ButtonBorder(),
191: new BasicBorders.MarginBorder() {
192: public Insets getBorderInsets(final Component c) {
193: return new Insets(0, 1, 1, 3);
194: }
195: }));
196:
197: return result;
198: }
199:
200: protected LayoutManager createLayoutManager() {
201: return new MetalComboBoxLayoutManager();
202: }
203: }
|