|
Line 0
Link Here
|
|
|
1 |
/***************************************************************************** |
| 2 |
* Copyright (c) 2013 CEA LIST. |
| 3 |
* |
| 4 |
* |
| 5 |
* All rights reserved. This program and the accompanying materials |
| 6 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
* which accompanies this distribution, and is available at |
| 8 |
* http://www.eclipse.org/legal/epl-v10.html |
| 9 |
* |
| 10 |
* Contributors: |
| 11 |
* Nizar GUEDIDI (CEA LIST) - Initial API and implementation |
| 12 |
*****************************************************************************/ |
| 13 |
package org.eclipse.papyrus.infra.services.navigation.preferences; |
| 14 |
|
| 15 |
import java.util.HashMap; |
| 16 |
import java.util.Map; |
| 17 |
|
| 18 |
import org.eclipse.core.runtime.preferences.IEclipsePreferences; |
| 19 |
import org.eclipse.core.runtime.preferences.InstanceScope; |
| 20 |
import org.eclipse.jface.preference.IPreferenceStore; |
| 21 |
import org.eclipse.jface.preference.PreferencePage; |
| 22 |
import org.eclipse.jface.viewers.ColumnLabelProvider; |
| 23 |
import org.eclipse.jface.viewers.ColumnWeightData; |
| 24 |
import org.eclipse.jface.viewers.TableLayout; |
| 25 |
import org.eclipse.jface.viewers.TableViewer; |
| 26 |
import org.eclipse.jface.viewers.ViewerCell; |
| 27 |
import org.eclipse.papyrus.infra.core.services.ServiceException; |
| 28 |
import org.eclipse.papyrus.infra.services.navigation.Activator; |
| 29 |
import org.eclipse.papyrus.infra.services.navigation.service.impl.NavigationServiceImpl; |
| 30 |
import org.eclipse.papyrus.infra.services.navigation.service.impl.NavigationServiceImpl.NavigationContributorWrapper; |
| 31 |
import org.eclipse.papyrus.infra.widgets.providers.CollectionContentProvider; |
| 32 |
import org.eclipse.swt.SWT; |
| 33 |
import org.eclipse.swt.custom.TableEditor; |
| 34 |
import org.eclipse.swt.graphics.Image; |
| 35 |
import org.eclipse.swt.layout.GridData; |
| 36 |
import org.eclipse.swt.layout.GridLayout; |
| 37 |
import org.eclipse.swt.widgets.Button; |
| 38 |
import org.eclipse.swt.widgets.Composite; |
| 39 |
import org.eclipse.swt.widgets.Control; |
| 40 |
import org.eclipse.swt.widgets.TableColumn; |
| 41 |
import org.eclipse.swt.widgets.TableItem; |
| 42 |
import org.eclipse.ui.IWorkbench; |
| 43 |
import org.eclipse.ui.IWorkbenchPreferencePage; |
| 44 |
import org.osgi.service.prefs.BackingStoreException; |
| 45 |
|
| 46 |
public class NavigationServicePreferencesPage extends PreferencePage implements IWorkbenchPreferencePage { |
| 47 |
|
| 48 |
public static final int ACTIVATION_COLUMN = 0; |
| 49 |
|
| 50 |
private Map<NavigationContributorWrapper, Button> checkboxes; |
| 51 |
|
| 52 |
private NavigationServiceImpl navigation = new NavigationServiceImpl(); |
| 53 |
|
| 54 |
/** |
| 55 |
* |
| 56 |
* Constructor. |
| 57 |
* |
| 58 |
*/ |
| 59 |
public NavigationServicePreferencesPage() { |
| 60 |
super("Navigation Services", org.eclipse.papyrus.infra.widgets.Activator.getDefault().getImageDescriptor("/icons/papyrus.png")); |
| 61 |
} |
| 62 |
|
| 63 |
public void init(IWorkbench workbench) { |
| 64 |
setPreferenceStore(Activator.getDefault().getPreferenceStore()); |
| 65 |
setDescription("Papyrus navigation services configuration.\nSelect the strategies you wish to activate."); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
protected Control createContents(Composite parent) { |
| 70 |
|
| 71 |
Composite self = new Composite(parent, SWT.NONE); |
| 72 |
self.setLayout(new GridLayout(1, true)); |
| 73 |
|
| 74 |
final TableViewer tableViewer = new TableViewer(self); |
| 75 |
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, false); |
| 76 |
tableViewer.getTable().setLayoutData(gridData); |
| 77 |
|
| 78 |
tableViewer.setContentProvider(CollectionContentProvider.instance); |
| 79 |
|
| 80 |
// NavigationServiceImpl navigation = new NavigationServiceImpl(); |
| 81 |
try { |
| 82 |
navigation.startService(); |
| 83 |
} catch (ServiceException ex) { |
| 84 |
return self; |
| 85 |
} |
| 86 |
|
| 87 |
tableViewer.setLabelProvider(new ColumnLabelProvider() { |
| 88 |
|
| 89 |
@Override |
| 90 |
public void update(ViewerCell cell) { |
| 91 |
if(cell.getColumnIndex() == 1) { |
| 92 |
super.update(cell); |
| 93 |
} else { |
| 94 |
return; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public Image getImage(Object element) { |
| 100 |
if(element instanceof NavigationContributorWrapper) { |
| 101 |
return null; |
| 102 |
} |
| 103 |
|
| 104 |
return super.getImage(element); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public String getText(Object element) { |
| 109 |
if(element instanceof NavigationContributorWrapper) { |
| 110 |
return ((NavigationContributorWrapper)element).getLabel(); |
| 111 |
} |
| 112 |
return super.getText(element); |
| 113 |
} |
| 114 |
}); |
| 115 |
|
| 116 |
TableLayout layout = new TableLayout(); |
| 117 |
|
| 118 |
new TableColumn(tableViewer.getTable(), SWT.LEFT); |
| 119 |
layout.addColumnData(new ColumnWeightData(10, 25, false)); |
| 120 |
|
| 121 |
new TableColumn(tableViewer.getTable(), SWT.LEFT); |
| 122 |
layout.addColumnData(new ColumnWeightData(100, 250, true)); |
| 123 |
|
| 124 |
tableViewer.getTable().setLayout(layout); |
| 125 |
tableViewer.getTable().setHeaderVisible(false); |
| 126 |
|
| 127 |
tableViewer.setInput(navigation.getNavigationContributors()); |
| 128 |
|
| 129 |
// Adds a checkbox for each service navigation, to toggle it |
| 130 |
checkboxes = new HashMap<NavigationContributorWrapper, Button>(); |
| 131 |
|
| 132 |
for(TableItem item : tableViewer.getTable().getItems()) { |
| 133 |
if(item.getData() instanceof NavigationContributorWrapper) { |
| 134 |
TableEditor editor = new TableEditor(tableViewer.getTable()); |
| 135 |
|
| 136 |
final Button button = new Button(tableViewer.getTable(), SWT.CHECK); |
| 137 |
final TableItem currentItem = item; |
| 138 |
|
| 139 |
final NavigationContributorWrapper strategy = (NavigationContributorWrapper)currentItem.getData(); |
| 140 |
|
| 141 |
checkboxes.put(strategy, button); |
| 142 |
|
| 143 |
button.setSelection(strategy.isActive()); |
| 144 |
|
| 145 |
editor.setEditor(button, item, ACTIVATION_COLUMN); |
| 146 |
editor.horizontalAlignment = SWT.CENTER; |
| 147 |
editor.grabHorizontal = true; |
| 148 |
|
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return self; |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
protected void performDefaults() { |
| 157 |
restoreDefaults(); |
| 158 |
super.performDefaults(); |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
protected void performApply() { |
| 163 |
IPreferenceStore preferences = Activator.getDefault().getPreferenceStore(); |
| 164 |
|
| 165 |
for(Map.Entry<NavigationContributorWrapper, Button> entry : checkboxes.entrySet()) { |
| 166 |
boolean checked = entry.getValue().getSelection(); |
| 167 |
String isActiveKey = NavigationContributorWrapper.getIsActiveKey(entry.getKey()); |
| 168 |
preferences.setValue(isActiveKey, checked); |
| 169 |
} |
| 170 |
|
| 171 |
try { |
| 172 |
IEclipsePreferences preferenceStore = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID); |
| 173 |
preferenceStore.flush(); |
| 174 |
} catch (BackingStoreException ex) { |
| 175 |
Activator.log.error(ex); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
@Override |
| 180 |
public boolean performOk() { |
| 181 |
performApply(); |
| 182 |
return super.performOk(); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Restores the default preferences |
| 187 |
*/ |
| 188 |
|
| 189 |
public void restoreDefaults() { |
| 190 |
IPreferenceStore preferences = Activator.getDefault().getPreferenceStore(); |
| 191 |
|
| 192 |
for (Map.Entry<NavigationContributorWrapper, Button> entry : checkboxes.entrySet()){ |
| 193 |
String isActiveKey = NavigationContributorWrapper.getIsActiveKey(entry.getKey()); |
| 194 |
boolean selected = preferences.getDefaultBoolean(isActiveKey); |
| 195 |
|
| 196 |
entry.getValue().setSelection(selected); |
| 197 |
} |
| 198 |
} |
| 199 |
} |