Skip to content

Commit

Permalink
Closes eclipse-vorto#305 - New model wizard allows to define namespaces.
Browse files Browse the repository at this point in the history
Signed-off-by: Erle Czar Mantos <[email protected]>
  • Loading branch information
Erle Czar Mantos committed Jun 28, 2016
1 parent 86fb5ce commit 65f50ce
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,6 @@ protected boolean checkProjectName(String projectName) {
return true;
}

protected boolean checkModelName(String modelName) {
if (checkForRegexPattern(modelName, false, MODEL_NAME_REGEX)) {
setErrorMessage("Model name should start with a capital letter and must not contain any special characters.");
return false;
}
return true;
}

protected boolean checkFBVersion(String fbVersion) {
if (checkForRegexPattern(fbVersion, false, VERSION_REGEX)) {
setErrorMessage("Version should follow pattern <MAJOR>.<MINOR>.<PATCH>");
return false;
}
return true;
}

protected boolean checkForRegexPattern(String input, boolean expectedBool,
String regexPattern) {
return Pattern.compile(regexPattern).matcher(input).matches() == expectedBool;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,21 @@
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
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.vorto.codegen.ui.context.IModelProjectContext;
import org.eclipse.vorto.codegen.ui.context.IModelProjectContext;
import org.eclipse.vorto.core.api.model.model.ModelId;
import org.eclipse.vorto.core.api.model.model.ModelIdFactory;
import org.eclipse.vorto.core.api.model.model.ModelType;
import org.eclipse.vorto.core.ui.model.IModelProject;

import com.google.common.base.Strings;

public abstract class ModelBaseWizardPage extends AbstractWizardPage implements IModelProjectContext {

private IModelProject modelProject;
Expand All @@ -38,156 +44,148 @@ protected ModelBaseWizardPage(String pageName, IModelProject modelProject) {
this.modelProject = modelProject;
}

protected ModifyListener modificationListener = new ModifyListener() {
public void modifyText(ModifyEvent e) {
modelNameChanged();
dialogChanged();
};
};

protected Text txtModelName;
protected Text txtVersion;
protected Text txtDescription;
protected String workspaceLocation;
protected Composite topContainer;
protected Group grp;

protected Text txtPlatform;
private Text txtModelName;
private Text txtNamespace;
private Text txtVersion;
private Text txtDescription;
private String workspaceLocation;

public void createControl(Composite parent) {
topContainer = new Composite(parent, SWT.NULL);
Composite topContainer = new Composite(parent, SWT.NULL);

setControl(topContainer);
topContainer.setLayout(new GridLayout(1, false));
FillLayout fillLayout = new FillLayout();
fillLayout.type = SWT.VERTICAL;
topContainer.setLayout(fillLayout);

grp = new Group(topContainer, SWT.NONE);
Group grp = new Group(topContainer, SWT.NONE);
grp.setText(getGroupTitle());
grp.setLayout(new GridLayout(2, false));
GridData gridGroup = new GridData(SWT.LEFT, SWT.CENTER,
false, false, 1, 1);
gridGroup.heightHint = 145;
gridGroup.widthHint = 570;
grp.setLayoutData(gridGroup);

Label lblModelName = new Label(grp, SWT.NONE);
lblModelName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
false, false, 1, 1));
lblModelName.setText(getModelLabel());

txtModelName = new Text(grp, SWT.BORDER);
GridData gridTxtFunctionBlockName = new GridData(SWT.FILL, SWT.CENTER,
false, false, 1, 1);
gridTxtFunctionBlockName.widthHint = 400;
txtModelName.setLayoutData(gridTxtFunctionBlockName);
txtModelName.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
modelNameChanged();
dialogChanged();

}
});

Label labelPlatform = new Label(grp, SWT.NONE);
GridData labelLayoutData = new GridData(SWT.RIGHT, SWT.CENTER,
false, false, 1, 1);
labelPlatform.setLayoutData(labelLayoutData);
labelPlatform.setText("Platform");
labelPlatform.setVisible(isMappingModelWizard());
labelLayoutData.exclude = !isMappingModelWizard();

