1 11 package org.eclipse.ui.internal.cheatsheets.dialogs; 12 13 14 import java.util.ArrayList ; 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 31 public class CheatSheetSelectionDialog extends SelectionDialog { 32 36 public class SimpleListContentProvider implements IStructuredContentProvider{ 37 38 private Object [] elements; 40 41 44 public SimpleListContentProvider() { 45 super(); 46 } 47 50 public void dispose() {} 51 55 public Object [] getElements(Object inputElement) { 56 return this.elements; 57 } 58 62 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {} 63 67 public void setElements(Object [] items) { 68 69 this.elements = items; 70 } 71 } 72 73 76 private final static int LIST_WIDTH = 60; 77 80 private final static int LIST_HEIGHT = 10; 81 84 private ListViewer listViewer; 85 86 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 list, CheatSheetCollectionElement cheatSheetsCollection) { 102 Object [] 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 [] 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 119 protected void configureShell(Shell newShell) { 120 super.configureShell(newShell); 121 } 123 124 127 protected Control createDialogArea(Composite parent) { 128 ArrayList list = new ArrayList (10); 129 CheatSheetCollectionElement cheatSheetsCollection = (CheatSheetCollectionElement)CheatSheetRegistryReader.getInstance().getCheatSheets(); 130 addCheatSheets(list, cheatSheetsCollection); 131 132 Composite composite = (Composite) super.createDialogArea(parent); 133 134 createMessageArea(composite); 136 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 listViewer.setLabelProvider(new LabelProvider() { 144 public String getText(Object element) { 145 return element == null ? ICheatSheetResource.EMPTY_STRING : ((CheatSheetElement) element).getLabel(null); 147 } 148 }); 149 150 SimpleListContentProvider cp = new SimpleListContentProvider(); 152 cp.setElements(list.toArray()); 153 listViewer.setContentProvider(cp); 154 listViewer.setInput(new Object ()); 156 if (getInitialElementSelections() != null) 158 listViewer.setSelection(new StructuredSelection(getInitialElementSelections()), true); 159 160 listViewer.addSelectionChangedListener(new ISelectionChangedListener() { 162 public void selectionChanged(SelectionChangedEvent event) { 163 getOkButton().setEnabled(!event.getSelection().isEmpty()); 165 } 166 }); 167 168 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 182 protected void okPressed() { 183 IStructuredSelection selection = (IStructuredSelection) listViewer.getSelection(); 184 setResult(selection.toList()); 185 186 super.okPressed(); 187 } 188 } 189 | Popular Tags |