1 /*******************************************************************************
2 * Copyright (c) 2016 Movidius Inc. and others
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
9 *******************************************************************************/
11 package org
.eclipse
.tracecompass
.tmf
.ui
.symbols
;
13 import java
.lang
.reflect
.InvocationTargetException
;
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
;
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
48 public class SymbolProviderConfigDialog
extends TitleAreaDialog
implements IPreferencePageContainer
{
50 private ISymbolProviderPreferencePage
[] fPreferencePages
;
51 private CTabItem
[] fPageTabs
;
52 private CTabFolder fTabFolder
;
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();
62 monitor
.beginTask(Messages
.SymbolProviderConfigDialog_loadingConfigurations
, fPreferencePages
.length
* 100);
64 for (int i
= 0; i
< fPreferencePages
.length
; i
++) {
65 ISymbolProviderPreferencePage page
= fPreferencePages
[i
];
66 page
.getSymbolProvider().loadConfiguration(monitor
);
75 * Create a new dialog that will use the given shall and preference 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.
85 public SymbolProviderConfigDialog(Shell parentShell
, ISymbolProviderPreferencePage
... pages
) {
87 fPreferencePages
= pages
;
91 protected Control
createDialogArea(Composite parent
) {
92 getShell().setText(Messages
.SymbolProviderConfigDialog_title
);
93 setTitle(Messages
.SymbolProviderConfigDialog_title
);
94 setMessage(Messages
.SymbolProviderConfigDialog_message
);
96 Composite composite
= (Composite
) super.createDialogArea(parent
);
97 composite
.setLayout(new GridLayout());
99 // if we have one single provider that we don't need a tab
100 if (fPreferencePages
.length
== 1) {
101 attachPreference(composite
, fPreferencePages
[0]);
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
);
118 fTabFolder
.addSelectionListener(new SelectionAdapter() {
120 public void widgetSelected(SelectionEvent e
) {
121 updateMessage(fTabFolder
.indexOf((CTabItem
) e
.item
));
129 public boolean isHelpAvailable() {
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
));
140 public IPreferenceStore
getPreferenceStore() {
146 public void updateTitle() {
151 public void updateButtons() {
156 protected void okPressed() {
157 boolean cancel
= false;
159 new ProgressMonitorDialog(getShell()).run(true, false, configJob
);
160 } catch (InvocationTargetException e
) {
161 setMessage(e
.getMessage(), IMessageProvider
.ERROR
);
163 } catch (InterruptedException e
) {
172 public void updateMessage() {
173 if (fTabFolder
== null) {
177 int curSelectionIndex
= fTabFolder
.getSelectionIndex();
178 if (curSelectionIndex
>= 0) {
179 updateMessage(curSelectionIndex
);
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
;
189 if (errorMessage
!= null) {
190 message
= errorMessage
;
191 messageType
= IMessageProvider
.ERROR
;
193 setMessage(message
, messageType
);
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
));
200 fPageTabs
[pageIndex
].setImage(null);