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
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
33 import org
.eclipse
.ui
.ISharedImages
;
34 import org
.eclipse
.ui
.PlatformUI
;
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
49 public class SymbolProviderConfigDialog
extends TitleAreaDialog
implements IPreferencePageContainer
{
51 private ISymbolProviderPreferencePage
[] fPreferencePages
;
52 private CTabItem
[] fPageTabs
;
53 private CTabFolder fTabFolder
;
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();
63 monitor
.beginTask(Messages
.SymbolProviderConfigDialog_loadingConfigurations
, fPreferencePages
.length
* 100);
65 for (int i
= 0; i
< fPreferencePages
.length
; i
++) {
66 ISymbolProviderPreferencePage page
= fPreferencePages
[i
];
67 page
.getSymbolProvider().loadConfiguration(monitor
);
76 * Create a new dialog that will use the given shall and preference 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.
86 public SymbolProviderConfigDialog(Shell parentShell
, ISymbolProviderPreferencePage
... pages
) {
88 fPreferencePages
= pages
;
92 protected Control
createDialogArea(Composite parent
) {
93 getShell().setText(Messages
.SymbolProviderConfigDialog_title
);
94 setTitle(Messages
.SymbolProviderConfigDialog_title
);
95 setMessage(Messages
.SymbolProviderConfigDialog_message
);
97 Composite composite
= (Composite
) super.createDialogArea(parent
);
98 composite
.setLayout(new GridLayout());
100 // if we have one single provider that we don't need a tab
101 if (fPreferencePages
.length
== 1) {
102 attachPreference(composite
, fPreferencePages
[0]);
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
);
119 fTabFolder
.addSelectionListener(new SelectionAdapter() {
121 public void widgetSelected(SelectionEvent e
) {
122 updateMessage(fTabFolder
.indexOf((CTabItem
) e
.item
));
130 public boolean isHelpAvailable() {
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
));
141 public IPreferenceStore
getPreferenceStore() {
147 public void updateTitle() {
152 public void updateButtons() {
157 protected void okPressed() {
158 boolean cancel
= false;
160 new ProgressMonitorDialog(getShell()).run(true, false, configJob
);
161 } catch (InvocationTargetException e
) {
162 setMessage(e
.getMessage(), IMessageProvider
.ERROR
);
164 } catch (InterruptedException e
) {
170 TmfSignalManager
.dispatchSignal(new TmfSymbolProviderUpdatedSignal(this));
174 public void updateMessage() {
175 if (fTabFolder
== null) {
179 int curSelectionIndex
= fTabFolder
.getSelectionIndex();
180 if (curSelectionIndex
>= 0) {
181 updateMessage(curSelectionIndex
);
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
;
191 if (errorMessage
!= null) {
192 message
= errorMessage
;
193 messageType
= IMessageProvider
.ERROR
;
195 setMessage(message
, messageType
);
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
));
202 fPageTabs
[pageIndex
].setImage(null);