package jas.plot; import java.lang.reflect.Method; import javax.swing.JMenuItem; import javax.swing.JOptionPane; /** * This JMenuItem subclass implements a convenient * reflection-based menu item. When this item is first * created, the class of the object specified by the source * argument to the constructor is searched for methods that match the * string given for the constructor's name argument. * The class will be searched for two methods: * * @see HasPopupItems * @see HasPopupItems#getPopupItems() */ final class PlotPopupItem extends JMenuItem { PlotPopupItem(String name, Object source) { super(name); setEnabled(false); if (name.endsWith("...")) name = name.substring(0, name.length() - 3); for (int i = name.indexOf(" "); i >= 0; i = name.indexOf(' ')) name = name.substring(0, i).concat(name.substring(i + 1)); try { action = source.getClass().getMethod("on"+ name, no_arg_class); // if no exceptions... enabler = source.getClass().getMethod("enable"+ name, no_arg_class); // if no exceptions... if (enabler.getReturnType() != boolean.class) enabler = null; // we ignore the method because its return is useless } catch (Exception e) { if (! (e instanceof NoSuchMethodException)) JOptionPane.showMessageDialog(this, e.toString(), "", JOptionPane.ERROR_MESSAGE); } } void callEnable() { if (enabler != null) { try { Boolean b = (Boolean) enabler.invoke(source, no_arg_object); setEnabled(b.booleanValue()); } catch (Exception e) { setEnabled(false); } } else setEnabled(action != null); } protected void fireActionPerformed(java.awt.event.ActionEvent event) { // let's not bother with super.fireActionPerformed(event) if (action != null) { try { action.invoke(source, no_arg_object); } catch (Exception e) { /* BUG */ } } } private Object source; private Method action = null; private Method enabler = null; static private final Class[] no_arg_class = new Class[0]; static private final Object[] no_arg_object = new Object[0]; }