txtPlatform = new Text(grp, SWT.BORDER);
GridData gridTxtPlatform = new GridData(SWT.FILL, SWT.CENTER,
false, false, 1, 1);
gridTxtPlatform.widthHint = 400;
txtPlatform.setLayoutData(gridTxtPlatform);
txtPlatform.setVisible(isMappingModelWizard());
gridTxtPlatform.exclude = !isMappingModelWizard();
txtPlatform.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
modelNameChanged();
dialogChanged();

}
});

txtModelName = newLabeledText(grp, getModelLabel(), SWT.BORDER, newTxtGridData(400, null), modificationListener);

txtNamespace = newLabeledText(grp, "Namespace:", SWT.BORDER, newTxtGridData(400, null), modificationListener);

decorate(grp);


Label lblVersion = new Label(grp, SWT.NONE);
lblVersion.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
false, 1, 1));
lblVersion.setText("Version:");

txtVersion = new Text(grp, 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) {
modelNameChanged();
dialogChanged();
}
});

Label lblDescription = new Label(grp, SWT.NONE);
lblDescription.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
false, 1, 1));
lblDescription.setText("Description:");

txtDescription = new Text(grp, 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);
txtVersion = newLabeledText(grp, "Version:", SWT.BORDER, newTxtGridData(400, null), modificationListener);

txtDescription = newLabeledText(grp, "Description:", SWT.BORDER | SWT.V_SCROLL | SWT.MULTI, newTxtGridData(null, 53), null);

initialize();
dialogChanged();
setControl(topContainer);
}

protected Text newLabeledText(Composite parent, String labelStr, int style, Object layoutData, ModifyListener listener) {
Label label = new Label(parent, SWT.NONE);
label.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
label.setText(labelStr);

Text text = new Text(parent, style);
text.setLayoutData(layoutData);
if (listener != null) {
text.addModifyListener(listener);
}

return text;
}

protected GridData newTxtGridData(Integer widthHint, Integer heightHint) {
GridData gridData = new GridData(SWT.FILL, SWT.TOP, false, false, 1, 1);
if (widthHint != null) {
gridData.widthHint = widthHint;
}
if (heightHint != null) {
gridData.heightHint = heightHint;
}
return gridData;
}

protected void initialize() {
txtModelName.setText(getDefaultModelName());
txtVersion.setText(getDefaultVersion());
txtDescription.setText(getDefaultDescription() + getDefaultModelName());
txtNamespace.setText(getDefaultNamespace());
}



public void dialogChanged() {
if (this.validateProject()) {
this.setErrorMessage(null);
setPageComplete(true);
} else {
setPageComplete(false);
}

}

public Composite getTopContainer() {
return topContainer;
}

protected boolean validateProject() {
boolean result = true;
String modelName = txtModelName.getText();
String fbVersion = txtVersion.getText();
result &= validateStrExist(modelName,
"Functionblock name must be specified");
result &= checkModelName(modelName);
result &= checkFBVersion(fbVersion);

result &= checkModelName(txtModelName.getText());
result &= checkNamespace(txtNamespace.getText());
result &= checkVersion(txtVersion.getText());

return result;
}

private boolean checkNamespace(String nameSpace) {
if (Strings.nullToEmpty(nameSpace).trim().isEmpty()) {
setErrorMessage("Model namespace must not be empty.");
return false;
}

/*
if (checkForRegexPattern(nameSpace, false, MODEL_NAME_REGEX)) {
setErrorMessage("Model namespace should start with a capital letter and must not contain any special characters.");
return false;
}*/

return true;
}

protected boolean checkModelName(String modelName) {
if (Strings.nullToEmpty(modelName).trim().isEmpty()) {
setErrorMessage("Model name must not be empty.");
return false;
}

if (checkForRegexPattern(modelName, false, MODEL_NAME_REGEX)) {
setErrorMessage("Model name should start with a capital letter and must not contain any special characters.");
return false;
}
return true;
}

