Source for org.jfree.report.modules.gui.swing.common.JStatusBar

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: JStatusBar.java 3525 2007-10-16 11:43:48Z tmorgner $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.report.modules.gui.swing.common;
  32: 
  33: import java.awt.BorderLayout;
  34: import java.awt.Dimension;
  35: import java.awt.IllegalComponentStateException;
  36: import java.util.Locale;
  37: import javax.swing.BorderFactory;
  38: import javax.swing.Icon;
  39: import javax.swing.JComponent;
  40: import javax.swing.JLabel;
  41: import javax.swing.JPanel;
  42: import javax.swing.UIManager;
  43: 
  44: import org.jfree.report.modules.gui.common.DefaultIconTheme;
  45: import org.jfree.report.modules.gui.common.IconTheme;
  46: 
  47: public class JStatusBar extends JComponent
  48: {
  49:   public static final int TYPE_ERROR = 3;
  50:   public static final int TYPE_WARNING = 2;
  51:   public static final int TYPE_INFORMATION = 1;
  52:   public static final int TYPE_NONE = 0;
  53: 
  54:   private JComponent otherComponents;
  55:   private JLabel statusHolder;
  56:   private IconTheme iconTheme;
  57:   private int statusType;
  58: 
  59:   public JStatusBar()
  60:   {
  61:     this(new DefaultIconTheme());
  62:   }
  63: 
  64:   public JStatusBar (final IconTheme theme)
  65:   {
  66:     setLayout(new BorderLayout());
  67:     setBorder(BorderFactory.createMatteBorder
  68:             (1, 0, 0, 0, UIManager.getDefaults().getColor("controlShadow")));
  69:     statusHolder = new JLabel(" ");
  70:     statusHolder.setMinimumSize(new Dimension(0, 20));
  71:     add(statusHolder, BorderLayout.CENTER);
  72: 
  73:     otherComponents = new JPanel();
  74:     add(otherComponents, BorderLayout.EAST);
  75:     this.iconTheme = theme;
  76:   }
  77: 
  78:   protected IconTheme getIconTheme()
  79:   {
  80:     return iconTheme;
  81:   }
  82: 
  83:   public void setIconTheme(final IconTheme iconTheme)
  84:   {
  85:     final IconTheme oldTheme = this.iconTheme;
  86:     this.iconTheme = iconTheme;
  87:     firePropertyChange("iconTheme", oldTheme, iconTheme);
  88: 
  89:     if (iconTheme == null)
  90:     {
  91:       statusHolder.setIcon(null);
  92:     }
  93:     else
  94:     {
  95:       updateTypeIcon(getStatusType());
  96:     }
  97:   }
  98: 
  99:   public JComponent getExtensionArea ()
 100:   {
 101:     return otherComponents;
 102:   }
 103: 
 104:   public int getStatusType()
 105:   {
 106:     return statusType;
 107:   }
 108: 
 109:   public String getStatusText()
 110:   {
 111:     return statusHolder.getText();
 112:   }
 113: 
 114:   public void setStatusText (final String text)
 115:   {
 116:     final String oldText = statusHolder.getText();
 117:     this.statusHolder.setText(text);
 118:     firePropertyChange("statusText", oldText, text);
 119:   }
 120: 
 121:   public void setStatusType (final int type)
 122:   {
 123:     final int oldType = statusType;
 124:     this.statusType = type;
 125:     firePropertyChange("statusType", oldType, type);
 126:     updateTypeIcon(type);
 127:   }
 128: 
 129:   public void setStatus (final int type, final String text)
 130:   {
 131:     this.statusType = type;
 132:     updateTypeIcon(type);
 133:     statusHolder.setText(text);
 134:   }
 135: 
 136:   private void updateTypeIcon(final int type)
 137:   {
 138:     if (iconTheme != null)
 139:     {
 140:       if (type == TYPE_ERROR)
 141:       {
 142:         final Icon res = getIconTheme().getSmallIcon(getLocale(), "statusbar.errorIcon");
 143:         statusHolder.setIcon(res);
 144:       }
 145:       else if (type == TYPE_WARNING)
 146:       {
 147:         final Icon res = getIconTheme().getSmallIcon(getLocale(), "statusbar.warningIcon");
 148:         statusHolder.setIcon(res);
 149:       }
 150:       else if (type == TYPE_INFORMATION)
 151:       {
 152:         final Icon res = getIconTheme().getSmallIcon(getLocale(), "statusbar.informationIcon");
 153:         statusHolder.setIcon(res);
 154:       }
 155:       else
 156:       {
 157:         final Icon res = getIconTheme().getSmallIcon(getLocale(), "statusbar.otherIcon");
 158:         statusHolder.setIcon(res);
 159:       }
 160:     }
 161:   }
 162: 
 163:   public void clear ()
 164:   {
 165:     setStatus(TYPE_NONE, " ");
 166:   }
 167: 
 168:   /**
 169:    * Gets the locale of this component.
 170:    *
 171:    * @return this component's locale; if this component does not have a locale,
 172:    *         the locale of its parent is returned
 173:    * @throws java.awt.IllegalComponentStateException
 174:    *          if the <code>Component</code> does not have its own locale and has
 175:    *          not yet been added to a containment hierarchy such that the locale
 176:    *          can be determined from the containing parent
 177:    * @see #setLocale
 178:    * @since JDK1.1
 179:    */
 180:   public Locale getLocale()
 181:   {
 182:     try
 183:     {
 184:       return super.getLocale();
 185:     }
 186:     catch(IllegalComponentStateException ice)
 187:     {
 188:       return Locale.getDefault();
 189:     }
 190:   }
 191: }