net.sourceforge.webcompmath.awt.beans
Class WCMExamplesBean

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JPanel
                  extended by net.sourceforge.webcompmath.awt.beans.WCMPanelBean
                      extended by net.sourceforge.webcompmath.awt.beans.WCMExamplesBean
All Implemented Interfaces:
java.awt.event.ActionListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible

public class WCMExamplesBean
extends WCMPanelBean
implements java.awt.event.ActionListener

This bean provides a flexible means to create a drop-down list of examples. Each example is a text string made up of semi-colon separated tokens. The tokens correspond to arguments to methods that will be called in loading the example, except that the first token is always the text that will be displayed in the drop-down list. An example template is defined that specifies a list of objects, method names, and argument types. When an example is loaded, the bean will parse the text string one token at a time, and for each token it finds, it will call the appropriate method on the specified object.

For example, the template could have a reference to a WCMExpressionInputBean object with a fieldname of myExprInBean, a method name of "setText" and a type of STRING. If the text for this token in the example string is "x^2+1", when the bean loads the example it will call myExprInBean.setText("x^2+1"). In this way, an example can trigger several method calls to set up the example, such as setting the text of a WCMExpressionInputBean, setting the xmin, xmax, ymin, ymax for a WCMDisplayCanvasBean or WCMLimitControlPanelBean, set the value of a WCMVariableInputBean, etc.

The current version of this bean only supports methods with zero or one arguments. The argument type needs to be specified by the user in the template, in order to handle overloaded methods. The type is specified as an enumeration with the following options: STRING, INT, FLOAT, DOUBLE, LIMITS, BOOLEAN, VALUE, NO_ARG, and DOUBLE_ARRAY.

When an example is loaded, the methods defined in the template are called in the order in which they were specified. This is useful for things like Animator.start(), which you can set up to be called as the last method when loading an example. If a token is missing in an example (either because there are two semi-colons next to each other ("5;x^2;;") or there are fewer tokens in the example than methods in the template, then the method is skipped. Note that this applies to NO_ARG methods, too. If you want a NO_ARG method to be called, put something in the token (e.g., "invoke"). This also means that to call a STRING method with "", you must put "; ;" in the example, and not ";;". The blank space between the semicolons causes the STRING method to be called, but the blank is stripped out before the method is called, thus passing an empty string to the method.

There is also a loadAppletExamples(JApplet applet) method which, when called from an applet using itself as the argument, will load the examples from applet parameter tags, just like the configurable applets based on GenericApplet do.

Here is an example. Note that loadSelectedExample() should get called at the end of applet/application initialization, so that all the elements of the applet/application are present and set up before the example is loaded.

myExample = new WCMExamplesBean();

myExample.addTemplateElement(myExprInBean, "setText", WCMExamplesBean.STRING);

myExample.setExample0("First example;x^2");

myExample.setExample1("Second example;x^3");

myExample.setExample2("Third example; x^4");

myExample.setSelectedExample(0);

myExample.loadSelectedExample();

Author:
Tom Downey
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JPanel
javax.swing.JPanel.AccessibleJPanel
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
static int BOOLEAN
          For methods like WCMAnimator.setPauseButton(boolean b).
static int DOUBLE
          For methods like VariableInput.setMin(double d).
static int DOUBLE_ARRAY
          The method takes an array of double.
static int FLOAT
          For methods like Graph1D.setLineWidth(float f).
static int INT
          For methods like Animator.setIntervals(int i).
static int LIMITS
          A set of four numbers, separated by commas (such as "-5, 5, -5, 5"), representing the xmin, xmax, ymin, and ymax limits of a WCMDisplayCanvasBean or WCMLimitControlPanelBean.
static int NO_ARG
          The method does not take an argument.
static int STRING
          For methods like WCMExpressionInputBean.setText(String s).
static int VALUE
          For methods that take a Value object as the argument, like Animator.setIntervals(Value v).
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
WCMExamplesBean()
          The default constructor.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent evt)
          Respond when user clicks a button; not meant to be called directly.
 void addExample(java.lang.String s)
          Add an example string.
 void addTemplateElement(java.lang.Object o, java.lang.String methodName, int type)
          Add an element to the template.
