KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.awt.*;
17 import java.awt.font.*;
18 import java.awt.geom.*;
19 import javax.swing.*;
20 import java.sql.*;
21
22 import org.compiere.model.*;
23 import org.compiere.util.*;
24 import org.compiere.plaf.*;
25
26 /**
27  * Schedule Time Panel.
28  * Manages slot layout
29  *
30  * @author Jorg Janke
31  * @version $Id: VScheduleTimePanel.java,v 1.8 2002/08/12 01:55:13 danb Exp $
32  */

33 public class VScheduleTimePanel extends JComponent
34 {
35
36     public VScheduleTimePanel()
37     {
38         setOpaque(false);
39         setSize();
40     }
41
42     /** Time Slots */
43     private MAssignmentSlot[] m_timeSlots = null;
44     private String JavaDoc[] m_lines = new String JavaDoc [] {""};
45
46
47     /** Line Height */
48     public static int LINE_HEIGHT = 35;
49     /** Heading Space */
50     public static int HEADING = 25;
51     /** Used Font */
52     private Font m_font = new Font("serif", Font.PLAIN, 12);
53     /** Line Width */
54     private int m_width = 120;
55
56     /**
57      * Set Time Slots and calculate width
58      * @param timeSlots time slots
59      */

60     public void setTimeSlots (MAssignmentSlot[] timeSlots)
61     {
62         Log.trace(Log.l5_DData, "VScheduleTimePanel.setTimeSlots");
63         m_timeSlots = timeSlots;
64         m_lines = new String JavaDoc[m_timeSlots.length];
65         //
66
FontMetrics fm = null;
67         Graphics g = getGraphics();
68         if (g == null)
69             g = Env.getGraphics(this);
70         if (g != null)
71             fm = g.getFontMetrics(m_font); // the "correct" way
72
else
73         {
74             Log.error("No Graphics");
75         // fm = getToolkit().getFontMetrics(m_font);
76
}
77         m_width = 0;
78         for (int i = 0; i < m_lines.length; i++)
79         {
80             m_lines[i] = m_timeSlots[i].getInfoTimeFrom();
81             int width = 0;
82             if (fm != null)
83                 width = fm.stringWidth(m_lines[i]);
84             if (width > m_width)
85                 m_width = width;
86         }
87         setSize();
88     // repaint();
89
} // setTimeSlots
90

91     /**
92      * Calculate & Set Size
93      */

94     private void setSize()
95     {
96         // Width
97
int width = m_width + 10; // slack
98
if (width <= 10)
99             width = 120; // default size
100

101         // Height
102
int height = LINE_HEIGHT;
103         int lines = m_lines.length;
104         if (lines < 2)
105             height *= 10; // default
106
else
107             height *= lines;
108         height += HEADING;
109         //
110
Dimension size = new Dimension(width, height);
111         setPreferredSize(size);
112         setMinimumSize(size);
113         setMaximumSize(size);
114     } // setSize
115

116     /**
117      * Get the height of the Header
118      * @return height of the header
119      */

120     public int getHeaderHeight()
121     {
122         return HEADING;
123     } // getHeaderHeight
124

125     /**
126      * Get the height of a slot
127      * @return height of a slot
128      */

129     public int getSlotHeight()
130     {
131         int height = getPreferredSize().height;
132         int part = (height-HEADING) / m_lines.length;
133         return part;
134     } // getSlotHeight
135

136     /**
137      * Get the height of a slot
138      * @return height of a slot
139      */

140     public int getSlotCount()
141     {
142         return m_lines.length;
143     } // getSlotCount
144

145     /**
146      * Get Y start position of Slot
147      * @param slot slot index
148      * @return y start position
149      */

150     public int getSlotYStart (int slot)
151     {
152         int part = getSlotHeight();
153         int y = HEADING + (slot * part);
154         return y;
155     } // getSlotYStart
156

157     /**
158      * Get Y end position of Slot
159      * @param slot slot index
160      * @return y end position
161      */

162     public int getSlotYEnd (int slot)
163     {
164         int part = getSlotHeight();
165         int y = HEADING + ((slot+1) * part);
166         return y;
167     } // getSlotYEnd
168

169     /**
170      * Return the Time Slot index for the time.
171      * Based on start time and not including end time
172      * @param time time (day is ignored)
173      * @param endTime if true, the end time is included
174      * @return slot index
175      */

176     public int getTimeSlotIndex (Timestamp time, boolean endTime)
177     {
178         // Just one slot
179
if (m_timeSlots.length <= 1)
180             return 0;
181         // search for it
182
for (int i = 0; i < m_timeSlots.length; i++)
183         {
184             if (m_timeSlots[i].inSlot (time, endTime))
185                 return i;
186         }
187         Log.error("VScheduleTimePanel.getSlotIndex - did not find Slot for " + time + " end=" + endTime);
188         return 0;
189     } // getTimeSlotIndex
190

191     /**
192      * Get Time Slot
193      * @param index time index
194      * @return Assignment Slot
195      */

196     public MAssignmentSlot getTimeSlot (int index)
197     {
198         if (index < 0 || index > m_timeSlots.length)
199             return null;
200         return m_timeSlots[index];
201     } // getTimeSlot
202

203     /**
204      * Get Time Slot Index
205      * @param yPos Y position
206      * @return Assignment Slot Index
207      */

208     public int getTimeSlotIndex (int yPos)
209     {
210         int index = yPos - getHeaderHeight();
211         index /= getSlotHeight();
212         if (index < 0)
213             return 0;
214         if (index >= m_timeSlots.length)
215             return m_timeSlots.length-1;
216         return index;
217     } // getTimeSlotIndex
218

219     /*************************************************************************/
220
221     /**
222      * Paint it
223      * @param g the <code>Graphics</code> object
224      */

225     public void paint (Graphics g)
226     {
227     // Log.trace(Log.l5_DData, "VScheduleTimePanel.paint", g.getClip());
228
Graphics2D g2D = (Graphics2D)g;
229         g2D.setFont(m_font);
230         Dimension size = getPreferredSize();
231         int w = size.width;
232         int h = size.height;
233
234         // Paint Background
235
g2D.setPaint(Color.white);
236         g2D.fill3DRect(1, 1, w-2, h-2, true);
237
238         // Header Background
239
Rectangle where = new Rectangle(0, 0, w, getHeaderHeight());
240         CompiereUtils.paint3Deffect(g2D, where, false, true);
241
242         // heading
243
TextLayout layout = null;
244     // layout = new TextLayout ("Heading", m_font, g2D.getFontRenderContext());
245
// float hh = layout.getAscent() + layout.getDescent();
246
// layout.draw (g2D, (w - layout.getAdvance())/2, // center
247
// ((HEADING - hh)/2) + layout.getAscent()); // center
248

249         // horizontal lines & text
250
g2D.setStroke(getStroke(true));
251         for (int i = 0; i < m_lines.length; i++)
252         {
253             int yy = getSlotYStart(i);
254             if (m_lines[i] != null && m_lines[i].length() > 0)
255             {
256                 layout = new TextLayout (m_lines[i], m_font, g2D.getFontRenderContext());
257                 g2D.setPaint(Color.blue);
258                 layout.draw (g2D, w - layout.getAdvance() - 3, // right aligned with 2 pt space
259
yy + layout.getAscent() + layout.getLeading()); // top aligned with leading space
260
}
261             //
262
g2D.setPaint(Color.gray);
263             g2D.drawLine(2, yy, w-2, yy); // top horiz line
264
}
265
266         // Paint Borders
267
g2D.setPaint(Color.black);
268         g2D.setStroke(getStroke(false));
269         g2D.drawLine(1, 1, 1, h-1); // left
270
g2D.drawLine(w-1, 1, w-1, h-1); // right
271
g2D.drawLine(1, 1, w-1, 1); // top
272
g2D.drawLine(1, getHeaderHeight(), w-1, getHeaderHeight()); // header
273
g2D.drawLine(1, h-1, w-1, h-1); // bottom line
274
} // paintComponent
275

276     /**
277      * Get Stroke
278      * @param slotLine if true return dashed line
279      * @return Stroke
280      */

281     public static Stroke getStroke (boolean slotLine)
282     {
283         if (slotLine)
284             return new BasicStroke (1.0f, BasicStroke.CAP_BUTT,
285                 BasicStroke.JOIN_MITER, 1.0f,
286                 new float[] {2.0f, 0.5f}, 0.0f);
287         return new BasicStroke(1.0f);
288     } // getStroke
289

290 } // VScheduleTimePanel
291
Popular Tags