KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > site > ArchiveSection


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 /*
12  * Created on Sep 29, 2003
13  */

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 /**
61  *
62  */

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 JavaDoc element) {
73             Object JavaDoc[] 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 JavaDoc[] getElements(Object JavaDoc 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 JavaDoc getColumnText(Object JavaDoc 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 ""; //$NON-NLS-1$
102
}
103         public Image getColumnImage(Object JavaDoc obj, int index) {
104             return null;
105         }
106     }
107     /**
108      * @param formPage
109      */

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     /*
120      * (non-Javadoc)
121      *
122      * @see org.eclipse.update.ui.forms.internal.FormSection#createClient(org.eclipse.swt.widgets.Composite,
123      * org.eclipse.update.ui.forms.internal.FormWidgetFactory)
124      */

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     /* (non-Javadoc)
148      * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
149      */

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 JavaDoc() {
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     /*
261      * (non-Javadoc)
262      *
263      * @see org.eclipse.update.ui.forms.internal.FormSection#doGlobalAction(java.lang.String)
264      */

265     public boolean doGlobalAction(String JavaDoc actionId) {
266         if (actionId.equals(ActionFactory.DELETE.getId())) {
267             BusyIndicator.showWhile(fTable.getDisplay(), new Runnable JavaDoc() {
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     /* (non-Javadoc)
308      * @see org.eclipse.ui.forms.AbstractFormPart#setFormInput(java.lang.Object)
309      */

310     public boolean setFormInput(Object JavaDoc 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