| /******************************************************************************* |
| * Copyright (c) 2014 Bosch Software Innovations GmbH and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * and Eclipse Distribution License v1.0 which accompany this distribution. |
| * |
| * The Eclipse Public License is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * The Eclipse Distribution License is available at |
| * http://www.eclipse.org/org/documents/edl-v10.php. |
| * |
| * Contributors: |
| * Bosch Software Innovations GmbH - Please refer to git log |
| * |
| *******************************************************************************/ |
| package org.eclipse.vorto.fbeditor.ui.internal.wizards; |
| |
| import java.io.IOException; |
| import java.util.regex.Pattern; |
| |
| import org.apache.commons.lang3.StringUtils; |
| import org.eclipse.core.resources.IWorkspace; |
| import org.eclipse.core.resources.ResourcesPlugin; |
| import org.eclipse.core.runtime.IPath; |
| import org.eclipse.core.runtime.Path; |
| import org.eclipse.jface.wizard.WizardPage; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.events.ModifyEvent; |
| import org.eclipse.swt.events.ModifyListener; |
| import org.eclipse.swt.events.SelectionAdapter; |
| import org.eclipse.swt.events.SelectionEvent; |
| 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.DirectoryDialog; |
| import org.eclipse.swt.widgets.Group; |
| import org.eclipse.swt.widgets.Label; |
| import org.eclipse.swt.widgets.Text; |
| import org.eclipse.vorto.api.common.generation.FunctionBlockMetaData; |
| |
| import com.google.common.base.Strings; |
| |
| |
| public class IoTWizardPage extends WizardPage { |
| |
| public static final String PROJECTNAME_REGEX = "[^a-zA-Z0-9 \\._]"; |
| |
| public static final String FUNCTIONBLOCK_REGEX = "^[A-Z][a-zA-Z]*$"; |
| public static final String VERSION_REGEX = "^\\d+\\.\\d+\\.\\d+(-\\w+)*$"; |
| public static final String DEFAULT_DESCRIPTION = "Function block model for "; |
| public static final String DEFAULT_FUNCTIONBLOCK_NAME = "NewFunctionBlock"; |
| private Text txtFunctionBlockName; |
| private Text txtVersion; |
| private Text txtProjectName; |
| private Text txtWorkspaceLocation; |
| private Text txtDescription; |
| private String workspaceLocation; |
| |
| public IoTWizardPage(String pageName) { |
| super(pageName); |
| } |
| |
| /** |
| * Create contents of the wizard. |
| * |
| * @param parent |
| */ |
| public void createControl(Composite parent) { |
| Composite topContainer = new Composite(parent, SWT.NULL); |
| |
| setControl(topContainer); |
| topContainer.setLayout(new GridLayout(1, false)); |
| |
| Group grpFunctionBlock = new Group(topContainer, SWT.NONE); |
| grpFunctionBlock.setText("Function Block Details"); |
| grpFunctionBlock.setLayout(new GridLayout(2, false)); |
| GridData gridGroupFunctionBlock = new GridData(SWT.LEFT, SWT.CENTER, |
| false, false, 1, 1); |
| gridGroupFunctionBlock.heightHint = 134; |
| gridGroupFunctionBlock.widthHint = 570; |
| grpFunctionBlock.setLayoutData(gridGroupFunctionBlock); |
| |
| Label lblFunctionBlockName = new Label(grpFunctionBlock, SWT.NONE); |
| lblFunctionBlockName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, |
| false, false, 1, 1)); |
| lblFunctionBlockName.setText("Function Block Name:"); |
| |
| txtFunctionBlockName = new Text(grpFunctionBlock, SWT.BORDER); |
| GridData gridTxtFunctionBlockName = new GridData(SWT.FILL, SWT.CENTER, |
| false, false, 1, 1); |
| gridTxtFunctionBlockName.widthHint = 400; |
| txtFunctionBlockName.setLayoutData(gridTxtFunctionBlockName); |
| txtFunctionBlockName.addModifyListener(new ModifyListener() { |
| public void modifyText(ModifyEvent e) { |
| fbNameChanged(); |
| dialogChanged(); |
| |
| } |
| }); |
| |
| Label lblVersion = new Label(grpFunctionBlock, SWT.NONE); |
| lblVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, |
| false, 1, 1)); |
| lblVersion.setText("Version:"); |
| |
| txtVersion = new Text(grpFunctionBlock, SWT.BORDER); |
| GridData gridTxtVersion = new GridData(SWT.FILL, SWT.CENTER, false, |
| false, 1, 1); |
| gridTxtVersion.widthHint = 411; |
| txtVersion.setLayoutData(gridTxtVersion); |
| txtVersion.addModifyListener(new ModifyListener() { |
| public void modifyText(ModifyEvent e) { |
| fbNameChanged(); |
| dialogChanged(); |
| } |
| }); |
| |
| Label lblDescription = new Label(grpFunctionBlock, SWT.NONE); |
| lblDescription.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, |
| false, 1, 1)); |
| lblDescription.setText("Description:"); |
| |
| txtDescription = new Text(grpFunctionBlock, SWT.BORDER | SWT.V_SCROLL |
| | SWT.MULTI); |
| GridData gridTxtDescription = new GridData(SWT.FILL, SWT.TOP, true, |
| false, 1, 1); |
| gridTxtDescription.heightHint = 53; |
| txtDescription.setLayoutData(gridTxtDescription); |
| |
| Group grpProjectDetails = new Group(topContainer, SWT.NONE); |
| grpProjectDetails.setLayout(new GridLayout(3, false)); |
| GridData gridGrpProjectDetails = new GridData(SWT.CENTER, SWT.CENTER, |
| false, false, 1, 1); |
| gridGrpProjectDetails.heightHint = 97; |
| gridGrpProjectDetails.widthHint = 575; |
| grpProjectDetails.setLayoutData(gridGrpProjectDetails); |
| grpProjectDetails.setText("Project Details"); |
| |
| Label lblProjectName = new Label(grpProjectDetails, SWT.NONE); |
| GridData gridLblProjectName = new GridData(SWT.RIGHT, SWT.CENTER, |
| false, false, 1, 1); |
| gridLblProjectName.widthHint = 78; |
| lblProjectName.setLayoutData(gridLblProjectName); |
| lblProjectName.setText("Project Name:"); |
| |
| txtProjectName = new Text(grpProjectDetails, SWT.BORDER); |
| GridData gridTxtProjectName = new GridData(SWT.FILL, SWT.CENTER, false, |
| false, 1, 1); |
| gridTxtProjectName.widthHint = 370; |
| txtProjectName.setLayoutData(gridTxtProjectName); |
| txtProjectName.addModifyListener(new ModifyListener() { |
| public void modifyText(ModifyEvent e) { |
| projectNameChanged(); |
| dialogChanged(); |
| } |
| }); |
| new Label(grpProjectDetails, SWT.NONE); |
| |
| Label lblLocation = new Label(grpProjectDetails, SWT.NONE); |
| GridData gridLblLocation = new GridData(SWT.RIGHT, SWT.CENTER, false, |
| false, 1, 1); |
| gridLblLocation.widthHint = 48; |
| lblLocation.setLayoutData(gridLblLocation); |
| lblLocation.setText("Location:"); |
| |
| txtWorkspaceLocation = new Text(grpProjectDetails, SWT.BORDER); |
| txtWorkspaceLocation.setEditable(false); |
| GridData gridTxtLocation = new GridData(SWT.LEFT, SWT.CENTER, false, |
| false, 1, 1); |
| gridTxtLocation.widthHint = 385; |
| txtWorkspaceLocation.setLayoutData(gridTxtLocation); |
| |
| Button btnBrowse = new Button(grpProjectDetails, SWT.NONE); |
| btnBrowse.setText("Browse..."); |
| btnBrowse.addSelectionListener(new SelectionAdapter() { |
| public void widgetSelected(SelectionEvent e) { |
| handleBrowse(e); |
| } |
| |
| }); |
| initialize(); |
| dialogChanged(); |
| setControl(topContainer); |
| } |
| |
| private void initialize() { |
| txtFunctionBlockName.setText(DEFAULT_FUNCTIONBLOCK_NAME); |
| txtVersion.setText(FunctionBlockMetaData.FB_DEFAULT_VERSION); |
| txtProjectName.setText(DEFAULT_FUNCTIONBLOCK_NAME); |
| txtWorkspaceLocation.setText(getWorkspaceLocation() + "/" |
| + DEFAULT_FUNCTIONBLOCK_NAME); |
| txtDescription |
| .setText(DEFAULT_DESCRIPTION + DEFAULT_FUNCTIONBLOCK_NAME); |
| |
| } |
| |
| private void handleBrowse(SelectionEvent e) { |
| DirectoryDialog directoryDialog = new DirectoryDialog(getShell()); |
| directoryDialog.setFilterPath(workspaceLocation); |
| directoryDialog.setText("Workspace folder selection"); |
| directoryDialog.setMessage("Select a directory for this project"); |
| |
| String selectedDirectory = directoryDialog.open(); |
| selectedDirectory = StringUtils.replace(selectedDirectory, "\\", "/"); |
| |
| if (selectedDirectory != null) { |
| workspaceLocation = selectedDirectory; |
| updateWorkspaceLocationField(workspaceLocation); |
| dialogChanged(); |
| } |
| } |
| |
| private void projectNameChanged() { |
| txtWorkspaceLocation.setText(getWorkspaceLocation() + "/" |
| + getProjectName()); |
| } |
| |
| public void dialogChanged() { |
| if (this.validateProject()) { |
| this.setErrorMessage(null); |
| setPageComplete(true); |
| } else { |
| setPageComplete(false); |
| } |
| |
| } |
| |
| private boolean validateProject() { |
| boolean result = true; |
| String projectName = getProjectName(); |
| String functionBlockName = getFunctionBlockName(); |
| String fbVersion = getFunctionBlockVersion(); |
| |
| result &= validateStrExist(projectName, |
| "Project name must be specified"); |
| result &= validateStrExist(functionBlockName, |
| "Functionblock name must be specified"); |
| result &= validateExistingSameProjectName(); |
| |
| // if(validateNoSpaceForLocation(projectLocation)){ |
| // setErrorMessage("Project location should not contain space(s)."); |
| // result = false; |
| // } |
| if (checkForRegexPattern(projectName, true, PROJECTNAME_REGEX)) { |
| setErrorMessage("Project name should not contain special characters."); |
| result = false; |
| } |
| |
| if (checkForRegexPattern(functionBlockName, false, FUNCTIONBLOCK_REGEX)) { |
| setErrorMessage("Functionblock name should start with a capital letter and must not contain any special characters."); |
| result = false; |
| } |
| |
| if (checkForRegexPattern(fbVersion, false, VERSION_REGEX)) { |
| setErrorMessage("Version should follow pattern <MAJOR>.<MINOR>.<PATCH>"); |
| result = false; |
| } |
| |
| return result; |
| } |
| |
| private boolean checkForRegexPattern(String input, boolean expectedBool, |
| String regexPattern) { |
| return Pattern.compile(regexPattern).matcher(input).find() == expectedBool; |
| } |
| |
| private boolean validateStrExist(String string, String errorMsgToBeShown) { |
| if (Strings.isNullOrEmpty(string)) { |
| this.setErrorMessage(errorMsgToBeShown); |
| return false; |
| } |
| return true; |
| } |
| |
| private boolean validateExistingSameProjectName() { |
| IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
| String projectName = getProjectName(); |
| |
| if (workspace.getRoot().getProject(getProjectName()).exists()) { |
| setErrorMessage("Project " + getProjectName() + " already exists."); |
| return false; |
| } |
| |
| IPath projectLocation = ResourcesPlugin.getWorkspace().getRoot() |
| .getLocation().append(projectName); |
| if (projectLocation.toFile().exists()) { |
| try { |
| String canonicalPath = projectLocation.toFile() |
| .getCanonicalPath(); |
| projectLocation = new Path(canonicalPath); |
| |
| String existingName = projectLocation.lastSegment(); |
| if (!existingName.equals(projectName)) { |
| setErrorMessage("Project " + getProjectName() |
| + " already exists."); |
| return false; |
| } |
| } catch (IOException e) { |
| } |
| |
| } |
| |
| return true; |
| } |
| |
| public void updateWorkspaceLocationField(String directory) { |
| txtWorkspaceLocation.setText(directory + "/" + getProjectName()); |
| } |
| |
| private void fbNameChanged() { |
| String fbName = getFunctionBlockName(); |
| txtProjectName.setText(fbName); |
| txtWorkspaceLocation.setText(getWorkspaceLocation() + "/" + fbName); |
| txtDescription.setText(DEFAULT_DESCRIPTION + fbName); |
| } |
| |
| public String getWorkspaceLocation() { |
| IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
| if (workspaceLocation == null) { |
| workspaceLocation = workspace.getRoot().getLocation().toString(); |
| } |
| return workspaceLocation; |
| } |
| |
| public String getProjectName() { |
| return txtProjectName.getText(); |
| } |
| |
| public String getFunctionBlockVersion() { |
| return txtVersion.getText(); |
| } |
| |
| public String getFunctionBlockName() { |
| return txtFunctionBlockName.getText(); |
| } |
| |
| public String getFunctionBlockDescription() { |
| return txtDescription.getText(); |
| } |
| |
| } |