KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > dialogs > CheatSheetSelectionDialog


1 /*******************************************************************************
2  * Copyright (c) 2002, 2004 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  *******************************************************************************/

11 package org.eclipse.ui.internal.cheatsheets.dialogs;
12
13
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.jface.dialogs.Dialog;
17 import org.eclipse.jface.viewers.*;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.widgets.*;
21 import org.eclipse.ui.dialogs.SelectionDialog;
22 import org.eclipse.jface.viewers.Viewer;
23 import org.eclipse.jface.viewers.IStructuredContentProvider;
24
25 import org.eclipse.ui.internal.cheatsheets.*;
26 import org.eclipse.ui.internal.cheatsheets.registry.*;
27
28 /**
29  * Dialog to allow the user to select a cheat sheet from a list.
30  */

31 public class CheatSheetSelectionDialog extends SelectionDialog {
32     /**
33      * The SimpleListContentProvider is a class designed to return a static list of items
34      * when queried for use in simple list dialogs.
35      */

36     public class SimpleListContentProvider implements IStructuredContentProvider{
37
38         //The elements to display
39
private Object JavaDoc[] elements;
40
41         /**
42          * SimpleListContentProvider constructor comment.
43          */

44         public SimpleListContentProvider() {
45             super();
46         }
47         /**
48          * Do nothing when disposing,
49          */

50         public void dispose() {}
51         /**
52          * Returns the elements to display in the viewer. The inputElement is ignored for this
53          * provider.
54          */

55         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
56             return this.elements;
57         }
58         /**
59          * Required method from IStructuredContentProvider. The input is assumed to not change
60          * for the SimpleListContentViewer so do nothing here.
61          */

62         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {}
63         /**
64          * Set the elements to display.
65          * @param items Object[]
66          */

67         public void setElements(Object JavaDoc[] items) {
68     
69             this.elements = items;
70         }
71     }
72
73     /**
74      * List width in characters.
75      */

76     private final static int LIST_WIDTH = 60;
77     /**
78      * List height in characters.
79      */

80     private final static int LIST_HEIGHT = 10;
81     /**
82      * List to display the resolutions.
83      */

84     private ListViewer listViewer;
85
86     /**
87      * Creates an instance of this dialog to display
88      * the a list of cheat sheets.
89      *
90      * @param shell the parent shell
91      */

92     public CheatSheetSelectionDialog(Shell shell) {
93         super(shell);
94
95         setTitle(Messages.CHEAT_SHEET_SELECTION_DIALOG_TITLE);
96         setMessage(Messages.CHEAT_SHEET_SELECTION_DIALOG_MSG);
97         
98         setShellStyle(getShellStyle() | SWT.RESIZE);
99     }
100
101     private void addCheatSheets(ArrayList JavaDoc list, CheatSheetCollectionElement cheatSheetsCollection) {
102         Object JavaDoc[] cheatSheets = cheatSheetsCollection.getCheatSheets();
103         for (int i = 0; i < cheatSheets.length; i++) {
104             if (!list.contains(cheatSheets[i])) {
105                 list.add(cheatSheets[i]);
106             }
107         }
108
109         Object JavaDoc[] cheatSheetsFromCollection = cheatSheetsCollection.getChildren();
110         for (int nX = 0; nX < cheatSheetsFromCollection.length; nX++) {
111             CheatSheetCollectionElement collection = (CheatSheetCollectionElement) cheatSheetsFromCollection[nX];
112             addCheatSheets(list, collection);
113         }
114     }
115
116     /* (non-Javadoc)
117      * Method declared on Window.
118      */

119     protected void configureShell(Shell newShell) {
120         super.configureShell(newShell);
121         // WorkbenchHelp.setHelp(newShell, IHelpContextIds.WELCOME_PAGE_SELECTION_DIALOG);
122
}
123
124     /* (non-Javadoc)
125      * Method declared on Dialog.
126      */

127     protected Control createDialogArea(Composite parent) {
128         ArrayList JavaDoc list = new ArrayList JavaDoc(10);
129         CheatSheetCollectionElement cheatSheetsCollection = (CheatSheetCollectionElement)CheatSheetRegistryReader.getInstance().getCheatSheets();
130         addCheatSheets(list, cheatSheetsCollection);
131
132         Composite composite = (Composite) super.createDialogArea(parent);
133
134         // Create label
135
createMessageArea(composite);
136         // Create list viewer
137
listViewer = new ListViewer(composite, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
138         GridData data = new GridData(GridData.FILL_BOTH);
139         data.heightHint = convertHeightInCharsToPixels(LIST_HEIGHT);
140         data.widthHint = convertWidthInCharsToPixels(LIST_WIDTH);
141         listViewer.getList().setLayoutData(data);
142         // Set the label provider
143
listViewer.setLabelProvider(new LabelProvider() {
144             public String JavaDoc getText(Object JavaDoc element) {
145                 // Return the features's label.
146
return element == null ? ICheatSheetResource.EMPTY_STRING : ((CheatSheetElement) element).getLabel(null);
147             }
148         });
149
150         // Set the content provider
151
SimpleListContentProvider cp = new SimpleListContentProvider();
152         cp.setElements(list.toArray());
153         listViewer.setContentProvider(cp);
154         listViewer.setInput(new Object JavaDoc()); // it is ignored but must be non-null
155

156         // Set the initial selection
157
if (getInitialElementSelections() != null)
158             listViewer.setSelection(new StructuredSelection(getInitialElementSelections()), true);
159
160         // Add a selection change listener
161
listViewer.addSelectionChangedListener(new ISelectionChangedListener() {
162             public void selectionChanged(SelectionChangedEvent event) {
163                 // Update OK button enablement
164
getOkButton().setEnabled(!event.getSelection().isEmpty());
165             }
166         });
167
168         // Add double-click listener
169
listViewer.addDoubleClickListener(new IDoubleClickListener() {
170             public void doubleClick(DoubleClickEvent event) {
171                 okPressed();
172             }
173         });
174         
175         Dialog.applyDialogFont(composite);
176         return composite;
177     }
178
179     /* (non-Javadoc)
180      * Method declared on Dialog.
181      */

182     protected void okPressed() {
183         IStructuredSelection selection = (IStructuredSelection) listViewer.getSelection();
184         setResult(selection.toList());
185
186         super.okPressed();
187     }
188 }
189
Popular Tags