001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.site;
011:
012: import org.eclipse.core.resources.IFolder;
013: import org.eclipse.core.runtime.CoreException;
014: import org.eclipse.jface.action.Action;
015: import org.eclipse.jface.action.IMenuListener;
016: import org.eclipse.jface.action.IMenuManager;
017: import org.eclipse.jface.action.MenuManager;
018: import org.eclipse.jface.action.Separator;
019: import org.eclipse.jface.viewers.ColumnWeightData;
020: import org.eclipse.jface.viewers.ISelection;
021: import org.eclipse.jface.viewers.ISelectionChangedListener;
022: import org.eclipse.jface.viewers.IStructuredContentProvider;
023: import org.eclipse.jface.viewers.IStructuredSelection;
024: import org.eclipse.jface.viewers.ITableLabelProvider;
025: import org.eclipse.jface.viewers.LabelProvider;
026: import org.eclipse.jface.viewers.SelectionChangedEvent;
027: import org.eclipse.jface.viewers.StructuredSelection;
028: import org.eclipse.jface.viewers.TableLayout;
029: import org.eclipse.jface.viewers.TableViewer;
030: import org.eclipse.pde.core.IModelChangedEvent;
031: import org.eclipse.pde.internal.core.isite.ISite;
032: import org.eclipse.pde.internal.core.isite.ISiteArchive;
033: import org.eclipse.pde.internal.core.isite.ISiteModel;
034: import org.eclipse.pde.internal.ui.PDEUIMessages;
035: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
036: import org.eclipse.pde.internal.ui.editor.PDEFormEditorContributor;
037: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
038: import org.eclipse.pde.internal.ui.editor.PDESection;
039: import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
040: import org.eclipse.pde.internal.ui.util.SWTUtil;
041: import org.eclipse.swt.SWT;
042: import org.eclipse.swt.custom.BusyIndicator;
043: import org.eclipse.swt.events.SelectionAdapter;
044: import org.eclipse.swt.events.SelectionEvent;
045: import org.eclipse.swt.graphics.Image;
046: import org.eclipse.swt.layout.GridData;
047: import org.eclipse.swt.layout.GridLayout;
048: import org.eclipse.swt.widgets.Button;
049: import org.eclipse.swt.widgets.Composite;
050: import org.eclipse.swt.widgets.Control;
051: import org.eclipse.swt.widgets.Table;
052: import org.eclipse.swt.widgets.TableColumn;
053: import org.eclipse.ui.actions.ActionFactory;
054: import org.eclipse.ui.forms.widgets.FormToolkit;
055: import org.eclipse.ui.forms.widgets.Section;
056: import org.eclipse.ui.model.WorkbenchContentProvider;
057:
058: /**
059: *
060: */
061: public class ArchiveSection extends PDESection {
062: private Table fTable;
063: private TableViewer fViewer;
064: private ISiteModel fModel;
065: private Button fAddButton;
066: private Button fEditButton;
067: private Button fRemoveButton;
068:
069: class FolderProvider extends WorkbenchContentProvider {
070: public boolean hasChildren(Object element) {
071: Object[] children = getChildren(element);
072: for (int i = 0; i < children.length; i++) {
073: if (children[i] instanceof IFolder) {
074: return true;
075: }
076: }
077: return false;
078: }
079: }
080:
081: class ContentProvider extends DefaultContentProvider implements
082: IStructuredContentProvider {
083: public Object[] getElements(Object parent) {
084: ISiteModel model = (ISiteModel) parent;
085: return model.getSite().getArchives();
086: }
087: }
088:
089: class ArchiveLabelProvider extends LabelProvider implements
090: ITableLabelProvider {
091: public String getColumnText(Object obj, int index) {
092: ISiteArchive archive = (ISiteArchive) obj;
093: switch (index) {
094: case 0:
095: return archive.getPath();
096: case 1:
097: return archive.getURL();
098: }
099: return ""; //$NON-NLS-1$
100: }
101:
102: public Image getColumnImage(Object obj, int index) {
103: return null;
104: }
105: }
106:
107: /**
108: * @param formPage
109: */
110: public ArchiveSection(PDEFormPage formPage, Composite parent) {
111: super (formPage, parent, Section.DESCRIPTION);
112: getSection().setText(
113: PDEUIMessages.SiteEditor_ArchiveSection_header);
114: getSection().setDescription(
115: PDEUIMessages.SiteEditor_ArchiveSection_instruction);
116: createClient(getSection(), formPage.getManagedForm()
117: .getToolkit());
118: }
119:
120: /*
121: * (non-Javadoc)
122: *
123: * @see org.eclipse.update.ui.forms.internal.FormSection#createClient(org.eclipse.swt.widgets.Composite,
124: * org.eclipse.update.ui.forms.internal.FormWidgetFactory)
125: */
126: public void createClient(Section section, FormToolkit toolkit) {
127: fModel = (ISiteModel) getPage().getModel();
128: fModel.addModelChangedListener(this );
129:
130: section.setLayout(FormLayoutFactory.createClearGridLayout(
131: false, 1));
132: Composite container = toolkit.createComposite(section);
133: container.setLayout(FormLayoutFactory
134: .createSectionClientGridLayout(false, 2));
135: container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
136:
137: GridData data = new GridData(GridData.FILL_BOTH);
138: section.setLayoutData(data);
139:
140: createTable(container, toolkit);
141: createTableViewer();
142: createButtons(container, toolkit);
143: toolkit.paintBordersFor(container);
144: section.setClient(container);
145: initialize();
146: }
147:
148: /* (non-Javadoc)
149: * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
150: */
151: public void dispose() {
152: fModel.removeModelChangedListener(this );
153: super .dispose();
154: }
155:
156: private void createButtons(Composite parent, FormToolkit toolkit) {
157: Composite container = toolkit.createComposite(parent);
158: GridLayout layout = new GridLayout();
159: layout.marginHeight = 10;
160: container.setLayout(layout);
161: container.setLayoutData(new GridData(
162: GridData.VERTICAL_ALIGN_BEGINNING));
163: fAddButton = toolkit.createButton(container,
164: PDEUIMessages.SiteEditor_add, SWT.PUSH);
165: fAddButton
166: .setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
167: fAddButton.addSelectionListener(new SelectionAdapter() {
168: public void widgetSelected(SelectionEvent e) {
169: showDialog(null);
170: }
171: });
172: fAddButton.setEnabled(isEditable());
173: fEditButton = toolkit.createButton(container,
174: PDEUIMessages.SiteEditor_edit, SWT.PUSH);
175: fEditButton
176: .setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
177: fEditButton.addSelectionListener(new SelectionAdapter() {
178: public void widgetSelected(SelectionEvent e) {
179: IStructuredSelection ssel = (IStructuredSelection) fViewer
180: .getSelection();
181: if (ssel != null && ssel.size() == 1)
182: showDialog((ISiteArchive) ssel.getFirstElement());
183: }
184: });
185: fRemoveButton = toolkit.createButton(container,
186: PDEUIMessages.SiteEditor_remove, SWT.PUSH);
187: fRemoveButton.setLayoutData(new GridData(
188: GridData.FILL_HORIZONTAL));
189: fRemoveButton.addSelectionListener(new SelectionAdapter() {
190: public void widgetSelected(SelectionEvent e) {
191: handleDelete();
192: }
193: });
194: fRemoveButton.setEnabled(false);
195: fEditButton.setEnabled(false);
196: toolkit.paintBordersFor(container);
197: }
198:
199: private void createTable(Composite container, FormToolkit toolkit) {
200: fTable = toolkit.createTable(container, SWT.FULL_SELECTION);
201: GridData gd = new GridData(GridData.FILL_BOTH);
202: gd.heightHint = 100;
203: fTable.setLayoutData(gd);
204: TableColumn col1 = new TableColumn(fTable, SWT.NULL);
205: col1.setText(PDEUIMessages.SiteEditor_ArchiveSection_col1);
206: TableColumn col2 = new TableColumn(fTable, SWT.NULL);
207: col2.setText(PDEUIMessages.SiteEditor_ArchiveSection_col2);
208: TableLayout tlayout = new TableLayout();
209: tlayout.addColumnData(new ColumnWeightData(50, 200));
210: tlayout.addColumnData(new ColumnWeightData(50, 200));
211: fTable.setLayout(tlayout);
212: fTable.setHeaderVisible(true);
213: createContextMenu(fTable);
214: }
215:
216: private void createTableViewer() {
217: fViewer = new TableViewer(fTable);
218: fViewer.setContentProvider(new ContentProvider());
219: fViewer.setLabelProvider(new ArchiveLabelProvider());
220: fViewer.setInput(getPage().getModel());
221: fViewer
222: .addSelectionChangedListener(new ISelectionChangedListener() {
223: public void selectionChanged(
224: SelectionChangedEvent event) {
225: handleSelectionChanged();
226: }
227: });
228: }
229:
230: private void handleSelectionChanged() {
231: ISelection selection = fViewer.getSelection();
232: getManagedForm().fireSelectionChanged(this , selection);
233: getPage().getPDEEditor().setSelection(selection);
234: if (!isEditable()) {
235: return;
236: }
237: if (selection != null
238: && selection instanceof IStructuredSelection) {
239: IStructuredSelection ssel = (IStructuredSelection) selection;
240: fRemoveButton.setEnabled(ssel.size() > 0);
241: fEditButton.setEnabled(ssel.size() == 1);
242: } else {
243: fRemoveButton.setEnabled(false);
244: fEditButton.setEnabled(false);
245: }
246: }
247:
248: private void showDialog(final ISiteArchive archive) {
249: final ISiteModel model = (ISiteModel) getPage().getModel();
250: BusyIndicator.showWhile(fTable.getDisplay(), new Runnable() {
251: public void run() {
252: NewArchiveDialog dialog = new NewArchiveDialog(fTable
253: .getShell(), model, archive);
254: dialog.create();
255: SWTUtil.setDialogSize(dialog, 400, -1);
256: dialog.open();
257: }
258: });
259: }
260:
261: private void handleDelete() {
262: try {
263: ISelection selection = fViewer.getSelection();
264: if (selection != null
265: && selection instanceof IStructuredSelection) {
266: IStructuredSelection ssel = (IStructuredSelection) selection;
267: if (ssel.size() > 0) {
268: ISiteArchive[] array = (ISiteArchive[]) ssel
269: .toList().toArray(
270: new ISiteArchive[ssel.size()]);
271: ISite site = ((ISiteModel) getPage().getModel())
272: .getSite();
273: site.removeArchives(array);
274: }
275: }
276: } catch (CoreException e) {
277: }
278: }
279:
280: /*
281: * (non-Javadoc)
282: *
283: * @see org.eclipse.update.ui.forms.internal.FormSection#doGlobalAction(java.lang.String)
284: */
285: public boolean doGlobalAction(String actionId) {
286: if (actionId.equals(ActionFactory.DELETE.getId())) {
287: BusyIndicator.showWhile(fTable.getDisplay(),
288: new Runnable() {
289: public void run() {
290: handleDelete();
291: }
292: });
293: return true;
294: }
295: return false;
296: }
297:
298: public void refresh() {
299: fViewer.refresh();
300: super .refresh();
301: }
302:
303: public void initialize() {
304: refresh();
305: }
306:
307: public void modelChanged(IModelChangedEvent e) {
308: markStale();
309: }
310:
311: private void createContextMenu(Control control) {
312: MenuManager popupMenuManager = new MenuManager();
313: IMenuListener listener = new IMenuListener() {
314: public void menuAboutToShow(IMenuManager mng) {
315: Action removeAction = new Action(
316: PDEUIMessages.SiteEditor_remove) {
317: public void run() {
318: doGlobalAction(ActionFactory.DELETE.getId());
319: }
320: };
321: removeAction.setEnabled(isEditable());
322: mng.add(removeAction);
323: mng.add(new Separator());
324: PDEFormEditorContributor contributor = getPage()
325: .getPDEEditor().getContributor();
326: contributor.contextMenuAboutToShow(mng);
327: }
328: };
329: popupMenuManager.addMenuListener(listener);
330: popupMenuManager.setRemoveAllWhenShown(true);
331: control.setMenu(popupMenuManager.createContextMenu(control));
332: }
333:
334: /* (non-Javadoc)
335: * @see org.eclipse.ui.forms.AbstractFormPart#setFormInput(java.lang.Object)
336: */
337: public boolean setFormInput(Object input) {
338: if (input instanceof ISiteArchive) {
339: fViewer.setSelection(new StructuredSelection(input), true);
340: return true;
341: }
342: return super.setFormInput(input);
343: }
344: }
|