1 package ru.indvdum.mywork.vaadin; |
|
2 |
|
3 import static ru.indvdum.mywork.MyWork.EM; |
|
4 |
|
5 import java.util.Calendar; |
|
6 import java.util.List; |
|
7 |
|
8 import javax.persistence.Query; |
|
9 |
|
10 import ru.indvdum.mywork.openjpa.model.Day; |
|
11 import ru.indvdum.mywork.openjpa.model.Task; |
|
12 import ru.indvdum.mywork.openjpa.model.Work; |
|
13 |
|
14 import com.vaadin.data.Property.ValueChangeEvent; |
|
15 import com.vaadin.data.Property.ValueChangeListener; |
|
16 import com.vaadin.ui.Alignment; |
|
17 import com.vaadin.ui.Button; |
|
18 import com.vaadin.ui.ComboBox; |
|
19 import com.vaadin.ui.HorizontalLayout; |
|
20 import com.vaadin.ui.Label; |
|
21 import com.vaadin.ui.RichTextArea; |
|
22 import com.vaadin.ui.TextField; |
|
23 import com.vaadin.ui.VerticalLayout; |
|
24 import com.vaadin.ui.Window; |
|
25 import com.vaadin.ui.AbstractSelect.Filtering; |
|
26 import com.vaadin.ui.Button.ClickEvent; |
|
27 import com.vaadin.ui.Button.ClickListener; |
|
28 |
|
29 /** |
|
30 * @author indvdum |
|
31 * 23.08.2011 19:11:56 |
|
32 * |
|
33 */ |
|
34 public class EditWorkDialog extends Window implements ClickListener { |
|
35 |
|
36 private MyWorkApplication myApp = null; |
|
37 private EditState state = null; |
|
38 private Work work = null; |
|
39 private ComboBox workDay = null; |
|
40 private ComboBox workTask = null; |
|
41 private TextField workHours = null; |
|
42 private RichTextArea workResult = null; |
|
43 |
|
44 public EditWorkDialog(MyWorkApplication myApp, Work work) throws Exception { |
|
45 super(); |
|
46 this.myApp = myApp; |
|
47 if(work == null) |
|
48 state = EditState.CREATE; |
|
49 else |
|
50 state = EditState.EDIT; |
|
51 switch(state){ |
|
52 case CREATE: |
|
53 setCaption("Creating new work"); |
|
54 work = new Work(); |
|
55 work.setHours(0f); |
|
56 work.setResult(""); |
|
57 this.work = work; |
|
58 break; |
|
59 case EDIT: |
|
60 setCaption("Editing work"); |
|
61 this.work = work; |
|
62 break; |
|
63 } |
|
64 init(); |
|
65 } |
|
66 |
|
67 private void init(){ |
|
68 setModal(true); |
|
69 setWidth("640px"); |
|
70 setHeight("480px"); |
|
71 VerticalLayout vl = new VerticalLayout(); |
|
72 vl.setSizeFull(); |
|
73 vl.setSpacing(true); |
|
74 vl.setMargin(true); |
|
75 setContent(vl); |
|
76 |
|
77 HorizontalLayout hlDay = new HorizontalLayout(); |
|
78 hlDay.setWidth("100%"); |
|
79 Label lblDay = new Label("Day:"); |
|
80 lblDay.setWidth("100%"); |
|
81 hlDay.addComponent(lblDay); |
|
82 hlDay.setExpandRatio(lblDay, 1f); |
|
83 workDay = new ComboBox(); |
|
84 workDay.setWidth("100%"); |
|
85 workDay.setImmediate(true); |
|
86 workDay.setFilteringMode(Filtering.FILTERINGMODE_CONTAINS); |
|
87 updateDays("select d from " + Day.class.getName() + " d order by d.day"); |
|
88 workDay.addListener(new ValueChangeListener(){ |
|
89 |
|
90 @Override |
|
91 public void valueChange(ValueChangeEvent event) { |
|
92 Day day = (Day)event.getProperty().getValue(); |
|
93 if(day == null) |
|
94 updateTasks("select t from " + Task.class.getName() + " t order by t.id desc"); |
|
95 else |
|
96 updateTasks("select t from " + Task.class.getName() + " t where t.id not in (select w.task.id from " + Work.class.getName() + " w where w.day.id = " + day.getId() + ") order by t.id desc"); |
|
97 } |
|
98 |
|
99 }); |
|
100 hlDay.addComponent(workDay); |
|
101 hlDay.setExpandRatio(workDay, 5f); |
|
102 vl.addComponent(hlDay); |
|
103 |
|
104 HorizontalLayout hlTask = new HorizontalLayout(); |
|
105 hlTask.setWidth("100%"); |
|
106 Label lblStart = new Label("Task:"); |
|
107 lblStart.setWidth("100%"); |
|
108 hlTask.addComponent(lblStart); |
|
109 hlTask.setExpandRatio(lblStart, 1f); |
|
110 workTask = new ComboBox(); |
|
111 workTask.setWidth("100%"); |
|
112 workTask.setImmediate(true); |
|
113 workTask.setFilteringMode(Filtering.FILTERINGMODE_CONTAINS); |
|
114 Day day = (Day)workDay.getValue(); |
|
115 if(day == null) |
|
116 updateTasks("select t from " + Task.class.getName() + " t order by t.id desc"); |
|
117 else |
|
118 updateTasks("select t from " + Task.class.getName() + " t where t.id not in (select w.task.id from " + Work.class.getName() + " w where w.day.id = " + day.getId() + ") order by t.id desc"); |
|
119 workTask.addListener(new ValueChangeListener(){ |
|
120 |
|
121 @Override |
|
122 public void valueChange(ValueChangeEvent event) { |
|
123 Task task = (Task)event.getProperty().getValue(); |
|
124 if(task == null) |
|
125 updateDays("select d from " + Day.class.getName() + " d order by d.day"); |
|
126 else |
|
127 updateDays("select d from " + Day.class.getName() + " d where d.id not in (select w.day.id from " + Work.class.getName() + " w where w.task.id = " + task.getId() + ") order by d.day"); |
|
128 } |
|
129 |
|
130 }); |
|
131 hlTask.addComponent(workTask); |
|
132 hlTask.setExpandRatio(workTask, 5f); |
|
133 vl.addComponent(hlTask); |
|
134 |
|
135 HorizontalLayout hlHours = new HorizontalLayout(); |
|
136 hlHours.setWidth("100%"); |
|
137 Label lblHours = new Label("Hours:"); |
|
138 lblHours.setWidth("100%"); |
|
139 hlHours.addComponent(lblHours); |
|
140 hlHours.setExpandRatio(lblHours, 1f); |
|
141 workHours = new TextField(); |
|
142 workHours.setWidth("100%"); |
|
143 workHours.setValue(work.getHours()); |
|
144 workHours.setImmediate(true); |
|
145 hlHours.addComponent(workHours); |
|
146 hlHours.setExpandRatio(workHours, 5f); |
|
147 vl.addComponent(hlHours); |
|
148 |
|
149 workResult = new RichTextArea("Result"); |
|
150 workResult.setValue(work.getResult()); |
|
151 workResult.setSizeFull(); |
|
152 vl.addComponent(workResult); |
|
153 vl.setExpandRatio(workResult, 5f); |
|
154 |
|
155 HorizontalLayout hlOk = new HorizontalLayout(); |
|
156 hlOk.setWidth("100%"); |
|
157 Button okButton = new Button(); |
|
158 switch(state){ |
|
159 case CREATE: |
|
160 okButton.setCaption("Create"); |
|
161 break; |
|
162 case EDIT: |
|
163 okButton.setCaption("Save"); |
|
164 break; |
|
165 } |
|
166 okButton.addListener((ClickListener)this); |
|
167 hlOk.addComponent(okButton); |
|
168 hlOk.setComponentAlignment(okButton, Alignment.MIDDLE_RIGHT); |
|
169 vl.addComponent(hlOk); |
|
170 } |
|
171 |
|
172 private void updateDays(String jpql){ |
|
173 if(workTask != null && workTask.getValue() != null && workDay != null && workDay.getValue() != null) |
|
174 return; |
|
175 workDay.removeAllItems(); |
|
176 Query query = EM.createQuery(jpql); |
|
177 List list = query.getResultList(); |
|
178 for(Day day: (List<Day>)list){ |
|
179 workDay.addItem(day); |
|
180 Calendar currCal = Calendar.getInstance(); |
|
181 currCal.set(Calendar.HOUR_OF_DAY, 0); |
|
182 currCal.set(Calendar.MINUTE, 0); |
|
183 currCal.set(Calendar.SECOND, 0); |
|
184 currCal.set(Calendar.MILLISECOND, 0); |
|
185 if(day.getDay().equals(currCal.getTime())) |
|
186 workDay.select(day); |
|
187 } |
|
188 } |
|
189 |
|
190 private void updateTasks(String jpql){ |
|
191 if(workTask != null && workTask.getValue() != null && workDay != null && workDay.getValue() != null) |
|
192 return; |
|
193 workTask.removeAllItems(); |
|
194 Query query = EM.createQuery(jpql); |
|
195 List list = query.getResultList(); |
|
196 for(Task task: (List<Task>)list) |
|
197 workTask.addItem(task); |
|
198 } |
|
199 |
|
200 @Override |
|
201 public void buttonClick(ClickEvent event) { |
|
202 Object dayObj = workDay.getValue(); |
|
203 Object taskObj = workTask.getValue(); |
|
204 Object hoursObj = workHours.getValue(); |
|
205 String result = workResult.getValue().toString(); |
|
206 if ( |
|
207 !(dayObj instanceof Day) |
|
208 || !(taskObj instanceof Task) |
|
209 || !(hoursObj.toString().matches("^[0-9]+[\\.]{0,1}[0-9]*")) |
|
210 ) { |
|
211 getWindow().showNotification("Invalid information entered"); |
|
212 } else { |
|
213 Day day = (Day)dayObj; |
|
214 Task task = (Task)taskObj; |
|
215 Float hours = Float.parseFloat(hoursObj.toString()); |
|
216 work.setDay(day); |
|
217 work.setTask(task); |
|
218 work.setHours(hours); |
|
219 work.setResult(result); |
|
220 try{ |
|
221 EM.getTransaction().begin(); |
|
222 EM.persist(work); |
|
223 EM.getTransaction().commit(); |
|
224 EM.clear(); |
|
225 } finally { |
|
226 if(EM.getTransaction().isActive()) |
|
227 EM.getTransaction().rollback(); |
|
228 } |
|
229 getParent().removeWindow(this); |
|
230 myApp.getWorkTable().requestRepaint(); |
|
231 } |
|
232 } |
|
233 |
|
234 } |
|