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.plugin;
011:
012: import java.util.Vector;
013:
014: import org.eclipse.core.resources.IProject;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.jdt.core.IPackageFragment;
017: import org.eclipse.jdt.core.JavaCore;
018: import org.eclipse.jdt.ui.ISharedImages;
019: import org.eclipse.jdt.ui.JavaElementLabelProvider;
020: import org.eclipse.jdt.ui.JavaUI;
021: import org.eclipse.jface.action.Action;
022: import org.eclipse.jface.action.IMenuManager;
023: import org.eclipse.jface.action.Separator;
024: import org.eclipse.jface.viewers.ILabelProvider;
025: import org.eclipse.jface.viewers.ISelection;
026: import org.eclipse.jface.viewers.IStructuredContentProvider;
027: import org.eclipse.jface.viewers.IStructuredSelection;
028: import org.eclipse.jface.viewers.ITableLabelProvider;
029: import org.eclipse.jface.viewers.LabelProvider;
030: import org.eclipse.jface.viewers.TableViewer;
031: import org.eclipse.jface.viewers.ViewerComparator;
032: import org.eclipse.jface.window.Window;
033: import org.eclipse.pde.core.IModelChangedEvent;
034: import org.eclipse.pde.core.plugin.IPluginLibrary;
035: import org.eclipse.pde.core.plugin.IPluginModelBase;
036: import org.eclipse.pde.internal.ui.PDEUIMessages;
037: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
038: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
039: import org.eclipse.pde.internal.ui.editor.TableSection;
040: import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
041: import org.eclipse.pde.internal.ui.parts.EditableTablePart;
042: import org.eclipse.swt.SWT;
043: import org.eclipse.swt.dnd.Clipboard;
044: import org.eclipse.swt.events.SelectionAdapter;
045: import org.eclipse.swt.events.SelectionEvent;
046: import org.eclipse.swt.graphics.Image;
047: import org.eclipse.swt.layout.GridData;
048: import org.eclipse.swt.layout.GridLayout;
049: import org.eclipse.swt.widgets.Button;
050: import org.eclipse.swt.widgets.Composite;
051: import org.eclipse.ui.actions.ActionFactory;
052: import org.eclipse.ui.forms.IFormPart;
053: import org.eclipse.ui.forms.IPartSelectionListener;
054: import org.eclipse.ui.forms.widgets.FormToolkit;
055: import org.eclipse.ui.forms.widgets.Section;
056:
057: public class LibraryVisibilitySection extends TableSection implements
058: IPartSelectionListener {
059:
060: private static int ADD_INDEX = 0;
061: private static int REMOVE_INDEX = 1;
062:
063: private Button fFullExportButton;
064: private Button fSelectedExportButton;
065: private IPluginLibrary fCurrentLibrary;
066: private Composite fPackageExportContainer;
067: private TableViewer fPackageExportViewer;
068: private Action fAddAction;
069: private Action fRemoveAction;
070:
071: class TableContentProvider extends DefaultContentProvider implements
072: IStructuredContentProvider {
073: public Object[] getElements(Object parent) {
074: if (parent instanceof IPluginLibrary) {
075: String[] filters = ((IPluginLibrary) parent)
076: .getContentFilters();
077: return filters == null ? new Object[0] : filters;
078: }
079: return new Object[0];
080: }
081: }
082:
083: class TableLabelProvider extends LabelProvider implements
084: ITableLabelProvider {
085:
086: public String getColumnText(Object obj, int index) {
087: return obj.toString();
088: }
089:
090: public Image getColumnImage(Object obj, int index) {
091: return JavaUI.getSharedImages().getImage(
092: ISharedImages.IMG_OBJS_PACKAGE);
093: }
094: }
095:
096: public LibraryVisibilitySection(PDEFormPage formPage,
097: Composite parent) {
098: super (formPage, parent, Section.DESCRIPTION, new String[] {
099: PDEUIMessages.ManifestEditor_ExportSection_add,
100: PDEUIMessages.ManifestEditor_ExportSection_remove });
101: getSection().setText(
102: PDEUIMessages.ManifestEditor_ExportSection_title);
103: getSection().setDescription(
104: PDEUIMessages.ManifestEditor_ExportSection_desc);
105: fHandleDefaultButton = false;
106: }
107:
108: public void createClient(Section section, FormToolkit toolkit) {
109: Composite container = toolkit.createComposite(section);
110: container.setLayout(FormLayoutFactory
111: .createSectionClientGridLayout(false, 1));
112:
113: String label = PDEUIMessages.ManifestEditor_ExportSection_fullExport;
114: fFullExportButton = toolkit.createButton(container, label,
115: SWT.RADIO);
116: fFullExportButton.setLayoutData(new GridData(
117: GridData.FILL_HORIZONTAL));
118: fFullExportButton.addSelectionListener(new SelectionAdapter() {
119: public void widgetSelected(SelectionEvent e) {
120: try {
121: if (fCurrentLibrary != null)
122: fCurrentLibrary.setExported(fFullExportButton
123: .getSelection());
124: getTablePart().setButtonEnabled(0,
125: !fFullExportButton.getSelection());
126: getTablePart().setButtonEnabled(1, false);
127: } catch (CoreException e1) {
128: }
129: }
130: });
131:
132: label = PDEUIMessages.ManifestEditor_ExportSection_selectedExport;
133: fSelectedExportButton = toolkit.createButton(container, label,
134: SWT.RADIO);
135: fSelectedExportButton.setLayoutData(new GridData(
136: GridData.FILL_HORIZONTAL));
137:
138: createPackageViewer(container, toolkit);
139: update(null);
140: makeActions();
141:
142: IPluginModelBase model = (IPluginModelBase) getPage()
143: .getModel();
144: model.addModelChangedListener(this );
145:
146: section.setLayout(FormLayoutFactory.createClearGridLayout(
147: false, 1));
148: section.setLayoutData(new GridData(GridData.FILL_BOTH));
149: section.setClient(container);
150: }
151:
152: private void makeActions() {
153: fAddAction = new Action(
154: PDEUIMessages.ManifestEditor_ExportSection_add) {
155: public void run() {
156: handleAdd();
157: }
158: };
159: fAddAction.setEnabled(isEditable());
160:
161: fRemoveAction = new Action(
162: PDEUIMessages.ManifestEditor_ExportSection_remove) {
163: public void run() {
164: handleRemove();
165: }
166: };
167: fRemoveAction.setEnabled(isEditable());
168: }
169:
170: private void createPackageViewer(Composite parent,
171: FormToolkit toolkit) {
172: fPackageExportContainer = toolkit.createComposite(parent);
173: fPackageExportContainer.setLayoutData(new GridData(
174: GridData.FILL_BOTH));
175: GridLayout layout = new GridLayout();
176: layout.marginWidth = 0;
177: layout.marginHeight = 0;
178: layout.numColumns = 2;
179: fPackageExportContainer.setLayout(layout);
180: fPackageExportContainer.setLayoutData(new GridData(
181: GridData.FILL_BOTH));
182:
183: EditableTablePart tablePart = getTablePart();
184: tablePart.setEditable(getPage().getModel().isEditable());
185: createViewerPartControl(fPackageExportContainer,
186: SWT.FULL_SELECTION, 2, toolkit);
187: fPackageExportViewer = tablePart.getTableViewer();
188: fPackageExportViewer
189: .setContentProvider(new TableContentProvider());
190: fPackageExportViewer.setLabelProvider(new TableLabelProvider());
191: fPackageExportViewer.setComparator(new ViewerComparator());
192: toolkit.paintBordersFor(parent);
193: }
194:
195: protected void selectionChanged(IStructuredSelection selection) {
196: Object item = selection.getFirstElement();
197: getTablePart().setButtonEnabled(1, item != null);
198: }
199:
200: protected void buttonSelected(int index) {
201: if (index == ADD_INDEX)
202: handleAdd();
203: else if (index == REMOVE_INDEX)
204: handleRemove();
205: }
206:
207: /* (non-Javadoc)
208: * @see org.eclipse.pde.internal.ui.editor.PDESection#doGlobalAction(java.lang.String)
209: */
210: public boolean doGlobalAction(String actionId) {
211:
212: if (!isEditable()) {
213: return false;
214: }
215:
216: if (actionId.equals(ActionFactory.DELETE.getId())) {
217: handleRemove();
218: return true;
219: }
220: return false;
221: }
222:
223: public void dispose() {
224: IPluginModelBase model = (IPluginModelBase) getPage()
225: .getModel();
226: if (model != null)
227: model.removeModelChangedListener(this );
228: super .dispose();
229: }
230:
231: protected void fillContextMenu(IMenuManager manager) {
232: if (fSelectedExportButton.isEnabled()
233: && fSelectedExportButton.getSelection()) {
234: manager.add(fAddAction);
235: manager.add(new Separator());
236: }
237: if (!fPackageExportViewer.getSelection().isEmpty()) {
238: manager.add(fRemoveAction);
239: }
240: // Copy, cut, and paste operations not supported for plug-ins that do
241: // not have a MANIFEST.MF
242: getPage().getPDEEditor().getContributor()
243: .contextMenuAboutToShow(manager, false);
244: }
245:
246: private void handleAdd() {
247: IPluginModelBase model = (IPluginModelBase) getPage()
248: .getModel();
249: IProject project = model.getUnderlyingResource().getProject();
250: try {
251: if (project.hasNature(JavaCore.NATURE_ID)) {
252: String[] names;
253: names = fCurrentLibrary.getContentFilters();
254: Vector existing = new Vector();
255: if (names != null) {
256: for (int i = 0; i < names.length; i++) {
257: existing.add(names[i]);
258: }
259: }
260: ILabelProvider labelProvider = new JavaElementLabelProvider();
261: PackageSelectionDialog dialog = new PackageSelectionDialog(
262: fPackageExportViewer.getTable().getShell(),
263: labelProvider, JavaCore.create(project),
264: existing, true);
265: if (dialog.open() == Window.OK) {
266: Object[] elements = dialog.getResult();
267: for (int i = 0; i < elements.length; i++) {
268: IPackageFragment fragment = (IPackageFragment) elements[i];
269: fCurrentLibrary.addContentFilter(fragment
270: .getElementName());
271: }
272: }
273: labelProvider.dispose();
274: }
275: } catch (CoreException e) {
276: }
277: }
278:
279: private void handleRemove() {
280: IStructuredSelection ssel = (IStructuredSelection) fPackageExportViewer
281: .getSelection();
282: Object[] items = ssel.toArray();
283: try {
284: for (int i = 0; i < items.length; i++) {
285: fCurrentLibrary
286: .removeContentFilter(items[i].toString());
287: }
288: } catch (CoreException e) {
289: }
290: }
291:
292: public void modelChanged(IModelChangedEvent e) {
293: if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
294: if (fCurrentLibrary != null)
295: update(null);
296: markStale();
297: return;
298: }
299: refresh();
300: }
301:
302: public void refresh() {
303: update(fCurrentLibrary);
304: super .refresh();
305: }
306:
307: public void selectionChanged(IFormPart source, ISelection selection) {
308: if (selection == null || selection.isEmpty())
309: update(null);
310: IStructuredSelection ssel = (IStructuredSelection) selection;
311: boolean enable = ssel.size() == 1;
312: setEnabled(enable);
313: if (enable && ssel.getFirstElement() instanceof IPluginLibrary)
314: update((IPluginLibrary) ssel.getFirstElement());
315: }
316:
317: private void setEnabled(boolean b) {
318: fFullExportButton.setEnabled(b);
319: fSelectedExportButton.setEnabled(b);
320: fPackageExportViewer.getTable().setEnabled(b);
321: if (!b) {
322: getTablePart().setButtonEnabled(ADD_INDEX, false);
323: getTablePart().setButtonEnabled(REMOVE_INDEX, false);
324: }
325: }
326:
327: private void update(IPluginLibrary library) {
328: fCurrentLibrary = library;
329: if (library == null) {
330: fFullExportButton.setEnabled(false);
331: fFullExportButton.setSelection(false);
332: fSelectedExportButton.setEnabled(false);
333: fSelectedExportButton.setSelection(false);
334: fPackageExportViewer.setInput(new Object[0]);
335: getTablePart().setButtonEnabled(ADD_INDEX, false);
336: getTablePart().setButtonEnabled(REMOVE_INDEX, false);
337: } else {
338: fFullExportButton.setEnabled(isEditable());
339: fSelectedExportButton.setEnabled(isEditable());
340: fFullExportButton.setSelection(library.isFullyExported());
341: fSelectedExportButton.setSelection(!library
342: .isFullyExported());
343: fPackageExportViewer.setInput(library);
344: getTablePart().setButtonEnabled(REMOVE_INDEX, false);
345: getTablePart().setButtonEnabled(
346: ADD_INDEX,
347: isEditable()
348: && fSelectedExportButton.getSelection());
349: }
350: }
351:
352: /* (non-Javadoc)
353: * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#canPaste(org.eclipse.swt.dnd.Clipboard)
354: */
355: public boolean canPaste(Clipboard clipboard) {
356: // Paste not supported for plug-ins that do not have a MANIFEST.MF
357: return false;
358: }
359:
360: }
|