View Javadoc

1   package de.jaret.examples.timebars.calendar.swing.renderer;
2   
3   import java.awt.Color;
4   import java.awt.Graphics;
5   import java.awt.Rectangle;
6   
7   import javax.swing.JComponent;
8   import javax.swing.JTextArea;
9   
10  import de.jaret.examples.timebars.calendar.model.Appointment;
11  import de.jaret.util.date.Interval;
12  import de.jaret.util.swing.GraphicsHelper;
13  import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
14  import de.jaret.util.ui.timebars.TimeBarViewerInterface;
15  import de.jaret.util.ui.timebars.swing.TimeBarViewer;
16  import de.jaret.util.ui.timebars.swing.renderer.TimeBarRenderer;
17  
18  public class AppointmentRenderer implements TimeBarRenderer {
19      /*** insets for drawing. */
20      protected static final int INSETS = 2;
21      /*** width or height times this factor = perentage used as the non painted border. */
22      protected static final double BORDERFACTOR = 0.1;
23  
24      /*** component used for rendering. */
25      private AppRendererComponent _component = new AppRendererComponent();
26  
27      public JComponent getTimeBarRendererComponent(TimeBarViewer tbv, Interval value, boolean isSelected,
28              boolean overlapping) {
29          _component.setUp(tbv, value, isSelected, overlapping);
30          return _component;
31      }
32  
33      /***
34       * Component for the renderer.
35       * 
36       * @author kliem
37       * @version $Id: AppointmentRenderer.java 1083 2011-07-01 20:29:16Z kliem $
38       */
39      @SuppressWarnings("serial")
40      class AppRendererComponent extends JComponent {
41          private TimeBarViewer _tbv;
42          private Interval _interval;
43          private boolean _selected;
44          private boolean _overlapping;
45  
46          /*** textarea used for rendering the appointment text. */
47          private JTextArea _area = new JTextArea();
48  
49          /***
50           * Set up the compoentn for rendering.
51           * 
52           * @param tbv viewer
53           * @param value interval
54           * @param isSelected true if selected
55           * @param overlapping true if overlapping
56           */
57          public void setUp(TimeBarViewer tbv, Interval value, boolean isSelected, boolean overlapping) {
58              _tbv = tbv;
59              _interval = value;
60              _selected = isSelected;
61              _overlapping = overlapping;
62          }
63  
64          /***
65           * {@inheritDoc}
66           */
67          protected void paintComponent(Graphics g) {
68              // draw focus
69              // drawFocus(gc, drawingArea, delegate, interval, selected, printing, overlap);
70  
71              boolean horizontal = _tbv.getTBOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL;
72              Rectangle iRect = getIRect(horizontal, _overlapping);
73  
74              Color fg = g.getColor();
75              String str = _interval.toString();
76  
77              if (horizontal) {
78                  // horizontal is fallback ONLY
79                  if (!_selected) {
80                      g.setColor(Color.LIGHT_GRAY);
81                  } else {
82                      g.setColor(Color.BLUE);
83                  }
84                  g.fillRect(iRect.x, iRect.y, iRect.width, iRect.height);
85                  g.setColor(Color.BLACK);
86                  g.drawRect(iRect.x, iRect.y, iRect.width, iRect.height);
87                  GraphicsHelper.drawStringCentered(g, str, iRect);
88              } else {
89                  // vertical
90                  String timeString = getTimeString((Appointment) _interval);
91  
92                  if (!_selected) {
93                      g.setColor(Color.LIGHT_GRAY);
94                  } else {
95                      g.setColor(Color.BLUE);
96                  }
97                  Color bgx = g.getColor();
98                  // fill the background
99                  g.fillRect(iRect.x, iRect.y, iRect.width, iRect.height);
100                 // draw the upper border containing the time span
101                 int h = 20;
102                 g.setColor(Color.YELLOW);
103                 g.fillRect(iRect.x, iRect.y, iRect.width, h);
104                 g.setColor(Color.BLACK);
105                 GraphicsHelper.drawStringCentered(g, timeString, new Rectangle(iRect.x, iRect.y, iRect.width, h));
106              
107                 Rectangle textRect = new Rectangle(iRect.x + INSETS, iRect.y + h + INSETS, iRect.width - INSETS,
108                         iRect.height - INSETS - h);
109                 _area.setBounds(textRect);
110                 _area.setBackground(bgx);
111                 _area.setText(((Appointment) _interval).getText());
112                 Graphics gg = g.create(textRect.x, textRect.y, textRect.width, textRect.height);
113                 _area.paint(gg);
114                 gg.dispose();
115 
116                 // draw the border
117                 g.setColor(Color.BLACK);
118                 g.drawRect(iRect.x, iRect.y, iRect.width, iRect.height);
119             }
120             g.setColor(fg);
121 
122         }
123 
124         private String getTimeString(Appointment interval) {
125             return interval.getBegin().toDisplayStringTime(false) + " - "
126                     + interval.getEnd().toDisplayStringTime(false);
127         }
128 
129         public Rectangle getContainingRectangle(TimeBarViewerDelegate delegate, Interval interval,
130                 Rectangle drawingArea, boolean overlapping) {
131             boolean horizontal = delegate.getOrientation() == TimeBarViewerInterface.Orientation.HORIZONTAL;
132             Rectangle iRect = getIRect(horizontal, overlapping);
133             return iRect;
134         }
135 
136         protected Rectangle getIRect(boolean horizontal, boolean overlap) {
137             int cwidth = getWidth();
138             int cheight = getHeight();
139             if (horizontal) {
140                 int borderHeight = (int) (cheight * BORDERFACTOR / 2);
141                 int height = cheight - (overlap ? 0 : 2 * borderHeight);
142                 int y = overlap ? 0 : borderHeight;
143                 return new Rectangle(0, y, cwidth - 1, height - 1);
144             } else {
145                 int borderWidth = (int) (cwidth * BORDERFACTOR / 2);
146                 int width = cwidth - (overlap ? 0 : 2 * borderWidth);
147                 int x = overlap ? 0 : borderWidth;
148                 return new Rectangle(x, 0, width - 1, cheight - 1);
149             }
150         }
151 
152     }
153     /***
154      * {@inheritDoc} Simple default implementation.
155      */
156 	public Rectangle getPreferredDrawingBounds(Rectangle intervalDrawingArea,
157 			TimeBarViewerDelegate delegate, Interval interval,
158 			boolean selected, boolean overlap) {
159 		return intervalDrawingArea;
160 	}
161 
162 }