|
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 /** |
|
18 * @author indvdum |
|
19 * 23.08.2011 19:11:50 |
|
20 * |
|
21 */ |
|
22 public class EditTaskDialog extends Window implements ClickListener { |
|
23 |
|
24 private MyWorkApplication myApp = null; |
|
25 private EditState state = null; |
|
26 private Task task = null; |
|
27 private TextField taskName = null; |
|
28 private RichTextArea taskDescription = null; |
|
29 |
|
30 public EditTaskDialog(MyWorkApplication myApp, Task task) throws Exception { |
|
31 super(); |
|
32 this.myApp = myApp; |
|
33 if(task == null) |
|
34 state = EditState.CREATE; |
|
35 else |
|
36 state = EditState.EDIT; |
|
37 switch(state){ |
|
38 case CREATE: |
|
39 setCaption("Creating new task"); |
|
40 task = new Task(); |
|
41 task.setName(""); |
|
42 task.setDescription(""); |
|
43 this.task = task; |
|
44 break; |
|
45 case EDIT: |
|
46 setCaption("Editing task"); |
|
47 this.task = task; |
|
48 break; |
|
49 } |
|
50 init(); |
|
51 } |
|
52 |
|
53 private void init(){ |
|
54 setModal(true); |
|
55 setWidth("640px"); |
|
56 setHeight("480px"); |
|
57 VerticalLayout vl = new VerticalLayout(); |
|
58 vl.setSizeFull(); |
|
59 vl.setSpacing(true); |
|
60 vl.setMargin(true); |
|
61 setContent(vl); |
|
62 |
|
63 HorizontalLayout hlName = new HorizontalLayout(); |
|
64 hlName.setWidth("100%"); |
|
65 Label lblName = new Label("Name:"); |
|
66 lblName.setWidth("100%"); |
|
67 hlName.addComponent(lblName); |
|
68 hlName.setExpandRatio(lblName, 1f); |
|
69 taskName = new TextField(); |
|
70 taskName.setWidth("100%"); |
|
71 taskName.setValue(task.getName()); |
|
72 hlName.addComponent(taskName); |
|
73 hlName.setExpandRatio(taskName, 5f); |
|
74 vl.addComponent(hlName); |
|
75 |
|
76 taskDescription = new RichTextArea("Description"); |
|
77 taskDescription.setValue(task.getDescription()); |
|
78 taskDescription.setSizeFull(); |
|
79 vl.addComponent(taskDescription); |
|
80 vl.setExpandRatio(taskDescription, 1f); |
|
81 |
|
82 HorizontalLayout hlOk = new HorizontalLayout(); |
|
83 hlOk.setWidth("100%"); |
|
84 Button okButton = new Button(); |
|
85 switch(state){ |
|
86 case CREATE: |
|
87 okButton.setCaption("Create"); |
|
88 break; |
|
89 case EDIT: |
|
90 okButton.setCaption("Save"); |
|
91 break; |
|
92 } |
|
93 okButton.addListener((ClickListener)this); |
|
94 hlOk.addComponent(okButton); |
|
95 hlOk.setComponentAlignment(okButton, Alignment.MIDDLE_RIGHT); |
|
96 vl.addComponent(hlOk); |
|
97 } |
|
98 |
|
99 @Override |
|
100 public void buttonClick(ClickEvent event) { |
|
101 task.setName(taskName.getValue().toString()); |
|
102 task.setDescription(taskDescription.getValue().toString()); |
|
103 try{ |
|
104 EM.getTransaction().begin(); |
|
105 EM.persist(task); |
|
106 EM.getTransaction().commit(); |
|
107 EM.clear(); |
|
108 } finally { |
|
109 if(EM.getTransaction().isActive()) |
|
110 EM.getTransaction().rollback(); |
|
111 } |
|
112 getParent().removeWindow(this); |
|
113 myApp.getTaskTable().requestRepaint(); |
|
114 } |
|
115 |
|
116 } |