001/*
002 *  jAckup.java
003 *  $Source: f:/cvsroot2/open/projects/jAckup/ca/bc/webarts/tools/jackup/jAckup.java,v $
004 *  $Name:  $
005 *  $Revision: 1.8 $
006 *  $Date: 2005-04-10 11:32:43 -0700 (Sun, 10 Apr 2005) $
007 *  Copyright (c) 2002 Tom B. Gutwin P.Eng.
008 *
009 *  This program is free software; you can redistribute it and/or
010 *  modify it under the terms of the GNU General Public License
011 *  as published by the Free Software Foundation; either version 2
012 *  of the License, or any later version.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
022 */
023package ca.bc.webarts.tools.jackup;
024
025import ca.bc.webarts.tools.Log;
026
027import ca.bc.webarts.widgets.ClassConstants;
028import ca.bc.webarts.widgets.ColouredLabel;
029import ca.bc.webarts.widgets.JAboutBox;
030import ca.bc.webarts.widgets.Splash;
031import ca.bc.webarts.widgets.Util;
032
033import java.awt.BorderLayout;
034import java.awt.Color;
035import java.awt.Component;
036import java.awt.Event;
037import java.awt.Font;
038import java.awt.Image;
039import java.awt.Label;
040import java.awt.Panel;
041import java.awt.event.ActionEvent;
042import java.awt.event.ActionListener;
043import java.awt.event.KeyEvent;
044import java.awt.event.WindowAdapter;
045import java.awt.event.WindowEvent;
046
047import java.io.File;
048
049import java.net.URL;
050
051import java.util.Iterator;
052
053import javax.swing.ImageIcon;
054import javax.swing.JButton;
055import javax.swing.JFrame;
056import javax.swing.JMenu;
057import javax.swing.JMenuBar;
058import javax.swing.JMenuItem;
059import javax.swing.JOptionPane;
060import javax.swing.JPanel;
061import javax.swing.JTabbedPane;
062import javax.swing.KeyStroke;
063
064import org.apache.log4j.Logger;
065import org.apache.log4j.LogManager;
066import org.apache.log4j.Priority;
067import org.apache.log4j.PropertyConfigurator;
068
069import org.dom4j.Document;
070import org.dom4j.DocumentException;
071import org.dom4j.Element;
072import org.dom4j.io.OutputFormat;
073import org.dom4j.io.SAXReader;
074import org.dom4j.io.XMLWriter;
075import org.dom4j.util.XMLErrorHandler;
076
077import org.xml.sax.SAXException;
078
079
080/**
081 *  jAckup Backup Utility.
082 *
083 * @author     tgutwin
084 * @created    August 12, 2002
085 */
086public class jAckup extends JFrame implements AutoMenuBarUser
087{
088  /**  Constant holding the users file seperator. ("/" or "\")  */
089  private final static String SYSTEM_FILE_SEPERATOR = File.separator;
090
091  /**  Constant holding the users file seperator. ("/" or "\")  */
092  private final static String SYSTEM_LINE_SEPERATOR =
093    System.getProperty("line.separator");
094
095  /**  The application name string  */
096  private final static String APP_NAME = "jAckup";
097
098  /**  The default jAckup Plan Filename that gets read in on a NEW command.  */
099  private final static String JACKUPPLAN_DEFAULT_FILENAME =
100    "jAckupPlanDefault.xml";
101
102  /**  A Class holder for its name (used in Logging).  */
103  private static String className_ = APP_NAME;
104
105  /**  The log4j logger object. */
106  protected static Logger logger_ = Logger.getLogger(jAckup.class.getName());
107
108  /**  A generic Class accessable Label that is used throughout for Debug
109   *   Stmnts to the screen. */
110  protected ColouredLabel myDebugLabel_ =
111    new ColouredLabel(new Color(5,5,100),
112                      Color.white,
113                      new Font("SanSerif", Font.PLAIN, 11),
114                      "Debug Label.");
115
116  /**  The XML jAckup Plan Document.  */
117  private Document jAckupPlanDoc_ = null;
118
119  /** Flags if jAckup will validate docs when read in **/
120  private static boolean validatingXsd_ = true;
121
122  /**
123   *  The MenuBar. It gets initialized by the method <code>initMenuBar</code>
124   *  of the AutoMenuBar class.
125   *
126   * @see    initMenuBar
127   */
128  private AutoMenuBar menuBar_ = null;
129
130  /**  Description of the Field */
131  private final static int NEW_MENUREF = 100;
132  /**  Description of the Field */
133  private final static int OPEN_MENUREF = 101;
134  /**  Description of the Field */
135  private final static int SAVE_MENUREF = 102;
136  /**  Description of the Field */
137  private final static int PRINT_MENUREF = 104;
138  /**  Description of the Field */
139  private final static int PRINTPREVIEW_MENUREF = 105;
140  /**  Description of the Field */
141  private final static int CLOSE_MENUREF = 107;
142  /**  Description of the Field */
143  private final static int EXIT_MENUREF = 108;
144  /**  Description of the Field */
145  private final static int FILESELECTIONS_MENUREF = 200;
146  /**  Description of the Field */
147  private final static int ARCHIVEFORMAT_MENUREF = 201;
148  /**  Description of the Field */
149  private final static int DEBUGLEVEL_MENUREF = 300;
150  /**  Description of the Field */
151  private final static int JLF_MENUREF = 302;
152  /**  Description of the Field */
153  private final static int PLF_MENUREF = 303;
154  /**  Description of the Field */
155  private final static int MLF_MENUREF = 304;
156  /**  Description of the Field */
157  private final static int HELP_MENUREF = 400;
158  /**  Description of the Field */
159  private final static int ABOUT_MENUREF = 402;
160
161
162  /**  Basic constructor for this Object.  */
163  public jAckup()
164  {
165    final String methodName = className_ + ": Constructor()";
166    logger_.debug("Entering " + methodName);
167
168    logger_.debug("Exiting " + methodName);
169  }
170
171
172  /**
173   *  Initializes the GUI Main Area Panel (the tabbed pane, the
174   *  inner panels etc.)
175   *
176   * @return JPanel the panel to go into the Main center area of jAckup
177   */
178  private JPanel initMainArea()
179  {
180    final String methodName = className_ + ": initMainArea()";
181    logger_.debug("Entering " + methodName);
182
183    JPanel retVal = new JPanel(new BorderLayout());
184    JTabbedPane tabbedPane = new JTabbedPane();
185    JPanel tab1Content = new JPanel(new BorderLayout());
186    JPanel tab2Content = new JPanel(new BorderLayout());
187    JPanel tab3Content = new JPanel(new BorderLayout());
188    JPanel tab4Content = new JPanel(new BorderLayout());
189    tab1Content.add(myDebugLabel_,"Center");
190    //tab2Content.add(myDebugLabel_,"Center");
191    //tab3Content.add(myDebugLabel_,"Center");
192    //tab4Content.add(myDebugLabel_,"Center");
193    tabbedPane.add("Tab1", tab1Content);
194    tabbedPane.add("Tab2", tab2Content);
195    tabbedPane.add("Tab3", tab3Content);
196    tabbedPane.add("Tab4", tab4Content);
197    tabbedPane.setForeground(Color.black);
198    tabbedPane.setBackgroundAt(0,new Color(200,255,255));
199    tabbedPane.setBackgroundAt(1,new Color(255,255,200));
200    tabbedPane.setBackgroundAt(2,new Color(255,200,255));
201    tabbedPane.setBackgroundAt(3,new Color(200,200,255));
202    tabbedPane.setToolTipTextAt(0,"Tab1 ToolTip");
203    tabbedPane.setToolTipTextAt(1,"Tab2 ToolTip");
204    tabbedPane.setToolTipTextAt(2,"Tab3 ToolTip");
205    tabbedPane.setToolTipTextAt(3,"Tab4 ToolTip");
206    retVal.add(tabbedPane,"Center");
207
208    logger_.debug("Exiting " + methodName);
209    return retVal;
210  }
211
212
213  /**
214   *  Initializes the GUI upon startup. Sets up the menu, the tabbed pane, the
215   *  inner panels etc.
216   */
217  private void initGui()
218  {
219    final String methodName = className_ + ": initGui()";
220    logger_.debug("Entering " + methodName);
221
222
223    /*
224     *  create the menu bar
225     */
226    menuBar_ = new AutoMenuBar(this); //initMenuBar();
227    menuBar_.addListeners();
228    JPanel mainAreaPanel = initMainArea();
229
230    getContentPane().add(menuBar_, "North");
231    getContentPane().add(mainAreaPanel, "Center");
232
233
234    logger_.debug("Exiting " + methodName);
235  }
236
237
238  /**  Forces a repaint to this apps menubar.  */
239  private void repaintMenuBar()
240  {
241    final String methodName = className_ + ": repaintMenuBar()";
242
243
244    if (menuBar_ != null)
245    {
246      menuBar_.repaint();
247    }
248
249
250  }
251
252
253  /**
254   *  Adds an ActionListener to the JMenuItem that is supplied in the passed
255   *  param.
256   *
257   * @param  menuItem  The feature to be added to the Listener attribute
258   */
259  public void addListener(JMenuItem menuItem)
260  {
261    // Logger Trace Method Enter
262    final String methodName = className_ + ": addListener(JMenuItem)";
263    logger_.debug("Entering " + methodName);
264
265    /*  use a simple indexOf helper function to look through the menu
266     *  items and return an int to represent its HASH position   */
267    String toolTipText = menuItem.getToolTipText();
268    int indexRef = menuBar_.indexOf(toolTipText);
269
270    StringBuffer sb = new StringBuffer(" Adding a listener for menu item ");
271    sb.append(toolTipText);
272    sb.append(" ");
273    sb.append(indexRef);
274    logger_.debug(sb.toString());
275
276    final Component finalMenuItem = menuItem;
277    /**  The log4j logger final object. */
278    final Logger finalLogger = logger_;
279    switch (indexRef)
280    {
281        case NEW_MENUREF:
282          /*
283           *  NEW jAckup Plan with defaults
284           */
285          menuItem.addActionListener(new newActionListener(finalMenuItem));
286          break;
287
288        case OPEN_MENUREF:
289          /*
290           *  Open  from File
291           */
292          menuItem.addActionListener(new openActionListener(finalMenuItem));
293          break;
294
295        case SAVE_MENUREF:
296          /*
297           *  Save Window Data To File
298           */
299          menuItem.addActionListener(new saveActionListener(finalMenuItem));
300          break;
301
302        case PRINT_MENUREF:
303          /*
304           *  print
305           */
306          menuItem.addActionListener(new printActionListener(finalMenuItem));
307          break;
308
309        case PRINTPREVIEW_MENUREF:
310          /*
311           *  print preview
312           */
313          menuItem.addActionListener(
314            new printpreviewActionListener(finalMenuItem));
315          break;
316
317        case CLOSE_MENUREF:
318          /*
319           *  close
320           */
321          menuItem.addActionListener(
322            new closeActionListener(finalMenuItem));
323          break;
324
325        case EXIT_MENUREF:
326          /*
327           *  exit
328           */
329          menuItem.addActionListener(
330            new exitActionListener(finalMenuItem));
331          break;
332
333        case DEBUGLEVEL_MENUREF:
334          /*
335           *  Set The Debug Level
336           */
337          menuItem.addActionListener(
338            new debuglevelActionListener(finalMenuItem));
339          break;
340
341        case JLF_MENUREF:
342          /*
343           *  Set the L&F to Java
344           */
345          menuItem.addActionListener(
346            new jlfActionListener(finalMenuItem));
347          break;
348
349        case PLF_MENUREF:
350          /*
351           *  Set the L&F to Platform
352           */
353          menuItem.addActionListener(
354            new plfActionListener(finalMenuItem));
355          break;
356
357        case MLF_MENUREF:
358          /*
359           *  Set the L&F to Motif
360           */
361           menuItem.addActionListener(
362            new mlfActionListener(finalMenuItem));
363          break;
364
365        case HELP_MENUREF:
366          /*
367           *  Open the Help window
368           */
369          menuItem.addActionListener(
370            new helpActionListener(finalMenuItem));
371          break;
372
373        case ABOUT_MENUREF:
374          /*
375           *  Open the About Box
376           */
377          menuItem.addActionListener(
378            new aboutActionListener(finalMenuItem));
379          break;
380
381        default:
382          // Logger Trace Method Enter
383          finalLogger.warn("Unknown MenuItem Reference. Cannot Add A Listener:"+
384                            indexRef);
385
386    }
387    logger_.debug("Exiting " + methodName);
388
389  }
390
391
392  /**
393   *  Gets the Log object that the jAckup instance is using.
394   *
395   * @return    The Log.
396   */
397  public Logger getLogger()
398  {
399    final String methodName = className_ + ": getLogger()";
400    return logger_;
401  }
402
403
404  /**
405   *  The main program for the jAckup class
406   *
407   * @param  arg  The command line arguments
408   */
409  public static void main(String[] arg)
410  {
411    // log4j logger config
412    PropertyConfigurator.configure("/logs/log4jConfig.txt");
413    logger_.debug("Entering jAckup:main(String[])");
414
415    String splashGraphic = "/images/dont_pan.jpg";
416    logger_.debug("Loading Splash Graphic.");
417    Image splashImage = Util.loadImage(splashGraphic);
418    Panel p = new Panel();
419    /*
420     *  p.setBorder(new CompoundBorder(
421     *  new MatteBorder(1,1,1,1,Color.black),
422     *  new EmptyBorder(12,12,12,12)));
423     */
424    p.setBackground(new Color(210, 210, 210));
425    Label l = new Label("Please wait while loading.");
426    p.add(l);
427    logger_.debug("Splashing.");
428    Splash frame = new Splash(splashImage, 8000, p);
429
430    logger_.debug("Instantiating the jAckup Object.");
431    final jAckup classInstance = new jAckup();
432    Util.setIconForApp(classInstance, "/images/jAckupIcon.jpg");
433
434    classInstance.addWindowListener(
435      new WindowAdapter()
436      {
437        public void windowClosing(WindowEvent e)
438        {
439          logger_.info("Window Closing - Close Logger and exit!");
440          //close up the log then vamoose
441          LogManager.shutdown();
442          System.exit(0);
443        }
444      }
445    );
446
447    classInstance.initGui();
448    classInstance.pack();
449    classInstance.toFront();
450    frame.finishSplash();
451    classInstance.setVisible(true);
452
453
454    logger_.info("Exiting jAckup:main(String[])");
455  }
456
457
458  private class newActionListener implements ActionListener
459  {
460    /**  The log4j logger final object. */
461    final Logger finalLogger = jAckup.logger_;
462    Component menuItem = null;
463
464    public newActionListener(Component mi)
465    {
466      super();
467      menuItem = mi;
468    }
469    /**
470     *  Creates a new jAckup file with default settings.
471     *
472     * @param  event  The Initiating event
473     */
474    public void actionPerformed(ActionEvent event)
475    {
476      // Logger Trace Method Enter
477      final String innerClassMethodName =
478        "newActionListener.actionPerformed - " + NEW_MENUREF;
479      finalLogger.debug("Entering " + innerClassMethodName);
480
481      repaintMenuBar();
482      /*
483       *  now add the specific stuff for this Menu Item
484       */
485      finalLogger.debug("New MenuItem Selected");
486      // read in the default jAckup Plan Document
487      try
488      {
489        SAXReader reader = new SAXReader(validatingXsd_);
490        // specify the schema to use
491        reader.setProperty(
492         "http://apache.org/xml/properties/schema/external-"+
493         "noNamespaceSchemaLocation",
494         "http://warp2/xml/jAckupPlan.xsd"
495        );
496
497        // add an error handler which turns any errors into XML
498        XMLErrorHandler errorHandler = new XMLErrorHandler();
499        reader.setErrorHandler( errorHandler );
500
501        StringBuffer defaultFilename = new StringBuffer().append(
502          System.getProperty("user.home"));
503        defaultFilename.append(File.separator);
504        defaultFilename.append(JACKUPPLAN_DEFAULT_FILENAME);
505        //jAckupPlanDoc_ = jAckupPlan.getDefaultDocument();
506
507        // Parse the document
508        finalLogger.info("Reading the default jAckup Plan: " +
509          defaultFilename.toString());
510        jAckupPlanDoc_ = reader.read(defaultFilename.toString());
511        StringBuffer sb = new StringBuffer ();
512        if (errorHandler.getErrors() != null &&
513            errorHandler.getErrors().hasContent())
514        {
515          finalLogger.info("Default Plan Read In & Validated!");
516          sb.append("Success Reading the XML File: ");
517          sb.append(defaultFilename.toString());
518          sb.append(SYSTEM_LINE_SEPERATOR);
519          sb.append(jAckupPlanDoc_.getRootElement().getName());
520          sb.append(SYSTEM_LINE_SEPERATOR);
521          sb.append(jAckupPlanDoc_.getRootElement().getNamespace());
522          sb.append("<BR>");
523          Iterator i = jAckupPlanDoc_.getRootElement().elementIterator();
524          Element nextElement = null;
525          Element nextE = null;
526          Iterator i2 = null;
527          while (i.hasNext())
528          {
529            nextElement = (Element)i.next();
530            sb.append(nextElement.getName());
531            sb.append(" ");
532            //sb.append(nextElement.attributeValue("rootPath"));
533            sb.append("<BR>");
534            i2 = nextElement.elementIterator();
535            while (i2.hasNext())
536            {
537              nextE = ((Element)i2.next());
538              sb.append("   ...");
539              sb.append(nextE.getName());
540              sb.append(" = ");
541              sb.append(nextE.attributeValue("name"));
542              sb.append("<BR>");
543            }
544            sb.append("<BR>");
545          }
546        }
547        else
548          sb.append("Crppaed Out Reading the XML File: ");
549
550        finalLogger.debug(sb.toString());
551        myDebugLabel_.setText(sb.toString());
552      }
553      catch (DocumentException docEx)
554      {
555        finalLogger.warn("Problem reading DEFAULT jAckup Plan File.",
556          docEx);
557        JOptionPane.showMessageDialog(menuItem,
558            "The Default settings file cannot be parsed and validated \n" +
559            "to create a New jAckup Plan: ",
560            "jAckupPlanDefault.xml\n"+docEx.getMessage() ,
561            JOptionPane.ERROR_MESSAGE);
562      }
563      catch (Exception ex)
564      {
565        finalLogger.warn("Problem reading DEFAULT jAckup Plan File.",
566                         ex);
567        JOptionPane.showMessageDialog(menuItem,
568            "Something crapped Out:\n" + ex.getMessage(),
569            "jAckupPlanDefault.xml",
570            JOptionPane.ERROR_MESSAGE);
571      }
572      // logger method exit
573      finalLogger.debug("Exiting " + innerClassMethodName);
574    }
575  }
576
577
578  private class openActionListener implements ActionListener
579  {
580    /**  The log4j logger final object. */
581    final Logger finalLogger = jAckup.logger_;
582    Component menuItem = null;
583
584    public openActionListener(Component mi)
585    {
586      super();
587      menuItem = mi;
588    }
589    /**
590     *  Creates a new jAckup file with default settings.
591     *
592     * @param  event  The Initiating event
593     */
594    public void actionPerformed(ActionEvent event)
595    {
596
597      // Logger Trace Method Enter
598      final String innerClassMethodName =
599        "openActionListener.actionPerformed - " + OPEN_MENUREF;
600      finalLogger.debug("Entering " + innerClassMethodName);
601
602      repaintMenuBar();
603      /*
604       *  now add the specific stuff for this Menu Item
605       */
606      String chosenFileName = Util.chooseAFilename(menuItem,
607                                                   Util.USERHOME);
608      if (chosenFileName != null && !chosenFileName.equals(""))
609      {
610        try
611        {
612          SAXReader reader = new SAXReader(validatingXsd_);
613          jAckupPlanDoc_ = reader.read(chosenFileName);
614          StringBuffer sb = new StringBuffer (
615            "Success Reading the XML File: ");
616          sb.append(chosenFileName);
617          sb.append(SYSTEM_LINE_SEPERATOR);
618          sb.append(jAckupPlanDoc_.getRootElement().getName());
619          sb.append(SYSTEM_LINE_SEPERATOR);
620          sb.append(jAckupPlanDoc_.getRootElement().getNamespace());
621          finalLogger.debug(sb.toString());
622          myDebugLabel_.setText(sb.toString());
623        }
624        catch (DocumentException docEx)
625        {
626          finalLogger.warn("Problem reading/validating jAckup Plan File:"+
627            chosenFileName, docEx);
628          JOptionPane.showMessageDialog(menuItem,
629              "The jAckup file selected cannot be read or validated.",
630              chosenFileName,
631              JOptionPane.ERROR_MESSAGE);
632        }
633        catch (Exception ex)
634        {
635          finalLogger.warn("Problem reading jAckup Plan File:"+
636            chosenFileName, ex);
637          JOptionPane.showMessageDialog(menuItem,
638              "Something crapped Out:\n" + ex.getMessage(),
639              chosenFileName,
640              JOptionPane.ERROR_MESSAGE);
641        }
642      }
643      // logger method exit
644      finalLogger.debug("Exiting " + innerClassMethodName);
645    }
646  }
647
648
649  private class saveActionListener implements ActionListener
650  {
651    /**  The log4j logger final object. */
652    final Logger finalLogger = jAckup.logger_;
653    Component menuItem = null;
654
655    public saveActionListener(Component mi)
656    {
657      super();
658      menuItem = mi;
659    }
660    /**
661     *  Creates a new jAckup file with default settings.
662     *
663     * @param  event  The Initiating event
664     */
665    public void actionPerformed(ActionEvent event)
666    {
667
668      // Logger Trace Method Enter
669      final String innerClassMethodName =
670        "saveActionListener.actionPerformed - " + SAVE_MENUREF;
671      finalLogger.debug("Entering " + innerClassMethodName);
672
673      repaintMenuBar();
674      /*
675       *  now add the specific stuff for this Menu Item
676       */
677
678
679      // logger method exit
680      finalLogger.debug("Exiting " + innerClassMethodName);
681    }
682  }
683
684  private class printActionListener implements ActionListener
685  {
686    /**  The log4j logger final object. */
687    final Logger finalLogger = jAckup.logger_;
688    Component menuItem = null;
689
690    public printActionListener(Component mi)
691    {
692      super();
693      menuItem = mi;
694    }
695    /**
696     *  Creates a new jAckup file with default settings.
697     *
698     * @param  event  The Initiating event
699     */
700    public void actionPerformed(ActionEvent event)
701    {
702
703      // Logger Trace Method Enter
704      final String innerClassMethodName =
705        "printActionListener.actionPerformed - " + PRINT_MENUREF;
706      finalLogger.debug("Entering " + innerClassMethodName);
707
708      repaintMenuBar();
709      /*
710       *  now add the specific stuff for this Menu Item
711       */
712
713
714      // logger method exit
715      finalLogger.debug("Exiting " + innerClassMethodName);
716    }
717  }
718
719  private class printpreviewActionListener implements ActionListener
720  {
721    /**  The log4j logger final object. */
722    final Logger finalLogger = jAckup.logger_;
723    Component menuItem = null;
724
725    public printpreviewActionListener(Component mi)
726    {
727      super();
728      menuItem = mi;
729    }
730    /**
731     *  Creates a new jAckup file with default settings.
732     *
733     * @param  event  The Initiating event
734     */
735    public void actionPerformed(ActionEvent event)
736    {
737
738      // Logger Trace Method Enter
739      final String innerClassMethodName =
740        "printpreviewActionListener.actionPerformed - " + PRINTPREVIEW_MENUREF;
741      finalLogger.debug("Entering " + innerClassMethodName);
742
743      repaintMenuBar();
744      /*
745       *  now add the specific stuff for this Menu Item
746       */
747
748
749      // logger method exit
750      finalLogger.debug("Exiting " + innerClassMethodName);
751    }
752  }
753
754
755  private class closeActionListener implements ActionListener
756  {
757    /**  The log4j logger final object. */
758    final Logger finalLogger = jAckup.logger_;
759    Component menuItem = null;
760
761    public closeActionListener(Component mi)
762    {
763      super();
764      menuItem = mi;
765    }
766    /**
767     *  Creates a new jAckup file with default settings.
768     *
769     * @param  event  The Initiating event
770     */
771    public void actionPerformed(ActionEvent event)
772    {
773
774      // Logger Trace Method Enter
775      final String innerClassMethodName =
776        "closeActionListener.actionPerformed - " + CLOSE_MENUREF;
777      finalLogger.debug("Entering " + innerClassMethodName);
778
779      repaintMenuBar();
780      /*
781       *  now add the specific stuff for this Menu Item
782       */
783
784      // logger method exit
785      finalLogger.debug("Exiting " + innerClassMethodName);
786    }
787  }
788
789
790  private class exitActionListener implements ActionListener
791  {
792    /**  The log4j logger final object. */
793    final Logger finalLogger = jAckup.logger_;
794    Component menuItem = null;
795
796    public exitActionListener(Component mi)
797    {
798      super();
799      menuItem = mi;
800    }
801    /**
802     *  Creates a new jAckup file with default settings.
803     *
804     * @param  event  The Initiating event
805     */
806    public void actionPerformed(ActionEvent event)
807    {
808
809      // Logger Trace Method Enter
810      final String innerClassMethodName =
811        "printpreviewActionListener.actionPerformed - " + EXIT_MENUREF;
812      finalLogger.debug("Entering " + innerClassMethodName);
813
814      repaintMenuBar();
815      /*
816       *  now add the specific stuff for this Menu Item
817       */
818      LogManager.shutdown();
819      System.exit(0);
820    }
821  }
822
823
824  private class debuglevelActionListener implements ActionListener
825  {
826    /**  The log4j logger final object. */
827    final Logger finalLogger = jAckup.logger_;
828    Component menuItem = null;
829
830    public debuglevelActionListener(Component mi)
831    {
832      super();
833      menuItem = mi;
834    }
835    /**
836     *  Creates a new jAckup file with default settings.
837     *
838     * @param  event  The Initiating event
839     */
840    public void actionPerformed(ActionEvent event)
841    {
842
843      // Logger Trace Method Enter
844      final String innerClassMethodName =
845        "debuglevelActionListener.actionPerformed - " + DEBUGLEVEL_MENUREF;
846      finalLogger.debug("Entering " + innerClassMethodName);
847
848      repaintMenuBar();
849      /*
850       *  now add the specific stuff for this Menu Item
851       */
852
853      // logger method exit
854      finalLogger.debug("Exiting " + innerClassMethodName);
855    }
856  }
857
858
859  private class jlfActionListener implements ActionListener
860  {
861    /**  The log4j logger final object. */
862    final Logger finalLogger = jAckup.logger_;
863    Component menuItem = null;
864
865    public jlfActionListener(Component mi)
866    {
867      super();
868      menuItem = mi;
869    }
870    /**
871     *  Creates a new jAckup file with default settings.
872     *
873     * @param  event  The Initiating event
874     */
875    public void actionPerformed(ActionEvent event)
876    {
877
878      // Logger Trace Method Enter
879      final String innerClassMethodName =
880        "jlfActionListener.actionPerformed - " + JLF_MENUREF;
881      finalLogger.debug("Entering " + innerClassMethodName);
882
883      repaintMenuBar();
884      /*
885       *  now add the specific stuff for this Menu Item
886       */
887      Util.setTheLookAndFeel(Util.METAL_LOOKANDFEEL, menuItem);
888
889      // logger method exit
890      finalLogger.debug("Exiting " + innerClassMethodName);
891    }
892  }
893
894
895  private class plfActionListener implements ActionListener
896  {
897    /**  The log4j logger final object. */
898    final Logger finalLogger = jAckup.logger_;
899    Component menuItem = null;
900
901    public plfActionListener(Component mi)
902    {
903      super();
904      menuItem = mi;
905    }
906    /**
907     *  Creates a new jAckup file with default settings.
908     *
909     * @param  event  The Initiating event
910     */
911    public void actionPerformed(ActionEvent event)
912    {
913
914      // Logger Trace Method Enter
915      final String innerClassMethodName = this.getClass().getName() + "." +
916        "actionPerformed - " + PLF_MENUREF;
917      finalLogger.debug("Entering " + innerClassMethodName);
918
919      repaintMenuBar();
920      /*
921       *  now add the specific stuff for this Menu Item
922       */
923      Util.setTheLookAndFeel(Util.WINDOZE_LOOKANDFEEL, menuItem);
924
925      // logger method exit
926      finalLogger.debug("Exiting " + innerClassMethodName);
927    }
928  }
929
930
931  private class mlfActionListener implements ActionListener
932  {
933    /**  The log4j logger final object. */
934    final Logger finalLogger = jAckup.logger_;
935    Component menuItem = null;
936
937    public mlfActionListener(Component mi)
938    {
939      super();
940      menuItem = mi;
941    }
942    /**
943     *  Creates a new jAckup file with default settings.
944     *
945     * @param  event  The Initiating event
946     */
947    public void actionPerformed(ActionEvent event)
948    {
949
950      // Logger Trace Method Enter
951      final String innerClassMethodName = this.getClass().getName() + "." +
952        "actionPerformed - " + MLF_MENUREF;
953      finalLogger.debug("Entering " + innerClassMethodName);
954
955      repaintMenuBar();
956      /*
957       *  now add the specific stuff for this Menu Item
958       */
959      Util.setTheLookAndFeel(Util.MOTIF_LOOKANDFEEL, menuItem);
960
961      // logger method exit
962      finalLogger.debug("Exiting " + innerClassMethodName);
963    }
964  }
965
966
967  private class helpActionListener implements ActionListener
968  {
969    /**  The log4j logger final object. */
970    final Logger finalLogger = jAckup.logger_;
971    Component menuItem = null;
972
973    public helpActionListener(Component mi)
974    {
975      super();
976      menuItem = mi;
977    }
978    /**
979     *  Creates a new jAckup file with default settings.
980     *
981     * @param  event  The Initiating event
982     */
983    public void actionPerformed(ActionEvent event)
984    {
985
986      // Logger Trace Method Enter
987      final String innerClassMethodName = this.getClass().getName() + "." +
988        "actionPerformed - " + HELP_MENUREF;
989      finalLogger.debug("Entering " + innerClassMethodName);
990
991      repaintMenuBar();
992      /*
993       *  now add the specific stuff for this Menu Item
994       */
995
996      // logger method exit
997      finalLogger.debug("Exiting " + innerClassMethodName);
998    }
999  }
1000
1001
1002  private class aboutActionListener implements ActionListener
1003  {
1004    /**  The log4j logger final object. */
1005    final Logger finalLogger = jAckup.logger_;
1006    Component menuItem = null;
1007
1008    public aboutActionListener(Component mi)
1009    {
1010      super();
1011      menuItem = mi;
1012    }
1013    /**
1014     *  Creates a new jAckup file with default settings.
1015     *
1016     * @param  event  The Initiating event
1017     */
1018    public void actionPerformed(ActionEvent event)
1019    {
1020
1021      // Logger Trace Method Enter
1022      final String innerClassMethodName = this.getClass().getName() + "." +
1023        "actionPerformed - " + ABOUT_MENUREF;
1024      finalLogger.debug("Entering " + innerClassMethodName);
1025
1026      repaintMenuBar();
1027      /*
1028       *  now add the specific stuff for this Menu Item
1029       */
1030      JAboutBox box = new JAboutBox("jAckup Backup Utility", "");
1031      box.show();
1032      box.toFront();
1033
1034      // logger method exit
1035      finalLogger.debug("Exiting " + innerClassMethodName);
1036    }
1037  }
1038
1039
1040}
1041
1042class AutoMenuBar extends JMenuBar
1043{
1044  /**  A Class holder for its name (used in Logging).  */
1045  private static String className_ = "AutoMenuBar";
1046
1047  /**  Constant holding the users file seperator. ("/" or "\")  */
1048  private final static String SYSTEM_FILE_SEPERATOR = File.separator;
1049
1050  /**  String Tokens used to automatically create the menu bar.  */
1051  private String menuName[] = {"File", "Settings", "Options", "Help"};
1052
1053  /**
1054   *  String Tokens used to automatically create the menu items for each menu on
1055   *  the menubar. It is a 2D array of strings that will be used to create
1056   *  menuitems. If there are 4 menus... there will be 4 lists holding the
1057   *  words/Strings to use for the menu items.
1058   */
1059  private String menuItemName[][] = {
1060      {"New", "Open", "Save", "---", "Print...", "Print Preview", "---",
1061      "Close", "Exit"},
1062      {"File Selections", "Archive Format"},
1063      {"Set Debug Output Level...", "---",
1064      "Java L&F", "Platform L&F", "Motif L&F"},
1065      {"Help", "---", "About jAckup..."}};
1066
1067  /**  An Enumeration index for the menu items. */
1068  private final static int MENUREF[][] =
1069    {
1070      {100, 101, 102, 103, 104, 105, 106, 107, 108},
1071      {200, 201},
1072      {300, 301, 302, 303, 304},
1073      {400, 401, 402}
1074    };
1075
1076  /**
1077   *  The Tokens used to automatically assign the Mnemonic for each menu items.
1078   *  It is a 2D array of chars that will be used to assign the Mnemonic. If
1079   *  there are 4 menus... there will be 4 lists holding the chars to use as the
1080   *  Mnemonic for the menu items.
1081   */
1082  private char menuItemMnemonic[][] = {
1083      {'N', 'o', 's', ' ', 'P', 'r', ' ', 'C', 'x'},
1084      {'F', 'A'},
1085      {'D', ' ', 'J', 'P', 'M'},
1086      {'H', ' ', 'b'}};
1087
1088  /**
1089   *  The Tokens used when automatically assigning the accel key for each menu
1090   *  items. It is a 2D array of ints that will be used to assign the accel key
1091   *  constant. If there are 4 menus... there will be 4 lists holding the ints
1092   *  to use as the accel key constant value for the menu items.
1093   */
1094  private int menuItemAccel[][] = {
1095      {KeyEvent.VK_N, KeyEvent.VK_O, KeyEvent.VK_S, 0,
1096      KeyEvent.VK_A, KeyEvent.VK_B, 0,
1097      KeyEvent.VK_W, KeyEvent.VK_Q},
1098      {0, 0},
1099      {0, 0, 0, 0, 0},
1100      {KeyEvent.VK_F1, 0, KeyEvent.VK_F12}};
1101
1102  /**
1103   *  The Tokens used when automatically assigning the 2nd level of accel key
1104   *  for each menu items. It is a 2D array of ints that will be used to assign
1105   *  the accel key constant. If there are 4 menus... there will be 4 lists
1106   *  holding the ints to use as the accel key constant value for the menu
1107   *  items.
1108   */
1109  private int menuItemAccel2[][] = {
1110      {Event.CTRL_MASK, Event.CTRL_MASK, Event.CTRL_MASK, 0, Event.CTRL_MASK |
1111      Event.SHIFT_MASK, Event.CTRL_MASK |
1112      Event.SHIFT_MASK, 0, Event.CTRL_MASK, Event.CTRL_MASK},
1113      {0, 0},
1114      {0, 0, 0, 0, 0},
1115      {0, 0, 0}};
1116
1117  /**
1118   *  String Tokens used to automatically create the menu items tooltip for each
1119   *  menu on the menubar. It is a 2D array of strings that will be used to
1120   *  create menuitems tooltips. If there are 4 menus... there will be 4 lists
1121   *  holding the words/Strings to use for the menu items tooltip.
1122   */
1123  private String menuToolTip[][] =
1124      {{"New jAckup Plan", "Open jAckup Plan from File...",
1125      "Save Current jAckup Plan",
1126      "---", "Print Current jAckup Plan", "Print Preview", "---",
1127      "Close Current jAckup Plan", "Exit Application"},
1128      {"View File Selections", "View Archive Format Settings"},
1129      {"Set Debug Output Level...", "---",
1130      "Set the User Interface to the Java-Metal L&F",
1131      "Set the User Interface to the Platform L&F",
1132      "Set the User Interface to the Motif L&F"},
1133      {"Help On using jAckup", "---", "About jAckup..."}};
1134
1135  /**
1136   *  boolean Tokens used to enable automatic creation of the menu items for
1137   *  each menu on the menubar. It is a 2D array of booleans each one a flag
1138   *  directly refering to the menu item arrays already setup. If there are 4
1139   *  menus... there will be 4 lists holding the flags to use. If a value of
1140   *  false is specified the menu item will NOT get created.
1141   */
1142  private boolean menuItemShown[][] = {
1143      {true, true, true, true, true, true, true, true, true},
1144      {true, true},
1145      {true, true, true, true, true},
1146      {true, true, true}};
1147
1148  /**
1149   *  boolean Tokens used to enable the menu items for each menu on the menubar.
1150   *  It is a 2D array of booleans each one a flag directly refering to the menu
1151   *  item arrays already setup. If there are 4 menus... there will be 4 lists
1152   *  holding the flags to use. If a value of false is specified the menu item
1153   *  will be grayed.
1154   */
1155  private boolean menuItemEnabled[][] = {
1156      {true, true, true, true, false, false, true, true, true},
1157      {true, true},
1158      {true, true, true, true, true},
1159      {false, true, true}};
1160
1161  /**
1162   *  Suffix applied to the key used in resource file lookups for an image.
1163   */
1164  private final static String IMAGESUFFIX = "Image.gif";
1165
1166  /**
1167   *  Prefix applied to the key used in resource file lookups for an image.
1168   */
1169  private final static String IMAGEPREFIX = "/images";
1170
1171  /**
1172   *  The Parent Owner of this Auto Menu Bar.
1173   **/
1174   private AutoMenuBarUser parent;
1175
1176
1177
1178  public AutoMenuBar(AutoMenuBarUser passedUser)
1179  {
1180    super();
1181    parent = passedUser;
1182    this.initMenuBar();
1183  }
1184
1185
1186  /**
1187   *  Populates the menuitms into it. It uses a set of
1188   *  predefined String arrays that it loops through to create the menus. This
1189   *  way the menus can be easily changed by simply adding or removing entrys in
1190   *  the arrays. You can set the name, Accelerator, Mnemonic, Tooltip,
1191   *  enabled/disabled (clickable/gray), and even if the item gets created at
1192   *  all! <P>
1193   *
1194   *  The fields it uses are:
1195   *  <UL>
1196   *    <LI> menuItemShown
1197   *    <LI> menuName
1198   *    <LI> menuItemName
1199   *    <LI> menuItemAccel
1200   *    <LI> menuItemAccel2
1201   *  </UL>
1202   *
1203   *
1204   * @return    JMenuBar
1205   * @see       jAckup#createMenuItem(int,int)
1206   */
1207  private void initMenuBar()
1208  {
1209    final String methodName = className_ + ": initMenuBar()";
1210
1211    int i;
1212    int j;
1213    JMenu tempMenu;
1214
1215    for (i = 0; i < menuName.length; i++)
1216    {
1217      tempMenu = new JMenu(menuName[i]);
1218      for (j = 0; j < menuItemName[i].length; j++)
1219      {
1220        if (menuItemShown[i][j])
1221        {
1222          if (menuItemName[i][j].equals("---"))
1223          {
1224            tempMenu.addSeparator();
1225          }
1226          else
1227          /*
1228           *  Use the HELPER function called *createMenuItem* to actually do
1229           *  the menuItem Object creation
1230           */
1231              if (menuItemAccel[i][j] != 0)
1232          {
1233            tempMenu.add(createMenuItem(i, j)).setAccelerator(
1234                KeyStroke.getKeyStroke(menuItemAccel[i][j],
1235                menuItemAccel2[i][j]));
1236          }
1237          else
1238          {
1239            tempMenu.add(createMenuItem(i, j));
1240          }
1241        }
1242      }
1243      this.add(tempMenu);
1244    }
1245  }
1246
1247
1248  /**
1249   *  Aa simple indexOf helper function to look through the menu items and
1250   *  return an int to represent its HASH position
1251   *
1252   * @param  s  Description of Parameter
1253   * @return    int A value that is the HASH in the menuItem array for the
1254   *      menuItem passed in as a param
1255   */
1256  protected int indexOf(String s)
1257  {
1258    final String methodName = className_ + ": indexOf(String " + s + ")";
1259    //
1260
1261    int i = 0;
1262    int j = 0;
1263    int retVal = 0;
1264    if (s != null)
1265    {
1266      try
1267      {
1268        for (j = 0; j < menuName.length; j++)
1269        {
1270          for (i = 0; i < menuToolTip[j].length; i++)
1271          {
1272            if (s.equals(menuToolTip[j][i]))
1273            {
1274              retVal = MENUREF[j][i];
1275              i = menuToolTip[j].length - 1;
1276              j = menuName.length - 1;
1277            }
1278          }
1279        }
1280      }
1281      catch (java.lang.ArrayIndexOutOfBoundsException t)
1282      {
1283         /* don't do anything just return the retVal =0;*/
1284         retVal =0;
1285      }
1286    }
1287    return retVal;
1288  }
1289
1290
1291   /**
1292   *  This is the hook through which all menu items are created. The fields it
1293   *  uses are:
1294   *  <UL>
1295   *    <LI> menuItemMnemonic
1296   *    <LI> menuToolTip
1297   *    <LI> menuItemEnabled
1298   *  </UL>
1299   *
1300   *
1301   * @param  menuRef      Description of Parameter
1302   * @param  menuItemRef  Description of Parameter
1303   * @return              Description of the Returned Value
1304   * @see                 jAckup#initMenuBar()
1305   */
1306  private JMenuItem createMenuItem(int menuRef, int menuItemRef)
1307  {
1308    final String methodName = className_ + ": createMenuItem(int, int)";
1309
1310    String cmd = menuItemName[menuRef][menuItemRef];
1311    JMenuItem mi = new JMenuItem(menuItemName[menuRef][menuItemRef]);
1312    mi.setMnemonic(menuItemMnemonic[menuRef][menuItemRef]);
1313    StringBuffer imgFile = new StringBuffer(IMAGEPREFIX);
1314    imgFile.append(SYSTEM_FILE_SEPERATOR);
1315    imgFile.append(menuItemName[menuRef][menuItemRef]);
1316    imgFile.append(IMAGESUFFIX);
1317    Image menuItmImg = Util.loadImage(imgFile.toString());
1318    mi.setHorizontalTextPosition(JButton.RIGHT);
1319
1320    if (menuItmImg != null)
1321    {
1322      mi.setIcon(new ImageIcon(menuItmImg));
1323    }
1324
1325    mi.setToolTipText(menuToolTip[menuRef][menuItemRef]);
1326    mi.setEnabled(menuItemEnabled[menuRef][menuItemRef]);
1327
1328    return mi;
1329  }
1330
1331  /**
1332   * loops through the menuitems adding all the listeners by calling the parent
1333   * addListener menthod.
1334   **/
1335  public void addListeners()
1336  {
1337
1338    // loop through menuitems adding the listeners.
1339    int i;
1340    int j;
1341    JMenu tempMenu;
1342
1343    for (i = 0; i < menuName.length; i++)
1344    {
1345      tempMenu = this.getMenu(i);
1346      for (j = 0; j < menuItemName[i].length; j++)
1347      {
1348        /*
1349         *  call the one size fits all method to add the appropriate
1350         *  listener to this menu item
1351         */
1352        if (!menuItemName[i][j].equals("---"))
1353          parent.addListener(tempMenu.getItem(j));
1354      }
1355    }
1356  }
1357
1358
1359}
1360
1361interface AutoMenuBarUser
1362{
1363  /**
1364   *  Adds an ActionListener to the JMenuItem that is supplied in the passed
1365   *  param.
1366   *
1367   * @param  menuItem  The feature to be added to the Listener attribute
1368   */
1369  public void addListener(JMenuItem menuItem);
1370}
1371
1372
1373