1 package drno
.swingui
.guiView
.components
;
4 import java
.awt
.Dimension
;
5 import java
.awt
.FontMetrics
;
6 import java
.awt
.Graphics
;
7 import java
.awt
.Graphics2D
;
8 import java
.awt
.Insets
;
10 import java
.awt
.PopupMenu
;
11 import java
.awt
.SystemColor
;
12 import java
.awt
.event
.ActionListener
;
13 import java
.awt
.event
.MouseEvent
;
14 import java
.awt
.event
.MouseListener
;
15 import java
.sql
.Timestamp
;
16 import java
.util
.Calendar
;
17 import java
.util
.Date
;
18 import java
.util
.GregorianCalendar
;
19 import java
.util
.HashMap
;
20 import java
.util
.TimeZone
;
21 import java
.util
.Vector
;
23 import javax
.swing
.JComponent
;
25 @SuppressWarnings("serial")
26 public class JTimePane
extends JComponent
{
39 Slot(int tid
, lDate dat
) {
45 private static final int CELL_HEIGHT
= 40;
46 private static final int CELL_HHEIGHT
= 20;
49 private int dayoffset
;
50 private int endoffset
;
52 private Vector
<ActionListener
> listeners
;
53 private PopupMenu mnuPop
;
55 protected int selectedItem
;
56 HashMap
<Integer
,Slot
> slots
;
59 //setBorder(BorderFactory.createLoweredBevelBorder());
63 listeners
= new Vector
<ActionListener
>();
64 addMouseListener(new MouseListener() {
65 public void mouseClicked(MouseEvent arg0
) {
69 public void mouseEntered(MouseEvent arg0
) {
70 // TODO Auto-generated method stub
74 public void mouseExited(MouseEvent arg0
) {
75 // TODO Auto-generated method stub
79 public void mousePressed(MouseEvent arg0
) {
80 // TODO Auto-generated method stub
81 userClickedMouse(arg0
);
85 public void mouseReleased(MouseEvent arg0
) {
86 // TODO Auto-generated method stub
93 protected int getIdAt(Point pos
) {
95 System
.err
.println("Why the hell is pos null");
98 int val
= (pos
.y
/CELL_HEIGHT
) + dayoffset
;
102 public Dimension
getMinDimension() {
103 return new Dimension(300,100);
107 public Dimension
getPreferredSize() {
108 int height
= CELL_HEIGHT
* (endoffset
- dayoffset
);
109 System
.out
.println("height: " + height
);
110 return new Dimension(300,height
);
113 public Object
getSelectedItem() {
114 System
.out
.println("1");
115 int id
= getIdAt(lastPos
);
116 System
.out
.println("2");
117 return slots
.get(id
);
120 public Timestamp
getSelectedTimestamp() {
121 GregorianCalendar cal
;
122 cal
= new GregorianCalendar();
124 if(curDate
!=null) cal
.setTime(curDate
);
125 int id
= getIdAt(lastPos
);
126 cal
.set(Calendar
.HOUR_OF_DAY
, id
);
127 cal
.set(Calendar
.MINUTE
, 0);
128 cal
.set(Calendar
.SECOND
, 0);
129 cal
.set(Calendar
.MILLISECOND
, 0);
130 return new Timestamp(cal
.getTimeInMillis());
133 public void insertDate(String description
, Object obj
, int from
, int to
) {
134 lDate dt
= new lDate();
135 dt
.Description
= description
;
137 from
= from
< 24 ? from
: 23;
138 from
= from
>= 0 ? from
: 0;
139 to
= to
< 24 ? to
: 23;
140 to
= to
>= 0 ? to
: 0;
141 to
= to
>=from ? to
: from
;
143 for(int i
= from
;i
<=to
;i
++) {
144 slots
.get(new Integer(i
)).date
= dt
;
148 public void paintComponent(Graphics g
) {
149 super.paintComponent(g
);
151 Insets insets
= getInsets();
152 Graphics2D g2
= (Graphics2D
)g
.create();
153 FontMetrics metrics
= g
.getFontMetrics();
155 for(int i
= dayoffset
;i
<endoffset
;i
++) {
156 int bottom
= ((i
)-dayoffset
) * (CELL_HEIGHT
);
157 //int top = ((i+1)-dayoffset) * (metrics.getHeight()*2+4);
160 g2
.setColor(SystemColor
.info
);
162 g2
.setColor(Color
.white
);
164 g2
.fillRect(insets
.left
, bottom
, getWidth()-insets
.right
, CELL_HEIGHT
-1);
166 g2
.setColor(Color
.GRAY
);
167 g2
.drawLine(insets
.left
, bottom
, getWidth()-insets
.right
, bottom
);
168 String text
= String
.format("%02d:00", i
);
171 insets
.left
+metrics
.stringWidth(text
)+5,
172 bottom
+CELL_HHEIGHT
+2,
173 getWidth()-insets
.right
,
174 bottom
+CELL_HHEIGHT
+2);
176 g2
.setColor(Color
.BLACK
);
177 g2
.drawString(text
, insets
.left
, bottom
+CELL_HHEIGHT
-2);
184 Vector
<mDate
> dates
= new Vector
<mDate
>();
185 mDate current
= null;
186 lDate date_old
= null;
187 for(int i
= dayoffset
;i
<endoffset
;i
++) {
188 int top
= ((i
)-dayoffset
) * (CELL_HEIGHT
);
189 int bottom
= ((i
+1)-dayoffset
) * (CELL_HEIGHT
);
191 lDate date
= slots
.get(new Integer(i
)).date
;
192 if(date_old
== null && date
!= null) {
193 current
= new mDate();
196 current
.bottom
= bottom
-3;
198 if(date_old
!= null && date
!= null && date_old
!= date
) {
199 if(current
!= null)dates
.add(current
);
200 current
= new mDate();
203 current
.bottom
= bottom
-3;
205 if(date_old
!= null && date
!= null && date_old
== date
) {
206 current
.bottom
= bottom
-3;
208 if(date_old
!= null && date
== null) {
209 //current.bottom = bottom-3;
215 for (mDate date
: dates
) {
216 int w
= insets
.left
+metrics
.stringWidth("00:00")+10;
217 g2
.setColor(Color
.orange
);
218 g2
.fillRoundRect(insets
.left
+metrics
.stringWidth("00:00")+10, date
.top
, getWidth() - insets
.right
-5-w
, date
.bottom
-date
.top
,
220 g2
.setColor(Color
.BLACK
);
221 g2
.drawRoundRect(insets
.left
+metrics
.stringWidth("00:00")+10, date
.top
, getWidth() - insets
.right
-5-w
, date
.bottom
-date
.top
,
224 g2
.drawString(date
.date
.Description
, w
+10, date
.top
+3+metrics
.getHeight());
229 public void removeAll() {
230 slots
= new HashMap
<Integer
,Slot
>();
232 for(int i
= 0 ;i
< 24;i
++) {
233 slots
.put(i
,new Slot(i
));
237 public void setPopupMenu(PopupMenu mnu
) {
241 public void setDate(Date dt
) {
245 public void setEndOfDay(int offset
) {
250 public void setStartOfDay(int offset
) {
258 private void userClickedMouse(MouseEvent arg0
)
260 Point pos
= arg0
.getPoint();
262 int newItem
= getIdAt(pos
);
264 if(newItem
!= selectedItem
) {
265 selectedItem
= newItem
;
268 if(arg0
.getButton() == MouseEvent
.BUTTON3
) {
269 //mnuPop.show(arg0.getComponent(), pos.x, pos.y);