| /******************************************************************************* |
| * Copyright (c) 2016 ALL4TEC & 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: |
| * ALL4TEC & CEA LIST - initial API and implementation |
| ******************************************************************************/ |
| package org.polarsys.esf.core.ui.wizards.saproject; |
| |
| import java.io.File; |
| |
| import org.eclipse.core.runtime.IPath; |
| import org.eclipse.jface.dialogs.Dialog; |
| import org.eclipse.jface.viewers.IStructuredSelection; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.layout.GridData; |
| import org.eclipse.swt.layout.GridLayout; |
| import org.eclipse.swt.widgets.Button; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Group; |
| import org.eclipse.swt.widgets.Label; |
| import org.eclipse.swt.widgets.Text; |
| import org.eclipse.ui.dialogs.WizardNewProjectCreationPage; |
| import org.polarsys.esf.core.common.messages.Messages; |
| import org.polarsys.esf.core.common.ui.widget.listener.ShowHideAdapter; |
| import org.polarsys.esf.core.ui.CoreUIActivator; |
| |
| /** |
| * This is the page used to create a new ESF project. |
| * |
| * @author $Author: jdumont $ |
| * @version $Revision: 83 $ |
| */ |
| public class ESFProjectWizardProjectCreationPage |
| extends WizardNewProjectCreationPage { |
| |
| /** Messages class. */ |
| private static final Messages MESSAGES_PROVIDER = CoreUIActivator.getMessages(); |
| |
| /** Page title. */ |
| private static final String PAGE_TITLE = |
| MESSAGES_PROVIDER.getString("ESFProjectWizard.title"); //$NON-NLS-1$ |
| |
| /** Page description. */ |
| private static final String PAGE_DESCRIPTION = |
| MESSAGES_PROVIDER.getString("ESFProjectWizard.page.newproject.description"); //$NON-NLS-1$ |
| |
| /** Label for name field of feared events library parameter. */ |
| private static final String FEARED_EVENTS_LIBRARY_NAME = |
| MESSAGES_PROVIDER.getString("ESFProjectWizard.label.fearedevents.name"); //$NON-NLS-1$ |
| |
| /** Title for feared events library parameters group. */ |
| private static final String FEARED_EVENTS_LIBRARY_GROUP = |
| MESSAGES_PROVIDER.getString("ESFProjectWizard.group.fearedevents"); //$NON-NLS-1$ |
| |
| /** Title for model file parameters group. */ |
| private static final String MODEL_FILE_GROUP = |
| MESSAGES_PROVIDER.getString("ESFProjectWizard.group.modelfile"); //$NON-NLS-1$ |
| |
| /** Label for the model name. */ |
| private static final String MODEL_NAME_LABEL = |
| MESSAGES_PROVIDER.getString("ESFProjectWizard.label.modelname"); //$NON-NLS-1$ |
| |
| /** Error message when the project name is already used by an existing file or folder . */ |
| private static final String NAME_USED_ERROR_MESSAGE = |
| MESSAGES_PROVIDER.getString("ESFProjectWizard.error.nameused"); //$NON-NLS-1$ |
| |
| /** Remember the selection given at the construction. */ |
| private IStructuredSelection mSelection = null; |
| |
| /** The text used to give the model name. */ |
| private Text mTextModelName = null; |
| |
| /** Text field used to give feared events library name. */ |
| private Text mFELibraryNameTextField = null; |
| |
| /** |
| * Constructor for this wizard page. |
| * Pass in the selection to build this page |
| * |
| * @param pPageId The page id, must be unique in a wizard |
| * @param pSelection The current selection in the workbench |
| */ |
| public ESFProjectWizardProjectCreationPage(final String pPageId, final IStructuredSelection pSelection) { |
| // Call the parent constructor |
| super(pPageId); |
| |
| // Remember the selection |
| mSelection = pSelection; |
| |
| // Initialise the page title, description |
| setTitle(PAGE_TITLE); |
| setDescription(PAGE_DESCRIPTION); |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public final void createControl(final Composite pParent) { |
| // Call the parent method |
| super.createControl(pParent); |
| |
| // Control is created on super. Get it, and add element on it |
| Composite vControl = (Composite) getControl(); |
| |
| // Add button to show/hide advance composite and add it to main control |
| Button vButton = createShowHideButton(vControl); |
| |
| // Create the advanced controls, and do not add it to parent now |
| Composite vAdvancedComposite = createAdvancedComposite(vControl); |
| |
| // Associate Button and Adapter. This can not be done in button creation, because button must be add first to |
| // parent, and at this time, advanced composite has not been created yet |
| ShowHideAdapter vSelectionAdapter = new ShowHideAdapter(vButton, vAdvancedComposite); |
| // Start in hidden state |
| vSelectionAdapter.setHidden(true); |
| vButton.addSelectionListener(vSelectionAdapter); |
| |
| // Apply the general font |
| Dialog.applyDialogFont(getControl()); |
| |
| } |
| |
| /** |
| * Create the Show/Hide advanced button control and push it on parent composite. |
| * |
| * @param pParent Receiver for the created button |
| * |
| * @return The created button |
| */ |
| private Button createShowHideButton(final Composite pParent) { |
| |
| Button vButton = new Button(pParent, SWT.PUSH); |
| |
| GridData vLayoutData = new GridData(); |
| |
| // Set the button layout data, to align it with the content above |
| // NB : The horizontal indent is necessary to align with other components which are |
| // included in one more level of composites |
| vLayoutData.horizontalIndent = 5; |
| vLayoutData.verticalIndent = 5; |
| |
| vButton.setLayoutData(vLayoutData); |
| vButton.setText(ShowHideAdapter.DEFAULT_HIDE_LABEL); |
| vButton.setImage(ShowHideAdapter.DEFAULT_HIDE_IMAGE); |
| |
| return vButton; |
| } |
| |
| /** |
| * Create advanced controls and add them to parent. |
| * |
| * @param pControl Target of created controls |
| * |
| * @return Composite created with advanced controls |
| */ |
| private Composite createAdvancedComposite(final Composite pControl) { |
| // Create a composite to receive advance fields |
| Composite vAdvancedComposite = new Composite(pControl, SWT.NULL); |
| vAdvancedComposite.setLayout(new GridLayout()); |
| |
| GridData vCompositeLayout = new GridData(GridData.FILL_BOTH); |
| vCompositeLayout.horizontalSpan = 2; |
| vAdvancedComposite.setLayoutData(vCompositeLayout); |
| |
| // Create the controls for the model file and model name. |
| createControlsModelData(vAdvancedComposite); |
| |
| // Create the controls for the feared events library name. |
| createControlsFearedEventsLibrary(vAdvancedComposite); |
| |
| // Create the controls used to select a working set |
| createWorkingSetGroup( |
| vAdvancedComposite, |
| mSelection, |
| new String[] {"org.eclipse.ui.resourceWorkingSetPage"}); //$NON-NLS-1$ |
| |
| return vAdvancedComposite; |
| } |
| |
| /** |
| * Create the controls linked to the model file and model name. |
| * |
| * @param pParent The parent composite to fill with the new controls |
| */ |
| private void createControlsModelData(final Composite pParent) { |
| |
| // Create a group which will contain all the controls for the model (file and name). |
| Group vGroupModelFile = new Group(pParent, SWT.SHADOW_ETCHED_IN); |
| vGroupModelFile.setText(MODEL_FILE_GROUP); |
| |
| GridLayout vLayout = new GridLayout(); |
| vLayout.numColumns = 2; |
| vGroupModelFile.setLayout(vLayout); |
| |
| GridData vGridData = new GridData(SWT.FILL, SWT.FILL, true, false); |
| vGroupModelFile.setLayoutData(vGridData); |
| |
| // Create the label for model name |
| Label vNameLabel = new Label(vGroupModelFile, SWT.LEFT); |
| vNameLabel.setText(MODEL_NAME_LABEL); |
| vGridData = new GridData(SWT.FILL, SWT.CENTER, false, false); |
| vNameLabel.setLayoutData(vGridData); |
| |
| // Create the text for the model name |
| // No listener on this model name, there is no rule to check. |
| mTextModelName = new Text(vGroupModelFile, SWT.SINGLE | SWT.BORDER); |
| vGridData = new GridData(SWT.FILL, SWT.CENTER, true, false); |
| mTextModelName.setLayoutData(vGridData); |
| |
| } |
| |
| /** |
| * Create the control to set name for feared events library. |
| * |
| * @param pParent The parent composite to fill with the new controls |
| */ |
| private void createControlsFearedEventsLibrary(final Composite pParent) { |
| |
| // Create group for feared events library |
| Group vFearedEventsGroup = new Group(pParent, SWT.NONE); |
| |
| // Set position and name for group |
| vFearedEventsGroup.setLayout(new GridLayout(2, false)); |
| vFearedEventsGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); |
| vFearedEventsGroup.setText(FEARED_EVENTS_LIBRARY_GROUP); |
| |
| Label vLabel = new Label(vFearedEventsGroup, SWT.NONE); |
| vLabel.setText(FEARED_EVENTS_LIBRARY_NAME); |
| |
| mFELibraryNameTextField = new Text(vFearedEventsGroup, SWT.SINGLE | SWT.BORDER); |
| mFELibraryNameTextField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
| } |
| |
| /** |
| * {@inheritDoc} |
| * |
| * Overridden to add custom validation steps. |
| * |
| * Indeed, the project name is verified but not compared to the current workspace |
| * content which is not referenced. For example, if there is a folder coming from |
| * an old project which has been deleted without deleting its content, the user |
| * must be warned. |
| */ |
| @Override |
| protected boolean validatePage() { |
| // Call the parent method to perform standard validation |
| boolean vValid = super.validatePage(); |
| |
| // If the standard validation hasn't identify any error, |
| // perform custom validations |
| if (vValid) { |
| |
| // Get the path of the project to create |
| // It can be outside the workspace |
| IPath vProjectPath = getLocationPath().append(getProjectName()); |
| |
| // Create a java File corresponding to this path and ensure that |
| // it doesn't correspond to an existing file or folder |
| File vProjectFolder = new File(vProjectPath.toOSString()); |
| if (vProjectFolder.exists()) { |
| // Warn the user |
| setErrorMessage(NAME_USED_ERROR_MESSAGE); |
| vValid = false; |
| } |
| } |
| |
| return vValid; |
| } |
| |
| /** |
| * Get model name. |
| * |
| * @return The name for ESF model, filled by user |
| */ |
| public final String getModelName() { |
| return mTextModelName.getText(); |
| } |
| |
| /** |
| * @return Name of feared events library |
| */ |
| public String getFearedEventsLibraryName() { |
| return mFELibraryNameTextField.getText(); |
| } |
| } |