1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/main/java/ru/indvdum/mywork/vaadin/EditWorkDialog.java Fri Aug 19 16:23:26 2011 +0300
1.3 @@ -0,0 +1,229 @@
1.4 +package ru.indvdum.mywork.vaadin;
1.5 +
1.6 +import static ru.indvdum.mywork.MyWork.EM;
1.7 +
1.8 +import java.util.Calendar;
1.9 +import java.util.List;
1.10 +
1.11 +import javax.persistence.Query;
1.12 +
1.13 +import ru.indvdum.mywork.openjpa.model.Day;
1.14 +import ru.indvdum.mywork.openjpa.model.Task;
1.15 +import ru.indvdum.mywork.openjpa.model.Work;
1.16 +
1.17 +import com.vaadin.data.Property.ValueChangeEvent;
1.18 +import com.vaadin.data.Property.ValueChangeListener;
1.19 +import com.vaadin.ui.Alignment;
1.20 +import com.vaadin.ui.Button;
1.21 +import com.vaadin.ui.ComboBox;
1.22 +import com.vaadin.ui.HorizontalLayout;
1.23 +import com.vaadin.ui.Label;
1.24 +import com.vaadin.ui.RichTextArea;
1.25 +import com.vaadin.ui.TextField;
1.26 +import com.vaadin.ui.VerticalLayout;
1.27 +import com.vaadin.ui.Window;
1.28 +import com.vaadin.ui.AbstractSelect.Filtering;
1.29 +import com.vaadin.ui.Button.ClickEvent;
1.30 +import com.vaadin.ui.Button.ClickListener;
1.31 +
1.32 +public class EditWorkDialog extends Window implements ClickListener {
1.33 +
1.34 + private MyWorkApplication myApp = null;
1.35 + private EditState state = null;
1.36 + private Work work = null;
1.37 + private ComboBox workDay = null;
1.38 + private ComboBox workTask = null;
1.39 + private TextField workHours = null;
1.40 + private RichTextArea workResult = null;
1.41 +
1.42 + public EditWorkDialog(MyWorkApplication myApp, EditState state, Work work) throws Exception {
1.43 + super();
1.44 + this.myApp = myApp;
1.45 + this.state = state;
1.46 + switch(state){
1.47 + case CREATE:
1.48 + setCaption("Creating new work");
1.49 + if(work != null)
1.50 + throw new Exception("Illegal argument: work must be null in creating state.");
1.51 + work = new Work();
1.52 + work.setHours(0f);
1.53 + work.setResult("");
1.54 + this.work = work;
1.55 + break;
1.56 + case EDIT:
1.57 + setCaption("Editing work");
1.58 + if(work == null)
1.59 + throw new Exception("Illegal argument: work must be not null in editing state.");
1.60 + this.work = work;
1.61 + break;
1.62 + }
1.63 + init();
1.64 + }
1.65 +
1.66 + private void init(){
1.67 + setModal(true);
1.68 + setWidth("640px");
1.69 + setHeight("480px");
1.70 + VerticalLayout vl = new VerticalLayout();
1.71 + vl.setSizeFull();
1.72 + vl.setSpacing(true);
1.73 + vl.setMargin(true);
1.74 + setContent(vl);
1.75 +
1.76 + HorizontalLayout hlDay = new HorizontalLayout();
1.77 + hlDay.setWidth("100%");
1.78 + Label lblDay = new Label("Day:");
1.79 + lblDay.setWidth("100%");
1.80 + hlDay.addComponent(lblDay);
1.81 + hlDay.setExpandRatio(lblDay, 1f);
1.82 + workDay = new ComboBox();
1.83 + workDay.setWidth("100%");
1.84 + workDay.setImmediate(true);
1.85 + workDay.setFilteringMode(Filtering.FILTERINGMODE_CONTAINS);
1.86 + updateDays("select d from " + Day.class.getName() + " d order by d.day");
1.87 + workDay.addListener(new ValueChangeListener(){
1.88 +
1.89 + @Override
1.90 + public void valueChange(ValueChangeEvent event) {
1.91 + Day day = (Day)event.getProperty().getValue();
1.92 + if(day == null)
1.93 + updateTasks("select t from " + Task.class.getName() + " t order by t.id desc");
1.94 + else
1.95 + 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");
1.96 + }
1.97 +
1.98 + });
1.99 + hlDay.addComponent(workDay);
1.100 + hlDay.setExpandRatio(workDay, 5f);
1.101 + vl.addComponent(hlDay);
1.102 +
1.103 + HorizontalLayout hlTask = new HorizontalLayout();
1.104 + hlTask.setWidth("100%");
1.105 + Label lblStart = new Label("Task:");
1.106 + lblStart.setWidth("100%");
1.107 + hlTask.addComponent(lblStart);
1.108 + hlTask.setExpandRatio(lblStart, 1f);
1.109 + workTask = new ComboBox();
1.110 + workTask.setWidth("100%");
1.111 + workTask.setImmediate(true);
1.112 + workTask.setFilteringMode(Filtering.FILTERINGMODE_CONTAINS);
1.113 + Day day = (Day)workDay.getValue();
1.114 + if(day == null)
1.115 + updateTasks("select t from " + Task.class.getName() + " t order by t.id desc");
1.116 + else
1.117 + 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");
1.118 + workTask.addListener(new ValueChangeListener(){
1.119 +
1.120 + @Override
1.121 + public void valueChange(ValueChangeEvent event) {
1.122 + Task task = (Task)event.getProperty().getValue();
1.123 + if(task == null)
1.124 + updateDays("select d from " + Day.class.getName() + " d order by d.day");
1.125 + else
1.126 + 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");
1.127 + }
1.128 +
1.129 + });
1.130 + hlTask.addComponent(workTask);
1.131 + hlTask.setExpandRatio(workTask, 5f);
1.132 + vl.addComponent(hlTask);
1.133 +
1.134 + HorizontalLayout hlHours = new HorizontalLayout();
1.135 + hlHours.setWidth("100%");
1.136 + Label lblHours = new Label("Hours:");
1.137 + lblHours.setWidth("100%");
1.138 + hlHours.addComponent(lblHours);
1.139 + hlHours.setExpandRatio(lblHours, 1f);
1.140 + workHours = new TextField();
1.141 + workHours.setWidth("100%");
1.142 + workHours.setValue(work.getHours());
1.143 + workHours.setImmediate(true);
1.144 + hlHours.addComponent(workHours);
1.145 + hlHours.setExpandRatio(workHours, 5f);
1.146 + vl.addComponent(hlHours);
1.147 +
1.148 + workResult = new RichTextArea("Result");
1.149 + workResult.setValue(work.getResult());
1.150 + workResult.setSizeFull();
1.151 + vl.addComponent(workResult);
1.152 + vl.setExpandRatio(workResult, 5f);
1.153 +
1.154 + HorizontalLayout hlOk = new HorizontalLayout();
1.155 + hlOk.setWidth("100%");
1.156 + Button okButton = new Button();
1.157 + switch(state){
1.158 + case CREATE:
1.159 + okButton.setCaption("Create");
1.160 + break;
1.161 + case EDIT:
1.162 + okButton.setCaption("Save");
1.163 + break;
1.164 + }
1.165 + okButton.addListener((ClickListener)this);
1.166 + hlOk.addComponent(okButton);
1.167 + hlOk.setComponentAlignment(okButton, Alignment.MIDDLE_RIGHT);
1.168 + vl.addComponent(hlOk);
1.169 + }
1.170 +
1.171 + private void updateDays(String jpql){
1.172 + if(workTask != null && workTask.getValue() != null && workDay != null && workDay.getValue() != null)
1.173 + return;
1.174 + workDay.removeAllItems();
1.175 + Query query = EM.createQuery(jpql);
1.176 + List list = query.getResultList();
1.177 + for(Day day: (List<Day>)list){
1.178 + workDay.addItem(day);
1.179 + Calendar currCal = Calendar.getInstance();
1.180 + currCal.set(Calendar.HOUR_OF_DAY, 0);
1.181 + currCal.set(Calendar.MINUTE, 0);
1.182 + currCal.set(Calendar.SECOND, 0);
1.183 + currCal.set(Calendar.MILLISECOND, 0);
1.184 + if(day.getDay().equals(currCal.getTime()))
1.185 + workDay.select(day);
1.186 + }
1.187 + }
1.188 +
1.189 + private void updateTasks(String jpql){
1.190 + if(workTask != null && workTask.getValue() != null && workDay != null && workDay.getValue() != null)
1.191 + return;
1.192 + workTask.removeAllItems();
1.193 + Query query = EM.createQuery(jpql);
1.194 + List list = query.getResultList();
1.195 + for(Task task: (List<Task>)list)
1.196 + workTask.addItem(task);
1.197 + }
1.198 +
1.199 + @Override
1.200 + public void buttonClick(ClickEvent event) {
1.201 + Object dayObj = workDay.getValue();
1.202 + Object taskObj = workTask.getValue();
1.203 + Object hoursObj = workHours.getValue();
1.204 + String result = workResult.getValue().toString();
1.205 + if (
1.206 + !(dayObj instanceof Day)
1.207 + || !(taskObj instanceof Task)
1.208 + || !(hoursObj.toString().matches("^[0-9]+[\\.]{0,1}[0-9]*"))
1.209 + ) {
1.210 + getWindow().showNotification("Invalid information entered");
1.211 + } else {
1.212 + Day day = (Day)dayObj;
1.213 + Task task = (Task)taskObj;
1.214 + Float hours = Float.parseFloat(hoursObj.toString());
1.215 + work.setDay(day);
1.216 + work.setTask(task);
1.217 + work.setHours(hours);
1.218 + work.setResult(result);
1.219 + try{
1.220 + EM.getTransaction().begin();
1.221 + EM.persist(work);
1.222 + EM.getTransaction().commit();
1.223 + } finally {
1.224 + if(EM.getTransaction().isActive())
1.225 + EM.getTransaction().rollback();
1.226 + }
1.227 + getParent().removeWindow(this);
1.228 + myApp.getWorkTable().requestRepaint();
1.229 + }
1.230 + }
1.231 +
1.232 +}