Commit | Line | Data |
---|---|---|
d90ae2a5 RK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 Movidius Inc. and others | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | *******************************************************************************/ | |
10 | ||
11 | package org.eclipse.tracecompass.tmf.ui.symbols; | |
12 | ||
13 | import java.lang.reflect.InvocationTargetException; | |
14 | ||
15 | import org.eclipse.jface.dialogs.IDialogConstants; | |
16 | import org.eclipse.jface.dialogs.IMessageProvider; | |
17 | import org.eclipse.jface.dialogs.ProgressMonitorDialog; | |
18 | import org.eclipse.jface.dialogs.TitleAreaDialog; | |
19 | import org.eclipse.jface.operation.IRunnableWithProgress; | |
20 | import org.eclipse.jface.preference.IPreferencePageContainer; | |
21 | import org.eclipse.jface.preference.IPreferenceStore; | |
22 | import org.eclipse.swt.SWT; | |
23 | import org.eclipse.swt.custom.CTabFolder; | |
24 | import org.eclipse.swt.custom.CTabItem; | |
25 | import org.eclipse.swt.events.SelectionAdapter; | |
26 | import org.eclipse.swt.events.SelectionEvent; | |
27 | import org.eclipse.swt.layout.GridData; | |
28 | import org.eclipse.swt.layout.GridLayout; | |
29 | import org.eclipse.swt.widgets.Composite; | |
30 | import org.eclipse.swt.widgets.Control; | |
31 | import org.eclipse.swt.widgets.Shell; | |
34170ea3 | 32 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager; |
d90ae2a5 RK |
33 | import org.eclipse.ui.ISharedImages; |
34 | import org.eclipse.ui.PlatformUI; | |
35 | ||
36 | /** | |
37 | * This class shall be used to configure one or more {@link ISymbolProvider}. It | |
38 | * receives an array of {@link ISymbolProviderPreferencePage} and creates a | |
39 | * dialog that can be used to configure the corresponding providers. If the | |
40 | * {@link #open()} method exits with {@link IDialogConstants#OK_ID} the caller | |
41 | * shall assume that the corresponding {@link ISymbolProvider}'s have a new | |
42 | * configuration. | |
43 | * | |
44 | * | |
45 | * @author Robert Kiss | |
46 | * @since 2.0 | |
47 | * | |
48 | */ | |
49 | public class SymbolProviderConfigDialog extends TitleAreaDialog implements IPreferencePageContainer { | |
50 | ||
51 | private ISymbolProviderPreferencePage[] fPreferencePages; | |
52 | private CTabItem[] fPageTabs; | |
53 | private CTabFolder fTabFolder; | |
54 | ||
55 | private IRunnableWithProgress configJob = (monitor) -> { | |
56 | // saving the configuration is fast and need UI access | |
57 | SymbolProviderConfigDialog.this.getContents().getDisplay().syncExec(() -> { | |
58 | for (int i = 0; i < fPreferencePages.length; i++) { | |
59 | ISymbolProviderPreferencePage page = fPreferencePages[i]; | |
60 | page.saveConfiguration(); | |
61 | } | |
62 | }); | |
6997036f | 63 | monitor.beginTask(Messages.SymbolProviderConfigDialog_loadingConfigurations, fPreferencePages.length * 100); |
d90ae2a5 RK |
64 | try { |
65 | for (int i = 0; i < fPreferencePages.length; i++) { | |
66 | ISymbolProviderPreferencePage page = fPreferencePages[i]; | |
67 | page.getSymbolProvider().loadConfiguration(monitor); | |
68 | monitor.worked(100); | |
69 | } | |
70 | } finally { | |
71 | monitor.done(); | |
72 | } | |
73 | }; | |
74 | ||
75 | /** | |
76 | * Create a new dialog that will use the given shall and preference pages. | |
77 | * | |
78 | * @param parentShell | |
79 | * The parent shell | |
80 | * @param pages | |
81 | * the pages that provides the configuration UI for | |
82 | * {@link ISymbolProvider}'s. The array shall not be empty and | |
83 | * shall not contain null elements. | |
84 | * | |
85 | */ | |
86 | public SymbolProviderConfigDialog(Shell parentShell, ISymbolProviderPreferencePage... pages) { | |
87 | super(parentShell); | |
88 | fPreferencePages = pages; | |
89 | } | |
90 | ||
91 | @Override | |
92 | protected Control createDialogArea(Composite parent) { | |
93 | getShell().setText(Messages.SymbolProviderConfigDialog_title); | |
94 | setTitle(Messages.SymbolProviderConfigDialog_title); | |
95 | setMessage(Messages.SymbolProviderConfigDialog_message); | |
96 | ||
97 | Composite composite = (Composite) super.createDialogArea(parent); | |
98 | composite.setLayout(new GridLayout()); | |
99 | ||
100 | // if we have one single provider that we don't need a tab | |
101 | if (fPreferencePages.length == 1) { | |
102 | attachPreference(composite, fPreferencePages[0]); | |
103 | updateMessage(0); | |
104 | } else { | |
105 | fTabFolder = new CTabFolder(composite, SWT.NONE); | |
106 | fTabFolder.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
107 | fPageTabs = new CTabItem[fPreferencePages.length]; | |
108 | for (int i = 0; i < fPreferencePages.length; i++) { | |
109 | ISymbolProviderPreferencePage page = fPreferencePages[i]; | |
110 | fPageTabs[i] = new CTabItem(fTabFolder, SWT.NONE); | |
111 | fPageTabs[i].setText(page.getTitle()); | |
112 | Composite child = new Composite(fTabFolder, SWT.NONE); | |
113 | child.setLayout(new GridLayout()); | |
114 | child.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true)); | |
115 | fPageTabs[i].setControl(child); | |
116 | attachPreference(child, page); | |
117 | updateMessage(i); | |
118 | } | |
119 | fTabFolder.addSelectionListener(new SelectionAdapter() { | |
120 | @Override | |
121 | public void widgetSelected(SelectionEvent e) { | |
122 | updateMessage(fTabFolder.indexOf((CTabItem) e.item)); | |
123 | } | |
124 | }); | |
125 | } | |
126 | return composite; | |
127 | } | |
128 | ||
129 | @Override | |
130 | public boolean isHelpAvailable() { | |
131 | return false; | |
132 | } | |
133 | ||
134 | private void attachPreference(Composite composite, ISymbolProviderPreferencePage page) { | |
135 | page.setContainer(this); | |
136 | page.createControl(composite); | |
137 | page.getControl().setLayoutData(new GridData(GridData.FILL_BOTH)); | |
138 | } | |
139 | ||
140 | @Override | |
141 | public IPreferenceStore getPreferenceStore() { | |
142 | // not used | |
143 | return null; | |
144 | } | |
145 | ||
146 | @Override | |
147 | public void updateTitle() { | |
148 | // not used | |
149 | } | |
150 | ||
151 | @Override | |
152 | public void updateButtons() { | |
153 | // nothing to do | |
154 | } | |
155 | ||
156 | @Override | |
157 | protected void okPressed() { | |
158 | boolean cancel = false; | |
159 | try { | |
160 | new ProgressMonitorDialog(getShell()).run(true, false, configJob); | |
161 | } catch (InvocationTargetException e) { | |
162 | setMessage(e.getMessage(), IMessageProvider.ERROR); | |
163 | cancel = true; | |
164 | } catch (InterruptedException e) { | |
165 | // ignore | |
166 | } | |
167 | if (!cancel) { | |
168 | super.okPressed(); | |
169 | } | |
34170ea3 | 170 | TmfSignalManager.dispatchSignal(new TmfSymbolProviderUpdatedSignal(this)); |
d90ae2a5 RK |
171 | } |
172 | ||
173 | @Override | |
174 | public void updateMessage() { | |
175 | if (fTabFolder == null) { | |
176 | updateMessage(0); | |
b587a6c3 AM |
177 | return; |
178 | } | |
179 | int curSelectionIndex = fTabFolder.getSelectionIndex(); | |
180 | if (curSelectionIndex >= 0) { | |
181 | updateMessage(curSelectionIndex); | |
d90ae2a5 RK |
182 | } |
183 | } | |
184 | ||
185 | private void updateMessage(int pageIndex) { | |
186 | ISymbolProviderPreferencePage currentPage = fPreferencePages[pageIndex]; | |
187 | String message = currentPage.getMessage(); | |
188 | String errorMessage = currentPage.getErrorMessage(); | |
189 | int messageType = IMessageProvider.NONE; | |
190 | ||
191 | if (errorMessage != null) { | |
192 | message = errorMessage; | |
193 | messageType = IMessageProvider.ERROR; | |
194 | } | |
195 | setMessage(message, messageType); | |
196 | ||
197 | if (fPreferencePages.length > 1) { | |
198 | // update the corresponding tab icon | |
199 | if (messageType == IMessageProvider.ERROR) { | |
200 | fPageTabs[pageIndex].setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_ERROR_TSK)); | |
201 | } else { | |
202 | fPageTabs[pageIndex].setImage(null); | |
203 | } | |
204 | } | |
205 | } | |
206 | ||
207 | } |