1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/main/java/ru/indvdum/mywork/vaadin/EditTaskDialog.java Fri Aug 19 16:23:26 2011 +0300
1.3 @@ -0,0 +1,111 @@
1.4 +package ru.indvdum.mywork.vaadin;
1.5 +
1.6 +import static ru.indvdum.mywork.MyWork.EM;
1.7 +import ru.indvdum.mywork.openjpa.model.Task;
1.8 +
1.9 +import com.vaadin.ui.Alignment;
1.10 +import com.vaadin.ui.Button;
1.11 +import com.vaadin.ui.HorizontalLayout;
1.12 +import com.vaadin.ui.Label;
1.13 +import com.vaadin.ui.RichTextArea;
1.14 +import com.vaadin.ui.TextField;
1.15 +import com.vaadin.ui.VerticalLayout;
1.16 +import com.vaadin.ui.Window;
1.17 +import com.vaadin.ui.Button.ClickEvent;
1.18 +import com.vaadin.ui.Button.ClickListener;
1.19 +
1.20 +public class EditTaskDialog extends Window implements ClickListener {
1.21 +
1.22 + private MyWorkApplication myApp = null;
1.23 + private EditState state = null;
1.24 + private Task task = null;
1.25 + private TextField taskName = null;
1.26 + private RichTextArea taskDescription = null;
1.27 +
1.28 + public EditTaskDialog(MyWorkApplication myApp, EditState state, Task task) throws Exception {
1.29 + super();
1.30 + this.myApp = myApp;
1.31 + this.state = state;
1.32 + switch(state){
1.33 + case CREATE:
1.34 + setCaption("Creating new task");
1.35 + if(task != null)
1.36 + throw new Exception("Illegal argument: task must be null in creating state.");
1.37 + task = new Task();
1.38 + task.setName("");
1.39 + task.setDescription("");
1.40 + this.task = task;
1.41 + break;
1.42 + case EDIT:
1.43 + setCaption("Editing task");
1.44 + if(task == null)
1.45 + throw new Exception("Illegal argument: task must be not null in editing state.");
1.46 + this.task = task;
1.47 + break;
1.48 + }
1.49 + init();
1.50 + }
1.51 +
1.52 + private void init(){
1.53 + setModal(true);
1.54 + setWidth("640px");
1.55 + setHeight("480px");
1.56 + VerticalLayout vl = new VerticalLayout();
1.57 + vl.setSizeFull();
1.58 + vl.setSpacing(true);
1.59 + vl.setMargin(true);
1.60 + setContent(vl);
1.61 +
1.62 + HorizontalLayout hlName = new HorizontalLayout();
1.63 + hlName.setWidth("100%");
1.64 + Label lblName = new Label("Name:");
1.65 + lblName.setWidth("100%");
1.66 + hlName.addComponent(lblName);
1.67 + hlName.setExpandRatio(lblName, 1f);
1.68 + taskName = new TextField();
1.69 + taskName.setWidth("100%");
1.70 + taskName.setValue(task.getName());
1.71 + hlName.addComponent(taskName);
1.72 + hlName.setExpandRatio(taskName, 5f);
1.73 + vl.addComponent(hlName);
1.74 +
1.75 + taskDescription = new RichTextArea("Description");
1.76 + taskDescription.setValue(task.getDescription());
1.77 + taskDescription.setSizeFull();
1.78 + vl.addComponent(taskDescription);
1.79 + vl.setExpandRatio(taskDescription, 1f);
1.80 +
1.81 + HorizontalLayout hlOk = new HorizontalLayout();
1.82 + hlOk.setWidth("100%");
1.83 + Button okButton = new Button();
1.84 + switch(state){
1.85 + case CREATE:
1.86 + okButton.setCaption("Create");
1.87 + break;
1.88 + case EDIT:
1.89 + okButton.setCaption("Save");
1.90 + break;
1.91 + }
1.92 + okButton.addListener((ClickListener)this);
1.93 + hlOk.addComponent(okButton);
1.94 + hlOk.setComponentAlignment(okButton, Alignment.MIDDLE_RIGHT);
1.95 + vl.addComponent(hlOk);
1.96 + }
1.97 +
1.98 + @Override
1.99 + public void buttonClick(ClickEvent event) {
1.100 + task.setName(taskName.getValue().toString());
1.101 + task.setDescription(taskDescription.getValue().toString());
1.102 + try{
1.103 + EM.getTransaction().begin();
1.104 + EM.persist(task);
1.105 + EM.getTransaction().commit();
1.106 + } finally {
1.107 + if(EM.getTransaction().isActive())
1.108 + EM.getTransaction().rollback();
1.109 + }
1.110 + getParent().removeWindow(this);
1.111 + myApp.getTaskTable().requestRepaint();
1.112 + }
1.113 +
1.114 +}