| /******************************************************************************* |
| * Copyright (c) 2000, 2017 IBM Corporation and others. |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| * Sebastian Davids, sdavids@gmx.de - 187316 [preferences] Mark Occurences Pref Page; Link to |
| *******************************************************************************/ |
| package org.eclipse.dltk.ui.preferences; |
| |
| import java.util.ArrayList; |
| import java.util.HashMap; |
| import java.util.Iterator; |
| import java.util.List; |
| import java.util.Map; |
| |
| import org.eclipse.core.runtime.Assert; |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.dltk.ui.PreferenceConstants; |
| import org.eclipse.dltk.ui.dialogs.StatusInfo; |
| import org.eclipse.jface.layout.PixelConverter; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.events.SelectionAdapter; |
| import org.eclipse.swt.events.SelectionEvent; |
| import org.eclipse.swt.events.SelectionListener; |
| import org.eclipse.swt.layout.GridData; |
| import org.eclipse.swt.layout.GridLayout; |
| import org.eclipse.swt.widgets.Button; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Control; |
| import org.eclipse.swt.widgets.Label; |
| import org.eclipse.swt.widgets.Link; |
| import org.eclipse.ui.dialogs.PreferencesUtil; |
| |
| /** |
| * Configures Java Editor hover preferences. |
| * |
| * @since 2.1 |
| */ |
| public class MarkOccurrencesConfigurationBlock |
| implements IPreferenceConfigurationBlock { |
| |
| private OverlayPreferenceStore fStore; |
| |
| private Map<Object, String> fCheckBoxes = new HashMap<>(); |
| private SelectionListener fCheckBoxListener = new SelectionListener() { |
| @Override |
| public void widgetDefaultSelected(SelectionEvent e) { |
| } |
| |
| @Override |
| public void widgetSelected(SelectionEvent e) { |
| Button button = (Button) e.widget; |
| fStore.setValue(fCheckBoxes.get(button), button.getSelection()); |
| } |
| }; |
| |
| /** |
| * List of master/slave listeners when there's a dependency. |
| * |
| * @see #createDependency(Button, String, Control) |
| * @since 3.0 |
| */ |
| private ArrayList<Object> fMasterSlaveListeners = new ArrayList<>(); |
| |
| private StatusInfo fStatus; |
| |
| public MarkOccurrencesConfigurationBlock(OverlayPreferenceStore store) { |
| Assert.isNotNull(store); |
| fStore = store; |
| |
| fStore.addKeys(createOverlayStoreKeys()); |
| } |
| |
| protected List<OverlayPreferenceStore.OverlayKey> createOverlayStoreKeys() { |
| List<OverlayPreferenceStore.OverlayKey> overlayKeys = new ArrayList<>(); |
| overlayKeys.add(new OverlayPreferenceStore.OverlayKey( |
| OverlayPreferenceStore.BOOLEAN, |
| PreferenceConstants.EDITOR_MARK_OCCURRENCES)); |
| overlayKeys.add(new OverlayPreferenceStore.OverlayKey( |
| OverlayPreferenceStore.BOOLEAN, |
| PreferenceConstants.EDITOR_STICKY_OCCURRENCES)); |
| return overlayKeys; |
| } |
| |
| /** |
| * Creates page for mark occurrences preferences. |
| * |
| * @param parent |
| * the parent composite |
| * @return the control for the preference page |
| */ |
| @Override |
| public Control createControl(final Composite parent) { |
| |
| Composite composite = new Composite(parent, SWT.NONE); |
| GridLayout layout = new GridLayout(); |
| layout.numColumns = 1; |
| layout.marginHeight = 0; |
| layout.marginWidth = 0; |
| composite.setLayout(layout); |
| |
| Link link = new Link(composite, SWT.NONE); |
| link.setText( |
| PreferencesMessages.MarkOccurrencesConfigurationBlock_link); |
| link.addSelectionListener(new SelectionAdapter() { |
| @Override |
| public void widgetSelected(SelectionEvent e) { |
| PreferencesUtil.createPreferenceDialogOn(parent.getShell(), |
| e.text, null, null); |
| } |
| }); |
| // TODO replace by link-specific tooltips when |
| // bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=88866 gets fixed |
| link.setToolTipText( |
| PreferencesMessages.MarkOccurrencesConfigurationBlock_link_tooltip); |
| |
| addFiller(composite); |
| |
| String label; |
| |
| label = PreferencesMessages.MarkOccurrencesConfigurationBlock_markOccurrences; |
| Button master = addCheckBox(composite, label, |
| PreferenceConstants.EDITOR_MARK_OCCURRENCES, 0); |
| |
| addFiller(composite); |
| |
| label = PreferencesMessages.MarkOccurrencesConfigurationBlock_stickyOccurrences; |
| Button slave = addCheckBox(composite, label, |
| PreferenceConstants.EDITOR_STICKY_OCCURRENCES, 0); |
| createDependency(master, PreferenceConstants.EDITOR_STICKY_OCCURRENCES, |
| slave); |
| |
| return composite; |
| } |
| |
| private void addFiller(Composite composite) { |
| PixelConverter pixelConverter = new PixelConverter(composite); |
| |
| Label filler = new Label(composite, SWT.LEFT); |
| GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL); |
| gd.horizontalSpan = 2; |
| gd.heightHint = pixelConverter.convertHeightInCharsToPixels(1) / 2; |
| filler.setLayoutData(gd); |
| } |
| |
| private Button addCheckBox(Composite parent, String label, String key, |
| int indentation) { |
| Button checkBox = new Button(parent, SWT.CHECK); |
| checkBox.setText(label); |
| |
| GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); |
| gd.horizontalIndent = indentation; |
| gd.horizontalSpan = 2; |
| checkBox.setLayoutData(gd); |
| checkBox.addSelectionListener(fCheckBoxListener); |
| |
| fCheckBoxes.put(checkBox, key); |
| |
| return checkBox; |
| } |
| |
| private void createDependency(final Button master, String masterKey, |
| final Control slave) { |
| indent(slave); |
| boolean masterState = fStore.getBoolean(masterKey); |
| slave.setEnabled(masterState); |
| SelectionListener listener = new SelectionListener() { |
| @Override |
| public void widgetSelected(SelectionEvent e) { |
| slave.setEnabled(master.getSelection()); |
| } |
| |
| @Override |
| public void widgetDefaultSelected(SelectionEvent e) { |
| } |
| }; |
| master.addSelectionListener(listener); |
| fMasterSlaveListeners.add(listener); |
| } |
| |
| private static void indent(Control control) { |
| GridData gridData = new GridData(); |
| gridData.horizontalIndent = 10; |
| control.setLayoutData(gridData); |
| } |
| |
| @Override |
| public void initialize() { |
| initializeFields(); |
| } |
| |
| void initializeFields() { |
| |
| Iterator<Object> iter = fCheckBoxes.keySet().iterator(); |
| while (iter.hasNext()) { |
| Button b = (Button) iter.next(); |
| String key = fCheckBoxes.get(b); |
| b.setSelection(fStore.getBoolean(key)); |
| } |
| |
| // Update slaves |
| iter = fMasterSlaveListeners.iterator(); |
| while (iter.hasNext()) { |
| SelectionListener listener = (SelectionListener) iter.next(); |
| listener.widgetSelected(null); |
| } |
| |
| } |
| |
| @Override |
| public void performOk() { |
| } |
| |
| @Override |
| public void performDefaults() { |
| restoreFromPreferences(); |
| initializeFields(); |
| } |
| |
| private void restoreFromPreferences() { |
| |
| } |
| |
| IStatus getStatus() { |
| if (fStatus == null) |
| fStatus = new StatusInfo(); |
| return fStatus; |
| } |
| |
| /* |
| * @see |
| * org.eclipse.jdt.internal.ui.preferences.IPreferenceConfigurationBlock |
| * #dispose() |
| * |
| * @since 3.0 |
| */ |
| @Override |
| public void dispose() { |
| } |
| } |