KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > calendar > ui > navigation > DateAreaBeanFactory


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.calendar.ui.navigation;
19
20 import java.awt.Color JavaDoc;
21 import java.awt.Font JavaDoc;
22 import java.awt.Graphics2D JavaDoc;
23 import java.awt.Rectangle JavaDoc;
24 import java.awt.RenderingHints JavaDoc;
25 import java.awt.geom.Rectangle2D JavaDoc;
26 import java.beans.PropertyChangeEvent JavaDoc;
27 import java.text.DateFormat JavaDoc;
28 import java.text.SimpleDateFormat JavaDoc;
29 import java.util.Calendar JavaDoc;
30
31 import javax.swing.BorderFactory JavaDoc;
32 import javax.swing.SwingConstants JavaDoc;
33 import javax.swing.UIManager JavaDoc;
34
35 import org.columba.calendar.ui.calendar.ActivityShapeFactory;
36
37 import com.miginfocom.ashape.shapes.RootAShape;
38 import com.miginfocom.ashape.shapes.TextAShape;
39 import com.miginfocom.beans.DateAreaBean;
40 import com.miginfocom.beans.DateHeaderBean;
41 import com.miginfocom.beans.GridDimensionLayoutBean;
42 import com.miginfocom.calendar.datearea.DateArea;
43 import com.miginfocom.calendar.datearea.DefaultDateArea;
44 import com.miginfocom.calendar.decorators.AbstractGridDecorator;
45 import com.miginfocom.calendar.decorators.DateSeparatorDecorator;
46 import com.miginfocom.calendar.decorators.SelectionGridDecorator;
47 import com.miginfocom.calendar.grid.DateGrid;
48 import com.miginfocom.calendar.grid.DefaultGridLineProvider;
49 import com.miginfocom.calendar.grid.Grid;
50 import com.miginfocom.calendar.grid.GridLineRepetition;
51 import com.miginfocom.calendar.grid.GridLineSpecProvider;
52 import com.miginfocom.calendar.grid.GridLineSpecification;
53 import com.miginfocom.calendar.header.CellDecorationRow;
54 import com.miginfocom.calendar.header.DateGridHeader;
55 import com.miginfocom.util.dates.DateFormatList;
56 import com.miginfocom.util.dates.DateRange;
57 import com.miginfocom.util.dates.DateRangeI;
58 import com.miginfocom.util.gfx.GfxUtil;
59 import com.miginfocom.util.gfx.geometry.AbsRect;
60 import com.miginfocom.util.gfx.geometry.SizeSpec;
61 import com.miginfocom.util.gfx.geometry.numbers.AtEnd;
62 import com.miginfocom.util.gfx.geometry.numbers.AtFixed;
63 import com.miginfocom.util.gfx.geometry.numbers.AtFraction;
64 import com.miginfocom.util.gfx.geometry.numbers.AtStart;
65 import com.miginfocom.util.repetition.DefaultRepetition;
66
67 public class DateAreaBeanFactory {
68
69     private static final Color JavaDoc labelColor = Color.DARK_GRAY;
70
71     // glocal grid line colors
72
private static final Color JavaDoc darkGrayColor = new Color JavaDoc(220, 220, 220);
73
74     private static final Color JavaDoc lightGrayColor = new Color JavaDoc(240, 240, 240);
75
76     private static final Color JavaDoc darkDarkGrayColor = new Color JavaDoc(180, 180, 180);
77
78     public static final RootAShape HORSHAPE = ActivityShapeFactory
79             .createDefaultShape(SwingConstants.HORIZONTAL);
80
81     public static final RootAShape VERSHAPE = ActivityShapeFactory
82             .createDefaultShape(SwingConstants.VERTICAL);
83
84     public static DateAreaBean initDateArea() {
85
86         GridDimensionLayoutBean monthlyVerticalGridDimensionLayout = new GridDimensionLayoutBean();
87         GridDimensionLayoutBean monthlyHorizontalGridDimensionLayout = new GridDimensionLayoutBean();
88         // monthlyActivityAShapeBean = new ActivityAShapeBean();
89
DateHeaderBean monthlyNorthDateHeaderBean = new DateHeaderBean();
90
91         DateAreaBean monthlyDateAreaBean = new DateAreaBean();
92
93         monthlyHorizontalGridDimensionLayout.setRowSizeNormal(new SizeSpec(
94                 new AtFixed(17.0f), null, null));
95
96         DateHeaderBean monthlyWestDateHeaderBean = new DateHeaderBean();
97         // monthlyWestDateHeaderBean
98
// .setLabelRotation(TextAShape.TYPE_SINGE_LINE_ROT_CCW);
99

100         monthlyWestDateHeaderBean.setHeaderRows(new CellDecorationRow[] {
101         // showing the week number
102
new CellDecorationRow(DateRangeI.RANGE_TYPE_WEEK,
103                         new DateFormatList("'w'w", null), new AtFixed(24f),
104                         new AbsRect(new AtStart(0.0f), new AtStart(0.0f),
105                                 new AtEnd(0.0f), new AtEnd(0.0f), null, null,
106                                 null), (java.awt.Paint JavaDoc[]) null,
107                         new java.awt.Paint JavaDoc[] { labelColor },
108                         new DefaultRepetition(0, 1, null, null),
109                         new java.awt.Font JavaDoc[] { UIManager.getFont("Label.font")
110                                 .deriveFont(9f) },
111                         new java.lang.Integer JavaDoc[] { null }, new AtFraction(0.5f),
112                         new AtStart(4f)) });
113
114         monthlyWestDateHeaderBean
115                 .setBackgroundPaint(new com.miginfocom.util.gfx.ShapeGradientPaint(
116                         new java.awt.Color JavaDoc(255, 255, 255), new java.awt.Color JavaDoc(
117                                 247, 247, 247), 0.0f, 0.7f, 0.6f, false));
118
119         monthlyWestDateHeaderBean
120                 .setTextAntiAlias(com.miginfocom.util.gfx.GfxUtil.AA_HINT_ON);
121
122         DateHeaderBean eastDateHeaderBean = new DateHeaderBean();
123         eastDateHeaderBean
124                 .setHeaderRows(new CellDecorationRow[] {
125                 // showing the month number
126
new CellDecorationRow(
127                         DateRangeI.RANGE_TYPE_MONTH,
128                         new DateFormatList("MMMM|MMM", null),
129                         new AtFixed(17f),
130                         new AbsRect(new AtStart(0.0f), new AtStart(0.0f),
131                                 new AtEnd(0.0f), new AtEnd(0.0f), null, null,
132                                 null),
133                         (java.awt.Paint JavaDoc[]) null,
134                         new java.awt.Paint JavaDoc[] { labelColor },
135                         new DefaultRepetition(0, 1, null, null),
136                         new java.awt.Font JavaDoc[] { UIManager.getFont("Label.font") },
137                         new java.lang.Integer JavaDoc[] { null }, new AtFraction(0.5f),
138                         new AtStart(4f)) });
139
140         eastDateHeaderBean
141                 .setBackgroundPaint(new com.miginfocom.util.gfx.ShapeGradientPaint(
142                         new java.awt.Color JavaDoc(255, 255, 255), new java.awt.Color JavaDoc(
143                                 247, 247, 247), 180.0f, 0.7f, 0.6f, false));
144
145         eastDateHeaderBean
146                 .setTextAntiAlias(com.miginfocom.util.gfx.GfxUtil.AA_HINT_ON);
147
148         eastDateHeaderBean.setLabelRotation(TextAShape.TYPE_SINGE_LINE_ROT_CW);
149
150         monthlyNorthDateHeaderBean
151                 .setBackgroundPaint(new com.miginfocom.util.gfx.ShapeGradientPaint(
152                         new java.awt.Color JavaDoc(240, 240, 240), new java.awt.Color JavaDoc(
153                                 255, 255, 255), 90.0f, 0.7f, 0.6f, false));
154
155         monthlyNorthDateHeaderBean
156                 .setHeaderRows(new CellDecorationRow[] { new com.miginfocom.calendar.header.CellDecorationRow(
157                         DateRangeI.RANGE_TYPE_DAY, new DateFormatList("1E",
158                                 null), new AtFixed(20.0f), new AbsRect(
159                                 new AtStart(0.0f), new AtStart(0.0f),
160                                 new AtEnd(0.0f), new AtEnd(0.0f), null, null,
161                                 null), (java.awt.Paint JavaDoc[]) null,
162                         new java.awt.Paint JavaDoc[] { labelColor }, null,
163                         new Font JavaDoc[] { UIManager.getFont("Label.font") },
164                         new Integer JavaDoc[] { null }, new AtFraction(0.5f),
165                         new AtFraction(0.5f)) });
166
167         monthlyNorthDateHeaderBean.setTextAntiAlias(GfxUtil.AA_HINT_ON);
168
169         monthlyDateAreaBean.setDateAreaOuterBorder(BorderFactory
170                 .createLineBorder(lightGrayColor));
171
172         monthlyDateAreaBean.setNorthDateHeader(monthlyNorthDateHeaderBean);
173         monthlyDateAreaBean.setWestDateHeader(monthlyWestDateHeaderBean);
174         monthlyDateAreaBean.setEastDateHeader(eastDateHeaderBean);
175         monthlyDateAreaBean
176                 .setPrimaryDimensionLayout(monthlyVerticalGridDimensionLayout);
177         monthlyDateAreaBean
178                 .setSecondaryDimensionLayout(monthlyHorizontalGridDimensionLayout);
179
180         ((DateGridHeader) monthlyWestDateHeaderBean.getHeader())
181                 .setGridLineSpecification(new GridLineSpecification(
182                         new DefaultGridLineProvider(new GridLineRepetition[] {
183                         // horizontal light gray column separator line
184
new GridLineRepetition(0, 1, new AtStart(0f),
185                                         null, 1, lightGrayColor,
186                                         new AtStart(3f), new AtEnd(-3f))
187
188                                 }), null
189
190                 ));
191         ((DateGridHeader) monthlyNorthDateHeaderBean.getHeader())
192                 .setGridLineSpecification(new GridLineSpecification(
193                         new DefaultGridLineProvider(new GridLineRepetition[] {
194                         // vertical light gray column separator line
195
new GridLineRepetition(0, 1, new AtStart(0f),
196                                         null, 1, lightGrayColor,
197                                         new AtStart(3f), new AtEnd(-3f))
198
199                                 }), null
200
201                 ));
202         // date area grid line
203
monthlyDateAreaBean.getDateArea().setGridLineSpecProvider(
204                 new GridLineSpecProvider() {
205                     public GridLineSpecification createSpecification(
206                             DateArea dateArea) {
207                         return new GridLineSpecification(
208                                 // horizontal grid lines
209
new DefaultGridLineProvider(
210                                         new GridLineRepetition[] {
211                                         // dark gray line at 12 and 13
212
new GridLineRepetition(0, 1,
213                                                 new AtStart(0.1f), new AtEnd(
214                                                         -1f), 1, lightGrayColor) }),
215                                 // vertical grid lines
216
new DefaultGridLineProvider(
217                                         new GridLineRepetition[] {
218                                         // light gray line every day
219
new GridLineRepetition(0, 1, null,
220                                                 null, 1, lightGrayColor) }),// vertical
221
// grid
222
// lines
223
new DefaultGridLineProvider(
224                                         new GridLineRepetition[] {
225                                         // light gray line every day
226
new GridLineRepetition(0, 1,
227                                                 new AtStart(1f),
228                                                 new AtEnd(-2f), 1,
229                                                 lightGrayColor) }));
230                     }
231                 });
232
233         monthlyDateAreaBean.setPrimaryDimension(SwingConstants.HORIZONTAL);
234         monthlyDateAreaBean
235                 .setPrimaryDimensionCellType(DateRangeI.RANGE_TYPE_DAY);
236         monthlyDateAreaBean.setPrimaryDimensionCellTypeCount(1);
237         monthlyDateAreaBean.setWrapBoundary(DateRangeI.RANGE_TYPE_WEEK);
238
239         DefaultDateArea dateArea = monthlyDateAreaBean.getDateArea();
240         dateArea.setActivitiesSupported(false);
241
242         // select current day
243
monthlyDateAreaBean.getDateArea()
244                 .addDecorator(
245                         new AbstractGridDecorator(monthlyDateAreaBean
246                                 .getDateArea(), 20) {
247                             public void doPaint(Graphics2D JavaDoc g2, Rectangle JavaDoc bounds) {
248                                 DateGrid dateGrid = (DateGrid) getGrid();
249
250                                 DateRangeI dr = new DateRange(System
251                                         .currentTimeMillis(),
252                                         DateRangeI.RANGE_TYPE_DAY, 1, null,
253                                         null);
254                                 Rectangle JavaDoc[] rects = dateGrid
255                                         .getBoundsForDateRange(dr,
256                                                 Grid.SIZE_MODE_INSIDE);
257
258                                 g2.setColor(new Color JavaDoc(250, 250, 250));
259                                 for (int i = 0; i < rects.length; i++)
260                                     g2.fill(rects[i]);
261                             }
262
263                             public void gridChanged(PropertyChangeEvent JavaDoc e) {
264                             }
265
266                             public void dispose() {
267                             }
268                         });
269
270         final DateFormat JavaDoc defaultFormat = new SimpleDateFormat JavaDoc("MMMM dd");
271
272         monthlyDateAreaBean.getDateArea()
273                 .addDecorator(
274                         new AbstractGridDecorator(monthlyDateAreaBean
275                                 .getDateArea(), 20) {
276                             public void doPaint(Graphics2D JavaDoc g2, Rectangle JavaDoc bounds) {
277                                 DateGrid dateGrid = (DateGrid) getGrid();
278
279                                 RenderingHints JavaDoc qualityHints = new RenderingHints JavaDoc(
280                                         RenderingHints.KEY_ANTIALIASING,
281                                         RenderingHints.VALUE_ANTIALIAS_ON);
282                                 g2.setRenderingHints(qualityHints);
283
284                                 g2.setFont(UIManager.getFont("Label.font"));
285
286                                 for (int i = 0; i < dateGrid.getRowCount(); i++) {
287                                     for (int j = 0; j < dateGrid
288                                             .getColumnCount(); j++) {
289                                         Rectangle JavaDoc r = dateGrid.getBoundsOfCell(
290                                                 i, j, Grid.SIZE_MODE_INSIDE,
291                                                 true);
292                                         DateRangeI range = dateGrid
293                                                 .getDateRangeForCell(i, j);
294                                         int day = range.getStart().get(
295                                                 Calendar.DAY_OF_MONTH);
296                                         int weekday = range.getStart().get(
297                                                 Calendar.DAY_OF_WEEK);
298                                         if (weekday == Calendar.SUNDAY)
299                                             g2
300                                                     .setColor(new Color JavaDoc(255,
301                                                             102, 102));
302                                         else
303                                             g2.setColor(darkDarkGrayColor);
304                                         String JavaDoc dayString = new Integer JavaDoc(day)
305                                                 .toString();
306
307                                         Rectangle2D JavaDoc rect = g2.getFontMetrics()
308                                                 .getStringBounds(dayString, g2);
309
310                                         int x2 = r.x + r.width / 2 - 1;
311                                         x2 -= Math.abs(rect.getWidth() / 2);
312                                         int y2 = r.y;
313                                         y2 += Math.abs(rect.getHeight());
314                                         g2.drawString(dayString, x2, y2);
315
316                                     }
317
318                                 }
319
320                             }
321
322                             public void gridChanged(PropertyChangeEvent JavaDoc e) {
323                             }
324
325                             public void dispose() {
326                             }
327                         });
328         monthlyDateAreaBean.getDateArea()
329                 .addDecorator(
330                         new SelectionGridDecorator(monthlyDateAreaBean
331                                 .getDateArea(), 10, new java.awt.Paint JavaDoc[] {
332                                 lightGrayColor, null, new Color JavaDoc(255,255,230,255), null },
333                                 new int[] { Grid.SIZE_MODE_INSIDE,
334                                         Grid.SIZE_MODE_INSIDE,
335                                         Grid.SIZE_MODE_INSIDE,
336                                         Grid.SIZE_MODE_INSIDE }));
337         dateArea.addDecorator(new DateSeparatorDecorator(dateArea, 700,
338                 DateRangeI.RANGE_TYPE_MONTH, darkDarkGrayColor));
339
340         //
341
// activity setup
342
//
343

344         dateArea.setActivitiesSupported(false);
345
346         return monthlyDateAreaBean;
347     }
348
349 }
350
Popular Tags