Commit | Line | Data |
---|---|---|
fbd124dd BH |
1 | /*******************************************************************************\r |
2 | * Copyright (c) 2010, 2011 Ericsson\r | |
3 | * \r | |
4 | * All rights reserved. This program and the accompanying materials are\r | |
5 | * made available under the terms of the Eclipse Public License v1.0 which\r | |
6 | * accompanies this distribution, and is available at\r | |
7 | * http://www.eclipse.org/legal/epl-v10.html\r | |
8 | * \r | |
9 | * Contributors:\r | |
10 | * Philippe Sawicki (INF4990.A2010@gmail.com) - Initial API and implementation\r | |
11 | * Mathieu Denis (mathieu.denis55@gmail.com) - Refactored code\r | |
12 | * Bernd Hufmann - Adapted to new messages file, fixed warnings\r | |
13 | *******************************************************************************/\r | |
638eac44 | 14 | package org.eclipse.linuxtools.internal.lttng.ui.views.latency.dialogs;\r |
fbd124dd BH |
15 | \r |
16 | import java.util.Vector;\r | |
17 | \r | |
18 | import org.eclipse.jface.dialogs.IDialogSettings;\r | |
19 | import org.eclipse.jface.dialogs.IMessageProvider;\r | |
20 | import org.eclipse.jface.dialogs.TitleAreaDialog;\r | |
5945cec9 FC |
21 | import org.eclipse.linuxtools.internal.lttng.core.latency.analyzer.EventMatcher;\r |
22 | import org.eclipse.linuxtools.internal.lttng.core.util.EventsPair;\r | |
0977b009 | 23 | import org.eclipse.linuxtools.internal.lttng.ui.Activator;\r |
638eac44 | 24 | import org.eclipse.linuxtools.internal.lttng.ui.views.latency.model.LatencyController;\r |
fbd124dd BH |
25 | import org.eclipse.swt.widgets.Composite;\r |
26 | import org.eclipse.swt.widgets.Control;\r | |
27 | import org.eclipse.swt.widgets.Display;\r | |
28 | import org.eclipse.swt.widgets.Shell;\r | |
29 | \r | |
30 | /**\r | |
31 | * <b><u>AbstractDialog</u></b>\r | |
32 | * <p> \r | |
33 | * Includes the main functions shared by all the different dialogs.\r | |
34 | * \r | |
35 | * @author Philippe Sawicki\r | |
36 | */\r | |
37 | public abstract class AbstractDialog extends TitleAreaDialog {\r | |
38 | \r | |
39 | // ------------------------------------------------------------------------\r | |
40 | // Attributes\r | |
41 | // ------------------------------------------------------------------------\r | |
42 | \r | |
43 | /**\r | |
44 | * The dialog window title.\r | |
45 | */\r | |
46 | protected String fDialogTitle;\r | |
47 | /**\r | |
48 | * The dialog window message.\r | |
49 | */\r | |
50 | protected String fDialogMessage;\r | |
51 | \r | |
52 | /**\r | |
53 | * The code returned by the dialog when the user closes the "Add" dialog.\r | |
54 | */\r | |
55 | public static final int ADD = 53445;\r | |
56 | /**\r | |
57 | * The code returned by the dialog when the user closes the "Delete" dialog.\r | |
58 | */\r | |
59 | public static final int DELETE = ADD + 1;\r | |
60 | /**\r | |
61 | * The code returned by the dialog when the user resets the latency pair to default.\r | |
62 | */\r | |
63 | public static final int RESET = DELETE + 1;\r | |
64 | \r | |
65 | /**\r | |
66 | * String ID of the number of pairs saved in the settings file.\r | |
67 | */\r | |
68 | protected static final String LATENCY_NB_MATCH_PAIRS = "NB_LATENCY_MATCH_PAIRS"; //$NON-NLS-1$\r | |
69 | /**\r | |
70 | * String ID of the start event pairs saved in the settings file.\r | |
71 | */\r | |
72 | protected static final String LATENCY_PAIRS_START = "LATENCY_PAIRS_START"; //$NON-NLS-1$\r | |
73 | /**\r | |
74 | * String ID of the end event pairs saved in the settings file.\r | |
75 | */\r | |
76 | protected static final String LATENCY_PAIRS_END = "LATENCY_PAIRS_END"; //$NON-NLS-1$\r | |
77 | \r | |
78 | /**\r | |
79 | * Dialog settings, saves the event pairs across sessions.\r | |
80 | */\r | |
81 | protected IDialogSettings fSettings;\r | |
82 | \r | |
83 | /**\r | |
84 | * Do the graphs canvas need to be redrawn due to latency pairs changes ?\r | |
85 | */\r | |
86 | protected boolean fRedrawGraphs = false;\r | |
87 | \r | |
88 | // ------------------------------------------------------------------------\r | |
89 | // Constructors\r | |
90 | // ------------------------------------------------------------------------\r | |
91 | \r | |
92 | /**\r | |
93 | * Constructor.\r | |
94 | * @param parentShell\r | |
95 | * The parent shell.\r | |
96 | * @param title\r | |
97 | * The dialog window's title.\r | |
98 | * @param message\r | |
99 | * The dialog window's message.\r | |
100 | */\r | |
101 | public AbstractDialog(Shell parentShell, String title, String message) {\r | |
102 | super(parentShell);\r | |
103 | fDialogTitle = title;\r | |
104 | fDialogMessage = message;\r | |
105 | \r | |
638eac44 | 106 | fSettings = Activator.getDefault().getDialogSettings();\r |
fbd124dd BH |
107 | }\r |
108 | \r | |
109 | /**\r | |
110 | * Constructor\r | |
111 | * @param parentShell\r | |
112 | * The parent shell.\r | |
113 | * @param title\r | |
114 | * The dialog window's title.\r | |
115 | */\r | |
116 | @SuppressWarnings("nls")\r | |
117 | public AbstractDialog(Shell parentShell, String title) {\r | |
118 | this(parentShell, title, "");\r | |
119 | }\r | |
120 | \r | |
121 | /**\r | |
122 | * Constructor.\r | |
123 | * @param parentShell\r | |
124 | * The parent shell.\r | |
125 | */\r | |
126 | @SuppressWarnings("nls")\r | |
127 | public AbstractDialog(Shell parentShell) {\r | |
128 | this(parentShell, "", "");\r | |
129 | }\r | |
130 | \r | |
131 | // ------------------------------------------------------------------------\r | |
132 | // Operations\r | |
133 | // ------------------------------------------------------------------------\r | |
134 | \r | |
135 | /**\r | |
136 | * Creates the dialog.\r | |
137 | * \r | |
138 | * <b>Note :</b> Since there is an issue with the table's vertical scroll bar, this dialog "resize" is necessary to\r | |
139 | * ensure a minimal height for the window.\r | |
140 | */\r | |
141 | @Override\r | |
142 | public void create() {\r | |
143 | super.create();\r | |
144 | // Set the title\r | |
145 | setTitle(fDialogTitle);\r | |
146 | // Set the message\r | |
147 | setMessage(fDialogMessage, IMessageProvider.INFORMATION);\r | |
148 | \r | |
149 | // Position the dialog at the center of the screen\r | |
150 | int windowWidth = Display.getCurrent().getPrimaryMonitor().getBounds().width;\r | |
151 | int windowHeight = Display.getCurrent().getPrimaryMonitor().getBounds().height;\r | |
152 | int dialogWidth = getShell().getSize().x;\r | |
153 | int dialogHeight = windowHeight / 2;\r | |
154 | \r | |
155 | int x = (windowWidth - dialogWidth) / 2;\r | |
156 | int y = (windowHeight - dialogHeight) / 2;\r | |
157 | \r | |
158 | getShell().setSize(getShell().getSize().x, dialogHeight);\r | |
159 | getShell().setLocation(x, y);\r | |
160 | }\r | |
161 | \r | |
162 | /**\r | |
163 | * Formats the "#" of the event in the table by adding "00" before it.\r | |
164 | * @param number\r | |
165 | * The number to format.\r | |
166 | * @param max\r | |
167 | * The maximum number of event pairs in the list.\r | |
168 | * @return The formatted string.\r | |
169 | */\r | |
170 | @SuppressWarnings("nls")\r | |
171 | protected String formatListNumber(int number, int max) {\r | |
172 | return String.format("%0" + max + "d", number);\r | |
173 | }\r | |
174 | \r | |
175 | /**\r | |
176 | * Returns the match pairs saved in the settings file.\r | |
177 | * @return The match pairs saved in the settings file.\r | |
178 | */\r | |
179 | protected EventsPair getMatchPairs() {\r | |
180 | try {\r | |
181 | // Check if the settings file has already some data (i.e. try provoking an exception)\r | |
182 | fSettings.getInt(LATENCY_NB_MATCH_PAIRS);\r | |
183 | \r | |
184 | String[] starts = fSettings.getArray(LATENCY_PAIRS_START);\r | |
185 | String[] ends = fSettings.getArray(LATENCY_PAIRS_END);\r | |
186 | \r | |
187 | EventMatcher.getInstance().resetMatches();\r | |
188 | for (int i = 0; i < starts.length; i++) {\r | |
189 | EventMatcher.getInstance().addMatch(starts[i], ends[i]);\r | |
190 | }\r | |
191 | \r | |
192 | return EventMatcher.getInstance().getEvents();\r | |
193 | } catch (NumberFormatException e) {\r | |
194 | return EventMatcher.getInstance().getEvents();\r | |
195 | }\r | |
196 | }\r | |
197 | \r | |
198 | /**\r | |
199 | * Saves the event match pairs to a settings file.\r | |
200 | * @param start\r | |
201 | * The start event types.\r | |
202 | * @param end\r | |
203 | * The end event types.\r | |
204 | */\r | |
205 | protected void saveMatchPairs(Vector<String> start, Vector<String> end) {\r | |
206 | fSettings.put(LATENCY_NB_MATCH_PAIRS, start.size());\r | |
207 | fSettings.put(LATENCY_PAIRS_START, start.toArray(new String[] {}));\r | |
208 | fSettings.put(LATENCY_PAIRS_END, end.toArray(new String[] {}));\r | |
209 | }\r | |
210 | \r | |
211 | /**\r | |
212 | * Ask the LatencyView to send a new analysis request to the views, so that they can be redrawn.\r | |
213 | */\r | |
214 | protected void redrawGraphs() {\r | |
215 | LatencyController.getInstance().refreshModels();\r | |
216 | }\r | |
217 | \r | |
218 | /*\r | |
219 | * (non-Javadoc)\r | |
220 | * @see org.eclipse.jface.dialogs.TitleAreaDialog#createDialogArea(org.eclipse.swt.widgets.Composite)\r | |
221 | */\r | |
222 | @Override\r | |
223 | protected abstract Control createDialogArea(Composite parent);\r | |
224 | \r | |
225 | /*\r | |
226 | * (non-Javadoc)\r | |
227 | * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)\r | |
228 | */\r | |
229 | @Override\r | |
230 | protected abstract void createButtonsForButtonBar(Composite parent);\r | |
231 | \r | |
232 | /*\r | |
233 | * (non-Javadoc)\r | |
234 | * @see org.eclipse.jface.dialogs.Dialog#isResizable()\r | |
235 | */\r | |
236 | @Override\r | |
237 | protected boolean isResizable() {\r | |
238 | return true;\r | |
239 | }\r | |
240 | } |