001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.wizards.buildpaths;
011:
012: import java.util.Arrays;
013:
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.widgets.Composite;
016:
017: import org.eclipse.jface.dialogs.Dialog;
018: import org.eclipse.jface.dialogs.IDialogSettings;
019: import org.eclipse.jface.viewers.ArrayContentProvider;
020: import org.eclipse.jface.viewers.DoubleClickEvent;
021: import org.eclipse.jface.viewers.IDoubleClickListener;
022: import org.eclipse.jface.viewers.ISelection;
023: import org.eclipse.jface.viewers.ISelectionChangedListener;
024: import org.eclipse.jface.viewers.LabelProvider;
025: import org.eclipse.jface.viewers.ListViewer;
026: import org.eclipse.jface.viewers.SelectionChangedEvent;
027: import org.eclipse.jface.viewers.ViewerComparator;
028: import org.eclipse.jface.wizard.WizardPage;
029:
030: import org.eclipse.ui.PlatformUI;
031:
032: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
033: import org.eclipse.jdt.internal.ui.JavaPlugin;
034: import org.eclipse.jdt.internal.ui.JavaPluginImages;
035: import org.eclipse.jdt.internal.ui.util.SelectionUtil;
036: import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
037:
038: /**
039: */
040: public class ClasspathContainerSelectionPage extends WizardPage {
041:
042: private static final String DIALOGSTORE_SECTION = "ClasspathContainerSelectionPage"; //$NON-NLS-1$
043: private static final String DIALOGSTORE_CONTAINER_IDX = "index"; //$NON-NLS-1$
044:
045: private static class ClasspathContainerLabelProvider extends
046: LabelProvider {
047: public String getText(Object element) {
048: return ((ClasspathContainerDescriptor) element).getName();
049: }
050: }
051:
052: private ListViewer fListViewer;
053: private ClasspathContainerDescriptor[] fContainers;
054: private IDialogSettings fDialogSettings;
055:
056: /**
057: * Constructor for ClasspathContainerWizardPage.
058: * @param containerPages
059: */
060: protected ClasspathContainerSelectionPage(
061: ClasspathContainerDescriptor[] containerPages) {
062: super ("ClasspathContainerWizardPage"); //$NON-NLS-1$
063: setTitle(NewWizardMessages.ClasspathContainerSelectionPage_title);
064: setDescription(NewWizardMessages.ClasspathContainerSelectionPage_description);
065: setImageDescriptor(JavaPluginImages.DESC_WIZBAN_ADD_LIBRARY);
066:
067: fContainers = containerPages;
068:
069: IDialogSettings settings = JavaPlugin.getDefault()
070: .getDialogSettings();
071: fDialogSettings = settings.getSection(DIALOGSTORE_SECTION);
072: if (fDialogSettings == null) {
073: fDialogSettings = settings
074: .addNewSection(DIALOGSTORE_SECTION);
075: fDialogSettings.put(DIALOGSTORE_CONTAINER_IDX, 0);
076: }
077: validatePage();
078: }
079:
080: /* (non-Javadoc)
081: * @see IDialogPage#createControl(Composite)
082: */
083: public void createControl(Composite parent) {
084: fListViewer = new ListViewer(parent, SWT.SINGLE | SWT.BORDER);
085: fListViewer
086: .setLabelProvider(new ClasspathContainerLabelProvider());
087: fListViewer.setContentProvider(new ArrayContentProvider());
088: fListViewer.setComparator(new ViewerComparator());
089: fListViewer.setInput(Arrays.asList(fContainers));
090: fListViewer
091: .addSelectionChangedListener(new ISelectionChangedListener() {
092: public void selectionChanged(
093: SelectionChangedEvent event) {
094: validatePage();
095: }
096: });
097: fListViewer.addDoubleClickListener(new IDoubleClickListener() {
098: public void doubleClick(DoubleClickEvent event) {
099: doDoubleClick();
100: }
101: });
102:
103: int selectionIndex = fDialogSettings
104: .getInt(DIALOGSTORE_CONTAINER_IDX);
105: if (selectionIndex >= fContainers.length) {
106: selectionIndex = 0;
107: }
108: fListViewer.getList().select(selectionIndex);
109: validatePage();
110: setControl(fListViewer.getList());
111: Dialog.applyDialogFont(fListViewer.getList());
112:
113: PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
114: IJavaHelpContextIds.BP_SELECT_CLASSPATH_CONTAINER);
115: }
116:
117: /**
118: * Method validatePage.
119: */
120: private void validatePage() {
121: setPageComplete(getSelected() != null);
122: }
123:
124: public ClasspathContainerDescriptor getSelected() {
125: if (fListViewer != null) {
126: ISelection selection = fListViewer.getSelection();
127: return (ClasspathContainerDescriptor) SelectionUtil
128: .getSingleElement(selection);
129: }
130: return null;
131: }
132:
133: public ClasspathContainerDescriptor[] getContainers() {
134: return fContainers;
135: }
136:
137: protected void doDoubleClick() {
138: if (canFlipToNextPage()) {
139: getContainer().showPage(getNextPage());
140: }
141: }
142:
143: /* (non-Javadoc)
144: * @see IWizardPage#canFlipToNextPage()
145: */
146: public boolean canFlipToNextPage() {
147: return isPageComplete(); // avoid the getNextPage call to prevent potential plugin load
148: }
149:
150: /* (non-Javadoc)
151: * @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean)
152: */
153: public void setVisible(boolean visible) {
154: if (!visible && fListViewer != null) {
155: fDialogSettings.put(DIALOGSTORE_CONTAINER_IDX, fListViewer
156: .getList().getSelectionIndex());
157: }
158: super.setVisible(visible);
159: }
160:
161: }
|