|
1 package ru.indvdum.mywork.vaadin; |
|
2 |
|
3 import static ru.indvdum.mywork.MyWork.EM; |
|
4 import ru.indvdum.mywork.openjpa.model.Task; |
|
5 |
|
6 import com.vaadin.ui.Alignment; |
|
7 import com.vaadin.ui.Button; |
|
8 import com.vaadin.ui.HorizontalLayout; |
|
9 import com.vaadin.ui.Label; |
|
10 import com.vaadin.ui.RichTextArea; |
|
11 import com.vaadin.ui.TextField; |
|
12 import com.vaadin.ui.VerticalLayout; |
|
13 import com.vaadin.ui.Window; |
|
14 import com.vaadin.ui.Button.ClickEvent; |
|
15 import com.vaadin.ui.Button.ClickListener; |
|
16 |
|
17 public class EditTaskDialog extends Window implements ClickListener { |
|
18 |
|
19 private MyWorkApplication myApp = null; |
|
20 private EditState state = null; |
|
21 private Task task = null; |
|
22 private TextField taskName = null; |
|
23 private RichTextArea taskDescription = null; |
|
24 |
|
25 public EditTaskDialog(MyWorkApplication myApp, EditState state, Task task) throws Exception { |
|
26 super(); |
|
27 this.myApp = myApp; |
|
28 this.state = state; |
|
29 switch(state){ |
|
30 case CREATE: |
|
31 setCaption("Creating new task"); |
|
32 if(task != null) |
|
33 throw new Exception("Illegal argument: task must be null in creating state."); |
|
34 task = new Task(); |
|
35 task.setName(""); |
|
36 task.setDescription(""); |
|
37 this.task = task; |
|
38 break; |
|
39 case EDIT: |
|
40 setCaption("Editing task"); |
|
41 if(task == null) |
|
42 throw new Exception("Illegal argument: task must be not null in editing state."); |
|
43 this.task = task; |
|
44 break; |
|
45 } |
|
46 init(); |
|
47 } |
|
48 |
|
49 private void init(){ |
|
50 setModal(true); |
|
51 setWidth("640px"); |
|
52 setHeight("480px"); |
|
53 VerticalLayout vl = new VerticalLayout(); |
|
54 vl.setSizeFull(); |
|
55 vl.setSpacing(true); |
|
56 vl.setMargin(true); |
|
57 setContent(vl); |
|
58 |
|
59 HorizontalLayout hlName = new HorizontalLayout(); |
|
60 hlName.setWidth("100%"); |
|
61 Label lblName = new Label("Name:"); |
|
62 lblName.setWidth("100%"); |
|
63 hlName.addComponent(lblName); |
|
64 hlName.setExpandRatio(lblName, 1f); |
|
65 taskName = new TextField(); |
|
66 taskName.setWidth("100%"); |
|
67 taskName.setValue(task.getName()); |
|
68 hlName.addComponent(taskName); |
|
69 hlName.setExpandRatio(taskName, 5f); |
|
70 vl.addComponent(hlName); |
|
71 |
|
72 taskDescription = new RichTextArea("Description"); |
|
73 taskDescription.setValue(task.getDescription()); |
|
74 taskDescription.setSizeFull(); |
|
75 vl.addComponent(taskDescription); |
|
76 vl.setExpandRatio(taskDescription, 1f); |
|
77 |
|
78 HorizontalLayout hlOk = new HorizontalLayout(); |
|
79 hlOk.setWidth("100%"); |
|
80 Button okButton = new Button(); |
|
81 switch(state){ |
|
82 case CREATE: |
|
83 okButton.setCaption("Create"); |
|
84 break; |
|
85 case EDIT: |
|
86 okButton.setCaption("Save"); |
|
87 break; |
|
88 } |
|
89 okButton.addListener((ClickListener)this); |
|
90 hlOk.addComponent(okButton); |
|
91 hlOk.setComponentAlignment(okButton, Alignment.MIDDLE_RIGHT); |
|
92 vl.addComponent(hlOk); |
|
93 } |
|
94 |
|
95 @Override |
|
96 public void buttonClick(ClickEvent event) { |
|
97 task.setName(taskName.getValue().toString()); |
|
98 task.setDescription(taskDescription.getValue().toString()); |
|
99 try{ |
|
100 EM.getTransaction().begin(); |
|
101 EM.persist(task); |
|
102 EM.getTransaction().commit(); |
|
103 } finally { |
|
104 if(EM.getTransaction().isActive()) |
|
105 EM.getTransaction().rollback(); |
|
106 } |
|
107 getParent().removeWindow(this); |
|
108 myApp.getTaskTable().requestRepaint(); |
|
109 } |
|
110 |
|
111 } |