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: * @author Vadim L. Bogdanov, Anton Avtamonov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.Component;
023: import java.awt.Dimension;
024: import java.awt.Graphics;
025: import java.awt.Insets;
026: import java.awt.LayoutManager;
027: import java.beans.PropertyChangeListener;
028:
029: import javax.accessibility.Accessible;
030: import javax.accessibility.AccessibleContext;
031: import javax.accessibility.AccessibleRole;
032: import javax.accessibility.AccessibleStateSet;
033: import javax.swing.plaf.ToolBarUI;
034:
035: import org.apache.harmony.x.swing.internal.nls.Messages;
036:
037: public class JToolBar extends JComponent implements SwingConstants,
038: Accessible {
039: private static final Insets DEFAULT_MARGIN = new Insets(0, 0, 0, 0);
040:
041: // TODO: implement accessibility
042: protected class AccessibleJToolBar extends AccessibleJComponent {
043: public AccessibleStateSet getAccessibleStateSet() {
044: throw new UnsupportedOperationException(Messages
045: .getString("swing.27")); //$NON-NLS-1$
046: }
047:
048: public AccessibleRole getAccessibleRole() {
049: throw new UnsupportedOperationException(Messages
050: .getString("swing.27")); //$NON-NLS-1$
051: }
052: }
053:
054: public static class Separator extends JSeparator {
055: private Dimension separatorSize;
056:
057: public Separator() {
058: setFocusable(false);
059: }
060:
061: public Separator(final Dimension size) {
062: setFocusable(false);
063: setSeparatorSize(size);
064: }
065:
066: public String getUIClassID() {
067: return "ToolBarSeparatorUI";
068: }
069:
070: public void setSeparatorSize(final Dimension size) {
071: if (size == null) {
072: return;
073: }
074: separatorSize = size;
075: }
076:
077: public Dimension getSeparatorSize() {
078: return separatorSize;
079: }
080:
081: public Dimension getMinimumSize() {
082: return new Dimension(getSeparatorSize());
083: }
084:
085: public Dimension getMaximumSize() {
086: return new Dimension(getSeparatorSize());
087: }
088:
089: public Dimension getPreferredSize() {
090: return new Dimension(getSeparatorSize());
091: }
092: }
093:
094: private class DefaultLayout extends BoxLayout {
095: public DefaultLayout() {
096: super (JToolBar.this , LINE_AXIS);
097: updateAxis();
098: }
099:
100: public void updateAxis() {
101: if (getOrientation() == HORIZONTAL) {
102: setAxis(LINE_AXIS);
103: } else {
104: setAxis(PAGE_AXIS);
105: }
106: }
107: }
108:
109: private boolean borderPainted = true;
110: private boolean floatable = true;
111: private Insets margin = (Insets) DEFAULT_MARGIN.clone();
112: private int orientation;
113: private boolean rollover;
114:
115: public JToolBar() {
116: this (null, HORIZONTAL);
117: }
118:
119: public JToolBar(final int orientation) {
120: this (null, orientation);
121: }
122:
123: public JToolBar(final String name) {
124: this (name, HORIZONTAL);
125: }
126:
127: public JToolBar(final String name, final int orientation) {
128: setName(name);
129:
130: setOrientation(orientation);
131: setLayout(new DefaultLayout());
132:
133: updateUI();
134: }
135:
136: public void setUI(final ToolBarUI ui) {
137: super .setUI(ui);
138: }
139:
140: public ToolBarUI getUI() {
141: return (ToolBarUI) ui;
142: }
143:
144: public void updateUI() {
145: setUI((ToolBarUI) UIManager.getUI(this ));
146: }
147:
148: public String getUIClassID() {
149: return "ToolBarUI";
150: }
151:
152: public int getComponentIndex(final Component c) {
153: return getComponentZOrder(c);
154: }
155:
156: public Component getComponentAtIndex(final int i) {
157: if (i >= 0 && i < getComponentCount()) {
158: return getComponent(i);
159: } else {
160: return null;
161: }
162: }
163:
164: public void setMargin(final Insets m) {
165: Insets oldValue = margin;
166: margin = m != null ? m : (Insets) DEFAULT_MARGIN.clone();
167: firePropertyChange("margin", oldValue, margin);
168: }
169:
170: public Insets getMargin() {
171: return margin;
172: }
173:
174: public void setBorderPainted(final boolean b) {
175: boolean oldValue = borderPainted;
176: borderPainted = b;
177: firePropertyChange("borderPainted", oldValue, borderPainted);
178: }
179:
180: public boolean isBorderPainted() {
181: return borderPainted;
182: }
183:
184: public void setFloatable(final boolean b) {
185: boolean oldValue = floatable;
186: floatable = b;
187: firePropertyChange("floatable", oldValue, floatable);
188: }
189:
190: public boolean isFloatable() {
191: return floatable;
192: }
193:
194: public void setOrientation(final int o) {
195: checkOrientation(o);
196: int oldValue = orientation;
197: orientation = o;
198: if (getLayout() instanceof DefaultLayout) {
199: ((DefaultLayout) getLayout()).updateAxis();
200: }
201: final int numConponents = getComponentCount();
202: for (int i = 0; i < numConponents; i++) {
203: if (getComponent(i) instanceof JSeparator) {
204: ((JSeparator) getComponent(i))
205: .setOrientation(getSeparatorOrientation());
206: }
207: }
208: firePropertyChange("orientation", oldValue, orientation);
209: }
210:
211: public int getOrientation() {
212: return orientation;
213: }
214:
215: public void setRollover(final boolean rollover) {
216: boolean oldValue = this .rollover;
217: this .rollover = rollover;
218: firePropertyChange("JToolBar.isRollover", oldValue,
219: this .rollover);
220: }
221:
222: public boolean isRollover() {
223: return rollover;
224: }
225:
226: public void addSeparator() {
227: add(createSeparator());
228: }
229:
230: public void addSeparator(final Dimension size) {
231: Separator separator = createSeparator();
232: separator.setSeparatorSize(size);
233: add(separator);
234: }
235:
236: public JButton add(final Action a) {
237: JButton b = createActionComponent(a);
238: b.setAction(a);
239: PropertyChangeListener actionListener = createActionChangeListener(b);
240: if (actionListener != null) {
241: b.addPropertyChangeListener(actionListener);
242: }
243: add(b);
244: return b;
245: }
246:
247: public void setLayout(final LayoutManager mgr) {
248: super .setLayout(mgr);
249: }
250:
251: public AccessibleContext getAccessibleContext() {
252: if (accessibleContext == null) {
253: accessibleContext = new AccessibleJToolBar();
254: }
255:
256: return accessibleContext;
257: }
258:
259: protected void paintBorder(final Graphics g) {
260: if (isBorderPainted()) {
261: super .paintBorder(g);
262: }
263: }
264:
265: protected JButton createActionComponent(final Action a) {
266: JButton b = new JButton();
267: if (a != null) {
268: b.configurePropertiesFromAction(a);
269: }
270: return b;
271: }
272:
273: protected PropertyChangeListener createActionChangeListener(
274: final JButton b) {
275: return null;
276: }
277:
278: protected void addImpl(final Component comp,
279: final Object constraints, final int index) {
280: super .addImpl(comp, constraints, index);
281: }
282:
283: private void checkOrientation(final int o) {
284: if (o != HORIZONTAL && o != VERTICAL) {
285: throw new IllegalArgumentException(Messages
286: .getString("swing.47")); //$NON-NLS-1$
287: }
288: }
289:
290: private Separator createSeparator() {
291: Separator separator = new Separator();
292: separator.setOrientation(getSeparatorOrientation());
293: return separator;
294: }
295:
296: private int getSeparatorOrientation() {
297: return (orientation == HORIZONTAL) ? VERTICAL : HORIZONTAL;
298: }
299: }
|