diff options
Diffstat (limited to 'plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.wizards/src/org/eclipse/papyrus/uml/diagram/wizards/profile/ProfileChooserComposite.java')
-rwxr-xr-x | plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.wizards/src/org/eclipse/papyrus/uml/diagram/wizards/profile/ProfileChooserComposite.java | 214 |
1 files changed, 177 insertions, 37 deletions
diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.wizards/src/org/eclipse/papyrus/uml/diagram/wizards/profile/ProfileChooserComposite.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.wizards/src/org/eclipse/papyrus/uml/diagram/wizards/profile/ProfileChooserComposite.java index 18cab526b23..c90cf41ef4a 100755 --- a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.wizards/src/org/eclipse/papyrus/uml/diagram/wizards/profile/ProfileChooserComposite.java +++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.wizards/src/org/eclipse/papyrus/uml/diagram/wizards/profile/ProfileChooserComposite.java @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2015 CEA LIST and others. + * Copyright (c) 2015, 2018 CEA LIST and others. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,12 +8,17 @@ * * Contributors: * CEA LIST - Initial API and implementation + * Pauline DEVILLE (CEA LIST) - Bug 493312 - [Wizard] Apply multiple profiles in new model wizard * *****************************************************************************/ package org.eclipse.papyrus.uml.diagram.wizards.profile; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; import java.util.LinkedList; +import java.util.List; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.IStatus; @@ -49,6 +54,11 @@ import org.eclipse.uml2.uml.Profile; */ public class ProfileChooserComposite extends Composite { + /** + * @since 3.1 + */ + public static final String PROFILE_SEPARATOR = "\n"; //$NON-NLS-1$ + private Text textField; private Button workspaceButton; @@ -57,14 +67,15 @@ public class ProfileChooserComposite extends Composite { private LinkedList<ViewerFilter> filters; - private IFile selectedFile; - private ElementTreeSelectionDialog treeDialog; private boolean workspaceProfile; private String[] workspaceFilters = new String[] { "profile.uml" }; //$NON-NLS-1$ + private List<IFile> selectedFile; + + private List<Object> selectedProfile; /** * Constructor. @@ -73,6 +84,9 @@ public class ProfileChooserComposite extends Composite { public ProfileChooserComposite(Composite parent) { super(parent, SWT.NONE); + this.selectedFile = new ArrayList<>(); + this.selectedProfile = new ArrayList<>(); + this.setLayout(new GridLayout(2, true)); this.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); @@ -90,37 +104,38 @@ public class ProfileChooserComposite extends Composite { filters = new LinkedList<ViewerFilter>(); setFilterExtensions(workspaceFilters); - textField = new Text(parent, SWT.BORDER); + textField = new Text(parent, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL); GridData fieldGrid = new GridData(SWT.FILL, SWT.FILL, true, false); - fieldGrid.verticalAlignment = SWT.FILL; + fieldGrid.verticalSpan = 10; fieldGrid.horizontalSpan = 2; textField.setLayoutData(fieldGrid); treeDialog = new ElementTreeSelectionDialog(getShell(), new ProfileChooserLabelProvider(), new ProfileChooserContentProvider()); treeDialog.setInput(RegisteredProfile.getRegisteredProfiles()); - treeDialog.setAllowMultiple(false); + treeDialog.setAllowMultiple(true); treeDialog.setTitle(Messages.ProfileChooserComposite_RegisteredProfilesDialog); SelectionListener selectionListener = new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { + refreshListFromTextField(); // since we can modified the text field we have to refresh list to display selection if (e.widget.equals(workspaceButton)) { - workspaceProfile = true; - IFile[] ifile = WorkspaceResourceDialog.openFileSelection(getShell(), null, null, false, new IFile[] { selectedFile }, filters); + IFile[] ifile = WorkspaceResourceDialog.openFileSelection(getShell(), null, null, true, selectedFile.toArray(), filters); if (ifile.length > 0) { - selectedFile = ifile[0]; - textField.setText(selectedFile.getFullPath().toString()); + selectedFile = new ArrayList<>(Arrays.asList(ifile)); + refreshTextField(); } } if (e.widget.equals(registeredButton)) { - workspaceProfile = false; + treeDialog.setInitialSelections(selectedProfile.toArray()); treeDialog.open(); - IRegisteredProfile profile = (IRegisteredProfile) treeDialog.getFirstResult(); + Object[] profiles = treeDialog.getResult(); - if (profile != null) { - textField.setText(profile.getPath().toString()); + if (profiles != null) { + selectedProfile = new ArrayList<>(Arrays.asList(profiles)); + refreshTextField(); } } } @@ -129,6 +144,7 @@ public class ProfileChooserComposite extends Composite { public void widgetDefaultSelected(SelectionEvent e) { // nothing to do } + }; workspaceButton = new Button(parent, SWT.NONE); @@ -145,40 +161,95 @@ public class ProfileChooserComposite extends Composite { } /** - * Check that the provided path matches against a known Profile and that it is defined + * This method refresh selectedElement and selectedProfile according to the text field + * + * @since 3.1 + */ + private void refreshListFromTextField() { + if (textField.isDisposed()) { + return; + } + + List<String> stringList = Arrays.asList(textField.getText().split(PROFILE_SEPARATOR)); + Iterator<IFile> fileIterator = selectedFile.iterator(); + while (fileIterator.hasNext()) { + IFile file = fileIterator.next(); + if (!stringList.contains(file.getFullPath().toString().trim())) { + fileIterator.remove(); + } + } + + Iterator<Object> profileIterator = selectedProfile.iterator(); + while (profileIterator.hasNext()) { + Object profile = profileIterator.next(); + if (profile instanceof IRegisteredProfile) { + String profilePath = registeredProfileToString((IRegisteredProfile) profile); + if (!stringList.contains(profilePath)) { + profileIterator.remove(); + } + } + } + } + + /** + * This method refresh the text field according to the content of selectedProfile and selectedFile. + * + * @since 3.1 + */ + private void refreshTextField() { + StringBuilder builder = new StringBuilder(); + for (Object profile : selectedProfile) { + if (profile instanceof IRegisteredProfile) { + builder.append(registeredProfileToString(((IRegisteredProfile) profile))); + builder.append(PROFILE_SEPARATOR); + } + } + for (IFile file : selectedFile) { + builder.append(fileProfileToString(file)); + builder.append(PROFILE_SEPARATOR); + } + // remove last profile separator + if (!builder.toString().isEmpty()) { + builder.delete(builder.length() - 1, builder.length()); + } + textField.setText(builder.toString()); + } + + /** + * Check that the provided path matches against a known Profile and that it is defined. * * @return - * The IStatus of the fetched profile + * The IStatus of the fetched profile. */ public IStatus getProfileDefinitionStatus() { ResourceSet resourceSet = Util.createTemporaryResourceSet(); - try { - String profilePath = getProfileURI(); - if (profilePath == null || profilePath.equalsIgnoreCase("")) {//$NON-NLS-1$ + List<String> profilePath = getProfilesURI(); + if (profilePath.isEmpty()) { return Status.OK_STATUS; } - URI profileURI = URI.createURI(profilePath); - Resource resource = resourceSet.getResource(profileURI, true); + for (String pp : profilePath) { + URI profileURI = URI.createURI(pp); + Resource resource = resourceSet.getResource(profileURI, true); - if (resource == null) { - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ProfileChooserComposite_ProfileStatus_NullResource); - } - if (resource.getContents().isEmpty()) { - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ProfileChooserComposite_ProfileStatus_EmptyResource); - } + if (resource == null) { + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ProfileChooserComposite_ProfileStatus_NullResource); + } + if (resource.getContents().isEmpty()) { + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ProfileChooserComposite_ProfileStatus_EmptyResource); + } - EObject eObject = resource.getContents().get(0); - if (!(eObject instanceof Profile)) { - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ProfileChooserComposite_ProfileStatus_NotAProfile); - } + EObject eObject = resource.getContents().get(0); + if (!(eObject instanceof Profile)) { + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ProfileChooserComposite_ProfileStatus_NotAProfile); + } - Profile profile = (Profile) eObject; - if (!profile.isDefined()) { - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ProfileChooserComposite_ProfileStatus_ProfileNotDefined); + Profile profile = (Profile) eObject; + if (!profile.isDefined()) { + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ProfileChooserComposite_ProfileStatus_ProfileNotDefined); + } } - return Status.OK_STATUS; } catch (WrappedException ex) { @@ -190,10 +261,13 @@ public class ProfileChooserComposite extends Composite { } /** - * Gets the selected profile's path + * Gets the selected profile's path. * * @return - * the selected profile's path + * the selected profile's path. + * + * @deprecated since 3.1 + * Use getProfilesURI() instead. */ public String getProfileURI() { if (textField.isDisposed()) { @@ -212,6 +286,61 @@ public class ProfileChooserComposite extends Composite { } /** + * Gets selected profiles' path. + * + * @return + * the list of selected profile's path. + * + * @since 3.1 + */ + public List<String> getProfilesURI() { + List<String> result = new ArrayList<>(); + List<String> stringList = new ArrayList<>(); + + if (textField.isDisposed()) { + return result; + } + + String path = textField.getText(); + if (path.trim().equals("")) { //$NON-NLS-1$ + return result; + } + + stringList = Arrays.asList(path.split(PROFILE_SEPARATOR)); + for (String elt : stringList) { + result.add(elt.trim()); + } + + return result; + } + + /** + * Get the text representing the file profile. + * + * @param file + * The file. + * @return text to display. + * + * @since 3.1 + */ + protected String fileProfileToString(IFile file) { + return file.getFullPath().toString(); + } + + /** + * Get the text representing the registered profile. + * + * @param registeredProfile + * The registered profile. + * @return text to display + * + * @since 3.1 + */ + protected String registeredProfileToString(IRegisteredProfile registeredProfile) { + return registeredProfile.getPath().toString(); + } + + /** * Sets the file extensions that this FileChooser accepts * Files that don't match one of these extensions will be hidden * @@ -227,4 +356,15 @@ public class ProfileChooserComposite extends Composite { return textField; } + /** + * @see org.eclipse.swt.widgets.Widget#dispose() + * + */ + @Override + public void dispose() { + super.dispose(); + selectedFile.clear(); + selectedProfile.clear(); + } + } |