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 java.awt.Color;
020: import java.awt.Component;
021: import java.util.Hashtable;
022:
023: import javax.swing.BorderFactory;
024: import javax.swing.JComponent;
025: import javax.swing.JLabel;
026: import javax.swing.border.Border;
027: import javax.swing.border.CompoundBorder;
028: import javax.swing.border.EmptyBorder;
029: import javax.swing.plaf.UIResource;
030:
031: /**
032: * StatusBar. <BR>A status bar is made of multiple zones. A zone can be any
033: * JComponent.
034: */
035: public class StatusBar extends JComponent {
036:
037: /**
038: * The key used to identified the default zone
039: */
040: public final static String DEFAULT_ZONE = "default";
041:
042: private Hashtable idToZones;
043: private Border zoneBorder;
044:
045: /**
046: * Construct a new StatusBar
047: *
048: */
049: public StatusBar() {
050: setLayout(LookAndFeelTweaks.createHorizontalPercentLayout());
051: idToZones = new Hashtable();
052: setZoneBorder(BorderFactory.createLineBorder(Color.lightGray));
053: }
054:
055: public void setZoneBorder(Border border) {
056: zoneBorder = border;
057: }
058:
059: /**
060: * Adds a new zone in the StatusBar
061: *
062: * @param id
063: * @param zone
064: * @param constraints one of the constraint support by the
065: * {@link com.l2fprod.common.swing.PercentLayout}
066: */
067: public void addZone(String id, Component zone, String constraints) {
068: // is there already a zone with this id?
069: Component previousZone = getZone(id);
070: if (previousZone != null) {
071: remove(previousZone);
072: idToZones.remove(id);
073: }
074:
075: if (zone instanceof JComponent) {
076: JComponent jc = (JComponent) zone;
077: if (jc.getBorder() == null
078: || jc.getBorder() instanceof UIResource) {
079: if (jc instanceof JLabel) {
080: jc.setBorder(new CompoundBorder(zoneBorder,
081: new EmptyBorder(0, 2, 0, 2)));
082: ((JLabel) jc).setText(" ");
083: } else {
084: jc.setBorder(zoneBorder);
085: }
086: }
087: }
088:
089: add(zone, constraints);
090: idToZones.put(id, zone);
091: }
092:
093: public Component getZone(String id) {
094: return (Component) idToZones.get(id);
095: }
096:
097: /**
098: * For example:
099: *
100: * <code>
101: * setZones(new String[]{"A","B"},
102: * new JComponent[]{new JLabel(), new JLabel()},
103: * new String[]{"33%","*"});
104: * </code>
105: *
106: * would construct a new status bar with two zones (two JLabels)
107: * named A and B, the first zone A will occupy 33 percents of the
108: * overall size of the status bar and B the left space.
109: *
110: * @param ids a value of type 'String[]'
111: * @param zones a value of type 'JComponent[]'
112: * @param constraints a value of type 'String[]'
113: */
114: public void setZones(String[] ids, Component[] zones,
115: String[] constraints) {
116: removeAll();
117: idToZones.clear();
118: for (int i = 0, c = zones.length; i < c; i++) {
119: addZone(ids[i], zones[i], constraints[i]);
120: }
121: }
122:
123: }
|