001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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: * Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog font should be activated and used by other components.
011: *******************************************************************************/package org.eclipse.ui.dialogs;
012:
013: import org.eclipse.jface.viewers.DoubleClickEvent;
014: import org.eclipse.jface.viewers.IDoubleClickListener;
015: import org.eclipse.jface.viewers.ISelectionChangedListener;
016: import org.eclipse.jface.viewers.IStructuredSelection;
017: import org.eclipse.jface.viewers.LabelProvider;
018: import org.eclipse.jface.viewers.ListViewer;
019: import org.eclipse.jface.viewers.SelectionChangedEvent;
020: import org.eclipse.jface.viewers.StructuredSelection;
021: import org.eclipse.swt.SWT;
022: import org.eclipse.swt.layout.GridData;
023: import org.eclipse.swt.widgets.Composite;
024: import org.eclipse.swt.widgets.Control;
025: import org.eclipse.swt.widgets.Shell;
026: import org.eclipse.ui.IMarkerResolution;
027: import org.eclipse.ui.PlatformUI;
028: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
029: import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
030: import org.eclipse.ui.internal.ide.dialogs.SimpleListContentProvider;
031:
032: /**
033: * Dialog to allow the user to select from a list of marker
034: * resolutions.
035: * <p>
036: * This dialog may be instantiated, it is not intented to
037: * be subclassed.
038: * </p>
039: *
040: * @since 2.0
041: */
042: public class MarkerResolutionSelectionDialog extends SelectionDialog {
043: /**
044: * List width in characters.
045: */
046: private final static int LIST_WIDTH = 60;
047:
048: /**
049: * List height in characters.
050: */
051: private final static int LIST_HEIGHT = 10;
052:
053: /**
054: * The marker resolutions.
055: */
056: private IMarkerResolution[] resolutions;
057:
058: /**
059: * List to display the resolutions.
060: */
061: private ListViewer listViewer;
062:
063: /**
064: * Creates an instance of this dialog to display
065: * the given resolutions.
066: * <p>
067: * There must be at least one resolution.
068: * </p>
069: *
070: * @param shell the parent shell
071: * @param markerResolutions the resolutions to display
072: */
073: public MarkerResolutionSelectionDialog(Shell shell,
074: IMarkerResolution[] markerResolutions) {
075: super (shell);
076: if (markerResolutions == null || markerResolutions.length == 0) {
077: throw new IllegalArgumentException();
078: }
079: resolutions = markerResolutions;
080: setTitle(IDEWorkbenchMessages.MarkerResolutionSelectionDialog_title);
081: setMessage(IDEWorkbenchMessages.MarkerResolutionSelectionDialog_messageLabel);
082: setInitialSelections(new Object[] { markerResolutions[0] });
083: }
084:
085: /* (non-Javadoc)
086: * Method declared on Window.
087: */
088: protected void configureShell(Shell newShell) {
089: super .configureShell(newShell);
090: PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell,
091: IIDEHelpContextIds.MARKER_RESOLUTION_SELECTION_DIALOG);
092: }
093:
094: /* (non-Javadoc)
095: * Method declared on Dialog.
096: */
097: protected Control createDialogArea(Composite parent) {
098: Composite composite = (Composite) super
099: .createDialogArea(parent);
100:
101: // Create label
102: createMessageArea(composite);
103: // Create list viewer
104: listViewer = new ListViewer(composite, SWT.SINGLE
105: | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
106: GridData data = new GridData(GridData.FILL_BOTH);
107: data.heightHint = convertHeightInCharsToPixels(LIST_HEIGHT);
108: data.widthHint = convertWidthInCharsToPixels(LIST_WIDTH);
109: listViewer.getList().setLayoutData(data);
110: listViewer.getList().setFont(parent.getFont());
111: // Set the label provider
112: listViewer.setLabelProvider(new LabelProvider() {
113: public String getText(Object element) {
114: // Return the resolution's label.
115: return element == null ? "" : ((IMarkerResolution) element).getLabel(); //$NON-NLS-1$
116: }
117: });
118:
119: // Set the content provider
120: SimpleListContentProvider cp = new SimpleListContentProvider();
121: cp.setElements(resolutions);
122: listViewer.setContentProvider(cp);
123: listViewer.setInput(new Object()); // it is ignored but must be non-null
124:
125: // Set the initial selection
126: listViewer.setSelection(new StructuredSelection(
127: getInitialElementSelections()), true);
128:
129: // Add a selection change listener
130: listViewer
131: .addSelectionChangedListener(new ISelectionChangedListener() {
132: public void selectionChanged(
133: SelectionChangedEvent event) {
134: // Update OK button enablement
135: getOkButton().setEnabled(
136: !event.getSelection().isEmpty());
137: }
138: });
139:
140: // Add double-click listener
141: listViewer.addDoubleClickListener(new IDoubleClickListener() {
142: public void doubleClick(DoubleClickEvent event) {
143: okPressed();
144: }
145: });
146: return composite;
147: }
148:
149: /* (non-Javadoc)
150: * Method declared on Dialog.
151: */
152: protected void okPressed() {
153: IStructuredSelection selection = (IStructuredSelection) listViewer
154: .getSelection();
155: setResult(selection.toList());
156: super.okPressed();
157: }
158: }
|