/** * Copyright (c) 2008 Aptana, Inc. * * 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. If redistributing this code, * this entire header must remain intact. * * This file is based on a JDT equivalent: ******************************************************************************* * Copyright (c) 2000, 2006 IBM Corporation 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: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.rubypeople.rdt.internal.ui.wizards.buildpaths; import java.util.Arrays; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IDialogSettings; import org.eclipse.jface.viewers.ArrayContentProvider; import org.eclipse.jface.viewers.DoubleClickEvent; import org.eclipse.jface.viewers.IDoubleClickListener; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.ListViewer; import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.jface.viewers.ViewerSorter; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; import org.rubypeople.rdt.internal.ui.RubyPlugin; import org.rubypeople.rdt.internal.ui.RubyPluginImages; import org.rubypeople.rdt.internal.ui.util.SelectionUtil; import org.rubypeople.rdt.internal.ui.wizards.NewWizardMessages; /** */ public class LoadpathContainerSelectionPage extends WizardPage { private static final String DIALOGSTORE_SECTION= "LoadpathContainerSelectionPage"; //$NON-NLS-1$ private static final String DIALOGSTORE_CONTAINER_IDX= "index"; //$NON-NLS-1$ private static class LoadpathContainerLabelProvider extends LabelProvider { public String getText(Object element) { return ((LoadpathContainerDescriptor) element).getName(); } } private static class LoadpathContainerSorter extends ViewerSorter { } private ListViewer fListViewer; private LoadpathContainerDescriptor[] fContainers; private IDialogSettings fDialogSettings; /** * Constructor for LoadpathContainerWizardPage. * @param containerPages */ protected LoadpathContainerSelectionPage(LoadpathContainerDescriptor[] containerPages) { super("LoadpathContainerWizardPage"); //$NON-NLS-1$ setTitle(NewWizardMessages.LoadpathContainerSelectionPage_title); setDescription(NewWizardMessages.LoadpathContainerSelectionPage_description); setImageDescriptor(RubyPluginImages.DESC_WIZBAN_ADD_LIBRARY); fContainers= containerPages; IDialogSettings settings= RubyPlugin.getDefault().getDialogSettings(); fDialogSettings= settings.getSection(DIALOGSTORE_SECTION); if (fDialogSettings == null) { fDialogSettings= settings.addNewSection(DIALOGSTORE_SECTION); fDialogSettings.put(DIALOGSTORE_CONTAINER_IDX, 0); } validatePage(); } /* (non-Javadoc) * @see IDialogPage#createControl(Composite) */ public void createControl(Composite parent) { fListViewer= new ListViewer(parent, SWT.SINGLE | SWT.BORDER); fListViewer.setLabelProvider(new LoadpathContainerLabelProvider()); fListViewer.setContentProvider(new ArrayContentProvider()); fListViewer.setSorter(new LoadpathContainerSorter()); fListViewer.setInput(Arrays.asList(fContainers)); fListViewer.addSelectionChangedListener(new ISelectionChangedListener() { public void selectionChanged(SelectionChangedEvent event) { validatePage(); } }); fListViewer.addDoubleClickListener(new IDoubleClickListener() { public void doubleClick(DoubleClickEvent event) { doDoubleClick(); } }); int selectionIndex= fDialogSettings.getInt(DIALOGSTORE_CONTAINER_IDX); if (selectionIndex >= fContainers.length) { selectionIndex= 0; } fListViewer.getList().select(selectionIndex); validatePage(); setControl(fListViewer.getList()); Dialog.applyDialogFont(fListViewer.getList()); // PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IRubyHelpContextIds.BP_SELECT_CLASSPATH_CONTAINER); } /** * Method validatePage. */ private void validatePage() { setPageComplete(getSelected() != null); } public LoadpathContainerDescriptor getSelected() { if (fListViewer != null) { ISelection selection= fListViewer.getSelection(); return (LoadpathContainerDescriptor) SelectionUtil.getSingleElement(selection); } return null; } public LoadpathContainerDescriptor[] getContainers() { return fContainers; } protected void doDoubleClick() { if (canFlipToNextPage()) { getContainer().showPage(getNextPage()); } } /* (non-Javadoc) * @see IWizardPage#canFlipToNextPage() */ public boolean canFlipToNextPage() { return isPageComplete(); // avoid the getNextPage call to prevent potential plugin load } /* (non-Javadoc) * @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean) */ public void setVisible(boolean visible) { if (!visible && fListViewer != null) { fDialogSettings.put(DIALOGSTORE_CONTAINER_IDX, fListViewer.getList().getSelectionIndex()); } super.setVisible(visible); } }