KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > schedule > ScheduleView


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.usertasks.schedule;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.Rectangle JavaDoc;
27 import java.text.DateFormat JavaDoc;
28 import java.text.SimpleDateFormat JavaDoc;
29 import java.util.Calendar JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.GregorianCalendar JavaDoc;
32 import javax.swing.JComponent JavaDoc;
33 import org.netbeans.modules.tasklist.usertasks.*;
34 import org.netbeans.modules.tasklist.usertasks.model.Duration;
35 import org.netbeans.modules.tasklist.usertasks.model.UserTaskObjectList;
36 import org.netbeans.modules.tasklist.usertasks.model.UserTask;
37 import org.netbeans.modules.tasklist.usertasks.model.UserTaskList;
38 import org.netbeans.modules.tasklist.usertasks.options.Settings;
39
40 /**
41  * Schedule component.
42  *
43  * @author tl
44  */

45 public class ScheduleView extends JComponent JavaDoc {
46     private static final int DAY_WIDTH = 50;
47     private static final int TASK_HEIGHT = 10;
48     private static final Font JavaDoc FONT = new Font JavaDoc("SansSerif", // NOI18N
49
Font.PLAIN, 12);
50     private static final Color JavaDoc LINES_COLOR = Color.GRAY;
51     private static final Color JavaDoc WEEKEND_COLOR = new Color JavaDoc(237, 237, 237);
52     private static final DateFormat JavaDoc MONTH_FORMAT =
53         new SimpleDateFormat JavaDoc("MMMM yyyy"); // NOI18N
54

55     private Date JavaDoc start = new Date JavaDoc();
56     private int days = 100;
57     private UserTaskList utl;
58     
59     /**
60      * Creates a new instance of ScheduleView
61      */

62     public ScheduleView() {
63     }
64
65     /**
66      * Shows another user task list.
67      *
68      * @param list new list or null
69      */

70     public void setUserTaskList(UserTaskList list) {
71         this.utl = list;
72         repaint();
73     }
74     
75     protected void paintComponent(java.awt.Graphics JavaDoc g) {
76         Rectangle JavaDoc r = g.getClipBounds();
77         g.setColor(Color.white);
78         g.fillRect(r.x, r.y, r.width, r.height);
79         
80         paintDays(g);
81         if (utl == null)
82             g.drawString("---", getWidth() / 2, getHeight() / 2); // NOI18N
83
else
84             paintTasks(g, utl.getSubtasks(), 0, 32);
85     }
86
87     /**
88      * Paints vertical lines for days
89      *
90      * @param g graphics object
91      */

92     private void paintDays(Graphics JavaDoc g) {
93         g.setColor(WEEKEND_COLOR);
94         g.fillRect(0, 0, getWidth(), 30);
95         
96         g.setFont(FONT);
97         
98         g.setColor(LINES_COLOR);
99         g.drawLine(0, 15, getWidth() - 1, 15);
100         g.drawLine(0, 30, getWidth() - 1, 30);
101         
102         Calendar JavaDoc gc = GregorianCalendar.getInstance();
103         gc.setTime(start);
104         
105         for (int x = 0; x < getWidth(); x += DAY_WIDTH) {
106             int day = gc.get(Calendar.DAY_OF_MONTH);
107             int dayOfWeek = gc.get(Calendar.DAY_OF_WEEK);
108             
109             if (day == 1) {
110                 g.drawString(MONTH_FORMAT.format(gc.getTime()), x, 13);
111             }
112
113             if (dayOfWeek == Calendar.SATURDAY ||
114                 dayOfWeek == Calendar.SUNDAY) {
115                 g.setColor(WEEKEND_COLOR);
116                 g.fillRect(x, 31, DAY_WIDTH, getHeight() - 31);
117             }
118             
119             g.setColor(Color.BLACK);
120             g.drawString(Integer.toString(day), x, 28); // NOI18N
121

122             g.setColor(LINES_COLOR);
123             g.drawLine(x, 15, x, getHeight());
124             gc.add(Calendar.DAY_OF_MONTH, 1);
125         }
126     }
127
128     /**
129      * Paints a list of tasks (and all subtasks)
130      *
131      * @param g Graphics object
132      * @param date start date as in Date.getTime()
133      * @param list list of tasks
134      * @param y Y-coordinate for the first task
135      */

136     private void paintTasks(Graphics JavaDoc g, UserTaskObjectList list, int x, int y) {
137         for (int i = 0; i < list.size(); i++) {
138             UserTask ut = list.getUserTask(i);
139             
140             Duration dur = new Duration(ut.getEffort(),
141                 Settings.getDefault().getMinutesPerDay(),
142                 Integer.MAX_VALUE, true);
143             int duration = dur.days;
144             if (dur.hours != 0 || dur.minutes != 0)
145                 duration++;
146             int w = duration * DAY_WIDTH;
147             
148             if (ut.getSubtasks().isEmpty()) {
149                 paintSimpleTask(g, x, y, w);
150             } else {
151                 paintSuperTask(g, x, y, w);
152                 paintTasks(g, ut.getSubtasks(), x, y + TASK_HEIGHT);
153             }
154             
155             x += w;
156             y += TASK_HEIGHT;
157         }
158     }
159     
160     /**
161      * Paints a simple task without children
162      * |-------------|
163      * |-------------|
164      *
165      * @param g graphics object
166      * @param x x coordinate of the upper left corner
167      * @param y y coordinate of the upper left corner
168      * @param width width of the task
169      */

170     private void paintSimpleTask(Graphics JavaDoc g, int x, int y, int width) {
171         g.setColor(new Color JavaDoc(140, 182, 206));
172         g.fillRect(x, y, width, TASK_HEIGHT);
173         g.setColor(Color.black);
174         g.drawRect(x, y, width - 1, TASK_HEIGHT - 1);
175     }
176     
177     /**
178      * Paints a task with children.
179      * |-------------|
180      * |/ \|
181      *
182      * @param g graphics object
183      * @param x x coordinate of the upper left corner
184      * @param y y coordinate of the upper left corner
185      * @param width width of the task
186      */

187     private void paintSuperTask(Graphics JavaDoc g, int x, int y, int width) {
188         g.setColor(Color.BLACK);
189         
190         int x2 = x + width;
191         g.fillRect(x, y, width, 2);
192                 
193         g.fillPolygon(new int[] {x, x + 7, x}, new int[] {y, y, y + 7}, 3);
194         g.fillPolygon(new int[] {x2, x2 - 7, x2}, new int[] {y, y, y + 7}, 3);
195     }
196     
197     /**
198      * @param d date difference in milliseconds
199      * @return width in the view
200      */

201     private int widthForDateDiff(long d) {
202         double days = d / (1000.0 * 60 * 60 * 24);
203         return (int) Math.round(days * DAY_WIDTH);
204     }
205     
206     /**
207      * Converts a date to the X-coordinate
208      *
209      * @param d a date as in Date.getTime()
210      * @return X-coordinate
211      */

212     private int xForDate(long d) {
213         long diff = d - start.getTime();
214         double days = diff / (1000.0 * 60 * 60 * 24);
215         return (int) Math.round(days * DAY_WIDTH);
216     }
217     
218     public java.awt.Dimension JavaDoc getPreferredSize() {
219         return new Dimension JavaDoc(days * DAY_WIDTH, 200);
220     }
221 }
222
Popular Tags