1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.  Oracle designates this
  10  * particular file as subject to the "Classpath" exception as provided
  11  * by Oracle in the LICENSE file that accompanied this code.
  12  *
  13  * This code is distributed in the hope that it will be useful, but WITHOUT
  14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16  * version 2 for more details (a copy is included in the LICENSE file that
  17  * accompanied this code).
  18  *
  19  * You should have received a copy of the GNU General Public License version
  20  * 2 along with this work; if not, write to the Free Software Foundation,
  21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22  *
  23  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  24  * or visit www.oracle.com if you need additional information or have any
  25  * questions.
  26  */
  27 package com.sun.javatest.exec;
  28 
  29 import java.util.ArrayList;
  30 import javax.swing.JMenuItem;
  31 
  32 /**
  33  * Class to manage custom menus.  This class allows the test suite architect to
  34  * add various custom menu items to designated places in the menu system of the
  35  * Test Manager.  The constants in this class represent those designated
  36  * positions and are semantic locations, not absolute.  This allows
  37  * reorganization of menus at the harness level without necessarily breaking
  38  * the positioning that the architect expected.
  39  *
  40  * This class can be used in two ways.  First, the concrete class overrides
  41  * <code>getMenuItems()</code> to return the correct set of items for the given
  42  * category.  This method may be the most simple for straight forward insertions.
  43  * The second method is to use <code>addMenuItem()</code> to sequentially
  44  * specify which menu items should appear.  The default implementation of
  45  * <code>getMenuItems()</code> will use data provided by using this second
  46  * method.
  47  *
  48  * By default, no menu category will have any custom menus (<code>getMenuItems()</code>
  49  * will always return null).
  50  *
  51  * The <code>JMenuItem</code> objects may be "pull-right" menus if desired.  It is
  52  * the responsibility of the architect to manage keystroke mneumonics.
  53  */
  54 public abstract class JavaTestMenuManager {
  55     /**
  56      * Get the menu items to go into the specified position in the menu system.
  57      * See the constants in this class for the possible value.
  58      * @param position The menu position, one of the constants of this class.
  59      * @return The custom menu items to be displayed in the given position.
  60      *         Null if there are none.  Never a zero-length array.
  61      * @throws IllegalArgumentException If the position parameter is out of
  62      *         range.  This is usually the fault of the harness itself, but
  63      *         may occur if classes are compiled against one development version
  64      *         of the harness and run with another.
  65      */
  66     public JMenuItem[] getMenuItems(int position) {
  67         if (bank == null)
  68             return null;
  69         else {
  70             ArrayList al = bank[position];
  71             if (al.size() == 0)
  72                 return null;
  73             else {
  74                 JMenuItem[] result = new JMenuItem[al.size()];
  75                 al.toArray(result);
  76                 return result;
  77             }
  78         }
  79     }
  80 
  81     /**
  82      * Add a menu item to the given menu position.
  83      * The item is added to the bottom, in that position, so you must add them
  84      * in the order you wish them to appear.
  85      * @param position The menu position, one of the constants of this class.
  86      * @param item The menu item to add.
  87      * @throws IndexOutOfBoundsException If the position index is out of
  88      *     range.  Be sure that you are using the constants given in this
  89      *     class to supply this parameter.
  90      */
  91     protected synchronized void addMenuItem(int position, JMenuItem item) {
  92         if (position > NUM_POSITIONS)
  93             throw new IndexOutOfBoundsException("Position index too large - " +
  94                 position);
  95         if (position < 0)
  96             throw new IndexOutOfBoundsException("Position index too small - " +
  97                 position);
  98 
  99         if (bank == null) {
 100             bank = new ArrayList[NUM_POSITIONS];
 101             for (int i = 0; i < NUM_POSITIONS; i++)
 102                 bank[i] = new ArrayList<>();
 103         }
 104 
 105         bank[position].add(item);
 106     }
 107 
 108     public static final int FILE_PRIMARY = 0;
 109     public static final int FILE_OTHER = 2;
 110 
 111     public static final int CONFIG_PRIMARY = 4;
 112     public static final int CONFIG_VIEW = 5;
 113     public static final int CONFIG_OTHER = 6;
 114 
 115     public static final int RUN_PRIMARY = 7;
 116     public static final int RUN_OTHER = 8;
 117 
 118     public static final int WINDOWS_MAIN = 9;
 119     public static final int PRESENTATION = 10;
 120     public static final int PREFERENCES = 11;
 121 
 122     public static final int LOG_VIEW = 12;
 123     public static final int LOG_CONFIG = 13;
 124 
 125     public static final int HELP_PRIMARY = 14;
 126     public static final int HELP_TESTSUITE = 15;
 127     public static final int HELP_ABOUT = 16;
 128 
 129     public static final int TOOLS_OTHER = 17;
 130 
 131     private static final int NUM_POSITIONS = 18;
 132     private ArrayList<JMenuItem>[] bank;
 133 }