1 11 14 package org.eclipse.pde.internal.ui.editor.site; 15 import org.eclipse.core.resources.IFolder; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.jface.action.Action; 18 import org.eclipse.jface.action.IMenuListener; 19 import org.eclipse.jface.action.IMenuManager; 20 import org.eclipse.jface.action.MenuManager; 21 import org.eclipse.jface.action.Separator; 22 import org.eclipse.jface.viewers.ColumnWeightData; 23 import org.eclipse.jface.viewers.ISelection; 24 import org.eclipse.jface.viewers.ISelectionChangedListener; 25 import org.eclipse.jface.viewers.IStructuredContentProvider; 26 import org.eclipse.jface.viewers.IStructuredSelection; 27 import org.eclipse.jface.viewers.ITableLabelProvider; 28 import org.eclipse.jface.viewers.LabelProvider; 29 import org.eclipse.jface.viewers.SelectionChangedEvent; 30 import org.eclipse.jface.viewers.StructuredSelection; 31 import org.eclipse.jface.viewers.TableLayout; 32 import org.eclipse.jface.viewers.TableViewer; 33 import org.eclipse.pde.core.IModelChangedEvent; 34 import org.eclipse.pde.internal.core.isite.ISite; 35 import org.eclipse.pde.internal.core.isite.ISiteArchive; 36 import org.eclipse.pde.internal.core.isite.ISiteModel; 37 import org.eclipse.pde.internal.ui.PDEUIMessages; 38 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 39 import org.eclipse.pde.internal.ui.editor.PDEFormEditorContributor; 40 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 41 import org.eclipse.pde.internal.ui.editor.PDESection; 42 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider; 43 import org.eclipse.pde.internal.ui.util.SWTUtil; 44 import org.eclipse.swt.SWT; 45 import org.eclipse.swt.custom.BusyIndicator; 46 import org.eclipse.swt.events.SelectionAdapter; 47 import org.eclipse.swt.events.SelectionEvent; 48 import org.eclipse.swt.graphics.Image; 49 import org.eclipse.swt.layout.GridData; 50 import org.eclipse.swt.layout.GridLayout; 51 import org.eclipse.swt.widgets.Button; 52 import org.eclipse.swt.widgets.Composite; 53 import org.eclipse.swt.widgets.Control; 54 import org.eclipse.swt.widgets.Table; 55 import org.eclipse.swt.widgets.TableColumn; 56 import org.eclipse.ui.actions.ActionFactory; 57 import org.eclipse.ui.forms.widgets.FormToolkit; 58 import org.eclipse.ui.forms.widgets.Section; 59 import org.eclipse.ui.model.WorkbenchContentProvider; 60 63 public class ArchiveSection extends PDESection { 64 private Table fTable; 65 private TableViewer fViewer; 66 private ISiteModel fModel; 67 private Button fAddButton; 68 private Button fEditButton; 69 private Button fRemoveButton; 70 71 class FolderProvider extends WorkbenchContentProvider { 72 public boolean hasChildren(Object element) { 73 Object [] children = getChildren(element); 74 for (int i = 0; i < children.length; i++) { 75 if (children[i] instanceof IFolder) { 76 return true; 77 } 78 } 79 return false; 80 } 81 } 82 class ContentProvider extends DefaultContentProvider 83 implements 84 IStructuredContentProvider { 85 public Object [] getElements(Object parent) { 86 ISiteModel model = (ISiteModel) parent; 87 return model.getSite().getArchives(); 88 } 89 } 90 class ArchiveLabelProvider extends LabelProvider 91 implements 92 ITableLabelProvider { 93 public String getColumnText(Object obj, int index) { 94 ISiteArchive archive = (ISiteArchive) obj; 95 switch (index) { 96 case 0 : 97 return archive.getPath(); 98 case 1 : 99 return archive.getURL(); 100 } 101 return ""; } 103 public Image getColumnImage(Object obj, int index) { 104 return null; 105 } 106 } 107 110 public ArchiveSection(PDEFormPage formPage, Composite parent) { 111 super(formPage, parent, Section.DESCRIPTION); 112 getSection() 113 .setText( 114 PDEUIMessages.SiteEditor_ArchiveSection_header); 115 getSection().setDescription( 116 PDEUIMessages.SiteEditor_ArchiveSection_instruction); 117 createClient(getSection(), formPage.getManagedForm().getToolkit()); 118 } 119 125 public void createClient(Section section, FormToolkit toolkit) { 126 fModel = (ISiteModel) getPage().getModel(); 127 fModel.addModelChangedListener(this); 128 129 130 section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 131 Composite container = toolkit.createComposite(section); 132 container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2)); 133 container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 134 135 GridData data = new GridData(GridData.FILL_BOTH); 136 section.setLayoutData(data); 137 138 139 createTable(container, toolkit); 140 createTableViewer(); 141 createButtons(container, toolkit); 142 toolkit.paintBordersFor(container); 143 section.setClient(container); 144 initialize(); 145 } 146 147 150 public void dispose() { 151 fModel.removeModelChangedListener(this); 152 super.dispose(); 153 } 154 private void createButtons(Composite parent, FormToolkit toolkit) { 155 Composite container = toolkit.createComposite(parent); 156 GridLayout layout = new GridLayout(); 157 layout.marginHeight = 10; 158 container.setLayout(layout); 159 container 160 .setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING)); 161 fAddButton = toolkit.createButton(container, PDEUIMessages.SiteEditor_add, SWT.PUSH); 162 fAddButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 163 fAddButton.addSelectionListener(new SelectionAdapter() { 164 public void widgetSelected(SelectionEvent e) { 165 showDialog(null); 166 } 167 }); 168 fAddButton.setEnabled(isEditable()); 169 fEditButton = toolkit.createButton(container, PDEUIMessages.SiteEditor_edit, SWT.PUSH); 170 fEditButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 171 fEditButton.addSelectionListener(new SelectionAdapter() { 172 public void widgetSelected(SelectionEvent e) { 173 IStructuredSelection ssel = (IStructuredSelection) fViewer 174 .getSelection(); 175 if (ssel != null && ssel.size() == 1) 176 showDialog((ISiteArchive) ssel.getFirstElement()); 177 } 178 }); 179 fRemoveButton = toolkit.createButton(container, PDEUIMessages.SiteEditor_remove, SWT.PUSH); 180 fRemoveButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 181 fRemoveButton.addSelectionListener(new SelectionAdapter() { 182 public void widgetSelected(SelectionEvent e) { 183 handleDelete(); 184 } 185 }); 186 fRemoveButton.setEnabled(false); 187 fEditButton.setEnabled(false); 188 toolkit.paintBordersFor(container); 189 } 190 private void createTable(Composite container, FormToolkit toolkit) { 191 fTable = toolkit.createTable(container, SWT.FULL_SELECTION); 192 GridData gd = new GridData(GridData.FILL_BOTH); 193 gd.heightHint = 100; 194 fTable.setLayoutData(gd); 195 TableColumn col1 = new TableColumn(fTable, SWT.NULL); 196 col1.setText(PDEUIMessages.SiteEditor_ArchiveSection_col1); 197 TableColumn col2 = new TableColumn(fTable, SWT.NULL); 198 col2.setText(PDEUIMessages.SiteEditor_ArchiveSection_col2); 199 TableLayout tlayout = new TableLayout(); 200 tlayout.addColumnData(new ColumnWeightData(50, 200)); 201 tlayout.addColumnData(new ColumnWeightData(50, 200)); 202 fTable.setLayout(tlayout); 203 fTable.setHeaderVisible(true); 204 createContextMenu(fTable); 205 } 206 private void createTableViewer() { 207 fViewer = new TableViewer(fTable); 208 fViewer.setContentProvider(new ContentProvider()); 209 fViewer.setLabelProvider(new ArchiveLabelProvider()); 210 fViewer.setInput(getPage().getModel()); 211 fViewer.addSelectionChangedListener(new ISelectionChangedListener() { 212 public void selectionChanged(SelectionChangedEvent event) { 213 handleSelectionChanged(); 214 } 215 }); 216 } 217 private void handleSelectionChanged() { 218 ISelection selection = fViewer.getSelection(); 219 getManagedForm().fireSelectionChanged(this, selection); 220 getPage().getPDEEditor().setSelection(selection); 221 if (!isEditable()) { 222 return; 223 } 224 if (selection != null && selection instanceof IStructuredSelection) { 225 IStructuredSelection ssel = (IStructuredSelection) selection; 226 fRemoveButton.setEnabled(ssel.size() > 0); 227 fEditButton.setEnabled(ssel.size() == 1); 228 } else { 229 fRemoveButton.setEnabled(false); 230 fEditButton.setEnabled(false); 231 } 232 } 233 private void showDialog(final ISiteArchive archive) { 234 final ISiteModel model = (ISiteModel) getPage().getModel(); 235 BusyIndicator.showWhile(fTable.getDisplay(), new Runnable () { 236 public void run() { 237 NewArchiveDialog dialog = new NewArchiveDialog(fTable 238 .getShell(), model, archive); 239 dialog.create(); 240 SWTUtil.setDialogSize(dialog, 400, -1); 241 dialog.open(); 242 } 243 }); 244 } 245 private void handleDelete() { 246 try { 247 ISelection selection = fViewer.getSelection(); 248 if (selection != null && selection instanceof IStructuredSelection) { 249 IStructuredSelection ssel = (IStructuredSelection) selection; 250 if (ssel.size() > 0) { 251 ISiteArchive[] array = (ISiteArchive[]) ssel.toList() 252 .toArray(new ISiteArchive[ssel.size()]); 253 ISite site = ((ISiteModel) getPage().getModel()).getSite(); 254 site.removeArchives(array); 255 } 256 } 257 } catch (CoreException e) { 258 } 259 } 260 265 public boolean doGlobalAction(String actionId) { 266 if (actionId.equals(ActionFactory.DELETE.getId())) { 267 BusyIndicator.showWhile(fTable.getDisplay(), new Runnable () { 268 public void run() { 269 handleDelete(); 270 } 271 }); 272 return true; 273 } 274 return false; 275 } 276 public void refresh() { 277 fViewer.refresh(); 278 super.refresh(); 279 } 280 public void initialize() { 281 refresh(); 282 } 283 public void modelChanged(IModelChangedEvent e) { 284 markStale(); 285 } 286 private void createContextMenu(Control control) { 287 MenuManager popupMenuManager = new MenuManager(); 288 IMenuListener listener = new IMenuListener() { 289 public void menuAboutToShow(IMenuManager mng) { 290 Action removeAction = new Action(PDEUIMessages.SiteEditor_remove) { 291 public void run() { 292 doGlobalAction(ActionFactory.DELETE.getId()); 293 } 294 }; 295 removeAction.setEnabled(isEditable()); 296 mng.add(removeAction); 297 mng.add(new Separator()); 298 PDEFormEditorContributor contributor = getPage().getPDEEditor() 299 .getContributor(); 300 contributor.contextMenuAboutToShow(mng); 301 } 302 }; 303 popupMenuManager.addMenuListener(listener); 304 popupMenuManager.setRemoveAllWhenShown(true); 305 control.setMenu(popupMenuManager.createContextMenu(control)); 306 } 307 310 public boolean setFormInput(Object input) { 311 if (input instanceof ISiteArchive){ 312 fViewer.setSelection(new StructuredSelection(input), true); 313 return true; 314 } 315 return super.setFormInput(input); 316 } 317 } 318 | Popular Tags |