Class FunctionEditor

All Implemented Interfaces:
ImageObserver, MenuContainer, PropertyChangeListener, Serializable, EventListener, Accessible
Direct Known Subclasses:
DataFunctionEditor, ParamEditor, UserFunctionEditor

public abstract class FunctionEditor extends JPanel implements PropertyChangeListener
A JPanel that manages a table of objects with editable names and expressions. Main component of FunctionTool. subclassed as DataFunctionEditor, ParamEditor (incl. InitialValueEditor), and UserFunctionEditor FunctionEditor DataFunctionEditor ParamEditor InitialValueEditor UserFunctionEditor
Author:
Douglas Brown
See Also:
  • Field Details

    • PROPERTY_FUNCTIONEDITOR_EDIT

      public static final String PROPERTY_FUNCTIONEDITOR_EDIT
      See Also:
    • PROPERTY_FUNCTIONEDITOR_CLIPBOARD

      public static final String PROPERTY_FUNCTIONEDITOR_CLIPBOARD
      See Also:
    • PROPERTY_FUNCTIONEDITOR_PARAM_DESCRIPTION

      public static final String PROPERTY_FUNCTIONEDITOR_PARAM_DESCRIPTION
      See Also:
    • PROPERTY_FUNCTIONEDITOR_DESCRIPTION

      public static final String PROPERTY_FUNCTIONEDITOR_DESCRIPTION
      See Also:
    • PROPERTY_FUNCTIONEDITOR_FOCUS

      public static final String PROPERTY_FUNCTIONEDITOR_FOCUS
      See Also:
    • PROPERTY_FUNCTIONEDITOR_ANGLESINRADIANS

      public static final String PROPERTY_FUNCTIONEDITOR_ANGLESINRADIANS
      See Also:
    • THETA

      public static final String THETA
    • OMEGA

      public static final String OMEGA
    • DEGREES

      public static final String DEGREES
      See Also:
    • ADD_EDIT

      public static final int ADD_EDIT
      See Also:
    • REMOVE_EDIT

      public static final int REMOVE_EDIT
      See Also:
    • NAME_EDIT

      public static final int NAME_EDIT
      See Also:
    • EXPRESSION_EDIT

      public static final int EXPRESSION_EDIT
      See Also:
    • allowPopopFieldTooltip

      public static final boolean allowPopopFieldTooltip
    • undoEditsEnabled

      protected static boolean undoEditsEnabled
    • editTypes

      protected static String[] editTypes
    • paramEditor

      protected ParamEditor paramEditor
    • functionPanel

      protected FunctionPanel functionPanel
    • objects

      protected ArrayList<FunctionEditor.FObject> objects
    • names

      protected String[] names
    • forbiddenNames

      protected HashSet<String> forbiddenNames
    • removablesAtTop

      protected boolean removablesAtTop
    • circularErrors

      protected BitSet circularErrors
    • errors

      protected BitSet errors
    • evaluate

      protected List<FunctionEditor.FObject> evaluate
    • referencesChecked

      protected HashSet<String> referencesChecked
    • anglesInDegrees

      protected boolean anglesInDegrees
    • confirmChanges

      protected boolean confirmChanges
    • skipAllName

      protected String skipAllName
      set to "t" in InitialValueEditor for getVariablesString
    • table

      protected FunctionEditor.Table table
    • tableModel

      protected FunctionEditor.TableModel tableModel
    • tableCellEditor

      protected org.opensourcephysics.tools.FunctionEditor.CellEditor tableCellEditor
    • tableCellRenderer

      protected org.opensourcephysics.tools.FunctionEditor.CellRenderer tableCellRenderer
    • addButtonPanel

      protected boolean addButtonPanel
    • newButtonTipText

      protected String newButtonTipText
    • titledBorderText

      protected String titledBorderText
  • Constructor Details

    • FunctionEditor

      public FunctionEditor()
      No-arg constructor
  • Method Details

    • checkGUI

      public void checkGUI()
    • getTable

      public FunctionEditor.Table getTable()
      Gets the table.
      Returns:
      the table
    • getPreferredSize

      public Dimension getPreferredSize()
      Override getPreferredSize().
      Overrides:
      getPreferredSize in class JComponent
      Returns:
      the table size plus button and instruction heights
    • setObjects

      public void setObjects(List<FunctionEditor.FObject> newObjects)
      Replaces the current objects with new ones.
      Parameters:
      newObjects - a list of objects
    • updateTable

      protected void updateTable()
    • getObjects

      public List<FunctionEditor.FObject> getObjects()
      Gets a shallow clone of the objects list.
      Returns:
      a list of objects
    • getNames

      public String[] getNames()
      Gets an array containing the names of the objects.
      Returns:
      an array of names
    • getName

      public abstract String getName(FunctionEditor.FObject obj)
      Returns the name of the object.
      Parameters:
      obj - the object
      Returns:
      the name
    • getExpression

      public abstract String getExpression(FunctionEditor.FObject obj)
      Returns the expression of the object.
      Parameters:
      obj - the object
      Returns:
      the expression
    • getDescription

      public abstract String getDescription(FunctionEditor.FObject obj)
      Returns the description of the object.
      Parameters:
      obj - the object
      Returns:
      the description
    • setDescription

      public void setDescription(FunctionEditor.FObject obj, String desc)
      Sets the description of the object. Subclasses should override and call this AFTER changing the object description.
      Parameters:
      obj - the object
      desc - the description
    • getTooltip

      public abstract String getTooltip(FunctionEditor.FObject obj)
      Returns a tooltip for the object.
      Parameters:
      obj - the object
      Returns:
      the tooltip
    • getObject

      public FunctionEditor.FObject getObject(String name)
      Gets an existing object with specified name. May return null.
      Parameters:
      name - the name
      Returns:
      the object
    • setExpression

      public void setExpression(String name, String expression, boolean postEdit)
      Sets the expression of an existing named object, if any.
      Parameters:
      name - the name
      expression - the expression
      postEdit - true to post an undoable edit
    • getConfirmChanges

      public boolean getConfirmChanges()
      Gets the confirmChanges flag.
      Returns:
      true if users are required to confirm changes to function names
    • setConfirmChanges

      public void setConfirmChanges(boolean confirm)
      Sets the confirmChanges flag.
      Parameters:
      confirm - true to require users to confirm changes to function names
    • addObject

      public FunctionEditor.FObject addObject(FunctionEditor.FObject obj, boolean postEdit)
      Adds an object.
      Parameters:
      obj - the object
      postEdit - true to post an undoable edit
      Returns:
      the added object
    • addObject

      public FunctionEditor.FObject addObject(FunctionEditor.FObject obj, int row, boolean postEdit, boolean firePropertyChange)
      Adds an object at a specified row.
      Parameters:
      obj - the object
      row - the row
      postEdit - true to post an undoable edit
      firePropertyChange - true to fire a property change event
      Returns:
      the added object
    • removeObject

      public FunctionEditor.FObject removeObject(FunctionEditor.FObject obj, boolean postEdit)
      Removes an object.
      Parameters:
      obj - the object to remove
      postEdit - true to post an undoable edit
      Returns:
      the removed object
    • refreshStrings

      public void refreshStrings()
      Refreshes button strings based on current locale.
    • propertyChange

      public void propertyChange(PropertyChangeEvent e)
      Responds to property change events.
      Specified by:
      propertyChange in interface PropertyChangeListener
      Parameters:
      e - the event
    • setCustomButtons

      public void setCustomButtons(AbstractButton[] buttons)
      Sets custom buttons on the button panel. Setting buttons to null removes all buttons from this editor.
    • getUndoableEdit

      protected UndoableEdit getUndoableEdit(int type, Object redo, int redoRow, int redoCol, Object undo, int undoRow, int undoCol, String name)
      Gets an undoable edit.
      Parameters:
      type - may be ADD_EDIT, REMOVE_EDIT, NAME_EDIT, or EXPRESSION_EDIT
      redo - the new state
      redoRow - the newly selected row
      redoCol - the newly selected column
      undo - the previous state
      undoRow - the previously selected row
      undoCol - the previously selected column
      name - the name of the edited object
    • isNameEditable

      public boolean isNameEditable(FunctionEditor.FObject obj)
      Determines if an object's name is editable.
      Parameters:
      obj - the object
      Returns:
      true if the name is editable
    • isExpressionEditable

      public boolean isExpressionEditable(FunctionEditor.FObject obj)
      Determines if an object's expression is editable.
      Parameters:
      obj - the object
      Returns:
      true if the expression is editable
    • isRemovable

      protected boolean isRemovable(FunctionEditor.FObject obj)
      Determines if an object is removable.
      Parameters:
      obj - the object
      Returns:
      true if removable
    • isImportant

      protected abstract boolean isImportant(FunctionEditor.FObject obj)
      Determines if an object is important.
      Parameters:
      obj - the object
      Returns:
      true if important
    • setAnglesInDegrees

      public void setAnglesInDegrees(boolean degrees)
      Sets the anglesInDegrees flag. Angles are displayed in degrees when true, radians when false.
      Parameters:
      degrees - true to display angles in degrees
    • evaluateAll

      public abstract void evaluateAll()
      Evaluates all current objects.
    • setArrays

      protected void setArrays()
    • hasReference

      public boolean hasReference(int i1, int i2)
    • isValidExpression

      protected boolean isValidExpression(String expression)
      Determines if a test expression is valid.
      Parameters:
      statement -
      Returns:
      true if valid
    • references

      protected boolean references(String name, HashSet<String> checked)
      Determines if a name is referenced by any functions in this editor
      Parameters:
      name - the name to look for
      checked - a set of previously checked names (to prevent endless loops)
      Returns:
    • setReferences

      protected abstract void setReferences(FunctionEditor.FObject obj, BitSet directRefrences)
      Subclasses implement to set objects referenced in an object's expression.
    • haveGUI

      protected boolean haveGUI()
    • setTitles

      protected abstract void setTitles()
    • createGUI

      protected void createGUI()
      Creates the GUI.
    • refreshGUI

      public void refreshGUI()
      Refreshes the GUI.
    • setBorderTitle

      public void setBorderTitle(String title)
      Sets the border title.
    • enableMenuButtons

      protected void enableMenuButtons()
      Refreshes button states.
    • setVisible

      public void setVisible(boolean visible)
      Overrides:
      setVisible in class JComponent
    • getParamEditor

      protected ParamEditor getParamEditor()
      Gets the param editor that defines parameters for functions.
    • setParamEditor

      protected void setParamEditor(ParamEditor editor)
      Sets the param editor that defines parameters for functions. By default, the editor pasted in is ignored unless not yet set.
    • getFunctionPanel

      public FunctionPanel getFunctionPanel()
      Gets the FunctionPanel that manages this editor.
    • setFunctionPanel

      public void setFunctionPanel(FunctionPanel panel)
      Sets the FunctionPanel that contains this editor.
      Parameters:
      panel - the function panel
    • getDefaultName

      protected String getDefaultName()
      Returns the default name for newly created objects.
    • getVariablesString

      protected String getVariablesString(String separator)
      Returns a String with the names of variables available for expressions. This default returns the names of all objects in this panel except the selected object.
      Parameters:
      separator -
      Returns:
    • getVariablesString

      public String getVariablesString(StringBuffer vars, String separator)
    • getPartlyEditableRowCount

      protected int getPartlyEditableRowCount()
      Returns the number of editable rows.
    • isInvalidExpression

      protected abstract boolean isInvalidExpression(FunctionEditor.FObject obj)
      Returns true if the object expression is invalid.
    • containsInvalidExpressions

      public boolean containsInvalidExpressions()
      Returns true if any objects have invalid expressions.
    • pasteAction

      protected void pasteAction()
      Pastes the clipboard contents.
    • getClipboardContentsAsync

      protected void getClipboardContentsAsync(Consumer<XMLControl[]> c)
      Gets the clipboard contents.
    • getSelectedObject

      protected FunctionEditor.FObject getSelectedObject()
      Returns the currently selected object, if any.
    • getSelectedObjects

      protected FunctionEditor.FObject[] getSelectedObjects()
      Returns the currently selected objects, if any.
    • createObject

      protected abstract FunctionEditor.FObject createObject(String name, String expression, FunctionEditor.FObject obj)
      Creates an object with specified name and expression. An existing object may be passed in for modification or cloning, but there is no guarantee the same object will be returned.
      Parameters:
      name - the name
      expression - the expression
      obj - an object to assign values (may be null)
      Returns:
      the object
    • isDisallowedName

      protected boolean isDisallowedName(FunctionEditor.FObject obj, String name)
      Returns true if a name is forbidden or in use.
      Parameters:
      obj - the object (may be null)
      name - the proposed name for the object
      Returns:
      true if disallowed
    • createUniqueObject

      protected FunctionEditor.FObject createUniqueObject(FunctionEditor.FObject obj, String proposedName, boolean confirmChanges)
      Creates an object with a unique name.
      Parameters:
      obj - the object (may be null)
      proposedName - the proposed name
      confirmChanges - true to require user to confirm changes
      Returns:
      the object
    • getVariablePoints

      protected static boolean getVariablePoints(JTextPane variablesPane, Point pt, int[] ret)
      Scan a JTextPane entry such as blah blah blah: vr fr x y t for the mouse being over one of these. Turn it red and return the character positions in the ret array.
      Parameters:
      variablesPane -
      pt -
      ret - [pt, pt+len]
      Returns:
      true if variable was found
    • format

      public static String format(double value, double zeroLevel)
      Formats a number.
      Parameters:
      value - the number
      zeroLevel - the level below which the value is considered zero
      Returns:
      the formatted string
    • round

      public static double round(double value, int sigfigs)
      Rounds a number.
      Parameters:
      value - the number
      sigfigs - the number of significant figures in the rounded value
      Returns:
      the rounded value
    • tabToNext

      public void tabToNext()