| package org.eclipse.pde.internal.ui.editor.feature; |
| /* |
| * (c) Copyright IBM Corp. 2000, 2001. |
| * All Rights Reserved. |
| */ |
| |
| import java.util.Iterator; |
| |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.jface.action.Action; |
| import org.eclipse.jface.action.IMenuManager; |
| import org.eclipse.jface.action.Separator; |
| import org.eclipse.jface.viewers.IStructuredContentProvider; |
| import org.eclipse.jface.viewers.IStructuredSelection; |
| import org.eclipse.jface.viewers.StructuredSelection; |
| import org.eclipse.jface.viewers.TableViewer; |
| import org.eclipse.jface.wizard.WizardDialog; |
| import org.eclipse.pde.core.IModel; |
| import org.eclipse.pde.core.IModelChangedEvent; |
| import org.eclipse.pde.internal.core.IModelProviderEvent; |
| import org.eclipse.pde.internal.core.IModelProviderListener; |
| import org.eclipse.pde.internal.core.PDECore; |
| import org.eclipse.pde.internal.core.WorkspaceModelManager; |
| import org.eclipse.pde.internal.core.ifeature.IFeature; |
| import org.eclipse.pde.internal.core.ifeature.IFeatureModel; |
| import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin; |
| import org.eclipse.pde.internal.ui.PDEPlugin; |
| import org.eclipse.pde.internal.ui.editor.PropertiesAction; |
| import org.eclipse.pde.internal.ui.editor.TableSection; |
| import org.eclipse.pde.internal.ui.elements.DefaultContentProvider; |
| import org.eclipse.pde.internal.ui.parts.TablePart; |
| import org.eclipse.pde.internal.ui.wizards.ListUtil; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.custom.BusyIndicator; |
| import org.eclipse.swt.layout.GridLayout; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.ui.IWorkbenchActionConstants; |
| import org.eclipse.update.ui.forms.internal.FormWidgetFactory; |
| |
| public class PluginSection |
| extends TableSection |
| implements IModelProviderListener { |
| private static final String PLUGIN_TITLE = |
| "FeatureEditor.PluginSection.pluginTitle"; |
| private static final String PLUGIN_DESC = |
| "FeatureEditor.PluginSection.pluginDesc"; |
| private static final String KEY_NEW = "FeatureEditor.PluginSection.new"; |
| public static final String POPUP_NEW = "Menus.new.label"; |
| public static final String POPUP_OPEN = "Actions.open.label"; |
| public static final String POPUP_DELETE = "Actions.delete.label"; |
| private boolean updateNeeded; |
| private Object[] references; |
| private OpenReferenceAction openAction; |
| private PropertiesAction propertiesAction; |
| private TableViewer pluginViewer; |
| private Action newAction; |
| private Action deleteAction; |
| |
| class PluginContentProvider |
| extends DefaultContentProvider |
| implements IStructuredContentProvider { |
| public Object[] getElements(Object parent) { |
| if (parent instanceof IFeature) { |
| return ((IFeature) parent).getPlugins(); |
| } |
| return new Object[0]; |
| } |
| } |
| |
| public PluginSection(FeatureReferencePage page) { |
| super(page, new String[] { PDEPlugin.getResourceString(KEY_NEW)}); |
| setHeaderText(PDEPlugin.getResourceString(PLUGIN_TITLE)); |
| setDescription(PDEPlugin.getResourceString(PLUGIN_DESC)); |
| getTablePart().setEditable(false); |
| } |
| |
| public void commitChanges(boolean onSave) { |
| } |
| |
| public Composite createClient(Composite parent, FormWidgetFactory factory) { |
| Composite container = createClientContainer(parent, 2, factory); |
| GridLayout layout = (GridLayout) container.getLayout(); |
| layout.verticalSpacing = 9; |
| |
| createViewerPartControl(container, SWT.MULTI, 2, factory); |
| TablePart tablePart = getTablePart(); |
| pluginViewer = tablePart.getTableViewer(); |
| pluginViewer.setContentProvider(new PluginContentProvider()); |
| pluginViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider()); |
| pluginViewer.setSorter(ListUtil.NAME_SORTER); |
| factory.paintBordersFor(container); |
| makeActions(); |
| return container; |
| } |
| |
| protected void handleDoubleClick(IStructuredSelection selection) { |
| openAction.run(); |
| } |
| |
| protected void buttonSelected(int index) { |
| if (index == 0) |
| handleNew(); |
| } |
| |
| public void dispose() { |
| IFeatureModel model = (IFeatureModel) getFormPage().getModel(); |
| model.removeModelChangedListener(this); |
| WorkspaceModelManager mng = PDECore.getDefault().getWorkspaceModelManager(); |
| mng.removeModelProviderListener(this); |
| super.dispose(); |
| } |
| public void expandTo(Object object) { |
| if (object instanceof IFeaturePlugin) { |
| pluginViewer.setSelection(new StructuredSelection(object), true); |
| } |
| } |
| protected void fillContextMenu(IMenuManager manager) { |
| manager.add(openAction); |
| // add new |
| manager.add(new Separator()); |
| manager.add(newAction); |
| manager.add(deleteAction); |
| // add delete |
| |
| getFormPage().getEditor().getContributor().contextMenuAboutToShow(manager); |
| manager.add(new Separator()); |
| manager.add(propertiesAction); |
| } |
| |
| private void handleNew() { |
| final IFeatureModel model = (IFeatureModel) getFormPage().getModel(); |
| BusyIndicator.showWhile(pluginViewer.getTable().getDisplay(), new Runnable() { |
| public void run() { |
| NewFeaturePluginWizardPage page = new NewFeaturePluginWizardPage(model); |
| ReferenceWizard wizard = new ReferenceWizard(model, page); |
| WizardDialog dialog = |
| new WizardDialog(PDEPlugin.getActiveWorkbenchShell(), wizard); |
| dialog.create(); |
| dialog.open(); |
| } |
| }); |
| } |
| private void handleSelectAll() { |
| IStructuredContentProvider provider = |
| (IStructuredContentProvider) pluginViewer.getContentProvider(); |
| Object[] elements = provider.getElements(pluginViewer.getInput()); |
| StructuredSelection ssel = new StructuredSelection(elements); |
| pluginViewer.setSelection(ssel); |
| } |
| private void handleDelete() { |
| IStructuredSelection ssel = (IStructuredSelection) pluginViewer.getSelection(); |
| |
| if (ssel.isEmpty()) |
| return; |
| IFeatureModel model = (IFeatureModel) getFormPage().getModel(); |
| IFeature feature = model.getFeature(); |
| |
| try { |
| IFeaturePlugin[] removed = new IFeaturePlugin[ssel.size()]; |
| int i = 0; |
| for (Iterator iter = ssel.iterator(); iter.hasNext();) { |
| IFeaturePlugin iobj = (IFeaturePlugin) iter.next(); |
| removed[i++] = iobj; |
| } |
| feature.removePlugins(removed); |
| } catch (CoreException e) { |
| PDEPlugin.logException(e); |
| } |
| } |
| public boolean doGlobalAction(String actionId) { |
| if (actionId.equals(org.eclipse.ui.IWorkbenchActionConstants.DELETE)) { |
| BusyIndicator.showWhile(pluginViewer.getTable().getDisplay(), new Runnable() { |
| public void run() { |
| handleDelete(); |
| } |
| }); |
| return true; |
| } |
| if (actionId.equals(IWorkbenchActionConstants.SELECT_ALL)) { |
| BusyIndicator.showWhile(pluginViewer.getTable().getDisplay(), new Runnable() { |
| public void run() { |
| handleSelectAll(); |
| } |
| }); |
| return true; |
| } |
| return false; |
| } |
| protected void selectionChanged(IStructuredSelection selection) { |
| getFormPage().setSelection(selection); |
| } |
| public void initialize(Object input) { |
| IFeatureModel model = (IFeatureModel) input; |
| update(input); |
| getTablePart().setButtonEnabled(0, model.isEditable()); |
| model.addModelChangedListener(this); |
| WorkspaceModelManager mng = PDECore.getDefault().getWorkspaceModelManager(); |
| mng.addModelProviderListener(this); |
| } |
| |
| public void modelChanged(IModelChangedEvent e) { |
| if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { |
| updateNeeded = true; |
| if (getFormPage().isVisible()) { |
| update(); |
| } |
| } else { |
| Object obj = e.getChangedObjects()[0]; |
| if (obj instanceof IFeaturePlugin) { |
| if (e.getChangeType() == IModelChangedEvent.CHANGE) { |
| pluginViewer.update(obj, null); |
| } else if (e.getChangeType() == IModelChangedEvent.INSERT) { |
| pluginViewer.add(e.getChangedObjects()); |
| } else if (e.getChangeType() == IModelChangedEvent.REMOVE) { |
| pluginViewer.remove(e.getChangedObjects()); |
| } |
| } |
| } |
| } |
| private void makeActions() { |
| IModel model = (IModel)getFormPage().getModel(); |
| newAction = new Action() { |
| public void run() { |
| handleNew(); |
| } |
| }; |
| newAction.setText(PDEPlugin.getResourceString(POPUP_NEW)); |
| newAction.setEnabled(model.isEditable()); |
| |
| deleteAction = new Action() { |
| public void run() { |
| BusyIndicator.showWhile(pluginViewer.getTable().getDisplay(), new Runnable() { |
| public void run() { |
| handleDelete(); |
| } |
| }); |
| } |
| }; |
| deleteAction.setText(PDEPlugin.getResourceString(POPUP_DELETE)); |
| deleteAction.setEnabled(model.isEditable()); |
| openAction = new OpenReferenceAction(pluginViewer); |
| propertiesAction = new PropertiesAction(getFormPage().getEditor()); |
| } |
| |
| public void modelsChanged(IModelProviderEvent event) { |
| updateNeeded = true; |
| update(); |
| } |
| |
| public void setFocus() { |
| if (pluginViewer != null) |
| pluginViewer.getTable().setFocus(); |
| } |
| |
| public void update() { |
| if (updateNeeded) { |
| references = null; |
| this.update(getFormPage().getModel()); |
| } |
| } |
| |
| public void update(Object input) { |
| IFeatureModel model = (IFeatureModel) input; |
| IFeature feature = model.getFeature(); |
| pluginViewer.setInput(feature); |
| updateNeeded = false; |
| } |
| } |