Commit a320227f authored by Adam Knapp's avatar Adam Knapp
Browse files

Hello world sample project for Titan Java project



UI and file generations are ready
The test port source code is not complete yet

Change-Id: I6365abf95b1aa8f0b20d80d6221a98f938c3df4d
Signed-off-by: default avatarAdam Knapp <adam.knapp@sigmatechnology.se>
parent 206ea18e
......@@ -35,6 +35,7 @@ import org.eclipse.titan.designer.parsers.ProjectSourceParser;
* @author Arpad Lovassy
*/
public final class ProjectSourceCompiler {
private final static String PACKAGE_RUNTIME_ROOT = "org.eclipse.titan.runtime.core";
public static boolean generated;
/**
......@@ -44,29 +45,53 @@ public final class ProjectSourceCompiler {
//intentionally empty
}
/** the root package of the generated java source */
private static String getPackageGeneratedRoot(final IProject project) {
/**
* Returns the string {@code org.eclipse.titan.runtime.core}
* @return Returns the string {@code org.eclipse.titan.runtime.core}
*/
public static String getPackageRuntimeRoot() {
return PACKAGE_RUNTIME_ROOT;
}
/**
* Returns the root package of the generated java source
* @param project Project to which the package is related to
* @return The root package of the generated java source
*/
public static String getPackageGeneratedRoot(final IProject project) {
final String projectName = project.getName().replaceAll("[^\\p{IsAlphabetic}^\\p{IsDigit}]", "_");
return MessageFormat.format("org.eclipse.titan.{0}.generated", projectName);
}
/**
* Returns the root package of the user provided java source
* @param project Project to which the package is related to
* @return The root package of the user provided java source
*/
public static String getPackageUserProvidedRoot(final IProject project) {
final String projectName = project.getName().replaceAll("[^\\p{IsAlphabetic}^\\p{IsDigit}]", "_");
return MessageFormat.format("org.eclipse.titan.{0}.user_provided", projectName);
}
private final static String PACKAGE_RUNTIME_ROOT = "org.eclipse.titan.runtime.core";
/** the root folder of the generated java source */
private static String getGeneratedRoot(final IProject project) {
/**
* Returns the root folder (path) of the generated java source
* @param project Project to which the folder is related to
* @return The root folder (path) of the generated java source
*/
public static String getGeneratedRoot(final IProject project) {
final String projectName = project.getName().replaceAll("[^\\p{IsAlphabetic}^\\p{IsDigit}]", "_");
return MessageFormat.format("java_src/org/eclipse/titan/{0}/generated", projectName);
}
private static String getUserProvidedRoot(final IProject project) {
/**
* Returns the root folder (path) of the user provided java source
* @param project Project to which the folder is related to
* @return The root folder (path) of the user provided java source
*/
public static String getUserProvidedRoot(final IProject project) {
final String projectName = project.getName().replaceAll("[^\\p{IsAlphabetic}^\\p{IsDigit}]", "_");
return MessageFormat.format("user_provided/org/eclipse/titan/{0}/user_provided", projectName);
......@@ -166,7 +191,7 @@ public final class ProjectSourceCompiler {
* @param contentBuilder string buffer, where the result is written
*
*/
private static void generateCommonHeaderComments(StringBuilder contentBuilder) {
public static void generateCommonHeaderComments(StringBuilder contentBuilder) {
if (contentBuilder == null)
return;
......
package org.eclipse.titan.designer.samples;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.resources.IProject;
import org.eclipse.titan.designer.compiler.ProjectSourceCompiler;
/**
* @author Adam Knapp
* */
public class HelloWorldJavaSample extends HelloWorldSample {
/** The contents of the files. key - filename, value - content*/
private static final Map<String, String> OTHER_FILE_CONTENT = new HashMap<String, String>();
private static final Map<String, String> SOURCE_FILE_CONTENT = new HashMap<String, String>();
protected static final String TESTPORT_JAVA_PACKAGE = "package {0};\n\n";
protected static final String TESTPORT_JAVA_IMPORT =
"import {0}.MyExample.PCOType_BASE;\n"
+ "import " + ProjectSourceCompiler.getPackageRuntimeRoot() + ".TitanCharString;\n";
protected static final String TESTPORT_JAVA_BODY =
"\npublic class PCOType extends PCOType_BASE {\n\n"
+ "\tpublic PCOType(String port_name) {\n"
+ "\t\tsuper(port_name);\n"
+ "\t}\n\n"
+ "\t@Override\n"
+ "\tprotected void outgoing_send(TitanCharString send_par) {\n"
+ "\n"
+ "\t}\n"
+ "}";
static {
SOURCE_FILE_CONTENT.put("MyExample.ttcn", MYEXAMPLE_TTCN);
SOURCE_FILE_CONTENT.put("MyExample.cfg", MYEXAMPLE_CFG);
}
@Override
protected void preconfigure(IProject project) {
if (project == null) {
return;
}
final String userProvidedRoot = ProjectSourceCompiler.getUserProvidedRoot(project);
StringBuilder testPort = new StringBuilder();
generateTestPort(project, testPort);
OTHER_FILE_CONTENT.clear();
OTHER_FILE_CONTENT.put(userProvidedRoot + "/PCOType.java", testPort.toString());
}
@Override
protected void configure(final IProject project) {
// Nothing to do yet
}
@Override
public Map<String, String> getOtherFileContent() {
return OTHER_FILE_CONTENT;
}
@Override
public Map<String, String> getSourceFileContent() {
return SOURCE_FILE_CONTENT;
}
/**
* Generates the user provided test port Java source for the Hello World project
* @param project Project in which the code is needed
* @param contentBuilder This will contain the generated code as string
*/
private void generateTestPort(IProject project, StringBuilder contentBuilder) {
if (contentBuilder == null) {
return;
}
ProjectSourceCompiler.generateCommonHeaderComments(contentBuilder);
contentBuilder.append(MessageFormat.format(TESTPORT_JAVA_PACKAGE, ProjectSourceCompiler.getPackageUserProvidedRoot(project)))
.append(MessageFormat.format(TESTPORT_JAVA_IMPORT, ProjectSourceCompiler.getPackageGeneratedRoot(project)))
.append(TESTPORT_JAVA_BODY);
}
}
......@@ -16,6 +16,7 @@ import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.titan.common.logging.ErrorReporter;
import org.eclipse.titan.common.utils.StringUtils;
import org.eclipse.titan.designer.GeneralConstants;
import org.eclipse.titan.designer.properties.data.ProjectBuildPropertyData;
......@@ -24,16 +25,16 @@ import org.eclipse.titan.designer.properties.data.ProjectBuildPropertyData;
* */
public class HelloWorldSample extends SampleProject {
private static final String NEWLINE = System.getProperty("line.separator");
protected static final String NEWLINE = StringUtils.lineSeparator();
private static final String NAME_TO_DISPLAY = "Hello World";
private static final String DESCRIPTION = "TTCN-3 version of \"Hello, world!\"";
private static final String HOSTNAME = setHostName();
protected static final String NAME_TO_DISPLAY = "Hello World";
protected static final String DESCRIPTION = "TTCN-3 version of \"Hello, world!\"";
protected static final String HOSTNAME = setHostName();
/** The contents of the files. key - filename, value - content*/
private static final Map<String, String> SOURCE_FILE_CONTENT = new HashMap<String, String>();
private static final String MYEXAMPLE_TTCN =
protected static final String MYEXAMPLE_TTCN =
"// TTCN-3 version of \"Hello, world!\"" + NEWLINE
+ "module MyExample" + NEWLINE
+ "{" + NEWLINE
......@@ -74,7 +75,7 @@ public class HelloWorldSample extends SampleProject {
+ "}" + NEWLINE
+ "}" + NEWLINE;
private static final String MYEXAMPLE_CFG =
protected static final String MYEXAMPLE_CFG =
"[LOGGING]" + NEWLINE
+ "LogFile := \"../log/MyExample-%n.log\"" + NEWLINE
+ "FileMask := LOG_ALL" + NEWLINE
......@@ -85,7 +86,7 @@ public class HelloWorldSample extends SampleProject {
+ "MyExample.control" + NEWLINE;
// copy of titan/hello/PCOType.hh
private static final String PCOTYPE_HH =
protected static final String PCOTYPE_HH =
"// This Test Port skeleton header file was generated by the" + NEWLINE
+ "// TTCN-3 Compiler of the TTCN-3 Test Executor version " + GeneralConstants.VERSION_STRING + NEWLINE
+ "// for (" + System.getProperty("user.name") + "@" + HOSTNAME + ")" + NEWLINE
......@@ -136,7 +137,7 @@ public class HelloWorldSample extends SampleProject {
+ "#endif";
// copy of titan/hello/PCOType.cc
private static final String PCOTYPE_CC =
protected static final String PCOTYPE_CC =
"// This Test Port skeleton source file was generated by the" + NEWLINE
+ "// TTCN-3 Compiler of the TTCN-3 Test Executor version " + GeneralConstants.VERSION_STRING + NEWLINE
+ "// for (" + System.getProperty("user.name") + "@" + HOSTNAME + ")" + NEWLINE
......
......@@ -20,9 +20,11 @@ import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.titan.common.logging.ErrorReporter;
import org.eclipse.titan.common.utils.FileUtils;
/**
* @author Szabolcs Beres
* @author Adam Knapp
* */
public abstract class SampleProject {
/**
......@@ -37,6 +39,8 @@ public abstract class SampleProject {
sourceFolder.create(true, true, new NullProgressMonitor());
}
preconfigure(project);
setupFiles(getSourceFileContent(), sourceFolder);
setupFiles(getOtherFileContent(), project);
......@@ -47,6 +51,12 @@ public abstract class SampleProject {
}
}
/**
* Preconfigures the project and/or prepares the source codes before the files are created.
* @param project the project to configure
*/
protected void preconfigure(final IProject project) {}
/**
* Configures the project after the files have been created.
* @param project the project to configure
......@@ -85,14 +95,8 @@ public abstract class SampleProject {
* @param folder
* @throws CoreException
*/
private void createFolder(final IFolder folder) throws CoreException {
final IContainer parent = folder.getParent();
if (parent instanceof IFolder) {
createFolder((IFolder) parent);
}
if (!folder.exists()) {
folder.create(true, true, new NullProgressMonitor());
}
private void createFolder(final IFolder folder) throws CoreException {
FileUtils.createDir(folder);
}
/**
......
/******************************************************************************
* Copyright (c) 2000-2021 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
******************************************************************************/
package org.eclipse.titan.designer.samples;
import java.util.HashMap;
import java.util.Map;
/**
* @author Szabolcs Beres
* */
public final class SampleProjects {
private static final Map<String, SampleProject> AVAILABLE_PROJECTS = new HashMap<String, SampleProject>();
static {
final SampleProject emptyProject = new EmptyProjectSample();
final HelloWorldSample helloWorld = new HelloWorldSample();
AVAILABLE_PROJECTS.put(emptyProject.getName(), emptyProject);
AVAILABLE_PROJECTS.put(helloWorld.getName(), helloWorld);
}
public static Map<String, SampleProject> getProjects() {
return AVAILABLE_PROJECTS;
}
/** private constructor to disable instantiation */
private SampleProjects() {
// Do nothing
}
}
package org.eclipse.titan.designer.samples;
import java.util.HashMap;
import java.util.Map;
/**
* @author Adam Knapp
* */
public final class SampleProjects {
private static final Map<String, SampleProject> AVAILABLE_PROJECTS = new HashMap<String, SampleProject>();
static {
final SampleProject emptyProject = new EmptyProjectSample();
final HelloWorldSample helloWorld = new HelloWorldSample();
AVAILABLE_PROJECTS.put(emptyProject.getName(), emptyProject);
AVAILABLE_PROJECTS.put(helloWorld.getName(), helloWorld);
}
private static final Map<String, SampleProject> AVAILABLE_JAVA_PROJECTS = new HashMap<String, SampleProject>();
static {
final SampleProject emptyProject = new EmptyProjectSample();
final HelloWorldJavaSample helloWorld = new HelloWorldJavaSample();
AVAILABLE_JAVA_PROJECTS.put(emptyProject.getName(), emptyProject);
AVAILABLE_JAVA_PROJECTS.put(helloWorld.getName(), helloWorld);
}
public static Map<String, SampleProject> getJavaProjects() {
return AVAILABLE_JAVA_PROJECTS;
}
public static Map<String, SampleProject> getProjects() {
return AVAILABLE_PROJECTS;
}
/** private constructor to disable instantiation */
private SampleProjects() {
// Do nothing
}
}
......@@ -11,7 +11,6 @@ import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
......@@ -37,7 +36,6 @@ import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.titan.common.logging.ErrorReporter;
import org.eclipse.titan.common.utils.FileUtils;
import org.eclipse.titan.designer.Activator;
import org.eclipse.titan.designer.compiler.ProjectSourceCompiler;
import org.eclipse.titan.designer.core.TITANNature;
......@@ -46,6 +44,7 @@ import org.eclipse.titan.designer.properties.data.MakeAttributesData;
import org.eclipse.titan.designer.properties.data.ProjectBuildPropertyData;
import org.eclipse.titan.designer.properties.data.ProjectDocumentHandlingUtility;
import org.eclipse.titan.designer.properties.data.ProjectFileHandler;
import org.eclipse.titan.designer.samples.SampleProject;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;
......@@ -66,6 +65,7 @@ public class NewTITANJavaProjectWizard extends BasicNewResourceWizard implements
private static final String CREATION_FAILED = "Project creation failed";
private NewTITANProjectCreationPage mainPage;
private NewTITANProjectContentPage contentPage;
private IConfigurationElement config;
private IProject newProject;
......@@ -79,6 +79,8 @@ public class NewTITANJavaProjectWizard extends BasicNewResourceWizard implements
mainPage.setTitle(NEWPROJECT_TITLE);
mainPage.setDescription(NEWPROJECT_DESCRIPTION);
addPage(mainPage);
contentPage = new NewTITANProjectContentPage(true);
addPage(contentPage);
}
@Override
......@@ -121,7 +123,7 @@ public class NewTITANJavaProjectWizard extends BasicNewResourceWizard implements
protected void execute(final IProgressMonitor monitor) throws CoreException {
createProject(description, newProjectHandle, monitor);
IFolder folder = newProjectHandle.getFolder("src");
IFolder folder = newProjectHandle.getFolder("java_src");
if (!folder.exists()) {
try {
folder.create(true, true, null);
......@@ -129,8 +131,7 @@ public class NewTITANJavaProjectWizard extends BasicNewResourceWizard implements
ErrorReporter.logExceptionStackTrace(e);
}
}
folder = newProjectHandle.getFolder("java_src");
folder = newProjectHandle.getFolder("user_provided");
if (!folder.exists()) {
try {
folder.create(true, true, null);
......@@ -138,7 +139,7 @@ public class NewTITANJavaProjectWizard extends BasicNewResourceWizard implements
ErrorReporter.logExceptionStackTrace(e);
}
}
folder = newProjectHandle.getFolder("user_provided");
folder = newProjectHandle.getFolder("java_bin");
if (!folder.exists()) {
try {
folder.create(true, true, null);
......@@ -146,8 +147,7 @@ public class NewTITANJavaProjectWizard extends BasicNewResourceWizard implements
ErrorReporter.logExceptionStackTrace(e);
}
}
folder = newProjectHandle.getFolder("java_bin");
folder = newProjectHandle.getFolder("src");
if (!folder.exists()) {
try {
folder.create(true, true, null);
......@@ -155,6 +155,13 @@ public class NewTITANJavaProjectWizard extends BasicNewResourceWizard implements
ErrorReporter.logExceptionStackTrace(e);
}
}
final SampleProject sample = contentPage.getSampleProject();
if (sample != null) {
sample.setupProject(newProjectHandle.getProject(), folder);
ProjectFileHandler pfHandler = new ProjectFileHandler(newProjectHandle.getProject());
pfHandler.saveProjectSettings();
}
}
};
......@@ -253,30 +260,15 @@ public class NewTITANJavaProjectWizard extends BasicNewResourceWizard implements
}
}
// the root package of the user generated java source, e.g. test port types, generally TestPortTypeName_PT
//private final static String PACKAGE_USER_PROVIDED_ROOT = "org.eclipse.titan.user_provided";
// the root folder of the user generated java source, e.g. for test port types
// private final static String DIR_USER_PROVIDED_ROOT = "user_provided/org/eclipse/titan/user_provided";
private static String getUserProvidedRoot(final IProject project) {
final String projectName = project.getName().replaceAll("[^\\p{IsAlphabetic}^\\p{IsDigit}]", "_");
return MessageFormat.format("user_provided/org/eclipse/titan/{0}/user_provided", projectName);
}
private void createUserProvidedPackageFolder() throws CoreException {
final IFolder folder = newProject.getFolder(getUserProvidedRoot(newProject));
FileUtils.createDir(folder);
}
@Override
public boolean performFinish() {
Activator.getDefault().pauseHandlingResourceChanges();
if(!isCreated) {
createNewProject();
}
if (newProject == null) {
Activator.getDefault().resumeHandlingResourceChanges();
return false;
......@@ -300,7 +292,6 @@ public class NewTITANJavaProjectWizard extends BasicNewResourceWizard implements
//createUserProvidedPackageFolder();
ProjectSourceCompiler.generateGeneratedPackageInfo(newProject);
ProjectSourceCompiler.generateUserProvidedPackageInfo(newProject);
} catch (CoreException exception) {
ErrorReporter.logExceptionStackTrace(exception);
......
......@@ -32,11 +32,23 @@ public class NewTITANProjectContentPage extends WizardPage {
private Composite pageComposite;
private SampleProject sampleProject = null;
private boolean titanJavaProject = false;
public NewTITANProjectContentPage() {
super(TITLE);
}
/**
* With this constructor one is able to notify this page
* about it is related to Titan C++ or Titan Java project
* @param isTitanJavaProject True if this page is related to Titan Java project; false otherwise
*/
public NewTITANProjectContentPage(boolean isTitanJavaProject) {
this();
this.titanJavaProject = isTitanJavaProject;
}
@Override
public String getDescription() {
return DESCRIPTION;
......@@ -52,6 +64,14 @@ public class NewTITANProjectContentPage extends WizardPage {
pageComposite.dispose();
super.dispose();
}
/**
* Returns whether this page is created for Titan C++ or Java project
* @return True, if this page is created for Titan Java project; false otherwise
*/
public boolean isTitanJavaProject() {
return titanJavaProject;
}
/**
* @return The name of the selected sample project or {@code null} if the
......@@ -101,7 +121,13 @@ public class NewTITANProjectContentPage extends WizardPage {
samplesList.setLayoutData(new GridData(GridData.FILL_BOTH));
int indexOfEmptyProject = 0;
int i = 0;
for (final Map.Entry<String, SampleProject> entry : SampleProjects.getProjects().entrySet()) {
Map<String, SampleProject> map = null;
if (titanJavaProject) {
map = SampleProjects.getJavaProjects();
} else {
map = SampleProjects.getProjects();
}
for (final Map.Entry<String, SampleProject> entry : map.entrySet()) {
samplesList.add(entry.getValue().getName());
if ("Empty Project".equals(entry.getValue().getName())) {
indexOfEmptyProject = i;
......@@ -109,7 +135,11 @@ public class NewTITANProjectContentPage extends WizardPage {
++i;
}
samplesList.select(indexOfEmptyProject);
sampleProject = SampleProjects.getProjects().get(samplesList.getSelection()[0]);
if (titanJavaProject) {
sampleProject = SampleProjects.getJavaProjects().get(samplesList.getSelection()[0]);
} else {
sampleProject = SampleProjects.getProjects().get(samplesList.getSelection()[0]);
}
final Label description = new Label(projectAndDescription, SWT.BORDER);
description.setLayoutData(new GridData(GridData.FILL_BOTH));
......@@ -121,7 +151,11 @@ public class NewTITANProjectContentPage extends WizardPage {
if (samplesList.getSelectionCount() != 1) {
return;
}
sampleProject = SampleProjects.getProjects().get(samplesList.getSelection()[0]);
if (titanJavaProject) {
sampleProject = SampleProjects.getJavaProjects().get(samplesList.getSelection()[0]);
} else {
sampleProject = SampleProjects.getProjects().get(samplesList.getSelection()[0]);
}
description.setText(sampleProject.getDescription());
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment