001/*
002 *  $Source: /cvsroot/open/projects/WebARTS/ca/bc/webarts/widgets/ColouredLabel.java,v $
003 *  $Name:  $
004 *  Current File Status:   $Revision: 1231 $
005 *  $Date: 2018-03-02 22:27:48 -0800 (Fri, 02 Mar 2018) $
006 *  $Locker:  $
007 *  Copyright (C) 2001 WebARTS Design, North Vancouver Canada
008*/
009/*
010 *  This program is free software; you can redistribute it and/or modify
011 *  it under the terms of the GNU General Public License as published by
012 *  the Free Software Foundation; either version 2 of the License, or
013 *  (at your option) any later version.
014 *
015 *  This program is distributed in the hope that it will be useful,
016 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
017 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
018 *  GNU General Public License for more details.
019 *
020 *  You should have received a copy of the GNU General Public License
021 *  along with this program; if not, write to the Free Software
022 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
023*/
024package ca.bc.webarts.widgets;
025
026import javax.swing.JLabel;
027import java.awt.Color;
028import java.awt.Font;
029
030// These are needed ONLY for the Demo in the main method
031import java.awt.Color;
032import java.awt.GraphicsDevice;
033import java.awt.GraphicsEnvironment;
034import java.awt.BorderLayout;
035import java.awt.event.ActionEvent;
036import java.awt.event.ActionListener;
037import java.awt.event.ComponentAdapter;
038import java.awt.event.ComponentEvent;
039import java.awt.event.KeyEvent;
040import java.awt.event.MouseAdapter;
041import java.awt.event.MouseEvent;
042import java.awt.event.WindowAdapter;
043import java.awt.event.WindowEvent;
044import javax.swing.JFrame;
045import javax.swing.JPanel;
046import javax.swing.JButton;
047import javax.swing.SwingUtilities;
048
049/**
050 *  A class to extend JLabel with some added easily settable colour and Font
051 *  settings.<P>
052 *
053 *  It allows easily setting the text colour and the background colour of the
054 *  label. <P>
055 *
056 *  Now you don't have to override paint.
057 *
058 *@author     Tom Gutwin
059 *@created    April 2, 2002
060 */
061public class ColouredLabel extends JLabel
062{
063  private static final String DEFAULT_FONT_FACE = "Arial, Helvetica";
064  /**
065   *  Description of the Field
066   */
067  protected Color textColour_ = Color.white;
068  /**
069   *  Description of the Field
070   */
071  protected Color backColour_ = Color.black;
072  /**
073   *  Description of the Field
074   */
075  protected String labelText_ = "";
076  /**
077   *  Description of the Field
078   */
079  protected String fontName_ = "default";
080  /**
081   *  Description of the Field
082   */
083  protected String[] fontSizes_ =
084  {"+8", "+7", "+6", "+5", "+4", "+3", "+2", "+1" , "+0", "-1", "-2", "-3", "-4", "-5", "-6", "-7", "-8"};
085  /**
086   *  Description of the Field
087   */
088  protected String fontSize_ = "-1";
089  /**
090   *  Description of the Field
091   */
092  protected int fontMidpoint_ = 11;
093  /**
094   *  Description of the Field
095   */
096  protected String boldOn_ = "";
097  /**
098   *  Description of the Field
099   */
100  protected String boldOff_ = "";
101  /**
102   *  Description of the Field
103   */
104  protected String italicOn_ = "";
105  /**
106   *  Description of the Field
107   */
108  protected String italicOff_ = "";
109  /**
110   *  Flags if this text gets wrapped.
111   */
112  protected boolean wrapText_ = true;
113  /**
114   *  how many characters in the text before a wrap.
115   */
116  protected short wrapCharWidth_ = 60;
117
118  ca.bc.webarts.tools.Log log_ = ca.bc.webarts.tools.Log.createLog( ca.bc.webarts.tools.Log.DEBUG );
119
120  /**
121   *  Basic constructor to set up the Label with the specified colour.
122   *
123   *@param  labelText  the text in the label
124   */
125  public ColouredLabel( String labelText )
126  {
127    log_ = ca.bc.webarts.tools.Log.createLog( ca.bc.webarts.tools.Log.DEBUG );
128    log_.startMethod();
129    labelText_ = labelText;
130    setText( labelText );
131    log_.endMethod();
132  }
133
134  /**
135   *  Constructor to set up the Label with the specified colour.
136   *
137   *@param  textColour  is the Color to use in the text in the label
138   *@param  labelText   the text in the label
139   */
140  public ColouredLabel( Color textColour, String labelText )
141  {
142    log_ = ca.bc.webarts.tools.Log.createLog( ca.bc.webarts.tools.Log.DEBUG );
143    log_.startMethod();
144    labelText_ = labelText;
145    textColour_ = textColour;
146    setText( labelText );
147    log_.endMethod();
148  }
149
150  /**
151   *  Constructor to set up the Label with the specified colours.
152   *
153   *@param  backColour  is the Color to use in the back of the label
154   *@param  textColour  is the Color to use in the text in the label
155   *@param  labelText   the text in the label
156   */
157  public ColouredLabel( Color backColour, Color textColour, String labelText )
158  {
159    log_ = ca.bc.webarts.tools.Log.createLog( ca.bc.webarts.tools.Log.DEBUG );
160    log_.startMethod();
161    labelText_ = labelText;
162    textColour_ = textColour;
163    backColour_ = backColour;
164    setText( labelText );
165    log_.endMethod();
166  }
167
168  /**
169   *  Constructor to set up the Label with the specified colours.
170   *
171   *@param  backColour  is the Color to use in the back of the label
172   *@param  textColour  is the Color to use in the text in the label
173   *@param  labelText   the text in the label
174   */
175  public ColouredLabel( Color backColour, Color textColour, String labelText, boolean wrapText )
176  {
177    log_ = ca.bc.webarts.tools.Log.createLog( ca.bc.webarts.tools.Log.DEBUG );
178    log_.startMethod();
179    labelText_ = labelText;
180    textColour_ = textColour;
181    backColour_ = backColour;
182    wrapText_ = wrapText;
183    setText( labelText );
184    log_.endMethod();
185  }
186
187  /**
188   *  Constructor to set up the Label with the specified colours.
189   *
190   *@param  backColour  is the Color to use in the back of the label
191   *@param  textColour  is the Color to use in the text in the label
192   *@param  newFont     is the Font to use in the text in the label
193   *@param  labelText   the text in the label
194   */
195  public ColouredLabel( Color backColour, Color textColour, Font newFont, String labelText )
196  {
197    log_ = ca.bc.webarts.tools.Log.createLog( ca.bc.webarts.tools.Log.DEBUG );
198    log_.startMethod();
199    labelText_ = labelText;
200    textColour_ = textColour;
201    backColour_ = backColour;
202    setFont( newFont );
203    setText( labelText );
204    log_.endMethod();
205  }
206
207  /**
208   *  Constructor to set up the Label with the specified colours.
209   *
210   *@param  backColour  is the Color to use in the back of the label
211   *@param  textColour  is the Color to use in the text in the label
212   *@param  newFont     is the Font to use in the text in the label
213   *@param  labelText   the text in the label
214   */
215  public ColouredLabel( Color backColour, Color textColour, Font newFont, String labelText, boolean wrapText )
216  {
217    log_ = ca.bc.webarts.tools.Log.createLog( ca.bc.webarts.tools.Log.DEBUG );
218    log_.startMethod();
219    labelText_ = labelText;
220    textColour_ = textColour;
221    backColour_ = backColour;
222    wrapText_ = wrapText;
223    setFont( newFont );
224    setText( labelText );
225    log_.endMethod();
226  }
227
228  /**
229   *  Set the number of chars before it fits in a wrap by inserting a <code><br /></code>.
230   *
231   *@param  numChars  the number of chars before it fits in a wrap
232   */
233  public void setWrapCharWidth( short numChars )
234  {
235    log_.startMethod();
236    wrapCharWidth_ = numChars;
237    log_.endMethod();
238  }
239
240  /**
241   *  Forces the display of the label text to the specified wrapping.
242   *
243   *@param  wrapText  flags if the text in the label will wrap or not
244   */
245  public void setWrapText( boolean wrapText )
246  {
247    log_.startMethod();
248    wrapText_ = wrapText;
249    log_.endMethod();
250  }
251
252  private String addWrap( String text, short wrapChars )
253  {
254    log_.startMethod();
255    for ( short i =0; i < text.length(); i += wrapChars )
256    {
257      text = text.substring(0, i ) + "<br />" + text.substring( i );
258    }
259
260    log_.endMethod();
261    return text;
262  }
263
264  /**
265   *  Overrides the JLabel setText to add the extra stuff to get the extra
266   *  colours and font stuff.
267   *
268   *@param  newText  the text to go in the label.
269   */
270  public void setText( String newText )
271  {
272    log_.startMethod();
273    if ( wrapText_ )
274    {
275      if ( newText.length() > wrapCharWidth_ )
276      {
277        newText = addWrap( newText, wrapCharWidth_ );
278        newText = Util.tokenReplace( newText, " ", "&nbsp;" );
279      }
280    }
281    super.setText( "<html><body bgcolor=\"" + getBackColourHtmlString() + "\" text=\"" + getTextColourHtmlString() + "\"><Font face=\"" + fontName_ + ", " + DEFAULT_FONT_FACE + "\" size=\"" + fontSize_ + "\">" + boldOn_ + italicOn_ + newText + italicOff_ + boldOff_ + "</font></body></html>" );
282    /*
283         *  super.setText("<Font face=\"" +
284         *  fontName_ + ", " + DEFAULT_FONT_FACE + "\" size=\"" +
285         *  fontSize_ + "color=\""+getTextColourHtmlString()+"\">" +
286         *  boldOn_ + italicOn_ + newText + italicOff_ + boldOff_ +
287         *  "</font>");
288    */
289    labelText_ = newText;
290    log_.endMethod();
291  }
292
293  /**
294   *  Allows setting of the Font to use in this label. It parses the Face,
295   *  Italic and Bold attributes.
296   *
297   *@param  newFont  the new font to use for this ColouredLabel.
298   */
299  public void setFont( Font newFont )
300  {
301    log_.startMethod();
302    super.setFont( newFont );
303    fontName_ = newFont.getName();
304    if ( newFont.isBold() )
305    {
306      boldOn_ = "<b>";
307      boldOff_ = "</b>";
308    }
309    else
310    {
311      boldOn_ = "";
312      boldOff_ = "";
313    }
314    if ( newFont.isItalic() )
315    {
316      italicOn_ = "<i>";
317      italicOff_ = "</i>";
318    }
319    else
320    {
321      italicOn_ = "";
322      italicOff_ = "";
323    }
324    if ( fontSizes_ != null )
325    {
326      // need to check... super calls this before init
327
328      int fontSizeLookup = fontMidpoint_ - newFont.getSize() + fontSizes_.length / 2;
329      fontSize_ = fontSizes_[fontSizeLookup];
330      coloursChanged();
331    }
332    log_.endMethod();
333  }
334
335  /**
336   *  A convienience method to directly set the Font using the face name
337   *  instead of sending a Font object.
338   *
339   *@param  fontName  The new fontName value
340   */
341  public void setFontName( String fontName )
342  {
343    log_.startMethod();
344    fontName_ = fontName;
345    coloursChanged();
346    log_.endMethod();
347  }
348
349  /**
350   *  Sets the current Text Colour being used in this label.
351   *
352   *@param  textColour  the current Text Colour to use in this label.
353   */
354  public void setTextColour( Color textColour )
355  {
356    log_.startMethod();
357    textColour_ = textColour;
358    coloursChanged();
359    log_.endMethod();
360  }
361
362  /**
363   *  Sets the current background Text Colour being used in this label.
364   *
365   *@param  backColour  the current background Colour to use in this label.
366   */
367  public void setBackColour( Color backColour )
368  {
369    log_.startMethod();
370    backColour_ = backColour;
371    coloursChanged();
372    log_.endMethod();
373  }
374
375  /**
376   *  Get method for the current Text Colour being used in this label.
377   *
378   *@return    the current Text Colour being used in this label.
379   */
380  public Color getTextColour()
381  {
382    return textColour_;
383  }
384
385  /**
386   *  Get method for the current Background Colour being used in this label.
387   *
388   *@return    the current Background Colour being used in this label.
389   */
390  public Color getBackColour()
391  {
392    return backColour_;
393  }
394
395  /**
396   *  Get the HTML string representing the Current Text Colour.
397   *
398   *@return    The textColourHtmlString value
399   */
400  private String getTextColourHtmlString()
401  {
402    log_.startMethod();
403    log_.endMethod();
404    return getTextColourHtmlString( textColour_ );
405  }
406
407  /**
408   *  Get the HTML string representing the Current Background Colour.
409   *
410   *@return    The backColourHtmlString value
411   */
412  private String getBackColourHtmlString()
413  {
414     return getTextColourHtmlString( backColour_ );
415  }
416
417  /**
418   *  Get the HTML string representing the specified Color.
419   *
420   *@param  c  is the Color to retrieve the html string for.
421   *@return    The textColourHtmlString value
422   */
423  public String getTextColourHtmlString( Color c )
424  {
425    log_.startMethod();
426    String r = "FF";
427    String g = "FF";
428    String b = "FF";
429
430    if ( c != null )
431    {
432      r = Integer.toHexString( c.getRed() );
433      g = Integer.toHexString( c.getGreen() );
434      b = Integer.toHexString( c.getBlue() );
435    }
436    String retVal = "#" + r + g + b;
437    log_.endMethod();
438    return retVal;
439  }
440
441  /**
442   *  Refreshes the text string with some new colour values.
443   */
444  private void coloursChanged()
445  {
446    log_.startMethod();
447    setText( labelText_ );
448    log_.endMethod();
449  }
450
451  public static void main( String[] args )
452  {
453    JFrame f = new JFrame( "ColouredLabel Demo Frame" );
454    JPanel p = new JPanel();
455    p.setLayout( new BorderLayout() );
456    f.getContentPane().setLayout( new BorderLayout() );
457    // f.setTitle("PopUpButton TEST Window.");
458
459    ColouredLabel l =    // Color backColour,
460    // Color textColour,
461    // String labelText,
462    // boolean wrapText
463    new ColouredLabel( Color.DARK_GRAY, Color.RED, "Demo Coloured Label", true );
464    p.add( l, BorderLayout.CENTER );
465    final JButton myTestButton = new JButton( "Close" );
466    f.addWindowListener ( new WindowAdapter()
467    {
468      public void windowClosing( WindowEvent e )
469      {
470        System.exit(0 );
471      }
472    } );
473    myTestButton.setText( "Press Me to close" );
474    p.add( myTestButton, BorderLayout.SOUTH );
475    f.getContentPane().add( p );
476    f.pack();
477    // KiwiUtils.centerWindow(appFrame_);
478    f.toFront();
479    f.setVisible( true );
480    f.repaint();
481
482  }
483}
484
485/*
486 *  Here is the revision log
487 *  ------------------------
488 *  $Log: ColouredLabel.java,v $
489 *  Revision 1.4  2002/04/02 22:10:23  anonymous
490 *  Added CVS Headerv And Log.
491 *
492*/
493