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