1 package org.xpcards.dialogs;
2
3 import java.util.Date;
4
5 import org.eclipse.swt.SWT;
6 import org.eclipse.swt.custom.StyledText;
7 import org.eclipse.swt.events.SelectionAdapter;
8 import org.eclipse.swt.events.SelectionEvent;
9 import org.eclipse.swt.layout.GridData;
10 import org.eclipse.swt.layout.GridLayout;
11 import org.eclipse.swt.widgets.Button;
12 import org.eclipse.swt.widgets.Composite;
13 import org.eclipse.swt.widgets.Display;
14 import org.eclipse.swt.widgets.Label;
15 import org.eclipse.swt.widgets.Shell;
16 import org.eclipse.swt.widgets.Text;
17 import org.xpcards.model.Task;
18
19 public class TaskDialog {
20
21 public static int OK_RESULT = 1;
22 public static int CANCEL_RESULT = -1;
23
24 Task task;
25 int result = CANCEL_RESULT;
26 Shell shell;
27 StyledText description;
28 Text text;
29 Date starts;
30
31
32 public TaskDialog(Shell parent) {
33 shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
34 shell.setLayout(new GridLayout());
35 }
36
37 private void createControlButtons() {
38 Composite composite = new Composite(shell, SWT.NULL);
39 composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
40 GridLayout layout = new GridLayout();
41 layout.numColumns = 2;
42 composite.setLayout(layout);
43
44 Button okButton = new Button(composite, SWT.PUSH);
45
46 // TODO OK Button I18n
47 okButton.setText("OK");
48 okButton.addSelectionListener(new SelectionAdapter() {
49 public void widgetSelected(SelectionEvent e) {
50 // TODO Commit changes to project
51 result = commit();
52 shell.close();
53 }
54 });
55
56 Button cancelButton = new Button(composite, SWT.PUSH);
57 // TODO Concel I18n
58 cancelButton.setText("Cancel");
59 cancelButton.addSelectionListener(new SelectionAdapter() {
60 public void widgetSelected(SelectionEvent e) {
61 result = rollback();
62 shell.close();
63 }
64 });
65
66 shell.setDefaultButton(okButton);
67 }
68
69 private void createTextWidgets() {
70 Composite composite = new Composite(shell, SWT.NULL);
71 composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
72 GridLayout layout = new GridLayout();
73 layout.numColumns = 2;
74 composite.setLayout(layout);
75
76 Label label = new Label(composite, SWT.RIGHT);
77 label.setText("Name");
78 text = new Text(composite, SWT.BORDER);
79 GridData gridData = new GridData();
80 gridData.widthHint = 300;
81 text.setLayoutData(gridData);
82
83 if (task != null) {
84 text.setText(task.getName());
85 }
86
87 label = new Label(composite, SWT.RIGHT);
88 label.setText("Description");
89 gridData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
90 label.setLayoutData(gridData);
91
92 description =
93 new StyledText(
94 composite,
95 SWT.MULTI | SWT.V_SCROLL | SWT.BORDER | SWT.WRAP);
96
97 gridData = new GridData(GridData.FILL_HORIZONTAL);
98 gridData.heightHint = 70;
99 description.setLayoutData(gridData);
100
101
102
103
104 if (task != null) {
105 description.setText(task.getDescription());
106 }
107
108 }
109
110 public String getTitle() {
111 return shell.getText();
112 }
113
114 public void setTitle(String title) {
115 shell.setText(title);
116 }
117
118
119
120 public int open() {
121 createTextWidgets();
122 createControlButtons();
123 result = CANCEL_RESULT;
124 shell.pack();
125 shell.open();
126 Display display = shell.getDisplay();
127 while (!shell.isDisposed()) {
128 if (!display.readAndDispatch())
129 display.sleep();
130 }
131
132 return result;
133 }
134
135 private int commit() {
136 if (text.getText() != null) {
137 if (task == null) {
138 task = new Task();
139 }
140 task.setName(text.getText());
141
142 if (description.getText() != null) {
143 task.setDescription(description.getText());
144 }
145 return OK_RESULT;
146 }
147 return CANCEL_RESULT;
148 }
149
150 private int rollback() {
151 return CANCEL_RESULT;
152 }
153
154 public static void main(String[] args) {
155 Display display = new Display();
156 Shell shell = new Shell(display);
157
158 TaskDialog d = new TaskDialog(shell);
159
160 int result = d.open();
161
162 System.err.println("Result is :" + result);
163 System.err.println("Task is:" + d.getTask());
164
165 }
166
167 /***
168 * @return
169 */
170 public Task getTask() {
171 return task;
172 }
173
174 /***
175 * @param task
176 */
177 public void setTask(Task task) {
178 this.task = task;
179 }
180
181 }
This page was automatically generated by Maven