KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > apps > search > VSchedulePanel


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.apps.search;
15
16 import javax.swing.*;
17 import java.awt.*;
18 import java.awt.font.*;
19 import java.awt.event.*;
20 import java.util.*;
21 import java.text.*;
22 import java.sql.*;
23 import java.math.*;
24
25 import org.compiere.model.*;
26 import org.compiere.util.*;
27 import org.compiere.plaf.*;
28 import org.compiere.grid.ed.*;
29
30 /**
31  * Schedule Panel
32  *
33  * @author Jorg Janke
34  * @version $Id: VSchedulePanel.java,v 1.9 2003/09/29 01:04:41 jjanke Exp $
35  */

36 public class VSchedulePanel extends JComponent implements MouseListener
37 {
38     /**
39      * Constructor
40      */

41     public VSchedulePanel ()
42     {
43         setHeight(250);
44         addMouseListener(this);
45     } // VSchedulePanel
46

47
48     /** Number of Days */
49     private int m_noDays = 1;
50     /** Height */
51     private int m_height = 250;
52
53     /** Day Slot Width */
54     private int m_dayWidth = 170;
55
56     /** TimePanel for layout info */
57     private VScheduleTimePanel m_timePanel = null;
58
59     /** Assignment Slots */
60     private MAssignmentSlot[] m_slots = null;
61     /** Position of Slots */
62     private Rectangle[] m_where = null;
63     /** Start Date */
64     private Timestamp m_startDate = null;
65     /** If true creates new assignments */
66     private boolean m_createNew = false;
67     /** Resource ID */
68     private int m_S_Resource_ID = 0;
69
70     private InfoSchedule m_infoSchedule = null;
71
72     /** Text Margin */
73     private static final int MARGIN = 2;
74
75     /**
76      * Set Type.
77      * Calculate number of days and set
78      * @param type schedule type - see VSchedule.TYPE_...
79      */

80     public void setType (int type)
81     {
82         if (type == VSchedule.TYPE_MONTH)
83             m_noDays = 31;
84         else if (type == VSchedule.TYPE_WEEK)
85             m_noDays = 7;
86         else
87             m_noDays = 1;
88         setSize();
89     } // setType
90

91     /**
92      * Set InfoSchedule for callback
93      * @param is InfoSchedule
94      */

95     public void setInfoSchedule (InfoSchedule is)
96     {
97         m_infoSchedule = is;
98     }
99
100     /**
101      * Enable/disable to Create New Assignments
102      * @param createNew if true, allows to create new Assignments
103      */

104     public void setCreateNew (boolean createNew)
105     {
106         m_createNew = createNew;
107     } // setCreateNew
108

109     /**
110      * From height, Calculate & Set Size
111      * @param height height in pixels
112      */

113     public void setHeight (int height)
114     {
115         m_height = height;
116         setSize();
117     } // setHeight
118

119     /**
120      * Set Size
121      */

122     public void setSize ()
123     {
124         // width
125
FontMetrics fm = null;
126         Graphics g = getGraphics();
127         if (g == null)
128             g = Env.getGraphics(this);
129         if (g != null)
130             fm = g.getFontMetrics(g.getFont()); // the "correct" way
131
m_dayWidth = 0;
132         for (int i = 0; i < m_noDays; i++)
133         {
134             if (fm != null)
135             {
136                 String JavaDoc string = getHeading(i);
137                 int width = fm.stringWidth(string);
138                 if (width > m_dayWidth)
139                     m_dayWidth = width;
140             }
141         }
142         m_dayWidth += 20;
143         if (m_dayWidth < 180) // minimum width
144
m_dayWidth = 180;
145
146         int w = m_noDays * m_dayWidth;
147         //
148
Dimension size = new Dimension(w, m_height);
149         setPreferredSize(size);
150         setMinimumSize(size);
151         setMaximumSize(size);
152     } // setHeight
153

154     /**
155      * Set time Panel for info about tile slots
156      * @param timePanel time panel
157      */

158     public void setTimePanel (VScheduleTimePanel timePanel)
159     {
160         m_timePanel = timePanel;
161     } // setTimePanel
162

163     /*************************************************************************/
164
165     /**
166      * Set Slots
167      * @param mass Assignment Slots
168      * @param S_Resource_ID resource
169      * @param startDate start date
170      * @param endDate end date
171      */

172     public void setAssignmentSlots (MAssignmentSlot[] mass, int S_Resource_ID,
173         Timestamp startDate, Timestamp endDate)
174     {
175         Log.trace(Log.l5_DData, "VSchedulePanel.setAssignmentSlots");
176         m_S_Resource_ID = S_Resource_ID;
177         m_noDays = TimeUtil.getDaysBetween (startDate, endDate);
178         m_startDate = startDate;
179         //
180
m_slots = mass;
181         m_where = new Rectangle[m_slots.length];
182         //
183
// Calculate Assignments
184
for (int i = 0; m_slots != null && i < m_slots.length; i++)
185         {
186             MAssignmentSlot mas = m_slots[i];
187             int dayIndex = TimeUtil.getDaysBetween (m_startDate, mas.getStartTime());
188             if (dayIndex < 0 || dayIndex >= m_noDays)
189                 System.out.println("Assignment " + i + " Invalid DateRange " + mas.getInfo());
190             //
191
int xWidth = m_dayWidth / mas.getXMax();
192             int xStart = dayIndex * m_dayWidth; // start day slot
193
xStart += mas.getXPos() * xWidth; // offset
194
int xEnd = xStart + xWidth;
195
196             int yStart = m_timePanel.getSlotYStart(mas.getYStart());
197             int yEnd = m_timePanel.getSlotYEnd(mas.getYEnd());
198         // System.out.println("Assignment " + i + ", Xpos=" + mas.getXPos() + ", Xmax=" + mas.getXMax()
199
// + ", Ystart=" + mas.getYStart() + ", Yend=" + mas.getYEnd() + " " + mas.getInfo());
200
m_where[i] = new Rectangle(xStart+1, yStart+1, xWidth-1, yEnd-yStart-1);
201         } // calculate assignments
202

203         //
204
setSize();
205         repaint();
206     } // setAssignmentSlots
207

208     /*************************************************************************/
209
210     /**
211      * Paint it
212      * @param g the <code>Graphics</code> object
213      */

214     public void paint (Graphics g)
215     {
216     // Log.trace(Log.l5_DData, "VSchedulePanel.paint", g.getClip());
217
Graphics2D g2D = (Graphics2D)g;
218         Dimension size = getPreferredSize();
219         Rectangle clipBounds = g2D.getClipBounds();
220         int w = size.width;
221         int h = size.height;
222
223         // Paint Background
224
g2D.setPaint(Color.white);
225         g2D.fill3DRect(1, 1, w-2, h-2, true);
226
227         if (m_timePanel == null) // required
228
return;
229         int headerHeight = m_timePanel.getHeaderHeight();
230
231         // horizontal lines -
232
g2D.setStroke(VScheduleTimePanel.getStroke(true));
233         for (int i = 1; i < m_timePanel.getSlotCount(); i++)
234         {
235             g2D.setPaint(Color.gray);
236             int yy = m_timePanel.getSlotYStart(i);
237             g2D.drawLine(1, yy, w-2, yy); // top horiz line
238
}
239
240         // heading and right vertical lines |
241
g2D.setStroke(VScheduleTimePanel.getStroke(false));
242         for (int i = 0; i < m_noDays; i++)
243         {
244             Rectangle where = new Rectangle(i * m_dayWidth, 0, m_dayWidth, headerHeight);
245             if (!where.intersects(clipBounds))
246                 continue;
247             // Header Background
248
CompiereUtils.paint3Deffect(g2D, where, false, true);
249             g2D.setPaint(Color.blue);
250             TextLayout layout = new TextLayout (getHeading(i), g2D.getFont(), g2D.getFontRenderContext());
251             float hh = layout.getAscent() + layout.getDescent();
252             layout.draw (g2D, where.x + (m_dayWidth - layout.getAdvance())/2, // center
253
((where.height - hh)/2) + layout.getAscent()); // center
254
// line
255
g2D.setPaint(Color.black);
256             int xEnd = (i+1) * m_dayWidth;
257             g2D.drawLine(xEnd, 1, xEnd, h-1); // right part
258
}
259
260         // Paint Assignments
261
for (int i = 0; m_slots != null && i < m_slots.length; i++)
262         {
263             if (!m_where[i].intersects(clipBounds))
264                 continue;
265
266             // Background
267
g2D.setColor(m_slots[i].getColor(true));
268             g2D.fill(m_where[i]);
269             // Text
270
String JavaDoc string = m_slots[i].getInfoNameDescription();
271             AttributedString as = new AttributedString (string);
272             as.addAttribute(TextAttribute.FONT, g2D.getFont());
273             as.addAttribute(TextAttribute.FOREGROUND, m_slots[i].getColor(false));
274             // Italics for Description
275
int startIt = string.indexOf('(');
276             int endIt = string.lastIndexOf(')');
277             if (startIt != -1 && endIt != -1)
278                 as.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, startIt, endIt);
279             // Paint multiple lines if required
280
AttributedCharacterIterator aci = as.getIterator();
281             LineBreakMeasurer measurer = new LineBreakMeasurer (aci, g2D.getFontRenderContext());
282             float wrappingWidth = m_where[i].width - (2*MARGIN);
283             float curY = m_where[i].y + MARGIN;
284             TextLayout layout = null;
285             int yEnd = m_where[i].y + m_where[i].height;
286             while (measurer.getPosition() < aci.getEndIndex() && curY < yEnd)
287             {
288                 layout = measurer.nextLayout(wrappingWidth);
289                 curY += layout.getAscent();
290                 if (curY < yEnd)
291                     layout.draw(g2D, m_where[i].x + MARGIN, curY);
292             }
293         } // assignments
294

295         // Paint Borders
296
g2D.setPaint(Color.black);
297         g2D.setStroke(VScheduleTimePanel.getStroke(false));
298         g2D.drawLine(1, 1, 1, h-1); // left
299
g2D.drawLine(1, 1, w-1, 1); // top
300
// heading line
301
g2D.drawLine(1, headerHeight, w-1, headerHeight);
302         // Final lines
303
g2D.setStroke(VScheduleTimePanel.getStroke(false));
304         g2D.drawLine(w-1, 1, w-1, h-1); // right
305
g2D.drawLine(1, h-1, w-1, h-1); // bottom line
306
} // paint
307

