Adds a custom button to the control's frame. @param methodName the name of the method; the method has no parameters @param text the button's text label @param toolTipText the button's tool tip text @param target the target for the method @return the custom button
(String methodName, String text, String toolTipText, final Object target)
| 587 | * @return the custom button |
| 588 | */ |
| 589 | public JButton addButton(String methodName, String text, String toolTipText, final Object target) { |
| 590 | TranslatableButton b = new TranslatableButton(text, toolTipText, target); |
| 591 | // changed to add translation tools for strings by D Brown 2007-10-17 |
| 592 | if (OSPRuntime.loadTranslatorTool) { |
| 593 | text = OSPRuntime.getTranslator().getProperty(target.getClass(), "custom_button." + text, text); //$NON-NLS-1$ |
| 594 | toolTipText = OSPRuntime.getTranslator().getProperty(target.getClass(), "custom_button." + toolTipText, //$NON-NLS-1$ |
| 595 | toolTipText); |
| 596 | } |
| 597 | b.setText(text); |
| 598 | b.setToolTipText(toolTipText); |
| 599 | Class<?>[] parameters = {}; |
| 600 | try { |
| 601 | final java.lang.reflect.Method m = target.getClass().getMethod(methodName, parameters); |
| 602 | b.addActionListener(new ActionListener() { |
| 603 | @Override |
| 604 | public void actionPerformed(ActionEvent e) { |
| 605 | Object[] args = {}; |
| 606 | try { |
| 607 | m.invoke(target, args); |
| 608 | } catch (IllegalAccessException iae) { |
| 609 | System.err.println(iae); |
| 610 | } catch (java.lang.reflect.InvocationTargetException ite) { |
| 611 | System.err.println(ite); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | }); |
| 616 | buttonPanel.setVisible(true); |
| 617 | buttonPanel.add(b); |
| 618 | validate(); |
| 619 | pack(); |
| 620 | } catch (NoSuchMethodException nsme) { |
| 621 | System.err.println( |
| 622 | "Error adding custom button " + text + ". The method " + methodName + "() does not exist."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 623 | } |
| 624 | customButtons.add(b); |
| 625 | return b; |
| 626 | } |
| 627 | |
| 628 | class TranslatableButton extends JButton { |
| 629 | String text, tip; |