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.ArrayList;
013: import java.util.List;
014:
015: import org.eclipse.jface.action.Action;
016: import org.eclipse.jface.action.IMenuManager;
017: import org.eclipse.jface.viewers.ISelection;
018: import org.eclipse.jface.viewers.IStructuredContentProvider;
019: import org.eclipse.jface.viewers.IStructuredSelection;
020: import org.eclipse.jface.viewers.ITableLabelProvider;
021: import org.eclipse.jface.viewers.LabelProvider;
022: import org.eclipse.jface.viewers.TableViewer;
023: import org.eclipse.jface.window.Window;
024: import org.eclipse.pde.core.IModelChangedEvent;
025: import org.eclipse.pde.core.plugin.IPluginModelBase;
026: import org.eclipse.pde.core.plugin.PluginRegistry;
027: import org.eclipse.pde.internal.core.ibundle.IBundleModel;
028: import org.eclipse.pde.internal.core.text.bundle.ExportPackageObject;
029: import org.eclipse.pde.internal.core.text.bundle.PackageFriend;
030: import org.eclipse.pde.internal.ui.PDEPlugin;
031: import org.eclipse.pde.internal.ui.PDEPluginImages;
032: import org.eclipse.pde.internal.ui.PDEUIMessages;
033: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
034: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
035: import org.eclipse.pde.internal.ui.editor.TableSection;
036: import org.eclipse.pde.internal.ui.editor.context.InputContextManager;
037: import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
038: import org.eclipse.pde.internal.ui.parts.EditableTablePart;
039: import org.eclipse.pde.internal.ui.wizards.PluginSelectionDialog;
040: import org.eclipse.swt.SWT;
041: import org.eclipse.swt.events.SelectionAdapter;
042: import org.eclipse.swt.graphics.Image;
043: import org.eclipse.swt.layout.GridData;
044: import org.eclipse.swt.layout.GridLayout;
045: import org.eclipse.swt.widgets.Button;
046: import org.eclipse.swt.widgets.Composite;
047: import org.eclipse.ui.actions.ActionFactory;
048: import org.eclipse.ui.forms.IFormPart;
049: import org.eclipse.ui.forms.IPartSelectionListener;
050: import org.eclipse.ui.forms.widgets.FormToolkit;
051: import org.eclipse.ui.forms.widgets.Section;
052: import org.osgi.framework.Constants;
053:
054: public class ExportPackageVisibilitySection extends TableSection
055: implements IPartSelectionListener {
056:
057: private static int ADD_INDEX = 0;
058: private static int REMOVE_INDEX = 1;
059:
060: private TableViewer fFriendViewer;
061: private Action fAddAction;
062: private Action fRemoveAction;
063: private Button fInternalButton;
064: private boolean fBlockChanges;
065: private ExportPackageObject[] fSelectedObjects;
066: private Image fImage;
067: private Button fVisibleButton;
068:
069: class TableContentProvider extends DefaultContentProvider implements
070: IStructuredContentProvider {
071: public Object[] getElements(Object parent) {
072: ExportPackageObject object = (ExportPackageObject) parent;
073: if (!object.isInternal())
074: return new Object[0];
075: return (object != null) ? object.getFriends()
076: : new Object[0];
077: }
078: }
079:
080: class TableLabelProvider extends LabelProvider implements
081: ITableLabelProvider {
082:
083: public String getColumnText(Object obj, int index) {
084: return obj.toString();
085: }
086:
087: public Image getColumnImage(Object obj, int index) {
088: return fImage;
089: }
090: }
091:
092: public ExportPackageVisibilitySection(PDEFormPage formPage,
093: Composite parent) {
094: super (formPage, parent, Section.DESCRIPTION, new String[] {
095: PDEUIMessages.ManifestEditor_ExportSection_add,
096: PDEUIMessages.ManifestEditor_ExportSection_remove });
097: fHandleDefaultButton = false;
098: }
099:
100: public void createClient(Section section, FormToolkit toolkit) {
101: section
102: .setText(PDEUIMessages.ExportPackageVisibilitySection_title);
103: section
104: .setDescription(PDEUIMessages.ExportPackageVisibilitySection_default);
105: Composite comp = toolkit.createComposite(section);
106: comp.setLayout(FormLayoutFactory.createSectionClientGridLayout(
107: false, 1));
108:
109: fVisibleButton = toolkit
110: .createButton(
111: comp,
112: PDEUIMessages.ExportPackageVisibilitySection_unconditional,
113: SWT.RADIO);
114:
115: fInternalButton = toolkit.createButton(comp,
116: PDEUIMessages.ExportPackageVisibilitySection_hideAll,
117: SWT.RADIO);
118: fInternalButton.addSelectionListener(new SelectionAdapter() {
119: public void widgetSelected(
120: org.eclipse.swt.events.SelectionEvent e) {
121: if (!fBlockChanges) {
122: for (int i = 0; i < fSelectedObjects.length; i++) {
123: fSelectedObjects[i].setInternal(fInternalButton
124: .getSelection());
125: }
126: getTablePart().setButtonEnabled(ADD_INDEX,
127: fInternalButton.getSelection());
128: getTablePart().setButtonEnabled(REMOVE_INDEX,
129: fInternalButton.getSelection());
130: fFriendViewer.refresh();
131: }
132: }
133: });
134:
135: Composite container = toolkit.createComposite(comp);
136: GridLayout layout = new GridLayout();
137: layout.marginWidth = 1;
138: layout.marginHeight = 1;
139: layout.numColumns = 2;
140: container.setLayout(layout);
141: container.setLayoutData(new GridData(GridData.FILL_BOTH));
142:
143: EditableTablePart tablePart = getTablePart();
144: tablePart.setEditable(getPage().getModel().isEditable());
145: createViewerPartControl(container, SWT.MULTI, 2, toolkit);
146: fFriendViewer = tablePart.getTableViewer();
147: fFriendViewer.setContentProvider(new TableContentProvider());
148: fFriendViewer.setLabelProvider(new TableLabelProvider());
149: toolkit.paintBordersFor(container);
150:
151: makeActions();
152: fImage = PDEPluginImages.DESC_PLUGIN_OBJ.createImage();
153: update(null);
154: getBundleModel().addModelChangedListener(this );
155:
156: section.setLayout(FormLayoutFactory.createClearGridLayout(
157: false, 1));
158: section.setLayoutData(new GridData(GridData.FILL_BOTH));
159: section.setClient(comp);
160: }
161:
162: private void makeActions() {
163: fAddAction = new Action(
164: PDEUIMessages.ManifestEditor_ExportSection_add) {
165: public void run() {
166: handleAdd();
167: }
168: };
169: fAddAction.setEnabled(isEditable());
170:
171: fRemoveAction = new Action(
172: PDEUIMessages.ManifestEditor_ExportSection_remove) {
173: public void run() {
174: handleRemove();
175: }
176: };
177: fRemoveAction.setEnabled(isEditable());
178: }
179:
180: protected void selectionChanged(IStructuredSelection selection) {
181: // Update global selection
182: getPage().getPDEEditor().setSelection(selection);
183:
184: Object item = selection.getFirstElement();
185: getTablePart().setButtonEnabled(1, item != null);
186: }
187:
188: protected void buttonSelected(int index) {
189: if (index == ADD_INDEX)
190: handleAdd();
191: else if (index == REMOVE_INDEX)
192: handleRemove();
193: }
194:
195: /* (non-Javadoc)
196: * @see org.eclipse.pde.internal.ui.editor.PDESection#doGlobalAction(java.lang.String)
197: */
198: public boolean doGlobalAction(String actionId) {
199:
200: if (!isEditable()) {
201: return false;
202: }
203:
204: if (actionId.equals(ActionFactory.DELETE.getId())) {
205: handleRemove();
206: return true;
207: }
208: if (actionId.equals(ActionFactory.CUT.getId())) {
209: // delete here and let the editor transfer
210: // the selection to the clipboard
211: handleRemove();
212: return false;
213: }
214: if (actionId.equals(ActionFactory.PASTE.getId())) {
215: doPaste();
216: return true;
217: }
218: return false;
219: }
220:
221: /* (non-Javadoc)
222: * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#canPaste(java.lang.Object, java.lang.Object[])
223: */
224: protected boolean canPaste(Object targetObject,
225: Object[] sourceObjects) {
226: // One export package object must be selected
227: if (isOneObjectSelected() == false) {
228: return false;
229: }
230: // All source objects have to be package friend objects and not already
231: // be a friend of the selected export package object
232: for (int i = 0; i < sourceObjects.length; i++) {
233: // Only package friends allowed
234: if ((sourceObjects[i] instanceof PackageFriend) == false) {
235: return false;
236: }
237: // No duplicate package friends allowed
238: PackageFriend friend = (PackageFriend) sourceObjects[i];
239: if (fSelectedObjects[0].hasFriend(friend.getName())) {
240: return false;
241: }
242: }
243: return true;
244: }
245:
246: /**
247: * @return
248: */
249: private boolean isOneObjectSelected() {
250: if ((fSelectedObjects == null)
251: || (fSelectedObjects.length != 1)) {
252: return false;
253: }
254: return true;
255: }
256:
257: /* (non-Javadoc)
258: * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#doPaste(java.lang.Object, java.lang.Object[])
259: */
260: protected void doPaste(Object targetObject, Object[] sourceObjects) {
261: // Paste all source objects
262: for (int i = 0; i < sourceObjects.length; i++) {
263: Object sourceObject = sourceObjects[i];
264: if ((sourceObject instanceof PackageFriend)
265: && isOneObjectSelected()) {
266: // Package friend object
267: PackageFriend friend = (PackageFriend) sourceObject;
268: // Adjust all the source object transient field values to
269: // acceptable values
270: friend.reconnect(fSelectedObjects[0]);
271: // Add the package friend to the export package object
272: fSelectedObjects[0].addFriend(friend);
273: }
274: }
275: }
276:
277: public void dispose() {
278: IBundleModel model = getBundleModel();
279: if (model != null)
280: model.removeModelChangedListener(this );
281: if (fImage != null)
282: fImage.dispose();
283: super .dispose();
284: }
285:
286: protected void fillContextMenu(IMenuManager manager) {
287: getPage().getPDEEditor().getContributor()
288: .contextMenuAboutToShow(manager);
289: }
290:
291: private void handleAdd() {
292: PluginSelectionDialog dialog = new PluginSelectionDialog(
293: PDEPlugin.getActiveWorkbenchShell(), getModels(), true);
294: dialog.create();
295: if (dialog.open() == Window.OK) {
296: Object[] selected = dialog.getResult();
297: for (int i = 0; i < selected.length; i++) {
298: IPluginModelBase model = (IPluginModelBase) selected[i];
299: for (int j = 0; j < fSelectedObjects.length; j++) {
300: fSelectedObjects[j].addFriend(new PackageFriend(
301: fSelectedObjects[j], model.getPluginBase()
302: .getId()));
303: }
304: }
305: }
306: }
307:
308: private IPluginModelBase[] getModels() {
309: ArrayList list = new ArrayList();
310: IPluginModelBase[] models = PluginRegistry
311: .getActiveModels(true);
312: for (int i = 0; i < models.length; i++) {
313: String id = models[i].getPluginBase().getId();
314: if (!fSelectedObjects[0].hasFriend(id))
315: list.add(models[i]);
316: }
317: return (IPluginModelBase[]) list
318: .toArray(new IPluginModelBase[list.size()]);
319: }
320:
321: private void handleRemove() {
322: Object[] removed = ((IStructuredSelection) fFriendViewer
323: .getSelection()).toArray();
324: for (int i = 0; i < removed.length; i++) {
325: for (int j = 0; j < fSelectedObjects.length; j++) {
326: fSelectedObjects[j]
327: .removeFriend((PackageFriend) removed[i]);
328: }
329: }
330: }
331:
332: public void modelChanged(IModelChangedEvent event) {
333: if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
334: markStale();
335: return;
336: }
337:
338: if (Constants.EXPORT_PACKAGE.equals(event.getChangedProperty())) {
339: refresh();
340: return;
341: }
342: int index = fFriendViewer.getTable().getSelectionIndex();
343: fFriendViewer.refresh();
344: fFriendViewer.getTable().setSelection(
345: Math.min(index,
346: fFriendViewer.getTable().getItemCount() - 1));
347: }
348:
349: public void refresh() {
350: update(null);
351: super .refresh();
352: }
353:
354: public void selectionChanged(IFormPart source, ISelection selection) {
355: List list = ((IStructuredSelection) selection).toList();
356: if (list.size() > 0) {
357: Object[] objects = list.toArray();
358: ExportPackageObject first = null;
359: for (int i = 0; i < objects.length; i++) {
360: if (!(objects[i] instanceof ExportPackageObject)) {
361: update(null);
362: return;
363: }
364: if (first == null) {
365: first = (ExportPackageObject) objects[i];
366: continue;
367: }
368: if (!first
369: .hasSameVisibility((ExportPackageObject) objects[i])) {
370: update(null);
371: return;
372: }
373: }
374: update((ExportPackageObject[]) list
375: .toArray(new ExportPackageObject[list.size()]));
376: } else {
377: update(null);
378: }
379: }
380:
381: private void update(ExportPackageObject[] objects) {
382: fBlockChanges = true;
383: fSelectedObjects = objects;
384:
385: ExportPackageObject object = objects == null ? null
386: : objects[0];
387: fVisibleButton.setEnabled(object != null && isEditable());
388: fVisibleButton.setSelection(objects != null
389: && !object.isInternal());
390:
391: fInternalButton.setEnabled(object != null && isEditable());
392: fInternalButton.setSelection(objects != null
393: && object.isInternal());
394:
395: getTablePart().setButtonEnabled(0,
396: fInternalButton.getSelection() && isEditable());
397: getTablePart().setButtonEnabled(1,
398: fInternalButton.getSelection() && isEditable());
399: fFriendViewer.setInput(object);
400: fBlockChanges = false;
401: }
402:
403: private BundleInputContext getBundleContext() {
404: InputContextManager manager = getPage().getPDEEditor()
405: .getContextManager();
406: return (BundleInputContext) manager
407: .findContext(BundleInputContext.CONTEXT_ID);
408: }
409:
410: private IBundleModel getBundleModel() {
411: BundleInputContext context = getBundleContext();
412: return (context != null) ? (IBundleModel) context.getModel()
413: : null;
414: }
415:
416: }
|