protected  void doLoadExample(java.lang.String example)
          Load a specific example string
 java.lang.String getExample(int index)
          Get an example that has been set.
 java.lang.String getExample0()
          Get an example string
 java.lang.String getExample1()
          Get an example string
 java.lang.String getExample2()
          Get an example string
 java.lang.String getExample3()
          Get an example string
 java.lang.String getExample4()
          Get an example string
 java.lang.String getExample5()
          Get an example string
 java.lang.String getExample6()
          Get an example string
 java.lang.String getExample7()
          Get an example string
 java.lang.String getExample8()
          Get an example string
 java.lang.String getExample9()
          Get an example string
 javax.swing.JComboBox getExampleMenu()
          Get the example menu itself.
 javax.swing.JButton getLoadExampleButton()
          Get the load button itself.
 boolean isUseLoadButton()
           
 void loadAppletExamples(javax.swing.JApplet applet)
          Get examples from HTML.
 void loadSelectedExample()
          Use this method during applet initialization if you have elected not to use a load button and you want the example loaded right away.
 void setExample(java.lang.String s, int index)
          Set an example string
 void setExample0(java.lang.String s)
          Set an example string
 void setExample1(java.lang.String s)
          Set an example string
 void setExample2(java.lang.String s)
          Set an example string
 void setExample3(java.lang.String s)
          Set an example string
 void setExample4(java.lang.String s)
          Set an example string
 void setExample5(java.lang.String s)
          Set an example string
 void setExample6(java.lang.String s)
          Set an example string
 void setExample7(java.lang.String s)
          Set an example string
 void setExample8(java.lang.String s)
          Set an example string
 void setExample9(java.lang.String s)
          Set an example string
 void setSelectedExample(int index)
          Set which example is selected.
 void setUseLoadButton(boolean useLoadButton)
           
 
Methods inherited from class net.sourceforge.webcompmath.awt.beans.WCMPanelBean
addImpl, getController, getMainController, paintComponent, remove, setController, setTie1, setTie2, setTie3
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

STRING

public static final int STRING
For methods like WCMExpressionInputBean.setText(String s).

See Also:
Constant Field Values

INT

public static final int INT
For methods like Animator.setIntervals(int i). Note that there is also a setIntervals(Value v) method in Animator and WCMAnimatorBean; this overloading is why you need to specify the argument type in the template.

See Also:
Constant Field Values

FLOAT

public static final int FLOAT
For methods like Graph1D.setLineWidth(float f).

See Also:
Constant Field Values

DOUBLE

public static final int DOUBLE
For methods like VariableInput.setMin(double d).

See Also:
Constant Field Values

LIMITS

public static final int LIMITS
A set of four numbers, separated by commas (such as "-5, 5, -5, 5"), representing the xmin, xmax, ymin, and ymax limits of a WCMDisplayCanvasBean or WCMLimitControlPanelBean.

See Also:
Constant Field Values

BOOLEAN

public static final int BOOLEAN
For methods like WCMAnimator.setPauseButton(boolean b). The token in the example should be either "true" or "false" (case is not important).

See Also:
Constant Field Values

VALUE

public static final int VALUE
For methods that take a Value object as the argument, like Animator.setIntervals(Value v). The token should be a number, which will be converted into a Constant before being passed to the method.

See Also:
Constant Field Values

NO_ARG

public static final int NO_ARG
The method does not take an argument. Useful when you want to call something like Animator.start() or WCMDisplayCanvasBean.setRestoreBuffer(). If the token is missing or is "SKIP", then the method will not be called; if the token is any other string (e.g., "INVOKE"), then the method will be called.

See Also:
Constant Field Values

DOUBLE_ARRAY

public static final int DOUBLE_ARRAY
The method takes an array of double. Like for LIMITS, the numbers are separated by commas.

See Also:
Constant Field Values
Constructor Detail

WCMExamplesBean

public WCMExamplesBean()
The default constructor.

Method Detail

addTemplateElement

public void addTemplateElement(java.lang.Object o,
                               java.lang.String methodName,
                               int type)
Add an element to the template. You should add elements in the order in which you want them to be entered into an example string. Errors are reported to System.out, so while debugging a template it may be useful to have the Java console open.

