Commit b61dfdf7 authored by arbennett's avatar arbennett

Merge remote-tracking branch 'origin/andrew/projectgenerator' into next

parents 4a6f944d 0c6ca9bf
......@@ -110,7 +110,7 @@ public class INIReaderTester {
// Create an IPSReader to test
INIReader reader = new INIReader("!");
assertNotNull(reader);
assertEquals(reader.getReaderType(), "INIReader");
assertEquals(reader.getReaderType(), "INI");
// Try to read in invalid INI file
IFile fakeFile = null;
......
......@@ -117,7 +117,7 @@ public class INIWriterTester {
INIReader reader = new INIReader();
INIWriter writer = new INIWriter();
assertNotNull(writer);
assertEquals(writer.getWriterType(), "INIWriter");
assertEquals(writer.getWriterType(), "INI");
// Try to read in invalid INI file
Form form = null;
......
......@@ -117,7 +117,7 @@ public class IPSReaderTester {
// Create an IPSReader to test
IPSReader reader = new IPSReader();
assertNotNull(reader);
assertEquals(reader.getReaderType(), "IPSReader");
assertEquals(reader.getReaderType(), "IPS");
// Try to read in invalid INI file
IFile fakeFile = null;
......
......@@ -496,7 +496,7 @@ public class INIReader implements ITemplatedReader {
*/
@Override
public String getReaderType() {
return "INIReader";
return "INI";
}
}
......@@ -183,7 +183,7 @@ public class INIWriter implements IWriter {
*/
@Override
public String getWriterType() {
return "INIWriter";
return "INI";
}
}
......@@ -210,7 +210,7 @@ public class IPSReader implements IReader {
*/
@Override
public String getReaderType() {
return "IPSReader";
return "IPS";
}
/**
......
......@@ -231,7 +231,7 @@ public class IPSWriter implements IWriter {
*/
@Override
public String getWriterType() {
return "IPSWriter";
return "IPS";
}
/**
......
......@@ -37,7 +37,7 @@ public class NewICEItemProjectTester {
private static final String SEP = System.getProperty("file.separator");
private static final String PROJECT_NAME = "org.eclipse.ice.newitem";
private static final int MANIFEST_LINE_COUNT = 25;
private static final int MODEL_LINE_COUNT = 182;
private static final int MODEL_LINE_COUNT = 228;
private static final int LAUNCHER_LINE_COUNT = 82;
/**
......
......@@ -14,6 +14,7 @@ Export-Package: org.eclipse.ice.projectgeneration;uses:="org.eclipse.core.resour
Bundle-Vendor: Oak Ridge National Laboratory
Import-Package: org.eclipse.core.resources,
org.eclipse.core.runtime;version="3.5.0",
org.eclipse.ice.io.serializable,
org.eclipse.jface,
org.eclipse.jface.dialogs,
org.eclipse.jface.operation,
......@@ -27,6 +28,9 @@ Import-Package: org.eclipse.core.resources,
org.eclipse.pde.internal.ui.wizards.plugin,
org.eclipse.pde.ui,
org.eclipse.pde.ui.templates,
org.eclipse.swt,
org.eclipse.swt.events,
org.eclipse.swt.layout,
org.eclipse.swt.widgets,
org.eclipse.ui,
org.eclipse.ui.dialogs,
......
/*******************************************************************************
* Copyright (c) 2014 UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Initial API and implementation and/or initial documentation - Jay Jay Billings,
* Jordan H. Deyton, Dasha Gorin, Alexander J. McCaskey, Taylor Patterson,
* Claire Saunders, Matthew Wang, Anna Wojtowicz
*******************************************************************************/
package org.eclipse.ice.projectgeneration;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.ResourceBundle;
import java.util.Set;
public class ICEProjectResources extends ResourceBundle {
@Override
protected Object handleGetObject(String key) {
if (key.equals("okKey")) return "Okay";
if (key.equals("cancelKey")) return "Cancel";
return null;
}
@Override
public Enumeration<String> getKeys() {
return Collections.enumeration(keySet());
}
protected Set<String> handleKeySet() {
return new HashSet<String>(Arrays.asList("okKey","cancelKey"));
}
}
/*******************************************************************************
* Copyright (c) 2014 UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Initial API and implementation and/or initial documentation - Jay Jay Billings,
* Jordan H. Deyton, Dasha Gorin, Alexander J. McCaskey, Taylor Patterson,
* Claire Saunders, Matthew Wang, Anna Wojtowicz
*******************************************************************************/
package org.eclipse.ice.projectgeneration.templates;
import java.net.URI;
import java.net.URISyntaxException;
import org.eclipse.pde.ui.templates.BaseOptionTemplateSection;
import org.eclipse.pde.ui.templates.StringOption;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
/**
* The DataFileOption is an extension of the StringOption which has
* some additional UI components to be used in a specific manner.
* The main functionality of the DataFileOption comes from having
* the browse button attached, which allows users to select files
* from their local machine to be used as the default datasets to
* be loaded when no other data is given
*
* @author arbennett
*/
public class DataFileOption extends StringOption {
private Text locationPathField;
private Button browseButton;
private Label locationLabel;
private FileDialog fileChooser;
private String chosenFile;
private String description;
/**
* Constructor
*
* @param section
* The wizard section to add the option to
* @param name
* The variable replacement string
* @param label
* A description to prompt the user in some way
*/
public DataFileOption(BaseOptionTemplateSection section, String name, String label) {
super(section, name, label);
description = label;
}
/**
* Create the user interface for the option
*/
public void createControl(Composite parent, int span) {
// Create the descriptive layer
locationLabel = new Label(parent, SWT.NONE);
locationLabel.setText(description);
// Create the text entry area where the path to the file will be displayed
locationPathField = new Text(parent, SWT.BORDER);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
data.widthHint = 250;
data.horizontalSpan = 2;
locationPathField.setLayoutData(data);
// Create the browse button and add a listener to open a file chooser
browseButton = new Button(parent, SWT.PUSH);
browseButton.setText("Browse");
browseButton.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent event) {
fileChooser = new FileDialog(parent.getShell());
fileChooser.setText("Select File");
fileChooser.setFilterExtensions(new String[] {"*.*"});
chosenFile = fileChooser.open();
locationPathField.setText(chosenFile);
setValue(chosenFile);
}
@Override public void widgetDefaultSelected(SelectionEvent e) {}
});
}
/**
* Get the path from the location path field
*
* @return the path
*/
public String getDataFilePath() {
URI fieldURI;
try {
fieldURI = new URI(chosenFile);
} catch (URISyntaxException e) {
return chosenFile;
}
String path = fieldURI.getPath();
return path != null ? path : chosenFile;
}
/**
* Check if the value of the option is null
*/
@Override
public boolean isEmpty() {
return getValue() == null;
}
/**
* Set whether the option is usable
*/
@Override
public void setEnabled(boolean enabled) {
locationLabel.setEnabled(enabled);
locationPathField.setEnabled(enabled);
browseButton.setEnabled(enabled);
}
}
......@@ -12,22 +12,32 @@
*******************************************************************************/
package org.eclipse.ice.projectgeneration.templates;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ResourceBundle;
import java.util.Locale;
import java.util.ArrayList;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.ice.projectgeneration.ICEProjectResources;
import org.eclipse.ice.io.serializable.IReader;
import org.eclipse.ice.io.serializable.IWriter;
import org.eclipse.ice.projectgeneration.templates.ICEProjectResources;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.pde.core.plugin.IPluginBase;
import org.eclipse.pde.core.plugin.IPluginElement;
import org.eclipse.pde.core.plugin.IPluginExtension;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.core.plugin.IPluginModelFactory;
import org.eclipse.pde.ui.IFieldData;
import org.eclipse.pde.ui.IPluginFieldData;
import org.eclipse.pde.ui.templates.OptionTemplateSection;
import org.eclipse.pde.ui.templates.TemplateOption;
/**
* Provides the information for the ICEItemWizard, the final page in the
......@@ -37,14 +47,17 @@ import org.eclipse.pde.ui.templates.OptionTemplateSection;
*/
public class ICEItemTemplate extends OptionTemplateSection {
protected static final String EXTENSION_POINT = "org.eclipse.ice.item.itemBuilder";
protected static final String BUNDLE_ID = "org.eclipse.ice.projectgeneration";
// Strings used for templating
protected static final String EXTENSION_POINT = "org.eclipse.ice.item.itemBuilder";
// Replacement strings in template files. See the templates_x.x directory
// for the template files.
protected static final String KEY_CLASS_NAME = "className";
protected static final String KEY_EXTENSION_NAME = "packageName";
protected static final String KEY_JOB_LAUNCHER_EXT = "createJobLauncher";
protected static final String KEY_MODEL_EXT = "createModel";
protected static final String KEY_IO_FORMAT_EXT = "ioFormat";
protected static final String KEY_DEFAULT_FILE_NAME = "defaultFileName";
/**
* Constructor
......@@ -56,10 +69,9 @@ public class ICEItemTemplate extends OptionTemplateSection {
/**
* Add pages to the wizard
* Adds the page to the wizard.
*/
public void addPages(Wizard wizard) {
// create one wizard page for the options
WizardPage p1 = createPage(0);
p1.setPageComplete(false);
p1.setTitle("New ICE Item Project");
......@@ -75,52 +87,123 @@ public class ICEItemTemplate extends OptionTemplateSection {
addOption(KEY_CLASS_NAME , "Item Class Base Name" , "" , 0);
addOption(KEY_JOB_LAUNCHER_EXT, "Create Job Launcher?", true, 0);
addOption(KEY_MODEL_EXT, "Create Model?", true, 0);
addOption(KEY_IO_FORMAT_EXT, "File Format", getIOFormatOptions(), "", 0);
addDataOption(KEY_DEFAULT_FILE_NAME, "Choose a default dataset (leave blank for none):", "", 0);
}
@Override
protected URL getInstallURL() {
return Platform.getBundle(BUNDLE_ID).getEntry("/");
}
@Override
protected ResourceBundle getPluginResourceBundle() {
return new ICEProjectResources();
}
@Override public String[] getNewFiles() { return new String[0]; }
@Override public String getSectionId() { return "ICEItem"; }
@Override public String getUsedExtensionPoint() { return EXTENSION_POINT; }
public void setExtensionName(String extName) {
addOption(KEY_EXTENSION_NAME, "Extension Base Name", extName, 0);
}
protected String getFormattedPackageName(String id) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < id.length(); i++) {
char ch = id.charAt(i);
if (buffer.length() == 0) {
if (Character.isJavaIdentifierStart(ch))
buffer.append(Character.toLowerCase(ch));
} else {
if (Character.isJavaIdentifierPart(ch) || ch == '.')
buffer.append(ch);
/**
* Determines the supported IO formats provided by ICE's IOService. These
* are used to provide the option for users to automatically use readers
* and writers for their new items.
*
* @return An array of 2 element arrays with the names of the supported IO formats
*/
private String[][] getIOFormatOptions() {
ArrayList<String> readerTypes = new ArrayList<String>();
ArrayList<String> writerTypes = new ArrayList<String>();
try {
// Find readers
for (IReader reader : IReader.getIReaders()) {
readerTypes.add(reader.getReaderType());
}
// Find writers
for (IWriter writer : IWriter.getIWriters()) {
writerTypes.add(writer.getWriterType());
}
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get IO formats that have both readers and writers
ArrayList<String[]> ioFormats = new ArrayList<String[]>();
for (String writer : writerTypes) {
if (readerTypes.contains(writer))
ioFormats.add(new String[] {writer, writer});
}
return buffer.toString().toLowerCase(Locale.ENGLISH);
// Provide a blank option for those that will implement their own readers/writers
ioFormats.add(0, new String[] {"", ""});
String[][] options = new String[ioFormats.size()][2];
options = ioFormats.toArray(options);
return options;
}
/**
* Convert a camel case string to a capitalized title. Example:
* `camelCase` -> `Camel Case`
*
* @param s
* The string to convert
* @return the converted string
*/
protected static String splitCamelCase(String s) {
return s.replaceAll(
String.format("%s|%s|%s",
"(?<=[A-Z])(?=[A-Z][a-z])",
"(?<=[^A-Z])(?=[A-Z])",
"(?<=[A-Za-z])(?=[^A-Za-z])"
),
" "
).trim();
String.format("%s|%s|%s",
"(?<=[A-Z])(?=[A-Z][a-z])",
"(?<=[^A-Z])(?=[A-Z])",
"(?<=[A-Za-z])(?=[^A-Za-z])"
),
" ").trim();
}
/**
* Extra code to run when the finish button is pressed. This handles
* the creation of the default dataset inside of the newly created plugin,
* if one is chosen to be used.
*/
public void execute(IProject project, IPluginModelBase model, IProgressMonitor monitor) throws CoreException {
initializeFields(model);
super.execute(project, model, monitor);
Path filePath = Paths.get(getStringOption(KEY_DEFAULT_FILE_NAME));
if (filePath != null && filePath.toFile().exists()) {
String fileName = filePath.getFileName().toString();
if (project.exists() && !project.isOpen()) {
project.open(null);
}
IFolder dataFolder = project.getFolder("data");
if (!dataFolder.exists()) {
dataFolder.create(false, true, null);
}
IFile dataFile = dataFolder.getFile(fileName);
FileInputStream dataContents;
try {
dataContents = new FileInputStream(filePath.toString());
dataFile.create(dataContents, false, null);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* Adds a DataFileOption to the section.
*
* @param name
* The name of the variable that will be used in templating
* @param label
* A label to describe the option
* @param value
* The default value of the option
* @param pageIndex
* The page to put the option onto
* @return The data file option
*/
protected TemplateOption addDataOption(String name, String label, String value, int pageIndex) {
DataFileOption option = new DataFileOption(this, name, label);
option.setValue(value);
registerOption(option, value, pageIndex);
return option;
}
/**
* Additional steps to perform when creating the plugin project. This handles
* creation of the extensions that are needed by the new item, including
* models and job launchers.
*/
@Override
protected void updateModel(IProgressMonitor monitor) throws CoreException {
IPluginBase plugin;
......@@ -160,4 +243,33 @@ public class ICEItemTemplate extends OptionTemplateSection {
plugin.add(extension);
}
}
@Override
protected URL getInstallURL() {
return Platform.getBundle(BUNDLE_ID).getEntry("/");
}
@Override
protected ResourceBundle getPluginResourceBundle() {
return new ICEProjectResources();
}
@Override
public String[] getNewFiles() {
return new String[0];
}
@Override
public String getSectionId() {
return "ICEItem";
}
@Override
public String getUsedExtensionPoint() {
return EXTENSION_POINT;
}
public void setExtensionName(String extName) {
addOption(KEY_EXTENSION_NAME, "Extension Base Name", extName, 0);
}
}
package $packageName$.model;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.core.resources.IFile;
......@@ -17,16 +26,12 @@ import org.eclipse.ice.io.serializable.IReader;
import org.eclipse.ice.io.serializable.IWriter;
import org.eclipse.ice.item.model.Model;
@XmlRootElement(name = "$className$Model")
public class $className$Model extends Model {
// TODO:
// These need to be filled in before using this item
// They can be set in the setupItemInfo() method
private String writerName;
private String readerName;
private String ioFormat;
private String outputName;
// End required variables
private String exportString;
private IIOService ioService;
......@@ -57,11 +62,12 @@ public class $className$Model extends Model {
protected void setupItemInfo() {
setName("$className$ Model");