1 11 package org.eclipse.ant.internal.ui.preferences; 12 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import org.eclipse.ant.internal.core.AntObject; 18 import org.eclipse.jface.dialogs.IDialogConstants; 19 import org.eclipse.jface.viewers.ColumnLayoutData; 20 import org.eclipse.jface.viewers.ColumnWeightData; 21 import org.eclipse.jface.viewers.DoubleClickEvent; 22 import org.eclipse.jface.viewers.IDoubleClickListener; 23 import org.eclipse.jface.viewers.ISelectionChangedListener; 24 import org.eclipse.jface.viewers.IStructuredSelection; 25 import org.eclipse.jface.viewers.SelectionChangedEvent; 26 import org.eclipse.jface.viewers.TableLayout; 27 import org.eclipse.jface.viewers.TableViewer; 28 import org.eclipse.swt.SWT; 29 import org.eclipse.swt.events.KeyAdapter; 30 import org.eclipse.swt.events.KeyEvent; 31 import org.eclipse.swt.events.SelectionAdapter; 32 import org.eclipse.swt.events.SelectionEvent; 33 import org.eclipse.swt.layout.GridData; 34 import org.eclipse.swt.layout.GridLayout; 35 import org.eclipse.swt.widgets.Button; 36 import org.eclipse.swt.widgets.Composite; 37 import org.eclipse.swt.widgets.Shell; 38 import org.eclipse.swt.widgets.TabFolder; 39 import org.eclipse.swt.widgets.TabItem; 40 import org.eclipse.swt.widgets.Table; 41 import org.eclipse.swt.widgets.TableColumn; 42 import org.eclipse.ui.PlatformUI; 43 44 48 public abstract class AntPage { 49 50 protected static final int ADD_BUTTON = IDialogConstants.CLIENT_ID + 1; 51 protected static final int EDIT_BUTTON = IDialogConstants.CLIENT_ID + 2; 52 protected static final int REMOVE_BUTTON = IDialogConstants.CLIENT_ID + 3; 53 54 protected SelectionAdapter selectionAdapter = new SelectionAdapter() { 55 public void widgetSelected(SelectionEvent e) { 56 buttonPressed(((Integer ) e.widget.getData()).intValue()); 57 } 58 }; 59 private AntRuntimePreferencePage preferencePage; 60 private TableViewer tableViewer; 61 private AntContentProvider contentProvider; 62 63 protected Button editButton; 64 protected Button removeButton; 65 66 private final String [] fTableColumnHeaders= { 67 AntPreferencesMessages.AntPage_0, AntPreferencesMessages.AntPage_1, AntPreferencesMessages.AntPage_2, AntPreferencesMessages.AntPage_3 68 }; 69 private final ColumnLayoutData[] fTableColumnLayouts= { 70 new ColumnWeightData(40), 71 new ColumnWeightData(20), 72 new ColumnWeightData(20), 73 new ColumnWeightData(20) 74 }; 75 76 79 public AntPage(AntRuntimePreferencePage preferencePage) { 80 super(); 81 this.preferencePage = preferencePage; 82 } 83 84 87 protected abstract void addButtonsToButtonGroup(Composite parent); 88 89 92 protected abstract void initialize(); 93 94 97 protected void addContent(Object o) { 98 if (contentProvider != null) { 99 contentProvider.add(o); 100 } 101 } 102 103 106 private void buttonPressed(int buttonId) { 107 switch (buttonId) { 108 case ADD_BUTTON : 109 add(); 110 break; 111 case EDIT_BUTTON : 112 edit(getSelection()); 113 break; 114 case REMOVE_BUTTON : 115 remove(); 116 break; 117 } 118 } 119 120 128 protected Button createPushButton(Composite parent, String buttonText, int buttonId) { 129 Button button = new Button(parent, SWT.PUSH); 130 button.setFont(parent.getFont()); 131 button.setText(buttonText); 132 button.setData(new Integer (buttonId)); 133 button.addSelectionListener(selectionAdapter); 134 preferencePage.setButtonLayoutData(button); 135 return button; 136 } 137 138 141 protected void createButtonGroup(Composite top) { 142 Composite buttonGroup = new Composite(top, SWT.NONE); 143 GridLayout layout = new GridLayout(); 144 layout.marginHeight = 0; 145 layout.marginWidth = 0; 146 buttonGroup.setLayout(layout); 147 buttonGroup.setLayoutData(new GridData(GridData.FILL_VERTICAL)); 148 buttonGroup.setFont(top.getFont()); 149 150 addButtonsToButtonGroup(buttonGroup); 151 } 152 153 156 protected void createTable(Composite parent) { 157 Table table = new Table(parent, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER); 158 GridData data= new GridData(GridData.FILL_BOTH); 159 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; 160 data.heightHint = table.getItemHeight(); 161 data.horizontalSpan= 1; 162 table.setLayoutData(data); 163 table.setFont(parent.getFont()); 164 165 TableLayout tableLayout = new TableLayout(); 166 table.setLayout(tableLayout); 167 table.setHeaderVisible(true); 168 table.setLinesVisible(true); 169 170 for (int i = 0; i < fTableColumnHeaders.length; i++) { 172 tableLayout.addColumnData(fTableColumnLayouts[i]); 173 TableColumn column = new TableColumn(table, SWT.NONE, i); 174 column.setResizable(fTableColumnLayouts[i].resizable); 175 column.setText(fTableColumnHeaders[i]); 176 } 177 178 contentProvider = getContentProvider(); 179 tableViewer = new TableViewer(table); 180 tableViewer.setContentProvider(contentProvider); 181 tableViewer.setLabelProvider(new AntObjectLabelProvider()); 182 tableViewer.addSelectionChangedListener(new ISelectionChangedListener() { 183 public void selectionChanged(SelectionChangedEvent event) { 184 tableSelectionChanged((IStructuredSelection) event.getSelection()); 185 } 186 }); 187 188 tableViewer.addDoubleClickListener(new IDoubleClickListener() { 189 public void doubleClick(DoubleClickEvent event) { 190 if (!event.getSelection().isEmpty() && editButton.isEnabled()) { 191 edit((IStructuredSelection)event.getSelection()); 192 } 193 } 194 }); 195 196 table.addKeyListener(new KeyAdapter() { 197 public void keyPressed(KeyEvent event) { 198 if (editButton.isEnabled() && event.character == SWT.DEL && event.stateMask == 0) { 199 remove(tableViewer); 200 } 201 } 202 }); 203 } 204 205 210 protected AntContentProvider getContentProvider() { 211 return new AntContentProvider(); 212 } 213 214 223 protected List getContents(boolean forDisplay) { 224 if (tableViewer == null || tableViewer.getControl().isDisposed()) { 225 return null; 226 } 227 List entries= getPreferencePage().getLibraryEntries(); 228 229 Object [] elements = contentProvider.getElements(tableViewer.getInput()); 230 List contents= new ArrayList (elements.length); 231 Object element; 232 AntObject antObject; 233 for (int i = 0; i < elements.length; i++) { 234 element= elements[i]; 235 if (element instanceof AntObject) { 236 antObject= (AntObject)element; 237 if (forDisplay) { 238 if (!antObject.isDefault() && !entries.contains(antObject.getLibraryEntry())) { 239 continue; 240 } 241 } else if (antObject.isDefault() || !entries.contains(antObject.getLibraryEntry())) { 242 continue; 243 } 244 } 245 contents.add(element); 246 } 247 return contents; 248 } 249 250 253 protected final IStructuredSelection getSelection() { 254 if (tableViewer == null || tableViewer.getControl().isDisposed()) { 255 return null; 256 } 257 return ((IStructuredSelection) tableViewer.getSelection()); 258 } 259 260 263 protected final Shell getShell() { 264 if (tableViewer == null || tableViewer.getControl().isDisposed()) { 265 return null; 266 } 267 return tableViewer.getControl().getShell(); 268 } 269 270 273 protected void remove() { 274 remove(tableViewer); 275 } 276 277 protected void remove(TableViewer viewer) { 278 AntContentProvider antContentProvider= (AntContentProvider)viewer.getContentProvider(); 279 IStructuredSelection sel = (IStructuredSelection) viewer.getSelection(); 280 Iterator itr = sel.iterator(); 281 while (itr.hasNext()) { 282 antContentProvider.remove(itr.next()); 283 } 284 } 285 286 290 protected void setInput(List inputs) { 291 if (tableViewer == null || tableViewer.getControl().isDisposed()) { 292 return; 293 } 294 tableViewer.setInput(inputs); 295 tableSelectionChanged((IStructuredSelection) tableViewer.getSelection()); 296 } 297 298 301 protected final void updateContent(Object element) { 302 if (tableViewer == null || tableViewer.getControl().isDisposed()) { 303 return; 304 } 305 tableViewer.update(element, null); 306 } 307 308 311 protected Composite createContents(Composite top) { 312 PlatformUI.getWorkbench().getHelpSystem().setHelp(top, getHelpContextId()); 313 GridLayout layout = new GridLayout(); 314 layout.numColumns = 2; 315 layout.marginHeight = 2; 316 layout.marginWidth = 2; 317 top.setLayout(layout); 318 319 top.setLayoutData(new GridData(GridData.FILL_BOTH)); 320 321 createTable(top); 322 createButtonGroup(top); 323 324 return top; 325 } 326 327 protected AntRuntimePreferencePage getPreferencePage() { 328 return preferencePage; 329 } 330 331 protected TableViewer getTableViewer() { 332 return tableViewer; 333 } 334 335 338 protected void tableSelectionChanged(IStructuredSelection newSelection) { 339 int size = newSelection.size(); 340 boolean enabled= true; 341 342 Iterator itr= newSelection.iterator(); 343 while (itr.hasNext()) { 344 Object element = itr.next(); 345 if (element instanceof AntObject) { 346 AntObject antObject= (AntObject)element; 347 if (antObject.isDefault()) { 348 enabled= false; 349 break; 350 } 351 } 352 } 353 editButton.setEnabled(enabled && size == 1); 354 removeButton.setEnabled(enabled && size > 0); 355 } 356 357 362 protected abstract void edit(IStructuredSelection selection); 363 364 368 protected abstract void add(); 369 370 376 protected abstract String getHelpContextId(); 377 378 protected void connectToFolder(final TabItem item, TabFolder folder) { 379 folder.addSelectionListener(new SelectionAdapter() { 380 public void widgetSelected(SelectionEvent e) { 381 if (e.item == item) { 382 setInput(getContents(true)); 384 } 385 } 386 }); 387 } 388 } 389 | Popular Tags |