001/*
002 *  $Source: /cvsroot2/open/projects/WebARTS/ca/bc/webarts/widgets/AutoButtonBar.java,v $
003 *  $Name:  $
004 *  Current File Status:   $Revision: 567 $
005 *  $Date: 2012-11-03 20:36:02 -0700 (Sat, 03 Nov 2012) $
006 *  $Locker:  $
007 *  Copyright (C) 2001-2004 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; version 2 of the License.
013 *
014 *  This program is distributed in the hope that it will be useful,
015 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
016 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 *  GNU General Public License for more details.
018 *
019 *  You should have received a copy of the GNU General Public License
020 *  along with this program; if not, write to the Free Software
021 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
022 */
023package ca.bc.webarts.widgets;
024
025import ca.bc.webarts.widgets.StatusBar;
026
027import java.awt.FlowLayout;
028import java.awt.Insets;
029import java.awt.Dimension;
030import java.awt.event.ActionListener;
031import java.net.URL;
032import javax.swing.*;
033import java.lang.Class;
034
035/**
036 *  A Tool Bar with multiple Icon Rollover enabled Buttons.
037 *  All creation is done automatically based on the String Arrays
038 *  <code>buttonIconName</code> and <code>buttonToolTip</code>
039 *  which hold the icon filenames and tooltip text repecively for each Button.
040 *
041 * @author     <A HREF="http://www.webarts.bc.ca/~tgutwin">Tom Gutwin</A>
042 * @created    November 17, 2000
043 */
044public class AutoButtonBar extends JToolBar
045{
046
047  /**
048   *  An array of strings that hold the names of the image files that will
049   *  be used for the button icons.  The order of the entries in this array
050   *  represents the order they will be added to the JToolBar.
051   *  <P><B><I>SPECIAL NOTE</I></B><BR>To get the names of the 'pressed' and
052   * 'hoverover' icon filenames, this class looks for a file with
053   *  the prepended words 'Pressed' and 'Over' to the Strings supplied in
054   *  the buttonIconName array.
055   *  <P>Example... <BR>icon filename = "Print.gif"<BR>hoverover icon
056   *  filename = "OverPrint.gif"<BR>pressed icon filename = "PressedPrint.gif"
057   *
058   */
059  public String [] buttonIconName;
060
061
062  /**
063   *  An array of Strings for the botton tool tip text.
064   */
065  public String [] buttonToolTip;
066
067
068  /**
069   *  The Codes Base URL.
070   */
071  private URL codeBase;
072
073
074  /**
075   *  A URL to hold the images for the buttons.
076   */
077  URL tempURL;
078  /**
079   *  A holder for the the users filesystem separator.
080   */
081  String fileSeperator = new String(System.getProperty("file.separator"));
082  /**
083   *  Description of the Field
084   */
085  private JButton [] toolButton;
086  /**
087   *  Description of the Field
088   */
089  private int numberOfFiles, i, debug = 3;
090  /**
091   *  Description of the Field
092   */
093  private StatusBar statusBar;
094  /**
095   * The relative resource locator to find the image files, relative to
096   * the baseURL.
097   **/
098  private String relImageLocator = "";
099
100  /**
101   * The Class Object representing the controller class that will be called to
102   * deal with this buttonBars action events.
103   **/
104  private static ActionListener controller = null;
105
106
107  /**
108   *  CONSTRUCTOR Creates a new AutoButtonBar but DOES NOT init its buttons.
109   */
110   public AutoButtonBar(    URL cb,
111                       String[] iconNames,
112                       String[] iconToolTips,
113                        Object  handler)
114   {
115
116    debugOutput(2, "Auto Creating the Tool Button Bar....");
117    buttonIconName = iconNames;
118    buttonToolTip = iconToolTips;
119    codeBase = cb ;
120
121    if (cb == null)
122    {
123      try
124      {
125        codeBase = new URL("FILE:/./");
126      }
127      catch (java.net.MalformedURLException t)
128      {
129        System.out.println("caught exception: " + t );
130        t.printStackTrace();
131      }
132    }
133    else
134      codeBase = cb;
135
136    setIcons(buttonIconName,buttonToolTip);
137    /* Set out the layout manager */
138    setLayout(new FlowLayout(FlowLayout.LEFT));
139    getInsets(new Insets(0, 0, 0, 0));
140    setMargin(new Insets(0, 0, 0, 0));
141
142    setController(handler);
143
144    if (iconNames.length == 0 )
145    {
146      debugOutput(1, "ERROR Creating the Tool Button Bar....");
147    }
148    else
149      createButtons();
150   }
151
152  /**
153   *  CONSTRUCTOR Creates a new AutoButtonBar but DOES NOT init its buttons.
154   */
155   public AutoButtonBar(URL cb)
156   {
157    debugOutput(2, "Auto Creating the Tool Button Bar....");
158    codeBase = cb ;
159
160    if (cb == null)
161      try
162      {
163        codeBase = new URL("FILE:/./");
164      }
165      catch (java.net.MalformedURLException t)
166      {
167        System.out.println("caught exception: " + t );
168        t.printStackTrace();
169      }
170    else
171      codeBase = cb;
172
173    /* Set out the layout manager */
174    setLayout(new FlowLayout(FlowLayout.LEFT));
175    getInsets(new Insets(0, 0, 0, 0));
176    setMargin(new Insets(0, 0, 0, 0));
177
178   }
179
180  /**
181   *  CONSTRUCTOR Creates a new AutoButtonBar that is assciated with the
182   *  passed in JInternalFrame components.
183   *
184   * @param  sb          The StatusBar that the functions of the buttons in
185   *                     this JToolBar will be associated with.
186   * @param  codeBase    The URL that all references to files will be
187   *                     appended to.
188   * @param  mdiPanel  Description of Parameter
189   */
190  public AutoButtonBar(JPanel mdiPanel, StatusBar sb, URL cBase) {
191    debugOutput(2, "Auto Creating the Tool Button Bar....");
192    statusBar = sb;
193    controller = null;
194    codeBase = cBase;
195    /* Set out the layout manager */
196    setLayout(new FlowLayout(FlowLayout.LEFT));
197    getInsets(new Insets(0, 0, 0, 0));
198    setMargin(new Insets(0, 0, 0, 0));
199    createButtons();
200  }
201
202   public void setIcons( String[] iconNames,
203                         String[] iconToolTips)
204   {
205    if (iconNames == null)
206      buttonIconName = new String[0];
207    else
208    {
209      buttonIconName = iconNames;
210      debugOutput(2,buttonIconName[0] );
211    }
212    if (iconToolTips == null)
213      buttonToolTip = new String[0];
214    else
215      buttonToolTip = iconToolTips;
216
217    toolButton = new JButton[buttonIconName.length];
218}
219
220   public boolean setController(Object handler)
221   {
222    Class [] interf = handler.getClass().getInterfaces();
223    boolean isAListener = false;
224    try
225    {
226      Class AListener = Class.forName("java.awt.event.ActionListener");
227
228      debugOutput(2, "Checking for a Listener....");
229      for (int i=0; i < interf.length;i++)
230      {
231        if (interf[i] == AListener)
232        {
233          isAListener = true;
234          continue;
235        }
236      }
237    }
238    catch (ClassNotFoundException ex)
239    {
240      debugOutput(2, "ClassNotFoundException....");
241    }
242
243    if (isAListener)
244    {
245      debugOutput(2, "      Got A Listener....");
246      controller = (ActionListener) handler;
247    }
248    return isAListener;
249   }
250
251
252  /**
253   * Create the buttons based on the initialized arrays of information.
254   **/
255   public void createButtons()
256   {
257    for (i = 0; i < buttonIconName.length; i++)
258    {
259      if (!buttonIconName[i].equals("---"))
260      {
261        try
262        {
263          /* create button  and set images */
264//System.out.println("ButtonIcon "  + (new Integer(i)).toString() +
265//                   " URL=" + codeBase + relImageLocator+"/"+ buttonIconName[i]);
266
267          tempURL = new URL(codeBase + relImageLocator+"/"+ buttonIconName[i]);
268//System.out.println("ButtonIconURL "  + (new Integer(i)).toString() +
269//                   " URL=" + tempURL.toString());
270          toolButton[i] = new JButton(buttonIconName[i], new ImageIcon(tempURL));
271          tempURL = new URL(codeBase,  relImageLocator+"/Pressed" + buttonIconName[i]);
272          toolButton[i].setPressedIcon(new ImageIcon(tempURL));
273          tempURL = new URL(codeBase,  relImageLocator+"/Over" + buttonIconName[i]);
274          toolButton[i].setRolloverIcon(new ImageIcon(tempURL));
275          toolButton[i].setBorderPainted(false);
276          toolButton[i].setToolTipText(buttonToolTip[i]);
277        }
278        catch (java.net.MalformedURLException t)
279        {
280          System.out.println("caught exception: " + t +
281                             "Cannot retrieve images");
282          t.printStackTrace();
283        }
284
285        /* set the buttons margins */
286        toolButton[i].setMargin(new Insets(1, 1, 1, 1));
287
288        /* add the listeners to the buttons */
289        if (controller != null)
290          toolButton[i].addActionListener(controller);
291
292        /* add the buttons to the toolBar */
293        add(toolButton[i]);
294      }
295      else
296      {
297        addSeparator(new Dimension(3, 3));
298      }
299      add(Box.createHorizontalGlue());
300    }
301   }
302
303
304   /**
305    * Sets the relative pathe field that is used ad a relative path the
306    * iconnames.
307    **/
308    public void setRelImageLocator(String s ) { relImageLocator = s;}
309
310
311   /**
312    * Sets the buttonIconName field.
313    */
314    public void setButtonIconName(String[] s) { buttonIconName = s;}
315
316
317   /**
318    * Sets the buttonToolTip field.
319    */
320    public void setButtonToolTip(String[] s) { buttonToolTip = s;}
321
322
323   /**
324   *  Sets the Debug attribute of the AutoButtonBar object
325   *
326   * @param  l  The new Debug value
327   */
328  public void setDebug(int l) {
329    debug = l;
330  }
331
332
333  /**
334   *  Gets the Debug attribute of the AutoButtonBar object
335   *
336   * @return    The Debug value
337   */
338  public int getDebug() {
339    return debug;
340  }
341
342
343  /**
344   *  Description of the Method
345   *
346   * @param  s  Description of Parameter
347   * @return    Description of the Returned Value
348   */
349  private int indexOf(String s) {
350    int i;
351    int retVal = 0;
352    for (i = 0; i < buttonIconName.length; i++)
353      if (s.equals(buttonToolTip[i])) {
354        retVal = i;
355        i = buttonIconName.length;
356      }
357    return retVal;
358  }
359
360
361  /**
362   *  Handles the debug messages by comparing the app debug level to the
363   *  message level. <P>If the message level is less or == then the message
364   *  gets output to System.out.
365   *
366   * @param  l       Description of Parameter
367   * @param  output  Description of Parameter
368   */
369  private void debugOutput(int l, String output) {
370    if (l <= debug)
371      System.out.println(output);
372  }
373
374
375}
376
377
378