KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > dialogs > MarkerResolutionSelectionDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog font should be activated and used by other components.
11  *******************************************************************************/

12
13 package org.eclipse.ui.dialogs;
14
15 import org.eclipse.jface.viewers.DoubleClickEvent;
16 import org.eclipse.jface.viewers.IDoubleClickListener;
17 import org.eclipse.jface.viewers.ISelectionChangedListener;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.viewers.LabelProvider;
20 import org.eclipse.jface.viewers.ListViewer;
21 import org.eclipse.jface.viewers.SelectionChangedEvent;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.ui.IMarkerResolution;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
31 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
32 import org.eclipse.ui.internal.ide.dialogs.SimpleListContentProvider;
33
34 /**
35  * Dialog to allow the user to select from a list of marker
36  * resolutions.
37  * <p>
38  * This dialog may be instantiated, it is not intented to
39  * be subclassed.
40  * </p>
41  *
42  * @since 2.0
43  */

44 public class MarkerResolutionSelectionDialog extends SelectionDialog {
45     /**
46      * List width in characters.
47      */

48     private final static int LIST_WIDTH = 60;
49
50     /**
51      * List height in characters.
52      */

53     private final static int LIST_HEIGHT = 10;
54
55     /**
56      * The marker resolutions.
57      */

58     private IMarkerResolution[] resolutions;
59
60     /**
61      * List to display the resolutions.
62      */

63     private ListViewer listViewer;
64
65     /**
66      * Creates an instance of this dialog to display
67      * the given resolutions.
68      * <p>
69      * There must be at least one resolution.
70      * </p>
71      *
72      * @param shell the parent shell
73      * @param markerResolutions the resolutions to display
74      */

75     public MarkerResolutionSelectionDialog(Shell shell,
76             IMarkerResolution[] markerResolutions) {
77         super(shell);
78         if (markerResolutions == null || markerResolutions.length == 0) {
79             throw new IllegalArgumentException JavaDoc();
80         }
81         resolutions = markerResolutions;
82         setTitle(IDEWorkbenchMessages.MarkerResolutionSelectionDialog_title);
83         setMessage(IDEWorkbenchMessages.MarkerResolutionSelectionDialog_messageLabel);
84         setInitialSelections(new Object JavaDoc[] { markerResolutions[0] });
85     }
86
87     /* (non-Javadoc)
88      * Method declared on Window.
89      */

90     protected void configureShell(Shell newShell) {
91         super.configureShell(newShell);
92         PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell,
93                 IIDEHelpContextIds.MARKER_RESOLUTION_SELECTION_DIALOG);
94     }
95
96     /* (non-Javadoc)
97      * Method declared on Dialog.
98      */

99     protected Control createDialogArea(Composite parent) {
100         Composite composite = (Composite) super.createDialogArea(parent);
101
102         // Create label
103
createMessageArea(composite);
104         // Create list viewer
105
listViewer = new ListViewer(composite, SWT.SINGLE | SWT.H_SCROLL
106                 | SWT.V_SCROLL | SWT.BORDER);
107         GridData data = new GridData(GridData.FILL_BOTH);
108         data.heightHint = convertHeightInCharsToPixels(LIST_HEIGHT);
109         data.widthHint = convertWidthInCharsToPixels(LIST_WIDTH);
110         listViewer.getList().setLayoutData(data);
111         listViewer.getList().setFont(parent.getFont());
112         // Set the label provider
113
listViewer.setLabelProvider(new LabelProvider() {
114             public String JavaDoc getText(Object JavaDoc element) {
115                 // Return the resolution's label.
116
return element == null ? "" : ((IMarkerResolution) element).getLabel(); //$NON-NLS-1$
117
}
118         });
119
120         // Set the content provider
121
SimpleListContentProvider cp = new SimpleListContentProvider();
122         cp.setElements(resolutions);
123         listViewer.setContentProvider(cp);
124         listViewer.setInput(new Object JavaDoc()); // it is ignored but must be non-null
125

126         // Set the initial selection
127
listViewer.setSelection(new StructuredSelection(
128                 getInitialElementSelections()), true);
129
130         // Add a selection change listener
131
listViewer.addSelectionChangedListener(new ISelectionChangedListener() {
132             public void selectionChanged(SelectionChangedEvent event) {
133                 // Update OK button enablement
134
getOkButton().setEnabled(!event.getSelection().isEmpty());
135             }
136         });
137
138         // Add double-click listener
139
listViewer.addDoubleClickListener(new IDoubleClickListener() {
140             public void doubleClick(DoubleClickEvent event) {
141                 okPressed();
142             }
143         });
144         return composite;
145     }
146
147     /* (non-Javadoc)
148      * Method declared on Dialog.
149      */

150     protected void okPressed() {
151         IStructuredSelection selection = (IStructuredSelection) listViewer
152                 .getSelection();
153         setResult(selection.toList());
154         super.okPressed();
155     }
156 }
157
Popular Tags