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;
018:
019: import com.l2fprod.common.swing.plaf.ButtonBarButtonUI;
020: import com.l2fprod.common.swing.plaf.ButtonBarUI;
021: import com.l2fprod.common.swing.plaf.JButtonBarAddon;
022: import com.l2fprod.common.swing.plaf.LookAndFeelAddons;
023:
024: import java.awt.Component;
025: import java.awt.event.ContainerAdapter;
026: import java.awt.event.ContainerEvent;
027: import java.awt.event.ContainerListener;
028: import java.beans.PropertyChangeEvent;
029: import java.beans.PropertyChangeListener;
030:
031: import javax.swing.AbstractButton;
032: import javax.swing.JComponent;
033: import javax.swing.SwingConstants;
034:
035: /**
036: * JButtonBar helps organizing buttons together (as seen in Mozilla Firefox
037: * or IntelliJ).<br>
038: *
039: * @javabean.class
040: * name="JButtonBar"
041: * shortDescription="JButtonBar helps organizing buttons together (as seen in Mozilla Firefox or IntelliJ)."
042: * stopClass="java.awt.Component"
043: *
044: * @javabean.icons
045: * mono16="JButtonBar16-mono.gif"
046: * color16="JButtonBar16.gif"
047: * mono32="JButtonBar32-mono.gif"
048: * color32="JButtonBar32.gif"
049: */
050: public class JButtonBar extends JComponent implements SwingConstants {
051:
052: public static final String UI_CLASS_ID = "ButtonBarUI";
053:
054: // ensure at least the default ui is registered
055: static {
056: LookAndFeelAddons.contribute(new JButtonBarAddon());
057: }
058:
059: public static final String ORIENTATION_CHANGED_KEY = "orientation";
060:
061: private int orientation;
062:
063: public JButtonBar() {
064: this (HORIZONTAL);
065: }
066:
067: public JButtonBar(int orientation) {
068: this .orientation = orientation;
069: updateUI();
070: addContainerListener(buttonTracker);
071: }
072:
073: /**
074: * Notification from the <code>UIManager</code> that the L&F has changed.
075: * Replaces the current UI object with the latest version from the <code>UIManager</code>.
076: *
077: * @see javax.swing.JComponent#updateUI
078: */
079: public void updateUI() {
080: setUI((ButtonBarUI) LookAndFeelAddons.getUI(this ,
081: ButtonBarUI.class));
082: }
083:
084: /**
085: * Returns the L&F object that renders this component.
086: *
087: * @return the ButtonBarUI object
088: * @see #setUI
089: */
090: public ButtonBarUI getUI() {
091: return (ButtonBarUI) ui;
092: }
093:
094: /**
095: * Sets the L&F object that renders this component.
096: *
097: * @param ui the <code>ButtonBarUI</code> L&F object
098: * @see javax.swing.UIDefaults#getUI
099: *
100: * @beaninfo bound: true hidden: true description: The UI object that
101: * implements the buttonbar's LookAndFeel.
102: */
103: public void setUI(ButtonBarUI ui) {
104: super .setUI(ui);
105: Component[] components = getComponents();
106: // whenever our UI is changed, make sure UI of our buttons is updated.
107: for (int i = 0, c = components.length; i < c; i++) {
108: if (components[i] instanceof AbstractButton) {
109: ui.installButtonBarUI((AbstractButton) components[i]);
110: }
111: }
112: }
113:
114: /**
115: * Returns the name of the L&F class that renders this component.
116: *
117: * @return the string {@link #UI_CLASS_ID}
118: * @see javax.swing.JComponent#getUIClassID
119: * @see javax.swing.UIDefaults#getUI
120: */
121: public String getUIClassID() {
122: return UI_CLASS_ID;
123: }
124:
125: /**
126: * Sets the orientation of the bar. The orientation must have either the
127: * value <code>HORIZONTAL</code> or <code>VERTICAL</code>. If <code>orientation</code>
128: * is an invalid value, an exception will be thrown.
129: *
130: * @param orientation the new orientation -- either <code>HORIZONTAL</code>
131: * or</code> VERTICAL</code>
132: * @exception IllegalArgumentException if orientation is neither <code>
133: * HORIZONTAL</code> nor <code>VERTICAL</code>
134: * @see #getOrientation @beaninfo description: The current orientation of the
135: * bar bound: true preferred: true
136: */
137: public void setOrientation(int orientation) {
138: if (!(orientation == HORIZONTAL || orientation == VERTICAL)) {
139: throw new IllegalArgumentException(
140: "orientation must be one of: "
141: + "VERTICAL, HORIZONTAL");
142: }
143:
144: if (this .orientation != orientation) {
145: int oldOrientation = this .orientation;
146: this .orientation = orientation;
147: firePropertyChange(ORIENTATION_CHANGED_KEY, oldOrientation,
148: this .orientation);
149: }
150: }
151:
152: public int getOrientation() {
153: return orientation;
154: }
155:
156: /**
157: * This listener ensures the UI of buttons added to the JButtonBar gets reset
158: * everytime it is changed to a pluggable UI which does not implement
159: * ButtonBarUI.
160: */
161: private static PropertyChangeListener uiUpdater = new PropertyChangeListener() {
162: public void propertyChange(PropertyChangeEvent evt) {
163: if (evt.getSource() instanceof AbstractButton) {
164: AbstractButton button = (AbstractButton) evt
165: .getSource();
166: if (button.getParent() instanceof JButtonBar
167: && !(button.getUI() instanceof ButtonBarButtonUI)) {
168: ((ButtonBarUI) ((JButtonBar) button.getParent()).ui)
169: .installButtonBarUI(button);
170: }
171: }
172: }
173: };
174:
175: /**
176: * This listener tracks buttons added to a JButtonBar. When a button is
177: * added, it updates its UI and ensures further ui changes will be tracked
178: * too. When the button is removed, it is no longer tracked.
179: */
180: private static ContainerListener buttonTracker = new ContainerAdapter() {
181: public void componentAdded(ContainerEvent e) {
182: JButtonBar container = (JButtonBar) e.getContainer();
183: if (e.getChild() instanceof AbstractButton) {
184: ((ButtonBarUI) container.ui)
185: .installButtonBarUI((AbstractButton) e
186: .getChild());
187: ((AbstractButton) e.getChild())
188: .addPropertyChangeListener("UI",
189: JButtonBar.uiUpdater);
190: }
191: }
192:
193: public void componentRemoved(ContainerEvent e) {
194: if (e.getChild() instanceof AbstractButton) {
195: ((AbstractButton) e.getChild())
196: .removePropertyChangeListener("UI",
197: JButtonBar.uiUpdater);
198: }
199: }
200: };
201:
202: }
|