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