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