001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.swing.plaf.basic;
018:
019: import com.l2fprod.common.swing.JButtonBar;
020: import com.l2fprod.common.swing.PercentLayout;
021: import com.l2fprod.common.swing.plaf.ButtonBarUI;
022:
023: import java.awt.Dimension;
024: import java.beans.PropertyChangeEvent;
025: import java.beans.PropertyChangeListener;
026:
027: import javax.swing.JComponent;
028: import javax.swing.plaf.ComponentUI;
029:
030: /**
031: * BasicButtonBarUI. <br>
032: *
033: */
034: public class BasicButtonBarUI extends ButtonBarUI {
035:
036: protected JButtonBar bar;
037: protected PropertyChangeListener propertyListener;
038:
039: public static ComponentUI createUI(JComponent c) {
040: return new BasicButtonBarUI();
041: }
042:
043: public void installUI(JComponent c) {
044: super .installUI(c);
045:
046: bar = (JButtonBar) c;
047:
048: installDefaults();
049: installListeners();
050:
051: updateLayout();
052: }
053:
054: public void uninstallUI(JComponent c) {
055: uninstallDefaults();
056: uninstallListeners();
057: super .uninstallUI(c);
058: }
059:
060: protected void installDefaults() {
061: }
062:
063: protected void uninstallDefaults() {
064: }
065:
066: protected void installListeners() {
067: propertyListener = createPropertyChangeListener();
068: bar.addPropertyChangeListener(propertyListener);
069: }
070:
071: protected void uninstallListeners() {
072: bar.removePropertyChangeListener(propertyListener);
073: }
074:
075: protected PropertyChangeListener createPropertyChangeListener() {
076: return new ChangeListener();
077: }
078:
079: protected void updateLayout() {
080: if (bar.getOrientation() == JButtonBar.HORIZONTAL) {
081: bar
082: .setLayout(new PercentLayout(
083: PercentLayout.HORIZONTAL, 2));
084: } else {
085: bar.setLayout(new PercentLayout(PercentLayout.VERTICAL, 2));
086: }
087: }
088:
089: public Dimension getPreferredSize(JComponent c) {
090: JButtonBar b = (JButtonBar) c;
091: Dimension preferred;
092: // it happens the layout is null - Netbeans 5.5 beta 2
093: if (b.getLayout() == null) {
094: preferred = new Dimension(100, 100);
095: } else {
096: preferred = b.getLayout().preferredLayoutSize(c);
097: }
098:
099: if (b.getOrientation() == JButtonBar.HORIZONTAL) {
100: return new Dimension(preferred.width, 53);
101: } else {
102: return new Dimension(74, preferred.height);
103: }
104: }
105:
106: private class ChangeListener implements PropertyChangeListener {
107: public void propertyChange(PropertyChangeEvent evt) {
108: if (evt.getPropertyName().equals(
109: JButtonBar.ORIENTATION_CHANGED_KEY)) {
110: updateLayout();
111: bar.revalidate();
112: bar.repaint();
113: }
114: }
115: }
116:
117: }
|