blob: 5bc1e43b2db978627c87f399b1d1e716c0ca8e14 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2012 SAP AG and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* SAP AG - initial API and implementation
*******************************************************************************/
package org.eclipse.platform.discovery.destprefs.internal.prefpage.ui;
import java.util.Collection;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.platform.discovery.destprefs.internal.i18n.DestPrefsMessages;
import org.eclipse.platform.discovery.destprefs.internal.prefpage.ui.nodes.CategoryNode;
import org.eclipse.platform.discovery.destprefs.internal.prefpage.ui.nodes.DestinationNode;
import org.eclipse.platform.discovery.destprefs.internal.xpparser.SearchDestinationsConfiguratorXPParser;
import org.eclipse.platform.discovery.runtime.internal.SearchProviderConfigurationFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
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.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
public class DestinationsPreferencePage extends PreferencePage implements IWorkbenchPreferencePage, IDestinationConfiguratorsView {
private final IDestinationConfiguratorsPresenter presenter;
private TreeViewer destinationsTreeViewer;
private Button addDestinationButton;
private Button removeDestinationButton;
private Button deleteDestinationButton;
private Button testDestinationButton;
public DestinationsPreferencePage(final IDestinationConfiguratorsPresenter presenter) {
this.presenter = presenter;
}
public DestinationsPreferencePage() {
this(new DestinationConfiguratorsPresenter(new SearchDestinationsConfiguratorXPParser(Platform.getExtensionRegistry()), SearchProviderConfigurationFactory.getDefault()));
}
@Override
public void init(IWorkbench workbench) {
noDefaultAndApplyButton();
this.presenter.setView(this);
}
@Override
protected Control createContents(final Composite parent) {
final Composite parentComposite = new Composite(parent, SWT.NONE);
parentComposite.setLayout(new GridLayout(2, false));
final Composite treeViewerComposite = new Composite(parentComposite, SWT.NONE);
treeViewerComposite.setLayout(new FillLayout(SWT.VERTICAL));
GridDataFactory.defaultsFor(treeViewerComposite).align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(treeViewerComposite);
destinationsTreeViewer = createDestinationsTreeViewer(treeViewerComposite);
final Composite buttonsComposite = new Composite(parentComposite, SWT.NONE);
buttonsComposite.setLayout(new GridLayout(1, true));
GridDataFactory.defaultsFor(buttonsComposite).align(SWT.END, SWT.FILL).grab(false, true).applyTo(buttonsComposite);
addDestinationButton = createAddDestinationButton(buttonsComposite);
removeDestinationButton = createEditDestinationButton(buttonsComposite);
deleteDestinationButton = createDeleteDestinationButton(buttonsComposite);
testDestinationButton = createTestDestinationButton(buttonsComposite);
return parentComposite;
}
private TreeViewer createDestinationsTreeViewer(final Composite parent) {
final TreeViewer viewer = new TreeViewer(parent, SWT.SINGLE | SWT.BORDER);
viewer.addSelectionChangedListener(destinationsSelectionChangeListener());
viewer.setLabelProvider(new DestinationsLabelProvider());
viewer.setContentProvider(new CategoryDestinationsContentProvider());
return viewer;
}
private ISelectionChangedListener destinationsSelectionChangeListener() {
return new ISelectionChangedListener() {
@Override
public void selectionChanged(final SelectionChangedEvent event) {
if (event.getSelection().isEmpty()) {
return;
}
final Object selectedElement = ((StructuredSelection) event.getSelection()).getFirstElement();
if (selectedElement instanceof CategoryNode) {
presenter.selectionChanged(new DestinationConfiguratorSelection(((CategoryNode) selectedElement).getDestinationProviderId(), null));
}
if (selectedElement instanceof DestinationNode) {
final DestinationNode destNode = (DestinationNode)selectedElement;
presenter.selectionChanged(new DestinationConfiguratorSelection(destNode.getParentNode().getDestinationProviderId(), destNode.getDestination()));
}
throw new IllegalArgumentException("Unsupported element: " + selectedElement);
}
};
}
private Button createAddDestinationButton(final Composite parent) {
final Button button = createDisabledButton(parent, DestPrefsMessages.DestinationsPrefPage_AddButton);
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
presenter.addDestination();
}
});
return button;
}
private Button createEditDestinationButton(final Composite parent) {
final Button button = createDisabledButton(parent, DestPrefsMessages.DestinationsPrefPage_EditButton);
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
presenter.editDestination();
}
});
return button;
}
private Button createDeleteDestinationButton(final Composite parent) {
final Button button = createDisabledButton(parent, DestPrefsMessages.DestinationsPrefPage_DeleteButton);
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
presenter.removeDestination();
}
});
return button;
}
private Button createTestDestinationButton(final Composite parent) {
final Button button = createDisabledButton(parent, DestPrefsMessages.DestinationsPrefPage_TestButton);
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
presenter.testDestination();
}
});
return button;
}
private Button createDisabledButton(final Composite parent, final String label) {
final Button button = new Button(parent, SWT.PUSH);
button.setText(label);
GridDataFactory.defaultsFor(button).applyTo(button);
button.setEnabled(false);
return button;
}
@Override
public void setInput(Collection<CategoryDestinationProviderPair> input) {
destinationsTreeViewer.setInput(input);
}
@Override
public void setStatus(IStatus status) {
this.setMessage(status.getMessage(), StatusSeverityToMessageTypeAdapter.messageTypeFor(status));
}
@Override
public void setAddEnabled(boolean enabled) {
this.addDestinationButton.setEnabled(enabled);
}
@Override
public void setEditEnabled(boolean enabled) {
this.removeDestinationButton.setEnabled(enabled);
}
@Override
public void setRemoveEnabled(boolean enabled) {
this.deleteDestinationButton.setEnabled(enabled);
}
@Override
public void setTestEnabled(boolean enabled) {
this.testDestinationButton.setEnabled(enabled);
}
private static class StatusSeverityToMessageTypeAdapter {
static int messageTypeFor(final IStatus status) {
switch (status.getSeverity()) {
case IStatus.ERROR:
return IMessageProvider.ERROR;
case IStatus.WARNING:
return IMessageProvider.WARNING;
default:
return IMessageProvider.INFORMATION;
}
}
}
}