1 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 33 public class VScheduleTimePanel extends JComponent 34 { 35 36 public VScheduleTimePanel() 37 { 38 setOpaque(false); 39 setSize(); 40 } 41 42 43 private MAssignmentSlot[] m_timeSlots = null; 44 private String [] m_lines = new String [] {""}; 45 46 47 48 public static int LINE_HEIGHT = 35; 49 50 public static int HEADING = 25; 51 52 private Font m_font = new Font("serif", Font.PLAIN, 12); 53 54 private int m_width = 120; 55 56 60 public void setTimeSlots (MAssignmentSlot[] timeSlots) 61 { 62 Log.trace(Log.l5_DData, "VScheduleTimePanel.setTimeSlots"); 63 m_timeSlots = timeSlots; 64 m_lines = new String [m_timeSlots.length]; 65 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); else 73 { 74 Log.error("No Graphics"); 75 } 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 } 91 94 private void setSize() 95 { 96 int width = m_width + 10; if (width <= 10) 99 width = 120; 101 int height = LINE_HEIGHT; 103 int lines = m_lines.length; 104 if (lines < 2) 105 height *= 10; else 107 height *= lines; 108 height += HEADING; 109 Dimension size = new Dimension(width, height); 111 setPreferredSize(size); 112 setMinimumSize(size); 113 setMaximumSize(size); 114 } 116 120 public int getHeaderHeight() 121 { 122 return HEADING; 123 } 125 129 public int getSlotHeight() 130 { 131 int height = getPreferredSize().height; 132 int part = (height-HEADING) / m_lines.length; 133 return part; 134 } 136 140 public int getSlotCount() 141 { 142 return m_lines.length; 143 } 145 150 public int getSlotYStart (int slot) 151 { 152 int part = getSlotHeight(); 153 int y = HEADING + (slot * part); 154 return y; 155 } 157 162 public int getSlotYEnd (int slot) 163 { 164 int part = getSlotHeight(); 165 int y = HEADING + ((slot+1) * part); 166 return y; 167 } 169 176 public int getTimeSlotIndex (Timestamp time, boolean endTime) 177 { 178 if (m_timeSlots.length <= 1) 180 return 0; 181 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 } 191 196 public MAssignmentSlot getTimeSlot (int index) 197 { 198 if (index < 0 || index > m_timeSlots.length) 199 return null; 200 return m_timeSlots[index]; 201 } 203 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 } 219 220 221 225 public void paint (Graphics g) 226 { 227 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 g2D.setPaint(Color.white); 236 g2D.fill3DRect(1, 1, w-2, h-2, true); 237 238 Rectangle where = new Rectangle(0, 0, w, getHeaderHeight()); 240 CompiereUtils.paint3Deffect(g2D, where, false, true); 241 242 TextLayout layout = null; 244 249 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, yy + layout.getAscent() + layout.getLeading()); } 261 g2D.setPaint(Color.gray); 263 g2D.drawLine(2, yy, w-2, yy); } 265 266 g2D.setPaint(Color.black); 268 g2D.setStroke(getStroke(false)); 269 g2D.drawLine(1, 1, 1, h-1); g2D.drawLine(w-1, 1, w-1, h-1); g2D.drawLine(1, 1, w-1, 1); g2D.drawLine(1, getHeaderHeight(), w-1, getHeaderHeight()); g2D.drawLine(1, h-1, w-1, h-1); } 276 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 } 290 } | Popular Tags |