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