308     /**
309      * Return heading for index
310      * @param index index
311      * @return heading
312      */

313     private String JavaDoc getHeading (int index)
314     {
315         GregorianCalendar cal = new GregorianCalendar();
316         if (m_startDate != null)
317             cal.setTime(m_startDate);
318         cal.add(java.util.Calendar.DAY_OF_YEAR, index);
319         //
320
SimpleDateFormat format = (SimpleDateFormat)DateFormat.getDateInstance
321             (DateFormat.FULL, Language.getLanguage().getLocale());
322         return format.format(cal.getTime());
323     } // getHeading
324

325     /**
326      * Mouse Clicked. Start AssignmentDialog
327      * @param e event
328      */

329     public void mouseClicked(MouseEvent e)
330     {
331         if (e.getClickCount() < 2)
332             return;
333
334         Log.trace(Log.l3_Util, "VSchedulePanel.mouseClicked", e.getPoint());
335         Rectangle hitRect = new Rectangle (e.getX()-1, e.getY()-1, 3, 3);
336
337         // Day
338
int dayIndex = e.getX() / m_dayWidth;
339         if (dayIndex >= m_noDays)
340             dayIndex = m_noDays-1;
341     // System.out.println("DayIndex=" + dayIndex + ": " + TimeUtil.addDays(m_startDate, dayIndex));
342

343         // Time
344
int timeIndex = m_timePanel.getTimeSlotIndex(e.getY());
345     // System.out.println("TimeIndex=" + timeIndex + ": " + m_timePanel.getTimeSlot(timeIndex));
346

347         // check if there is an existing assignment
348
for (int i = 0; i < m_slots.length; i++)
349         {
350             if (m_where[i].intersects(hitRect))
351             {
352                 MAssignmentSlot mas = m_slots[i];
353                 System.out.println("Existing=" + mas.getInfo());
354                 if (!mas.isAssignment())
355                     return;
356                 //
357
VAssignmentDialog vad = new VAssignmentDialog (Env.getFrame(this),
358                     m_slots[i].getMAssignment(), false, m_createNew);
359                 m_infoSchedule.mAssignmentCallback(vad.getMAssignment());
360                 return;
361             }
362         }
363         if (m_createNew)
364         {
365             MAssignment ma = new MAssignment(Env.getCtx(), 0);
366             ma.setS_Resource_ID(m_S_Resource_ID);
367             ma.setAssignDateFrom(TimeUtil.getDayTime(TimeUtil.addDays(m_startDate, dayIndex),
368                 m_timePanel.getTimeSlot(timeIndex).getStartTime()));
369             ma.setQty(new BigDecimal(1));
370             VAssignmentDialog vad = new VAssignmentDialog (Env.getFrame(this), ma, false, m_createNew);
371             m_infoSchedule.mAssignmentCallback(vad.getMAssignment());
372             return;
373         }
374     } // mouseClicked
375

376
377     public void mousePressed(MouseEvent e)
378     {
379     }
380     public void mouseReleased(MouseEvent e)
381     {
382     }
383     public void mouseEntered(MouseEvent e)
384     {
385     }
386     public void mouseExited(MouseEvent e)
387     {
388     }
389
390     /**
391      * Dispose
392      */

393     public void dispose()
394     {
395         m_infoSchedule = null;
396         m_timePanel = null;
397         m_where = null;
398         m_slots = null;
399         this.removeAll();
400     } // dispose
401

402 } // VSchedulePanel
403
Popular Tags