diff options
author | Vincent Lorenzo | 2021-02-18 09:43:14 +0000 |
---|---|---|
committer | vincent lorenzo | 2021-02-19 08:05:26 +0000 |
commit | 67949d661290d53a36288586674604103d76456c (patch) | |
tree | 7fa34eea297f19686aa0a5db41ef08d7439d1860 | |
parent | 49bb3d5212aa9472546a35cb8d018e17dc7af866 (diff) | |
download | org.eclipse.papyrus-67949d661290d53a36288586674604103d76456c.tar.gz org.eclipse.papyrus-67949d661290d53a36288586674604103d76456c.tar.xz org.eclipse.papyrus-67949d661290d53a36288586674604103d76456c.zip |
Bug 571291: [Property] provide a new widget to edit a string representing a workspace file
Change-Id: I331d184d339633fdfeb51ee3dc133aeadb09bbff
Signed-off-by: Vincent Lorenzo <vincent.lorenzo@cea.fr>
10 files changed, 640 insertions, 8 deletions
diff --git a/plugins/infra/properties/org.eclipse.papyrus.infra.properties.ui/META-INF/MANIFEST.MF b/plugins/infra/properties/org.eclipse.papyrus.infra.properties.ui/META-INF/MANIFEST.MF index 2ea473dece6..f9c20d7a058 100644 --- a/plugins/infra/properties/org.eclipse.papyrus.infra.properties.ui/META-INF/MANIFEST.MF +++ b/plugins/infra/properties/org.eclipse.papyrus.infra.properties.ui/META-INF/MANIFEST.MF @@ -41,7 +41,7 @@ Require-Bundle: org.eclipse.core.databinding.property;bundle-version="[1.8.0,2.0 org.eclipse.uml2.types;bundle-version="[2.5.0,3.0.0)", org.eclipse.xwt;bundle-version="[1.4.0,2.0.0)" Bundle-Vendor: %Bundle-Vendor -Bundle-Version: 4.0.0.qualifier +Bundle-Version: 4.1.0.qualifier Eclipse-BuddyPolicy: dependent Bundle-Activator: org.eclipse.papyrus.infra.properties.internal.ui.Activator Bundle-ManifestVersion: 2 diff --git a/plugins/infra/properties/org.eclipse.papyrus.infra.properties.ui/model/Environment.xmi b/plugins/infra/properties/org.eclipse.papyrus.infra.properties.ui/model/Environment.xmi index be88069acbf..f8c3bbc4342 100644 --- a/plugins/infra/properties/org.eclipse.papyrus.infra.properties.ui/model/Environment.xmi +++ b/plugins/infra/properties/org.eclipse.papyrus.infra.properties.ui/model/Environment.xmi @@ -196,6 +196,10 @@ label="DirectorySelector" widgetClass="StringDirectorySelector" namespace="//@namespaces.3"/> + <propertyEditorTypes + label="WorkspaceFileSelector" + widgetClass="StringWorkspaceFileSelector" + namespace="//@namespaces.3"/> <compositeWidgetTypes label="Composite" widgetClass="Composite"/> diff --git a/plugins/infra/properties/org.eclipse.papyrus.infra.properties.ui/pom.xml b/plugins/infra/properties/org.eclipse.papyrus.infra.properties.ui/pom.xml index ec0889a7d12..fc6b9b05bee 100644 --- a/plugins/infra/properties/org.eclipse.papyrus.infra.properties.ui/pom.xml +++ b/plugins/infra/properties/org.eclipse.papyrus.infra.properties.ui/pom.xml @@ -9,6 +9,6 @@ <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>org.eclipse.papyrus.infra.properties.ui</artifactId> - <version>4.0.0-SNAPSHOT</version> + <version>4.1.0-SNAPSHOT</version> <packaging>eclipse-plugin</packaging> </project>
\ No newline at end of file diff --git a/plugins/infra/properties/org.eclipse.papyrus.infra.properties.ui/src/org/eclipse/papyrus/infra/properties/ui/widgets/StringWorkspaceFileSelector.java b/plugins/infra/properties/org.eclipse.papyrus.infra.properties.ui/src/org/eclipse/papyrus/infra/properties/ui/widgets/StringWorkspaceFileSelector.java new file mode 100755 index 00000000000..03307747ee3 --- /dev/null +++ b/plugins/infra/properties/org.eclipse.papyrus.infra.properties.ui/src/org/eclipse/papyrus/infra/properties/ui/widgets/StringWorkspaceFileSelector.java @@ -0,0 +1,194 @@ +/***************************************************************************** + * Copyright (c) 2021 CEA LIST. + * + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.properties.ui.widgets; + +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.papyrus.infra.properties.ui.modelelement.DataSource; +import org.eclipse.papyrus.infra.properties.ui.modelelement.EMFModelElement; +import org.eclipse.papyrus.infra.properties.ui.modelelement.ModelElement; +import org.eclipse.swt.widgets.Composite; + +/** + * Adapted code from StringFileSelector + * + * This widget allows to select a workspace file for String field + * The String field is read-only. + * The file selection can be restricted to the current project or displayed all workspace project using the property showOnlyCurrentProject + * The files to show can be filtered by their extension + * + * @since 4.1 + * + */ +public class StringWorkspaceFileSelector extends AbstractPropertyEditor { + + /** + * The StringFileSelector widget used by this property editor + */ + protected org.eclipse.papyrus.infra.widgets.editors.StringWorkspaceFileSelector selector; + + /** + * The filtered extensions + * This should be a 1-1 mapping with {@link #filterNames} + */ + protected String[] filterExtensions; + + /** + * The name of the filters. + * This should be a 1-1 mapping with {@link #filterExtensions} + */ + protected String[] filterNames; + + /** + * if <code>true</code> the Browse workspace action will show only the current project + */ + protected boolean showOnlyCurrentProject = false;; + + /** + * + * Constructor. + * + * @param parent + * @param style + */ + public StringWorkspaceFileSelector(Composite parent, int style) { + selector = createSelector(parent, style); + super.setEditor(selector); + } + + /** + * Creates the selector + * + * @param parent + * The composite in which the widget will be displayed + * @param style + * The style for the widget + * @return the selector + */ + protected org.eclipse.papyrus.infra.widgets.editors.StringWorkspaceFileSelector createSelector(Composite parent, int style) { + return new org.eclipse.papyrus.infra.widgets.editors.StringWorkspaceFileSelector(parent, style); + } + + /** + * + * @param filterExtensions + */ + public void setFilterExtensions(String[] filterExtensions) { + this.filterExtensions = filterExtensions; + checkFilters(); + } + + /** + * + * @return + * the filter extension + */ + public String[] getFilterExtensions() { + return this.filterExtensions; + } + + /** + * + * @see org.eclipse.papyrus.infra.properties.ui.widgets.AbstractPropertyEditor#setProperty(java.lang.String) + * + * @param path + */ + @Override + public void setProperty(String path) { + super.setProperty(path); + selector.setProjectName(getCurrentProjectName()); + } + + /** + * + * @see org.eclipse.papyrus.infra.properties.ui.widgets.AbstractPropertyEditor#setInput(org.eclipse.papyrus.infra.properties.ui.modelelement.DataSource) + * + * @param input + */ + @Override + public void setInput(DataSource input) { + super.setInput(input); + selector.setProjectName(getCurrentProjectName()); + } + + /** + * + * @param filterNames + */ + public void setFilterNames(String[] filterNames) { + this.filterNames = filterNames; + checkFilters(); + } + + /** + * + * @return + * the name of the filters + */ + public String[] getFilterNames() { + return this.filterNames; + } + + /** + * Checks if the filters are valid + */ + protected void checkFilters() { + if (filterExtensions != null && filterNames != null) { + selector.setFilters(filterExtensions, filterNames); + } + } + + /** + * + * @param showOnlyCurrentProject + * if <code>true</code> the Browse action will only show the current project + */ + public void setShowOnlyCurrentProject(final boolean showOnlyCurrentProject) { + this.showOnlyCurrentProject = showOnlyCurrentProject; + this.selector.setShowOnlyCurrentProject(showOnlyCurrentProject); + } + + /** + * + * @return + * the name of the current project + */ + protected final String getCurrentProjectName() { + String currentProjectName = null; + if (input != null && propertyPath != null) { + ModelElement modelElement = input.getModelElement(propertyPath); + if (modelElement instanceof EMFModelElement) { + final EObject source = ((EMFModelElement) modelElement).getSource(); + final URI uri = source.eResource().getURI(); + if (uri.isPlatformResource() && uri.segmentCount() >= 2) { + currentProjectName = uri.segment(1); + } + } + } + return currentProjectName; + } + + /** + * + * @return + * boolean indicating if we want to show only the current project in the Browse action + */ + public boolean getShowOnlyCurrentProject() { + return this.showOnlyCurrentProject; + } + +}
\ No newline at end of file diff --git a/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/META-INF/MANIFEST.MF b/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/META-INF/MANIFEST.MF index ed08e83d949..aeb857a98a2 100644 --- a/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/META-INF/MANIFEST.MF +++ b/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/META-INF/MANIFEST.MF @@ -33,7 +33,7 @@ Export-Package: org.eclipse.papyrus.infra.widgets, org.eclipse.papyrus.infra.widgets.wizard.pages Bundle-Vendor: %providerName Bundle-ActivationPolicy: lazy -Bundle-Version: 4.0.0.qualifier +Bundle-Version: 4.1.0.qualifier Bundle-Name: %pluginName Bundle-Localization: plugin Bundle-ManifestVersion: 2 diff --git a/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/pom.xml b/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/pom.xml index a5a11d32348..5d25a653cfc 100644 --- a/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/pom.xml +++ b/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/pom.xml @@ -9,6 +9,6 @@ <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>org.eclipse.papyrus.infra.widgets</artifactId> - <version>4.0.0-SNAPSHOT</version> + <version>4.1.0-SNAPSHOT</version> <packaging>eclipse-plugin</packaging> </project>
\ No newline at end of file diff --git a/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/src/org/eclipse/papyrus/infra/widgets/editors/StringWorkspaceFileSelector.java b/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/src/org/eclipse/papyrus/infra/widgets/editors/StringWorkspaceFileSelector.java new file mode 100755 index 00000000000..2a32e20ff19 --- /dev/null +++ b/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/src/org/eclipse/papyrus/infra/widgets/editors/StringWorkspaceFileSelector.java @@ -0,0 +1,352 @@ +/***************************************************************************** + * Copyright (c) 2021 CEA LIST. + * + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.infra.widgets.editors; + +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; + +import org.eclipse.core.resources.IFile; +import org.eclipse.jface.viewers.ILabelProvider; +import org.eclipse.jface.window.Window; +import org.eclipse.papyrus.infra.core.services.ServiceException; +import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService; +import org.eclipse.papyrus.infra.services.labelprovider.service.impl.LabelProviderServiceImpl; +import org.eclipse.papyrus.infra.widgets.Activator; +import org.eclipse.papyrus.infra.widgets.messages.Messages; +import org.eclipse.papyrus.infra.widgets.providers.SingleProjectContentProvider; +import org.eclipse.papyrus.infra.widgets.providers.WorkspaceContentProvider; +import org.eclipse.papyrus.infra.widgets.util.FileUtil; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; + +/** + * Adapted code from StringFileSelector + * + * This widget allows to select a workspace file for String field + * The String field is read-only. + * The file selection can be restricted to the current project or displayed all workspace project using the property showOnlyCurrentProject + * The files to show can be filtered by their extension + * + * @since 4.1 + * + */ +public class StringWorkspaceFileSelector extends StringEditor { + + /** + * the delete button + */ + private Button deleteButton; + + /** + * the button to browse the workspace + */ + private Button browseWorkspace; + + /** + * the list of the name of the file filters + */ + private List<String> filterNames; + + /** + * the list of the file extension to filter + */ + private List<String> filterExtensions; + + /** + * boolean indicating if the elements must be read only + */ + private boolean readOnly = false; + + /** + * boolean indicating if the browse workspace must show only the current project or all workspace project + */ + private boolean showOnlyCurrentProject = false; + + /** + * the name of the current project + */ + protected String currentProjectName; + + /** + * + * Constructor. + * + * @param parent + * @param style + */ + public StringWorkspaceFileSelector(Composite parent, int style) { + super(parent, style | SWT.READ_ONLY); // the string field is read only + super.setReadOnly(readOnly); + ((GridLayout) getLayout()).numColumns = 5; + + deleteButton = factory.createButton(this, "", SWT.PUSH); //$NON-NLS-1$ + deleteButton.setImage(org.eclipse.papyrus.infra.widgets.Activator.getDefault().getImage("/icons/Delete_12x12.gif")); //$NON-NLS-1$ + deleteButton.setLayoutData(new GridData()); + browseWorkspace = factory.createButton(this, Messages.StringWorkspaceFileSelector_BrowseWorkspace, SWT.PUSH); + browseWorkspace.setLayoutData(new GridData()); + + filterNames = new LinkedList<>(); + filterExtensions = new LinkedList<>(); + + deleteButton.addSelectionListener(new SelectionListener() { + + @Override + public void widgetSelected(SelectionEvent e) { + setResult(""); //$NON-NLS-1$ + } + + @Override + public void widgetDefaultSelected(SelectionEvent e) { + // Nothing + } + + }); + + browseWorkspace.addSelectionListener(new SelectionListener() { + + @Override + public void widgetSelected(SelectionEvent e) { + LabelProviderService labelProviderService = new LabelProviderServiceImpl(); + try { + labelProviderService.startService(); + } catch (ServiceException ex) { + Activator.log.error(ex); + } + + ILabelProvider labelProvider = labelProviderService.getLabelProvider(); + + IFile currentFile = getIFile(text.getText()); + + TreeSelectorDialog dialog = new TreeSelectorDialog(getShell()); + if (labelText != null) { + dialog.setTitle(labelText); + } + + WorkspaceContentProvider contentProvider = createWorkspaceContentProvider(); + + if (!(filterExtensions.isEmpty() || filterNames.isEmpty())) { + // The filters have been defined + contentProvider.setExtensionFilters(new LinkedHashMap<String, String>()); // Reset the default filters + + // Use our own filters + for (int i = 0; i < Math.min(filterNames.size(), filterExtensions.size()); i++) { + contentProvider.addExtensionFilter(filterExtensions.get(i), filterNames.get(i)); + } + } + + dialog.setContentProvider(contentProvider); + dialog.setLabelProvider(labelProvider); + + + if (currentFile != null && currentFile.exists()) { + dialog.setInitialSelections(new Object[] { currentFile }); + } + + int code = dialog.open(); + if (code == Window.OK) { + Object[] result = dialog.getResult(); + if (result.length > 0) { + Object file = result[0]; + if (file instanceof IFile) { + setResult((IFile) file); + } + } + } + } + + @Override + public void widgetDefaultSelected(SelectionEvent e) { + // Nothing + } + + }); + } + + /** + * + * @param path + * a file path + * @return + * the IFile corresponding to the path. This method is required to be overridden by others projects if required + */ + protected IFile getIFile(final String path) { + return FileUtil.getIFile(path); + } + + /** + * + * @return + * a new {@link WorkspaceContentProvider} + */ + protected WorkspaceContentProvider createWorkspaceContentProvider() { + if (this.showOnlyCurrentProject && this.currentProjectName != null && !this.currentProjectName.isEmpty()) { + return new SingleProjectContentProvider(this.currentProjectName); + } + return new WorkspaceContentProvider(); + } + + /** + * + * @param file + * the selected file + */ + protected void setResult(IFile file) { + setResult(file.getFullPath().toString()); + } + + /** + * + * @param path + * the path of the selected file + */ + protected void setResult(String path) { + this.text.setText(path); + notifyChange(); + } + + /** + * Set the filters extension and their name + * + * @param filterExtensions + * the filters on file extension + * @param filterNames + * the name of the filters + * + */ + public void setFilters(String[] filterExtensions, String[] filterNames) { + if (filterExtensions.length != filterNames.length) { + // This is a simple warning. Only valid filters will be retained. + Activator.log.warn("FilterExtensions and FilterNames do not match"); //$NON-NLS-1$ + } + + setFilterNames(getFilterLabels(filterNames, filterExtensions)); + setFilterExtensions(filterExtensions); + } + + /** + * + * @param filterNames + * the name of the filters + * @param filterExtensions + * the filters on file extension + * @return + * the label to display for each filter + */ + protected String[] getFilterLabels(String[] filterNames, String[] filterExtensions) { + int size = Math.min(filterNames.length, filterExtensions.length); + String[] filters = new String[size]; + for (int i = 0; i < size; i++) { + filters[i] = filterNames[i] + " (" + filterExtensions[i] + ")"; //$NON-NLS-1$ //$NON-NLS-2$ + } + return filters; + } + + /** + * + * @param filterExtensions + * the filters on file extension + */ + public void setFilterExtensions(String[] filterExtensions) { + this.filterExtensions = Arrays.asList(filterExtensions); + } + + /** + * + * @param filterNames + * the name of the filters + */ + public void setFilterNames(String[] filterNames) { + this.filterNames = Arrays.asList(filterNames); + } + + /** + * + * @param filteredExtension + * the filter extension + * @param filterName + * the name of the filter + */ + public void addFilteredExtension(String filteredExtension, String filterName) { + if (filteredExtension != null) { + if (filterName == null) { + filterName = filteredExtension; + } + + filterExtensions.add(filteredExtension); + filterNames.add(filterName); + } + } + + /** + * + * @see org.eclipse.papyrus.infra.widgets.editors.StringEditor#getEditableType() + * + * @return + * the String type + */ + @Override + public Object getEditableType() { + return String.class; + } + + /** + * + * @see org.eclipse.papyrus.infra.widgets.editors.StringEditor#setReadOnly(boolean) + * + * @param readOnly + * boolean indicating if the text field and the button must be read-only or not + */ + @Override + public void setReadOnly(boolean readOnly) { + super.setReadOnly(readOnly); + this.readOnly = readOnly; + updateButtons(); + } + + /** + * this method update the button status + */ + private void updateButtons() { + this.deleteButton.setEnabled(!readOnly); + this.browseWorkspace.setEnabled(!readOnly); + } + + /** + * + * @param showOnlyCurrentProject + * boolean indicating if we want to show only the content of the current project or all workspace project + */ + public void setShowOnlyCurrentProject(final boolean showOnlyCurrentProject) { + this.showOnlyCurrentProject = showOnlyCurrentProject; + } + + /** + * + * @param projectName + * the name of the current project + */ + public void setProjectName(final String projectName) { + this.currentProjectName = projectName; + } + +} diff --git a/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/src/org/eclipse/papyrus/infra/widgets/messages/Messages.java b/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/src/org/eclipse/papyrus/infra/widgets/messages/Messages.java index 62031f8254d..46ba820bd8d 100644 --- a/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/src/org/eclipse/papyrus/infra/widgets/messages/Messages.java +++ b/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/src/org/eclipse/papyrus/infra/widgets/messages/Messages.java @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2010, 2014 CEA LIST and others. + * Copyright (c) 2010, 2014, 2021 CEA LIST and others. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -11,7 +11,7 @@ * Contributors: * Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation * Christian W. Damus (CEA) - bug 402525 - * + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - bug 571291 *****************************************************************************/ package org.eclipse.papyrus.infra.widgets.messages; @@ -226,6 +226,11 @@ public class Messages extends NLS { public static String StringWithClearEditor_ClearFilter; /** + * @since 4.1 + */ + public static String StringWorkspaceFileSelector_BrowseWorkspace; + + /** * @since 3.0 */ public static String BundleIconExplorerDialog_Message; diff --git a/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/src/org/eclipse/papyrus/infra/widgets/messages/messages.properties b/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/src/org/eclipse/papyrus/infra/widgets/messages/messages.properties index 0f8b685b2c8..4ea9dd3aebb 100644 --- a/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/src/org/eclipse/papyrus/infra/widgets/messages/messages.properties +++ b/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/src/org/eclipse/papyrus/infra/widgets/messages/messages.properties @@ -1,5 +1,5 @@ ############################################################################### -# Copyright (c) 2010, 2014 CEA LIST and others. +# Copyright (c) 2010, 2014, 2021 CEA LIST and others. # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Public License 2.0 @@ -11,7 +11,8 @@ # Contributors: # Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation # Christian W. Damus (CEA) - bug 402525 -# +# Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - bug 571291 +# ############################################################################### AbstractFilteredContentProvider_CaseSensitiveLabel=Aa @@ -80,6 +81,7 @@ IntegerMask_ErrorTooManyValues=The mask-based integer editor cannot be used with ProviderBasedBrowseStrategy_0=The provider has not been initialized StringWithClearEditor_ClearFilter=Clear the filter +StringWorkspaceFileSelector_BrowseWorkspace=Browse Workspace BundleIconExplorerDialog_Bundle=Bundle BundleIconExplorerDialog_CantFindBundleIdMessage=impossible to find bundle with id: BundleIconExplorerDialog_Message=Please select an icon in the following list diff --git a/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/src/org/eclipse/papyrus/infra/widgets/providers/SingleProjectContentProvider.java b/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/src/org/eclipse/papyrus/infra/widgets/providers/SingleProjectContentProvider.java new file mode 100755 index 00000000000..17cfe1ca59a --- /dev/null +++ b/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/src/org/eclipse/papyrus/infra/widgets/providers/SingleProjectContentProvider.java @@ -0,0 +1,75 @@ +/***************************************************************************** + * Copyright (c) 2021 CEA LIST. + * + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.infra.widgets.providers; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.ResourcesPlugin; + +/** + * + * Workspace Content Provider able to show only the current project + * + * @since 4.1 + */ +public class SingleProjectContentProvider extends WorkspaceContentProvider { + + /** + * the name of the project to show + */ + private String projectName; + + /** + * + * Constructor. + * + * @param projectName + * the name of the project to show + */ + public SingleProjectContentProvider(final String projectName) { + super(); + this.projectName = projectName; + } + + @Override + public Object[] getElements() { + // no override required for the initial usecase + return super.getElements(); + } + + /** + * + * @see org.eclipse.papyrus.infra.widgets.providers.EncapsulatedContentProvider#getElements(java.lang.Object) + * + * @param inputElement + * @return + */ + @Override + public Object[] getElements(Object inputElement) { + if (projectName != null + && !projectName.isEmpty() + && ResourcesPlugin.getWorkspace() != null + && ResourcesPlugin.getWorkspace().getRoot() != null) { + final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(this.projectName); + if (project != null) { + return new Object[] { project }; + } + } + + return super.getElements(inputElement); + } + + +} |