protected boolean checkVersion(String version) {
if (Strings.nullToEmpty(version).trim().isEmpty()) {
setErrorMessage("Model version must not be empty.");
return false;
}

if (checkForRegexPattern(version, false, VERSION_REGEX)) {
setErrorMessage("Model version should follow pattern <MAJOR>.<MINOR>.<PATCH>");
return false;
}
return true;
}

protected void modelNameChanged() {
String modelName = txtModelName.getText();
txtDescription.setText(getDefaultDescription() + modelName);
}

public String getWorkspaceLocation() {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
if (workspaceLocation == null) {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
workspaceLocation = workspace.getRoot().getLocation().toString();
}
return workspaceLocation;
}

protected boolean isMappingModelWizard() {
return false;
}

public IModelProject getModelProject() {
Expand All @@ -207,6 +205,12 @@ public String getProjectName() {
public String getModelDescription() {
return txtDescription.getText();
}

protected ModelId getModelId(ModelType modelType) {
return ModelIdFactory.newInstance(modelType, txtNamespace.getText(), txtVersion.getText(), txtModelName.getText());
}

protected void decorate(Composite parent) {};

protected abstract String getDefaultVersion();

Expand All @@ -217,4 +221,8 @@ public String getModelDescription() {
protected abstract String getGroupTitle();

protected abstract String getModelLabel();

protected String getDefaultNamespace() {
return "com.mycompany.type";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
import org.eclipse.vorto.core.ui.model.ModelProjectFactory;
import org.eclipse.vorto.wizard.AbstractVortoWizard;

public abstract class AbstractDatatypeWizard extends AbstractVortoWizard
implements INewWizard {
public abstract class AbstractDatatypeWizard extends AbstractVortoWizard implements INewWizard {

private IModelProject modelProject;

Expand All @@ -45,40 +44,39 @@ public AbstractDatatypeWizard(IModelProject modelProject) {
}

private DatatypeWizardPage iotWizardPage;

private String modelFolder = "datatypes/";

private Datatype datatype;

public void addPages() {
iotWizardPage = new DatatypeWizardPage(datatype, "New Datatype",modelProject);
iotWizardPage = new DatatypeWizardPage(datatype, "New Datatype", modelProject);
setTitle("Create " + datatype.name().toLowerCase() + " type ");
setDescription("Please enter the details for creating a "
+ datatype.name().toLowerCase() + " model.");
setDescription("Please enter the details for creating a " + datatype.name().toLowerCase() + " model.");
addPage(iotWizardPage);
}

public boolean performFinish() {
new ModelGenerationTask(ModelType.Datatype.getExtension(), new DataTypeFileTemplate(datatype.name().toLowerCase()), modelFolder).generate(iotWizardPage,
InvocationContext.simpleInvocationContext(), new ProjectFileOutputter(this.modelProject.getProject()));
new ModelGenerationTask(ModelType.Datatype.getExtension(),
new DataTypeFileTemplate(datatype.name().toLowerCase()), modelFolder).generate(iotWizardPage,
InvocationContext.simpleInvocationContext(),
new ProjectFileOutputter(this.modelProject.getProject()));
openDatatypeWithDefaultEditor();
return true;
}

private void openDatatypeWithDefaultEditor() {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IProject project = workspace.getRoot().getProject(
iotWizardPage.getProjectName());

IProject project = workspace.getRoot().getProject(iotWizardPage.getProjectName());

final IModelProject modelProject = ModelProjectFactory.getInstance().getProject(project);

final IFile modelFile = modelProject.getModelElementById(iotWizardPage.getModelId()).getModelFile();

Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
IWorkbenchWindow activeWindow = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow();
IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (activeWindow != null) {
IWorkbenchPage page = activeWindow.getActivePage();
if (page != null) {
Expand All @@ -91,7 +89,7 @@ public void run() {
}
}
});

}

@Override
Expand Down
Loading

0 comments on commit 65f50ce

Please sign in to comment.