|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 /** |
|
19 * |
|
20 */ |
|
21 package com.nokia.carbide.cpp.internal.pi.actions; |
|
22 |
|
23 import java.text.DecimalFormat; |
|
24 |
|
25 import org.eclipse.swt.SWT; |
|
26 import org.eclipse.swt.events.FocusAdapter; |
|
27 import org.eclipse.swt.events.FocusEvent; |
|
28 import org.eclipse.swt.events.SelectionAdapter; |
|
29 import org.eclipse.swt.events.SelectionEvent; |
|
30 import org.eclipse.swt.events.SelectionListener; |
|
31 import org.eclipse.swt.graphics.GC; |
|
32 import org.eclipse.swt.graphics.Point; |
|
33 import org.eclipse.swt.graphics.Rectangle; |
|
34 import org.eclipse.swt.layout.GridData; |
|
35 import org.eclipse.swt.layout.GridLayout; |
|
36 import org.eclipse.swt.widgets.Button; |
|
37 import org.eclipse.swt.widgets.Display; |
|
38 import org.eclipse.swt.widgets.Label; |
|
39 import org.eclipse.swt.widgets.Shell; |
|
40 import org.eclipse.swt.widgets.Text; |
|
41 |
|
42 import com.nokia.carbide.cpp.pi.editors.PIPageEditor; |
|
43 |
|
44 public class EventDialog |
|
45 { |
|
46 private String eventName; |
|
47 private String eventComment; |
|
48 private long eventTime; |
|
49 |
|
50 private String newEventName; |
|
51 private String newEventComment; |
|
52 private long newEventTime; |
|
53 |
|
54 private Shell shell; |
|
55 private Label label; |
|
56 private GridData gridData; |
|
57 DecimalFormat timeFormat = new DecimalFormat(Messages.getString("EventDialog.timeFormat")); //$NON-NLS-1$ |
|
58 |
|
59 private Text nameText; |
|
60 // private Text typeText; |
|
61 private Text commentText; |
|
62 private Text timeText; |
|
63 |
|
64 public EventDialog(Display display, String eventType, String name, String comment, long time) |
|
65 { |
|
66 showDialog(display, eventType, name, comment, time, false); |
|
67 } |
|
68 |
|
69 public EventDialog(Display display, String eventType, String name, String comment, long time, boolean timeChange) |
|
70 { |
|
71 showDialog(display, eventType, name, comment, time, timeChange); |
|
72 } |
|
73 |
|
74 private void showDialog(Display display, String type, String name, String comment, long time, |
|
75 boolean timeChange) |
|
76 { |
|
77 // store original values |
|
78 this.eventName = name; |
|
79 this.eventComment = comment; |
|
80 this.eventTime = time; |
|
81 |
|
82 this.newEventName = name; |
|
83 this.newEventComment = comment; |
|
84 this.newEventTime = time; |
|
85 |
|
86 // create the shell |
|
87 shell = new Shell(display, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM); |
|
88 shell.setText(type + Messages.getString("EventDialog.event1") + eventName + Messages.getString("EventDialog.event2") + eventTime/1000d + Messages.getString("EventDialog.event3")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
|
89 shell.setLayout(new GridLayout(2, true)); |
|
90 |
|
91 // do let them change the name |
|
92 label = new Label(shell, SWT.LEFT); |
|
93 label.setText(Messages.getString("EventDialog.eventName")); //$NON-NLS-1$ |
|
94 label.setFont(PIPageEditor.helvetica_9); |
|
95 |
|
96 nameText = new Text(shell, SWT.BORDER); |
|
97 gridData = new GridData(GridData.FILL_HORIZONTAL); |
|
98 nameText.setLayoutData(gridData); |
|
99 nameText.setFont(PIPageEditor.helvetica_9); |
|
100 nameText.setData(Messages.getString("EventDialog.typeName")); //$NON-NLS-1$ |
|
101 |
|
102 if (eventName == null) |
|
103 nameText.setText(""); //$NON-NLS-1$ |
|
104 else |
|
105 nameText.setText(eventName); |
|
106 nameText.setEditable(true); |
|
107 |
|
108 // add the listener(s) |
|
109 nameText.addSelectionListener(new SelectionAdapter() { |
|
110 public void widgetSelected(SelectionEvent e) { |
|
111 newEventName = nameText.getText(); |
|
112 } |
|
113 }); |
|
114 nameText.addFocusListener(new FocusAdapter() { |
|
115 public void focusLost(FocusEvent e) { |
|
116 newEventName = nameText.getText(); |
|
117 } |
|
118 }); |
|
119 |
|
120 // // do not let them change the event type |
|
121 // label = new Label(shell, SWT.LEFT); |
|
122 // label.setText("Type:"); |
|
123 // label.setFont(PIPageEditor.helvetica_9); |
|
124 // |
|
125 // typeText = new Text(shell, SWT.NONE); |
|
126 // gridData = new GridData(GridData.FILL_HORIZONTAL); |
|
127 // typeText.setLayoutData(gridData); |
|
128 // typeText.setFont(PIPageEditor.helvetica_9); |
|
129 // typeText.setData("Type a type"); |
|
130 // typeText.setText(type); |
|
131 // typeText.setEditable(false); |
|
132 |
|
133 // do let them change the comment |
|
134 label = new Label(shell, SWT.LEFT); |
|
135 label.setText(Messages.getString("EventDialog.comment")); //$NON-NLS-1$ |
|
136 label.setFont(PIPageEditor.helvetica_9); |
|
137 |
|
138 commentText = new Text(shell, SWT.BORDER); |
|
139 gridData = new GridData(GridData.FILL_HORIZONTAL); |
|
140 commentText.setLayoutData(gridData); |
|
141 commentText.setFont(PIPageEditor.helvetica_9); |
|
142 commentText.setData(Messages.getString("EventDialog.typeComment")); //$NON-NLS-1$ |
|
143 |
|
144 if (eventComment == null) |
|
145 commentText.setText(""); //$NON-NLS-1$ |
|
146 else |
|
147 commentText.setText(eventComment); |
|
148 commentText.setEditable(true); |
|
149 |
|
150 // add the listener(s) |
|
151 commentText.addSelectionListener(new SelectionAdapter() { |
|
152 public void widgetSelected(SelectionEvent e) { |
|
153 newEventComment = commentText.getText(); |
|
154 } |
|
155 }); |
|
156 commentText.addFocusListener(new FocusAdapter() { |
|
157 public void focusLost(FocusEvent e) { |
|
158 newEventComment = commentText.getText(); |
|
159 } |
|
160 }); |
|
161 |
|
162 // maybe let them change the time |
|
163 label = new Label(shell, SWT.LEFT); |
|
164 label.setText(Messages.getString("EventDialog.time")); //$NON-NLS-1$ |
|
165 label.setFont(PIPageEditor.helvetica_9); |
|
166 |
|
167 timeText = new Text(shell, SWT.NONE); |
|
168 gridData = new GridData(GridData.FILL_HORIZONTAL); |
|
169 timeText.setLayoutData(gridData); |
|
170 timeText.setFont(PIPageEditor.helvetica_9); |
|
171 timeText.setData(Messages.getString("EventDialog.typeTime")); //$NON-NLS-1$ |
|
172 timeText.setText(timeFormat.format(eventTime/1000d)); |
|
173 timeText.setEditable(false); |
|
174 |
|
175 // add the listener(s) |
|
176 timeText.addSelectionListener(new SelectionAdapter() { |
|
177 public void widgetDefaultSelected(SelectionEvent e) { |
|
178 newEventTime = convert(timeText, newEventTime); |
|
179 } |
|
180 }); |
|
181 timeText.addFocusListener(new FocusAdapter() { |
|
182 public void focusLost(FocusEvent e) { |
|
183 newEventTime = convert(timeText, newEventTime); |
|
184 } |
|
185 }); |
|
186 |
|
187 // create the OK button |
|
188 Button okButton = new Button(shell, SWT.NONE); |
|
189 okButton.setText(Messages.getString("EventDialog.ok")); //$NON-NLS-1$ |
|
190 gridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER); |
|
191 okButton.setLayoutData(gridData); |
|
192 |
|
193 // add the listener(s) |
|
194 okButton.addSelectionListener(new SelectionListener(){ |
|
195 public void widgetSelected(SelectionEvent e) { |
|
196 if ( ((eventName == null) && (newEventName != null)) |
|
197 || ((eventName != null) && !eventName.equals(newEventName)) |
|
198 || ((eventComment == null) && (newEventComment != null)) |
|
199 || ((eventComment != null) && !eventComment.equals(newEventComment)) |
|
200 || eventTime != newEventTime) |
|
201 { |
|
202 // the file has changed |
|
203 PIPageEditor.currentPageEditor().setDirty(); |
|
204 } |
|
205 shell.close(); |
|
206 } |
|
207 |
|
208 public void widgetDefaultSelected(SelectionEvent e) { |
|
209 widgetSelected(e); |
|
210 } |
|
211 }); |
|
212 |
|
213 // create the Cancel button |
|
214 Button cancelButton = new Button(shell, SWT.NONE); |
|
215 cancelButton.setText(Messages.getString("EventDialog.cancel")); //$NON-NLS-1$ |
|
216 gridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER); |
|
217 cancelButton.setLayoutData(gridData); |
|
218 |
|
219 // add the listener(s) |
|
220 cancelButton.addSelectionListener(new SelectionListener(){ |
|
221 public void widgetSelected(SelectionEvent e) { |
|
222 newEventName = eventName; |
|
223 newEventComment = eventComment; |
|
224 newEventTime = eventTime; |
|
225 shell.close(); |
|
226 } |
|
227 |
|
228 public void widgetDefaultSelected(SelectionEvent e) { |
|
229 widgetSelected(e); |
|
230 } |
|
231 }); |
|
232 |
|
233 shell.pack(); |
|
234 shell.open(); |
|
235 |
|
236 GC gc = new GC(shell); |
|
237 Point point = gc.stringExtent(shell.getText()); |
|
238 gc.dispose(); |
|
239 |
|
240 Rectangle bounds = shell.getBounds(); |
|
241 bounds.width = point.x + 100; |
|
242 shell.setBounds(bounds); |
|
243 |
|
244 while (!shell.isDisposed()) { |
|
245 if (!display.readAndDispatch()) { |
|
246 display.sleep(); |
|
247 } |
|
248 } |
|
249 } |
|
250 |
|
251 private long convert(Text text, long currentTime) |
|
252 { |
|
253 double new_double; |
|
254 |
|
255 // convert, catch (NumberFormatException e1) |
|
256 try { |
|
257 new_double = Double.parseDouble(text.getText().replace(',','.')); |
|
258 if (new_double >= 0) |
|
259 currentTime = (long) (new_double * 1000); |
|
260 } catch (NumberFormatException exc) { |
|
261 // just keep the old value |
|
262 } |
|
263 |
|
264 text.setText(timeFormat.format(currentTime/10/100d)); |
|
265 |
|
266 return currentTime; |
|
267 } |
|
268 |
|
269 public String getName() |
|
270 { |
|
271 return this.eventName; |
|
272 } |
|
273 public String getComment() |
|
274 { |
|
275 return this.eventComment; |
|
276 } |
|
277 |
|
278 public long getTime() |
|
279 { |
|
280 return this.eventTime; |
|
281 } |
|
282 |
|
283 public String getNewName() |
|
284 { |
|
285 return this.newEventName; |
|
286 } |
|
287 |
|
288 public String getNewComment() |
|
289 { |
|
290 return this.newEventComment; |
|
291 } |
|
292 |
|
293 public long getNewTime() |
|
294 { |
|
295 return this.newEventTime; |
|
296 } |
|
297 } |
|
298 |