moved src dirs right
[educational.data.git] / src / Dr.NO / src / drno / swingui / guiView / components / JTimePane.java
blobed3c6693778f6bd9837b719bf84d98c7f5d78578
1 package drno.swingui.guiView.components;
3 import java.awt.Color;
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;
9 import java.awt.Point;
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 {
27 class lDate {
28 String Description;
29 Object Value;
32 class Slot {
33 lDate date;
34 int Time;
35 Slot(int tid) {
36 Time = tid;
37 date = null;
39 Slot(int tid, lDate dat) {
40 Time = tid;
41 date = dat;
45 private static final int CELL_HEIGHT = 40;
46 private static final int CELL_HHEIGHT = 20;
48 private Date curDate;
49 private int dayoffset;
50 private int endoffset;
51 Point lastPos;
52 private Vector<ActionListener> listeners;
53 private PopupMenu mnuPop;
55 protected int selectedItem;
56 HashMap<Integer,Slot> slots;
58 public JTimePane() {
59 //setBorder(BorderFactory.createLoweredBevelBorder());
60 removeAll();
61 dayoffset = 6;
62 endoffset = 20;
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
90 });
93 protected int getIdAt(Point pos) {
94 if(pos == null) {
95 System.err.println("Why the hell is pos null");
96 return -1;
98 int val = (pos.y/CELL_HEIGHT) + dayoffset;
99 return val;
102 public Dimension getMinDimension() {
103 return new Dimension(300,100);
106 @Override
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;
136 dt.Value = obj;
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);
159 if((i % 2) == 0)
160 g2.setColor(SystemColor.info);
161 else
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);
170 g2.drawLine(
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);
179 class mDate {
180 int bottom;
181 lDate date;
182 int top;
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();
194 current.date = date;
195 current.top = top+3;
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();
201 current.date = date;
202 current.top = top+3;
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;
210 dates.add(current);
212 date_old = date;
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,
219 20,20);
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,
222 20,20);
224 g2.drawString(date.date.Description, w+10, date.top+3+metrics.getHeight());
226 g2.dispose();
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) {
238 mnuPop = mnu;
241 public void setDate(Date dt) {
242 curDate = dt;
245 public void setEndOfDay(int offset) {
246 endoffset = offset;
247 repaint();
250 public void setStartOfDay(int offset) {
251 dayoffset = offset;
252 repaint();
256 * @param arg0
258 private void userClickedMouse(MouseEvent arg0)
260 Point pos = arg0.getPoint();
261 lastPos = pos;
262 int newItem = getIdAt(pos);
264 if(newItem != selectedItem) {
265 selectedItem = newItem;
266 repaint();
268 if(arg0.getButton() == MouseEvent.BUTTON3) {
269 //mnuPop.show(arg0.getComponent(), pos.x, pos.y);