ComponentActor.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import javax.swing.*;
  2. //***************************************************************************
  3. // This is a class that performs actions and reactions on a Jcomponent.
  4. // For example, it might make a text area as editable or non-editable.
  5. //
  6. // Author: Ali Badereddin
  7. // Date: September 11, 2005
  8. //
  9. //***************************************************************************
  10. public class ComponentActor extends Actor
  11. {
  12. // Define the action types that are specific for the component actor
  13. public final static int ENABLE = 0; // Enable or disable a component
  14. public final static int MAKE_VISIBLE = 1; // Show or hide a component
  15. // Represents hte component that the actor acts on.
  16. JComponent component;
  17. //---------------------------------------------------------------------
  18. // Constructor...
  19. //---------------------------------------------------------------------
  20. public ComponentActor (JComponent c)
  21. {
  22. // Get component
  23. component = c;
  24. // Set initial action type to ENABLE
  25. actionType = ENABLE;
  26. }
  27. //---------------------------------------------------------------------
  28. // Perform an action.
  29. //---------------------------------------------------------------------
  30. public void doAction ()
  31. {
  32. // Do nothing if no component is supplied
  33. if (component == null)
  34. return;
  35. // Perform action based on action type
  36. switch (actionType)
  37. {
  38. case ENABLE:
  39. component.setEnabled (true);
  40. break;
  41. case MAKE_VISIBLE:
  42. component.setVisible (true);
  43. break;
  44. }
  45. }
  46. //---------------------------------------------------------------------
  47. // Reverse the action..
  48. //---------------------------------------------------------------------
  49. public void undoAction ()
  50. {
  51. // Do nothing if no component is supplied
  52. if (component == null)
  53. return;
  54. // Perform reaction based on action type
  55. switch (actionType)
  56. {
  57. case ENABLE:
  58. component.setEnabled (false);
  59. break;
  60. case MAKE_VISIBLE:
  61. component.setVisible (false);
  62. break;
  63. }
  64. }
  65. //---------------------------------------------------------------------
  66. // Change component.
  67. //---------------------------------------------------------------------
  68. public void setComponent (JComponent c)
  69. {
  70. component = c;
  71. }
  72. //---------------------------------------------------------------------
  73. // Get component.
  74. //---------------------------------------------------------------------
  75. public JComponent getComponent ()
  76. {
  77. return component;
  78. }
  79. //---------------------------------------------------------------------
  80. // Set action type...
  81. //---------------------------------------------------------------------
  82. public void setActionType (int at)
  83. {
  84. actionType = at;
  85. }
  86. //---------------------------------------------------------------------
  87. // Get action type...
  88. //---------------------------------------------------------------------
  89. public int getActionType ()
  90. {
  91. return actionType;
  92. }
  93. }