KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > calendar > ui > calendar > ActivityShapeFactory


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.calendar;
19
20 import java.awt.BasicStroke JavaDoc;
21 import java.awt.Color JavaDoc;
22 import java.awt.Cursor JavaDoc;
23 import java.awt.Font JavaDoc;
24 import java.awt.Insets JavaDoc;
25 import java.awt.event.MouseEvent JavaDoc;
26
27 import javax.swing.SwingConstants JavaDoc;
28 import javax.swing.UIManager JavaDoc;
29
30 import org.columba.calendar.parser.ParserHelper;
31
32 import com.miginfocom.ashape.AShapeUtil;
33 import com.miginfocom.ashape.interaction.DefaultInteractionBroker;
34 import com.miginfocom.ashape.interaction.MouseKeyInteractor;
35 import com.miginfocom.ashape.interaction.OverrideFilter;
36 import com.miginfocom.ashape.shapes.AShape;
37 import com.miginfocom.ashape.shapes.ContainerAShape;
38 import com.miginfocom.ashape.shapes.DrawAShape;
39 import com.miginfocom.ashape.shapes.FeatherAShape;
40 import com.miginfocom.ashape.shapes.FillAShape;
41 import com.miginfocom.ashape.shapes.RootAShape;
42 import com.miginfocom.ashape.shapes.TextAShape;
43 import com.miginfocom.calendar.activity.Activity;
44 import com.miginfocom.calendar.activity.ActivityInteractor;
45 import com.miginfocom.calendar.activity.view.ActivityView;
46 import com.miginfocom.calendar.datearea.DefaultDateArea;
47 import com.miginfocom.util.command.DefaultCommand;
48 import com.miginfocom.util.gfx.GfxUtil;
49 import com.miginfocom.util.gfx.RoundRectangle;
50 import com.miginfocom.util.gfx.SliceSpec;
51 import com.miginfocom.util.gfx.geometry.AbsRect;
52 import com.miginfocom.util.gfx.geometry.PlaceRect;
53 import com.miginfocom.util.gfx.geometry.numbers.AtEnd;
54 import com.miginfocom.util.gfx.geometry.numbers.AtStart;
55 import com.miginfocom.util.states.GenericStates;
56
57 public class ActivityShapeFactory {
58
59     public static final String JavaDoc DEFAULT_CONTAINER_SHAPE_NAME = "defaultContainer";
60
61     public static final String JavaDoc DEFAULT_BACKGROUND_SHAPE_NAME = "defaultBackground";
62
63     public static final String JavaDoc DEFAULT_TITLE_TEXT_SHAPE_NAME = "defaultTitleText";
64
65     public static final String JavaDoc DEFAULT_MAIN_TEXT_SHAPE_NAME = "defaultMainText";
66
67     public static final String JavaDoc DEFAULT_OUTLINE_SHAPE_NAME = "defaultOutline";
68
69     public static final String JavaDoc DEFAULT_SHADOW_SHAPE_SHAPE_NAME = "defaultShadowShape";
70
71     public static final String JavaDoc DEFAULT_SHADOW_SHAPE_NAME = "defaultShadow";
72
73     /**
74      * Creates the default shape.
75      *
76      * @param dimension
77      * <code>SwingConstants.VERTICAL</code> or
78      * <code>SwingConstants.HORIZONTAL</code>.
79      */

80     public static RootAShape createDefaultShape(int dimension) {
81         Color JavaDoc bgPaint = new Color JavaDoc(0, 0, 255, 40);
82         Color JavaDoc outlinePaint = new Color JavaDoc(100, 100, 150);
83         Color JavaDoc textPaint = new Color JavaDoc(50, 50, 50);
84         // Color shadowPaint = new Color(0, 0, 0, 100);
85
Color JavaDoc shadowPaint = null;
86
87         Font JavaDoc textFont = UIManager.getFont("Label.font");
88
89         RootAShape root = new RootAShape();
90         ContainerAShape container = new ContainerAShape(
91                 DEFAULT_CONTAINER_SHAPE_NAME, AbsRect.FILL);
92
93         FillAShape bgAShape = new FillAShape(DEFAULT_BACKGROUND_SHAPE_NAME,
94                 new RoundRectangle(0, 0, 1, 1, 8, 8), AbsRect.FILL_INSIDE,
95                 bgPaint, GfxUtil.AA_HINT_ON);
96
97         PlaceRect titleTextAbsRect = new AbsRect(new AtStart(2),
98                 new AtStart(1), new AtEnd(0), new AtEnd(0), null, null, null);
99         TextAShape titleText = new TextAShape(DEFAULT_TITLE_TEXT_SHAPE_NAME,
100                 "$startTime$ $summary$",
101                 titleTextAbsRect, TextAShape.TYPE_WRAP_TEXT, textFont,
102                 textPaint, new AtStart(0), new AtStart(-3), GfxUtil.AA_HINT_ON);
103         // TextAShape titleText = new TextAShape(DEFAULT_TITLE_TEXT_SHAPE_NAME,
104
// "$startTime$ - $endTimeExcl$ ($timeZoneShort$)",
105
// titleTextAbsRect, TextAShape.TYPE_SINGE_LINE, textFont,
106
// textPaint, new AtStart(0), new AtStart(-3), GfxUtil.AA_HINT_ON);
107
titleText.setAttribute(AShape.A_CLIP_TYPE, AShape.CLIP_PARENT_BOUNDS);
108
109         PlaceRect mainTextAbsRect = new AbsRect(new AtStart(2),
110                 new AtStart(16), new AtEnd(0), new AtEnd(0), null, null, null);
111         
112         TextAShape mainText = new TextAShape(DEFAULT_MAIN_TEXT_SHAPE_NAME,
113                 "$summary$", mainTextAbsRect, TextAShape.TYPE_WRAP_TEXT,
114                 textFont, textPaint, new AtStart(0), new AtStart(0),
115                 GfxUtil.AA_HINT_ON);
116
117         DrawAShape outlineAShape = new DrawAShape(DEFAULT_OUTLINE_SHAPE_NAME,
118                 new RoundRectangle(0, 0, 1, 1, 8, 8), AbsRect.FILL,
119                 outlinePaint, new BasicStroke JavaDoc(1f), GfxUtil.AA_HINT_ON);
120         outlineAShape.setAttribute(AShape.A_MOUSE_CURSOR, Cursor
121                 .getPredefinedCursor(Cursor.MOVE_CURSOR));
122         outlineAShape.setAttribute(AShape.A_REPORT_HIT_AREA, Boolean.TRUE);
123
124         PlaceRect bgAbsRect = new AbsRect(new AtStart(0), new AtStart(0),
125                 new AtEnd(0), new AtEnd(0), null, null,
126                 new Insets JavaDoc(-2, -2, 2, 2));
127         FillAShape filledShadow = new FillAShape(
128                 DEFAULT_SHADOW_SHAPE_SHAPE_NAME, new RoundRectangle(0, 0, 1, 1,
129                         5, 5), bgAbsRect, shadowPaint, GfxUtil.AA_HINT_ON);
130         SliceSpec shwSI = new SliceSpec(new Insets JavaDoc(10, 10, 10, 10),
131                 SliceSpec.TYPE_TILE_CUT, SliceSpec.OPT_BORDER);
132         FeatherAShape shadowShape = new FeatherAShape(
133                 DEFAULT_SHADOW_SHAPE_NAME, filledShadow, new Color JavaDoc(255, 255,
134                         255, 0), 5, shwSI);
135
136         bgAShape.addSubShape(titleText);
137         // bgAShape.addSubShape(mainText);
138

139         container.addSubShape(shadowShape);
140         container.addSubShape(bgAShape);
141         container.addSubShape(outlineAShape);
142         root.addSubShape(container);
143         root.setRepaintPadding(new Insets JavaDoc(4, 4, 4, 4));
144
145         if (dimension == SwingConstants.VERTICAL) {
146             AShapeUtil.enableMouseOverCursor(root);
147             AShapeUtil.enableMouseOverState(outlineAShape);
148
149             AShapeUtil.setResizeBoxes(outlineAShape, dimension, 4);
150
151             // Drag, resize interactions
152
Integer JavaDoc button = new Integer JavaDoc(MouseEvent.BUTTON1);
153
154             AShapeUtil.addMouseFireEvent(outlineAShape,
155                     MouseKeyInteractor.MOUSE_PRESS,
156                     DefaultDateArea.AE_SELECTED_PRESSED, true, false, button);
157             AShapeUtil.addMouseFireEvent(outlineAShape,
158                     MouseKeyInteractor.MOUSE_PRESS,
159                     DefaultDateArea.AE_DRAG_PRESSED, true, true, button);
160
161             DefaultCommand entCmd = new DefaultCommand(
162                     DefaultInteractionBroker.CMD_FIRE_INTERACTION_EVENT, null,
163                     DefaultDateArea.AE_MOUSE_ENTERED, null);
164             DefaultCommand exitCmd = new DefaultCommand(
165                     DefaultInteractionBroker.CMD_FIRE_INTERACTION_EVENT, null,
166                     DefaultDateArea.AE_MOUSE_EXITED, null);
167             AShapeUtil.addEnterExitCommands(outlineAShape, entCmd, exitCmd,
168                     true);
169
170             AShapeUtil.addMouseFireEvent(outlineAShape,
171                     MouseKeyInteractor.MOUSE_CLICK, DefaultDateArea.AE_CLICKED,
172                     true, false, button);
173             AShapeUtil.addMouseFireEvent(outlineAShape,
174                     MouseKeyInteractor.MOUSE_DOUBLE_CLICK,
175                     DefaultDateArea.AE_DOUBLE_CLICKED, true, true, button);
176             AShapeUtil.addMouseFireEvent(outlineAShape,
177                     MouseKeyInteractor.MOUSE_POPUP_TRIGGER,
178                     DefaultDateArea.AE_POPUP_TRIGGER, true, true, null);
179
180             // Block mouse moves to the underlaying component won't restore the
181
// Cursor
182
AShapeUtil.addMouseEventBlock(outlineAShape, false, new Integer JavaDoc(
183                     MouseEvent.MOUSE_MOVED));
184             
185             // all day events
186
ActivityInteractor.setStaticOverride(
187                     AShapeUtil.DEFAULT_TITLE_TEXT_SHAPE_NAME, AShape.A_TEXT,
188                     new OverrideFilter() {
189
190                         private final String JavaDoc newTemplate = ActivityInteractor.TEMPL_SUMMARY.toString();
191
192                         public Object JavaDoc getOverride(Object JavaDoc subject,
193                                 Object JavaDoc defaultObject) {
194                             Activity act = ((ActivityView) subject).getModel();
195                             return ParserHelper.isAllDayEvent(act
196                                     .getBaseDateRange().getStart(), act
197                                     .getBaseDateRange().getEnd()) ? newTemplate
198                                     : defaultObject;
199                         }
200                     });
201
202             ActivityInteractor.setStaticOverride(
203                     AShapeUtil.DEFAULT_MAIN_TEXT_SHAPE_NAME, AShape.A_TEXT,
204                     new OverrideFilter() {
205
206                         private final String JavaDoc newTemplate = ActivityInteractor.TEMPL_SUMMARY.toString();
207
208                         public Object JavaDoc getOverride(Object JavaDoc subject,
209                                 Object JavaDoc defaultObject) {
210                             Activity act = ((ActivityView) subject).getModel();
211                             return ParserHelper.isAllDayEvent(act
212                                     .getBaseDateRange().getStart(), act
213                                     .getBaseDateRange().getEnd()) ? newTemplate
214                                     : defaultObject;
215                         }
216                     });
217
218         } else {
219             AShapeUtil.enableMouseOverCursor(root);
220             AShapeUtil.enableMouseOverState(outlineAShape);
221
222             // AShapeUtil.addResizeBoxes(root, SwingConstants.HORIZONTAL, 4);
223

224             // Drag, resize interactions
225

226             Integer JavaDoc button = new Integer JavaDoc(MouseEvent.BUTTON1);
227             AShapeUtil.addMouseFireEvent(outlineAShape,
228                     MouseKeyInteractor.MOUSE_PRESS,
229                     DefaultDateArea.AE_SELECTED_PRESSED, true, false, button);
230             AShapeUtil.addMouseFireEvent(outlineAShape,
231                     MouseKeyInteractor.MOUSE_PRESS,
232                     DefaultDateArea.AE_DRAG_PRESSED, true, true, button);
233
234             AShapeUtil.addMouseEventBlock(outlineAShape, false, new Integer JavaDoc(
235                     MouseEvent.MOUSE_MOVED));
236
237         }
238
239         // different border for recurrent events
240
// ActivityInteractor.setStaticOverride(
241
// AShapeUtil.DEFAULT_OUTLINE_SHAPE_NAME, AShape.A_STROKE,
242
// new OverrideFilter() {
243
// public Object getOverride(Object subject,
244
// Object defaultObject) {
245
// return ((ActivityView) subject).getModel()
246
// .isRecurrent() ? new BasicStroke(1.0f,
247
// BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL,
248
// 0, new float[] { 9 }, 0) : defaultObject;
249
// }
250
// });
251

252         // differnt outline color is selected
253
AShapeUtil.setStateOverride(outlineAShape, GenericStates.SELECTED,
254                 AShape.A_PAINT, new Color JavaDoc(150, 200, 0));
255
256         // bold outline if selected
257
AShapeUtil.setStateOverride(outlineAShape, GenericStates.SELECTED,
258                 AShape.A_STROKE, new BasicStroke JavaDoc(2.0f));
259
260         return root;
261     }
262
263 }
264
Popular Tags