898e1598e18b98dbf4a071bd69698fa5f4e13214
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / symbols / SymbolProviderConfigDialog.java
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;
32 import org.eclipse.ui.ISharedImages;
33 import org.eclipse.ui.PlatformUI;
34
35 /**
36 * This class shall be used to configure one or more {@link ISymbolProvider}. It
37 * receives an array of {@link ISymbolProviderPreferencePage} and creates a
38 * dialog that can be used to configure the corresponding providers. If the
39 * {@link #open()} method exits with {@link IDialogConstants#OK_ID} the caller
40 * shall assume that the corresponding {@link ISymbolProvider}'s have a new
41 * configuration.
42 *
43 *
44 * @author Robert Kiss
45 * @since 2.0
46 *
47 */
48 public class SymbolProviderConfigDialog extends TitleAreaDialog implements IPreferencePageContainer {
49
50 private ISymbolProviderPreferencePage[] fPreferencePages;
51 private CTabItem[] fPageTabs;
52 private CTabFolder fTabFolder;
53
54 private IRunnableWithProgress configJob = (monitor) -> {
55 // saving the configuration is fast and need UI access
56 SymbolProviderConfigDialog.this.getContents().getDisplay().syncExec(() -> {
57 for (int i = 0; i < fPreferencePages.length; i++) {
58 ISymbolProviderPreferencePage page = fPreferencePages[i];
59 page.saveConfiguration();
60 }
61 });
62 monitor.beginTask(Messages.SymbolProviderConfigDialog_loadingConfigurations, fPreferencePages.length * 100);
63 try {
64 for (int i = 0; i < fPreferencePages.length; i++) {
65 ISymbolProviderPreferencePage page = fPreferencePages[i];
66 page.getSymbolProvider().loadConfiguration(monitor);
67 monitor.worked(100);
68 }
69 } finally {
70 monitor.done();
71 }
72 };
73
74 /**
75 * Create a new dialog that will use the given shall and preference pages.
76 *
77 * @param parentShell
78 * The parent shell
79 * @param pages
80 * the pages that provides the configuration UI for
81 * {@link ISymbolProvider}'s. The array shall not be empty and
82 * shall not contain null elements.
83 *
84 */
85 public SymbolProviderConfigDialog(Shell parentShell, ISymbolProviderPreferencePage... pages) {
86 super(parentShell);
87 fPreferencePages = pages;
88 }
89
90 @Override
91 protected Control createDialogArea(Composite parent) {
92 getShell().setText(Messages.SymbolProviderConfigDialog_title);
93 setTitle(Messages.SymbolProviderConfigDialog_title);
94 setMessage(Messages.SymbolProviderConfigDialog_message);
95
96 Composite composite = (Composite) super.createDialogArea(parent);
97 composite.setLayout(new GridLayout());
98
99 // if we have one single provider that we don't need a tab
100 if (fPreferencePages.length == 1) {
101 attachPreference(composite, fPreferencePages[0]);
102 updateMessage(0);
103 } else {
104 fTabFolder = new CTabFolder(composite, SWT.NONE);
105 fTabFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
106 fPageTabs = new CTabItem[fPreferencePages.length];
107 for (int i = 0; i < fPreferencePages.length; i++) {
108 ISymbolProviderPreferencePage page = fPreferencePages[i];
109 fPageTabs[i] = new CTabItem(fTabFolder, SWT.NONE);
110 fPageTabs[i].setText(page.getTitle());
111 Composite child = new Composite(fTabFolder, SWT.NONE);
112 child.setLayout(new GridLayout());
113 child.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true));
114 fPageTabs[i].setControl(child);
115 attachPreference(child, page);
116 updateMessage(i);
117 }
118 fTabFolder.addSelectionListener(new SelectionAdapter() {
119 @Override
120 public void widgetSelected(SelectionEvent e) {
121 updateMessage(fTabFolder.indexOf((CTabItem) e.item));
122 }
123 });
124 }
125 return composite;
126 }
127
128 @Override
129 public boolean isHelpAvailable() {
130 return false;
131 }
132
133 private void attachPreference(Composite composite, ISymbolProviderPreferencePage page) {
134 page.setContainer(this);
135 page.createControl(composite);
136 page.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
137 }
138
139 @Override
140 public IPreferenceStore getPreferenceStore() {
141 // not used
142 return null;
143 }
144
145 @Override
146 public void updateTitle() {
147 // not used
148 }
149
150 @Override
151 public void updateButtons() {
152 // nothing to do
153 }
154
155 @Override
156 protected void okPressed() {
157 boolean cancel = false;
158 try {
159 new ProgressMonitorDialog(getShell()).run(true, false, configJob);
160 } catch (InvocationTargetException e) {
161 setMessage(e.getMessage(), IMessageProvider.ERROR);
162 cancel = true;
163 } catch (InterruptedException e) {
164 // ignore
165 }
166 if (!cancel) {
167 super.okPressed();
168 }
169 }
170
171 @Override
172 public void updateMessage() {
173 if (fTabFolder == null) {
174 updateMessage(0);
175 return;
176 }
177 int curSelectionIndex = fTabFolder.getSelectionIndex();
178 if (curSelectionIndex >= 0) {
179 updateMessage(curSelectionIndex);
180 }
181 }
182
183 private void updateMessage(int pageIndex) {
184 ISymbolProviderPreferencePage currentPage = fPreferencePages[pageIndex];
185 String message = currentPage.getMessage();
186 String errorMessage = currentPage.getErrorMessage();
187 int messageType = IMessageProvider.NONE;
188
189 if (errorMessage != null) {
190 message = errorMessage;
191 messageType = IMessageProvider.ERROR;
192 }
193 setMessage(message, messageType);
194
195 if (fPreferencePages.length > 1) {
196 // update the corresponding tab icon
197 if (messageType == IMessageProvider.ERROR) {
198 fPageTabs[pageIndex].setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_ERROR_TSK));
199 } else {
200 fPageTabs[pageIndex].setImage(null);
201 }
202 }
203 }
204
205 }
This page took 0.036044 seconds and 4 git commands to generate.