|
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 import java.util.Enumeration; |
|
25 |
|
26 import org.eclipse.swt.SWT; |
|
27 import org.eclipse.swt.events.FocusAdapter; |
|
28 import org.eclipse.swt.events.FocusEvent; |
|
29 import org.eclipse.swt.events.ModifyEvent; |
|
30 import org.eclipse.swt.events.ModifyListener; |
|
31 import org.eclipse.swt.events.SelectionAdapter; |
|
32 import org.eclipse.swt.events.SelectionEvent; |
|
33 import org.eclipse.swt.events.SelectionListener; |
|
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.Event; |
|
39 import org.eclipse.swt.widgets.Label; |
|
40 import org.eclipse.swt.widgets.Shell; |
|
41 import org.eclipse.swt.widgets.Text; |
|
42 |
|
43 import com.nokia.carbide.cpp.internal.pi.analyser.PIChangeEvent; |
|
44 import com.nokia.carbide.cpp.internal.pi.manager.PluginInitialiser; |
|
45 import com.nokia.carbide.cpp.internal.pi.plugin.model.IEventListener; |
|
46 import com.nokia.carbide.cpp.pi.editors.PIPageEditor; |
|
47 |
|
48 |
|
49 public class TimeSetDialog { |
|
50 |
|
51 private Shell shell; |
|
52 private Label label; |
|
53 private Text startTimeText; |
|
54 private Text endTimeText; |
|
55 private Label labelMsg; |
|
56 private Button ok; |
|
57 private Button cancel; |
|
58 private GridData gridData; |
|
59 private double startTime; |
|
60 private double endTime; |
|
61 private DecimalFormat timeFormat = new DecimalFormat(Messages.getString("TimeSetDialog.decimalFormat")); //$NON-NLS-1$ |
|
62 |
|
63 public TimeSetDialog(Display display, double start, double end) |
|
64 { |
|
65 this.startTime = start; |
|
66 this.endTime = end; |
|
67 |
|
68 shell = new Shell(display, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM); |
|
69 shell.setText(Messages.getString("TimeSetDialog.selectInterval")); //$NON-NLS-1$ |
|
70 shell.setLayout(new GridLayout(2, true)); |
|
71 |
|
72 // create the start time label and input box |
|
73 label = new Label(shell, SWT.LEFT); |
|
74 label.setText(Messages.getString("TimeSetDialog.startTime")); //$NON-NLS-1$ |
|
75 label.setFont(PIPageEditor.helvetica_9); |
|
76 |
|
77 startTimeText = new Text(shell, SWT.BORDER); |
|
78 gridData = new GridData(GridData.FILL_HORIZONTAL); |
|
79 startTimeText.setLayoutData(gridData); |
|
80 startTimeText.setFont(PIPageEditor.helvetica_9); |
|
81 startTimeText.setData(Messages.getString("TimeSetDialog.startMustBeFloat")); //$NON-NLS-1$ |
|
82 startTimeText.setText(timeFormat.format(startTime)); |
|
83 |
|
84 // add the listeners |
|
85 startTimeText.addFocusListener(new FocusAdapter() { |
|
86 public void focusLost(FocusEvent e) { |
|
87 startTime = convert(startTimeText, startTime); |
|
88 startTimeText.setText(timeFormat.format(startTime)); |
|
89 } |
|
90 }); |
|
91 startTimeText.addSelectionListener(new SelectionAdapter() { |
|
92 public void widgetDefaultSelected(SelectionEvent e) { |
|
93 startTime = convert(startTimeText, startTime); |
|
94 startTimeText.setText(timeFormat.format(startTime)); |
|
95 } |
|
96 }); |
|
97 startTimeText.addModifyListener(new ModifyListener() { |
|
98 public void modifyText(ModifyEvent arg0) { |
|
99 startTime = convert(startTimeText, startTime); |
|
100 checkOk(); |
|
101 } |
|
102 }); |
|
103 |
|
104 // create the start time label and input box |
|
105 label = new Label(shell, SWT.LEFT); |
|
106 label.setText(Messages.getString("TimeSetDialog.endTime")); //$NON-NLS-1$ |
|
107 label.setFont(PIPageEditor.helvetica_9); |
|
108 |
|
109 endTimeText = new Text(shell, SWT.BORDER); |
|
110 gridData = new GridData(GridData.FILL_HORIZONTAL); |
|
111 endTimeText.setLayoutData(gridData); |
|
112 endTimeText.setData(Messages.getString("TimeSetDialog.endMustBeFloat")); //$NON-NLS-1$ |
|
113 endTimeText.setFont(PIPageEditor.helvetica_9); |
|
114 endTimeText.setText(timeFormat.format(endTime)); |
|
115 |
|
116 // add the listeners |
|
117 endTimeText.addFocusListener(new FocusAdapter() { |
|
118 public void focusLost(FocusEvent e) { |
|
119 endTime = convert(endTimeText, endTime); |
|
120 endTimeText.setText(timeFormat.format(endTime)); |
|
121 } |
|
122 }); |
|
123 endTimeText.addSelectionListener(new SelectionAdapter() { |
|
124 public void widgetDefaultSelected(SelectionEvent e) { |
|
125 endTime = convert(endTimeText, endTime); |
|
126 endTimeText.setText(timeFormat.format(endTime)); |
|
127 } |
|
128 }); |
|
129 endTimeText.addModifyListener(new ModifyListener() { |
|
130 public void modifyText(ModifyEvent arg0) { |
|
131 endTime = convert(endTimeText, endTime); |
|
132 checkOk(); |
|
133 } |
|
134 }); |
|
135 |
|
136 label = new Label(shell, SWT.LEFT); |
|
137 gridData = new GridData(GridData.FILL_BOTH); |
|
138 gridData.horizontalSpan = 2; |
|
139 gridData.verticalAlignment = GridData.CENTER; |
|
140 label.setLayoutData(gridData); |
|
141 |
|
142 double maxEndTime = PIPageEditor.currentPageEditor().getMaxEndTime(); |
|
143 labelMsg = new Label(shell, SWT.LEFT); |
|
144 gridData = new GridData(GridData.FILL_BOTH); |
|
145 gridData.horizontalSpan = 2; |
|
146 gridData.verticalAlignment = GridData.CENTER; |
|
147 labelMsg.setLayoutData(gridData); |
|
148 labelMsg.setFont(PIPageEditor.helvetica_9); |
|
149 labelMsg.setText(Messages.getString("TimeSetDialog.maxEndTime1") + maxEndTime + Messages.getString("TimeSetDialog.maxEndTime2")); //$NON-NLS-1$ //$NON-NLS-2$ |
|
150 |
|
151 // create the OK button |
|
152 ok = new Button(shell, SWT.NONE); |
|
153 ok.setText(Messages.getString("TimeSetDialog.ok")); //$NON-NLS-1$ |
|
154 gridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER); |
|
155 ok.setLayoutData(gridData); |
|
156 ok.addSelectionListener(new SelectionListener(){ |
|
157 |
|
158 public void widgetSelected(SelectionEvent e) { |
|
159 PIPageEditor.currentPageEditor().setLocalTime(startTime, endTime); |
|
160 PIChangeEvent.action("changeSelection"); //$NON-NLS-1$ |
|
161 |
|
162 // after the graphs have been updated, notify plugins that might have tables but no graphs |
|
163 Enumeration enu = PluginInitialiser.getPluginInstances( |
|
164 "com.nokia.carbide.cpp.internal.pi.plugin.model.IEventListener"); //$NON-NLS-1$ |
|
165 if (enu != null) { |
|
166 Event event = new Event(); |
|
167 event.start = (int) (startTime * 1000); |
|
168 event.end = (int) (endTime * 1000); |
|
169 |
|
170 while (enu.hasMoreElements()) |
|
171 { |
|
172 IEventListener plugin = (IEventListener)enu.nextElement(); |
|
173 plugin.receiveEvent("changeSelection", event); //$NON-NLS-1$ |
|
174 } |
|
175 } |
|
176 |
|
177 shell.close(); |
|
178 } |
|
179 |
|
180 public void widgetDefaultSelected(SelectionEvent e) { |
|
181 widgetSelected(e); |
|
182 } |
|
183 }); |
|
184 |
|
185 // create the Cancel button |
|
186 cancel = new Button(shell, SWT.NONE); |
|
187 cancel.setText(Messages.getString("TimeSetDialog.cancel")); //$NON-NLS-1$ |
|
188 gridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER); |
|
189 cancel.setLayoutData(gridData); |
|
190 cancel.addSelectionListener(new SelectionListener(){ |
|
191 |
|
192 public void widgetSelected(SelectionEvent e) { |
|
193 shell.close(); |
|
194 } |
|
195 |
|
196 public void widgetDefaultSelected(SelectionEvent e) { |
|
197 widgetSelected(e); |
|
198 } |
|
199 }); |
|
200 |
|
201 shell.pack(); |
|
202 shell.open(); |
|
203 |
|
204 while (!shell.isDisposed()) { |
|
205 if (!shell.getDisplay().readAndDispatch()) { |
|
206 shell.getDisplay().sleep(); |
|
207 } |
|
208 } |
|
209 } |
|
210 |
|
211 private void checkOk() { |
|
212 double maxEndTime = PIPageEditor.currentPageEditor().getMaxEndTime(); |
|
213 boolean isOk = false; |
|
214 |
|
215 if (startTime > endTime) { |
|
216 labelMsg.setText(Messages.getString("TimeSetDialog.endTimeMustBeGreater")); //$NON-NLS-1$ |
|
217 labelMsg.setForeground(shell.getDisplay().getSystemColor(SWT.COLOR_RED)); |
|
218 } else if (!isValidPositive(startTimeText) || !isValidPositive(endTimeText)) { |
|
219 labelMsg.setText(Messages.getString("TimeSetDialog.timesMustBePositiveValues")); //$NON-NLS-1$ |
|
220 labelMsg.setForeground(shell.getDisplay().getSystemColor(SWT.COLOR_RED)); |
|
221 } else if (startTime > maxEndTime || endTime > maxEndTime) { |
|
222 labelMsg.setText(Messages.getString("TimeSetDialog.maxEndTime1") + maxEndTime + Messages.getString("TimeSetDialog.maxEndTime2")); //$NON-NLS-1$ //$NON-NLS-2$ |
|
223 labelMsg.setForeground(shell.getDisplay().getSystemColor(SWT.COLOR_RED)); |
|
224 } else { |
|
225 isOk = true; |
|
226 labelMsg.setText(Messages.getString("TimeSetDialog.maxEndTime1") + maxEndTime + Messages.getString("TimeSetDialog.maxEndTime2")); //$NON-NLS-1$ //$NON-NLS-2$ |
|
227 labelMsg.setForeground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK)); |
|
228 } |
|
229 ok.setEnabled(isOk); |
|
230 } |
|
231 |
|
232 private double convert(Text text, double currentTime) |
|
233 { |
|
234 double new_double; |
|
235 |
|
236 // convert, catch (NumberFormatException e1) |
|
237 try { |
|
238 new_double = Double.parseDouble(text.getText().replace(',','.')); |
|
239 if (new_double == -0.0) |
|
240 currentTime = 0.0f; |
|
241 else if (new_double >= 0) |
|
242 currentTime = new_double; |
|
243 } catch (NumberFormatException exc) { |
|
244 // just keep the old value |
|
245 } |
|
246 |
|
247 return currentTime; |
|
248 } |
|
249 |
|
250 private boolean isValidPositive(Text text) { |
|
251 try { |
|
252 return Double.parseDouble(text.getText().replace(',','.')) >= 0; |
|
253 } catch (NumberFormatException exc) { |
|
254 return false; |
|
255 } |
|
256 } |
|
257 |
|
258 public void dispose() |
|
259 { |
|
260 if (this.shell != null) { |
|
261 if (!this.shell.isDisposed()) { |
|
262 this.shell.close(); |
|
263 } |
|
264 this.shell.dispose(); |
|
265 } |
|
266 |
|
267 this.shell = null; |
|
268 } |
|
269 } |