/plugins/SVNPlugin/branches/1.2.0_svn1.4/src/ise/plugin/svn/gui/dateselector/NavigableDateSelector.java

# · Java · 187 lines · 108 code · 24 blank · 55 comment · 22 complexity · d6c6c63ce629d73b4789b3c7d9a5992e MD5 · raw file

  1. package ise.plugin.svn.gui.dateselector;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import javax.swing.border.*;
  6. import java.util.Date;
  7. import java.util.Calendar;
  8. import java.net.URL;
  9. /** This class is wrapper for a {@link DateSelector} that adds a
  10. * navigation bar to manipulate the wrapped selector.
  11. * See {@link DateSelectorPanel} for a description and picture
  12. * of date selectors.
  13. * <DL>
  14. * <DT><b>Images</b>
  15. * <DD>
  16. * <P>
  17. * The navigation-bar arrows in the current implementation are images
  18. * loaded as a "resource" from the CLASSPATH. Four files are used:
  19. * <blockquote>
  20. * $CLASSPATH/images/10px.red.arrow.right.double.gif<br>
  21. * $CLASSPATH/images/10px.red.arrow.left.double.gif<br>
  22. * $CLASSPATH/images/10px.red.arrow.right.gif<br>
  23. * $CLASSPATH/images/10px.red.arrow.left.gif
  24. * </blockquote>
  25. * where <em>$CLASSPATH</em> is any directory on your CLASSPATH.
  26. * If the <code>DateSelectorPanel</code>
  27. * can't find the image file, it uses character representations
  28. * (<code>"&gt;"</code>, <code>"&gt;&gt;"</code>,
  29. * <code>"&lt;"</code>, <code>"&lt;&lt;"</code>).
  30. * The main problem with this approach is that you can't change
  31. * the color of the arrows without changing the image files. On
  32. * the plus side, arbitrary images can be used for the movement
  33. * icons.
  34. * Future versions of this class will provide some way for you
  35. * to specify that the arrows be rendered internally in colors
  36. * that you specify at run time.
  37. * </DD>
  38. * </DL>
  39. * @see DateSelector
  40. * @see DateSelectorPanel
  41. * @see DateSelectorDialog
  42. * @see TitledDateSelector
  43. */
  44. public class NavigableDateSelector extends JPanel implements DateSelector {
  45. private DateSelector selector;
  46. // Names of images files used for the navigator bar.
  47. private static final String NEXT_YEAR = "ise/plugin/svn/gui/dateselector/images/10px.red.arrow.right.double.gif";
  48. private static final String NEXT_MONTH = "ise/plugin/svn/gui/dateselector/images/10px.red.arrow.right.gif";
  49. private static final String PREVIOUS_YEAR = "ise/plugin/svn/gui/dateselector/images/10px.red.arrow.left.double.gif";
  50. private static final String PREVIOUS_MONTH = "ise/plugin/svn/gui/dateselector/images/10px.red.arrow.left.gif";
  51. // These constants are used both to identify the button, and
  52. // as the button caption in the event that the appropriate
  53. // immage file can't be located.
  54. private static final String FORWARD_MONTH = ">" ;
  55. private static final String FORWARD_YEAR = ">>" ;
  56. private static final String BACK_MONTH = "<" ;
  57. private static final String BACK_YEAR = "<<" ;
  58. private JPanel navigation = null;
  59. /** Wrap an existing DateSelector to add a a navigation bar
  60. * modifies the wrapped DateSelector.
  61. */
  62. public NavigableDateSelector( DateSelector selector ) {
  63. this.selector = selector;
  64. setBorder( null );
  65. setOpaque( false );
  66. setLayout( new BorderLayout() );
  67. add( ( JPanel ) selector, BorderLayout.CENTER );
  68. navigation = new JPanel();
  69. navigation.setLayout( new FlowLayout() );
  70. navigation.setBorder( null );
  71. navigation.setBackground( ise.plugin.svn.gui.dateselector.Colors.LIGHT_YELLOW );
  72. navigation.add( makeNavigationButton( BACK_YEAR ) );
  73. navigation.add( makeNavigationButton( BACK_MONTH ) );
  74. navigation.add( makeNavigationButton( FORWARD_MONTH ) );
  75. navigation.add( makeNavigationButton( FORWARD_YEAR ) );
  76. add( navigation, BorderLayout.SOUTH );
  77. }
  78. /**
  79. * Create a navigable date selector by wrapping the indicated one.
  80. * @param selector the raw date selector to wrap;
  81. * @param background_color the background color of the navigation
  82. * bar (or null for transparent). The default color is
  83. * {@link ise.plugin.svn.gui.dateselector.Colors#LIGHT_YELLOW}.
  84. * @see #setBackground
  85. */
  86. public NavigableDateSelector( DateSelector selector, Color background_color ) {
  87. this( selector );
  88. navigation.setBackground( background_color );
  89. }
  90. /** Convenience constructor. Creates the wrapped DateSelector
  91. * for you. (It creates a {@link DateSelectorPanel} using
  92. * the no-arg constructor.
  93. */
  94. public NavigableDateSelector() {
  95. this( new DateSelectorPanel() );
  96. }
  97. public void changeNavigationBarColor( Color background_color ) {
  98. if ( background_color != null )
  99. navigation.setBackground( background_color );
  100. else
  101. navigation.setOpaque( false );
  102. }
  103. private final NavigationHandler navigation_listener = new NavigationHandler();
  104. /** Handle clicks from the navigation-bar buttons. */
  105. private class NavigationHandler implements ActionListener {
  106. public void actionPerformed( ActionEvent e ) {
  107. String direction = e.getActionCommand();
  108. if ( direction == FORWARD_YEAR )
  109. selector.roll( Calendar.YEAR, true );
  110. else if ( direction == BACK_YEAR )
  111. selector.roll( Calendar.YEAR, false );
  112. else if ( direction == FORWARD_MONTH ) {
  113. selector.roll( Calendar.MONTH, true );
  114. if ( selector.get( Calendar.MONTH ) == Calendar.JANUARY )
  115. selector.roll( Calendar.YEAR, true );
  116. }
  117. else if ( direction == BACK_MONTH ) {
  118. selector.roll( Calendar.MONTH, false );
  119. if ( selector.get( Calendar.MONTH ) == Calendar.DECEMBER )
  120. selector.roll( Calendar.YEAR, false );
  121. }
  122. else {
  123. assert false: "Unexpected direction"
  124. ;
  125. }
  126. }
  127. }
  128. private JButton makeNavigationButton( String caption ) {
  129. ClassLoader loader = getClass().getClassLoader();
  130. URL image =
  131. ( caption == FORWARD_YEAR ) ? loader.getResource( NEXT_YEAR ) :
  132. ( caption == BACK_YEAR ) ? loader.getResource( PREVIOUS_YEAR ) :
  133. ( caption == FORWARD_MONTH ) ? loader.getResource( NEXT_MONTH ) :
  134. loader.getResource( PREVIOUS_MONTH ) ;
  135. JButton b = ( image != null ) ? new JButton( new ImageIcon( image ) )
  136. : new JButton( caption )
  137. ;
  138. b.setBorder( new EmptyBorder( 0, 4, 0, 4 ) );
  139. b.setFocusPainted( false );
  140. b.setActionCommand( caption );
  141. b.addActionListener( navigation_listener );
  142. b.setOpaque( false );
  143. return b;
  144. }
  145. public synchronized void addActionListener( ActionListener l ) {
  146. selector.addActionListener( l );
  147. }
  148. public synchronized void removeActionListener( ActionListener l ) {
  149. selector.removeActionListener( l );
  150. }
  151. public Date getSelectedDate() {
  152. return selector.getSelectedDate();
  153. }
  154. public Date getCurrentDate() {
  155. return selector.getCurrentDate();
  156. }
  157. public void roll( int f, boolean up ) {
  158. selector.roll( f, up );
  159. }
  160. public int get( int f ) {
  161. return selector.get( f );
  162. }
  163. }