View Javadoc

1   package org.telscenter.pas.authortool.menu;
2   
3   import java.lang.reflect.Constructor;
4   import java.lang.reflect.InvocationTargetException;
5   
6   import javax.swing.JMenu;
7   import javax.swing.JMenuBar;
8   
9   import org.telscenter.pas.authortool.context.CurnitAuthoringContext;
10  
11  import com.jgoodies.looks.HeaderStyle;
12  import com.jgoodies.looks.Options;
13  
14  public class MenuProvider {
15  	private JMenuBar menuBar;
16  
17  	private CurnitAuthoringContext curnitAuthoringContext;
18  
19  	private final Class[] menuClasses;
20  
21  	public MenuProvider(final CurnitAuthoringContext curnitAuthoringContext) {
22  		this.curnitAuthoringContext = curnitAuthoringContext;
23  		menuBar = new JMenuBar();
24  		init();
25  
26  		menuClasses = curnitAuthoringContext.getMenuClasses();
27  
28  		for (final Class element : menuClasses) {
29  			try {
30  				addMenu(element);
31  			} catch (final Exception e) {
32  				// TODO Auto-generated catch block
33  				e.printStackTrace();
34  			}// try
35  		}// for
36  	}// MenuProvider
37  
38  	/***
39  	 * @param element
40  	 */
41  	private void addMenu(final Class menuClass) throws SecurityException,
42  			NoSuchMethodException, IllegalArgumentException,
43  			InstantiationException, IllegalAccessException,
44  			InvocationTargetException {
45  		menuBar.add(constructMenu(menuClass));
46  	}
47  
48  	protected JMenu constructMenu(final Class menuClass)
49  			throws SecurityException, NoSuchMethodException,
50  			IllegalArgumentException, InstantiationException,
51  			IllegalAccessException, InvocationTargetException {
52  		final Class[] parameters = new Class[] { CurnitAuthoringContext.class };
53  		final Constructor constr = menuClass.getConstructor(parameters);
54  		final Object[] initargs = new Object[] { curnitAuthoringContext };
55  		return (JMenu) constr.newInstance(initargs);
56  	}
57  
58  	private void init() {
59  		menuBar.putClientProperty(Options.HEADER_STYLE_KEY, HeaderStyle.SINGLE);
60  	}
61  
62  	public JMenuBar getMenuBar() {
63  		return menuBar;
64  	}// getMenuBar
65  
66  	/***
67  	 * @param menu
68  	 * @return
69  	 * @throws InvocationTargetException
70  	 * @throws IllegalAccessException
71  	 * @throws InstantiationException
72  	 * @throws NoSuchMethodException
73  	 * @throws IllegalArgumentException
74  	 * @throws SecurityException
75  	 */
76  	public void addNewMenu(JMenu menu) throws SecurityException,
77  			IllegalArgumentException, NoSuchMethodException,
78  			InstantiationException, IllegalAccessException,
79  			InvocationTargetException {
80  		// remove all the menus
81  		menuBar.removeAll();
82  		
83  		//add the new one just before the last menu
84  		for (int i = 0; i < menuClasses.length; i++) {
85  			Class mc = menuClasses[i];
86  			if (menu != null) {
87  				if (i == menuClasses.length - 1) {
88  					menuBar.add(menu);
89  				}// if
90  			}//  if
91  			menuBar.add(constructMenu(mc));
92  
93  		}// for
94  		menuBar.repaint();
95  		menuBar.revalidate();
96  	}
97  
98  }// MenuProvider