Parameters:
o - the object whose method is going to be called
methodName - the name of the method to call
type - the type of argument to pass to the method, one of STRING, INT, FLOAT, DOUBLE, LIMITS, BOOLEAN, VALUE, NO_ARG, DOUBLE_ARRAY.

setExample0

public void setExample0(java.lang.String s)
Set an example string

Parameters:
s - the example

getExample0

public java.lang.String getExample0()
Get an example string

Returns:
the example

setExample1

public void setExample1(java.lang.String s)
Set an example string

Parameters:
s - the example

getExample1

public java.lang.String getExample1()
Get an example string

Returns:
the example

setExample2

public void setExample2(java.lang.String s)
Set an example string

Parameters:
s - the example

getExample2

public java.lang.String getExample2()
Get an example string

Returns:
the example

setExample3

public void setExample3(java.lang.String s)
Set an example string

Parameters:
s - the example

getExample3

public java.lang.String getExample3()
Get an example string

Returns:
the example

setExample4

public void setExample4(java.lang.String s)
Set an example string

Parameters:
s - the example

getExample4

public java.lang.String getExample4()
Get an example string

Returns:
the example

setExample5

public void setExample5(java.lang.String s)
Set an example string

Parameters:
s - the example

getExample5

public java.lang.String getExample5()
Get an example string

Returns:
the example

setExample6

public void setExample6(java.lang.String s)
Set an example string

Parameters:
s - the example

getExample6

public java.lang.String getExample6()
Get an example string

Returns:
the example

setExample7

public void setExample7(java.lang.String s)
Set an example string

Parameters:
s - the example

getExample7

public java.lang.String getExample7()
Get an example string

Returns:
the example

setExample8

public void setExample8(java.lang.String s)
Set an example string

Parameters:
s - the example

getExample8

public java.lang.String getExample8()
Get an example string

Returns:
the example

setExample9

public void setExample9(java.lang.String s)
Set an example string

Parameters:
s - the example

getExample9

public java.lang.String getExample9()
Get an example string

Returns:
the example

setExample

public void setExample(java.lang.String s,
                       int index)
Set an example string

Parameters:
s - the example
index - the index of the example, starting at 0

getExample

public java.lang.String getExample(int index)
Get an example that has been set.

Parameters:
index - the index of the example
Returns:
the example string

addExample

public void addExample(java.lang.String s)
Add an example string. This is used if you need more than 10 examples. The new example is added at the end of any existing examples.

Parameters:
s - the example

loadSelectedExample

public void loadSelectedExample()
Use this method during applet initialization if you have elected not to use a load button and you want the example loaded right away. Set the example first with a call to setSelectedExample(int index) if you want an example other than the first one.


setSelectedExample

public void setSelectedExample(int index)
Set which example is selected. This is typically use to select the first example shown to the user at startup.

Parameters:
index - the index of the example to set

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent evt)
Respond when user clicks a button; not meant to be called directly.

Specified by:
actionPerformed in interface java.awt.event.ActionListener
Parameters:
evt - event generated when user clicks a button

doLoadExample

protected void doLoadExample(java.lang.String example)
Load a specific example string

Parameters:
example -

loadAppletExamples

public void loadAppletExamples(javax.swing.JApplet applet)
Get examples from HTML. This method should be called when you are using a WCMExamplesBean in an applet and you want to load the examples from parameters in the applet tag. You should call this method in your initialization method for the applet. If you have only one example, the param name can be "Example". If you have multiple examples, use "Example1", "Example2", etc. You can also start with "Example0" (unlike the generic applets). But, don't use both "Example" and "Example0" (you can use "Example" and "Example1", though).

Parameters:
applet - the applet in which the WCMExamplesBean is embedded

isUseLoadButton

public boolean isUseLoadButton()
Returns:
the useLoadButton

setUseLoadButton

public void setUseLoadButton(boolean useLoadButton)
Parameters:
useLoadButton - the useLoadButton to set

getExampleMenu

public javax.swing.JComboBox getExampleMenu()
Get the example menu itself. Useful if you want to set parameters on it, such as color.

Returns:
the exampleMenu

getLoadExampleButton

public javax.swing.JButton getLoadExampleButton()
Get the load button itself. Useful if you want to set parameters on it, such as color.

Returns:
the loadExampleButton