Commit 309db1bb authored by arbennett's avatar arbennett
Browse files

Adding support for default data sets; fixing tests.


Signed-off-by: default avatararbennett <bennett.andr@gmail.com>
parent 26949305
......@@ -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;
......
......@@ -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;
/**
......
......@@ -28,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,
......
......@@ -4,32 +4,49 @@ 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.pde.ui.templates.TemplateOption;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
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.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class DataFileOption extends TemplateOption {
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
* @param name
* @param label
*/
public DataFileOption(BaseOptionTemplateSection section, String name, String label) {
super(section, name, label);
description = label;
}
@Override
/**
* Create the user interface for the option
*/
public void createControl(Composite parent, int span) {
locationLabel = new Label(parent, SWT.NONE);
locationLabel.setText("Default dataset to load into the form:");
locationLabel.setText(description);
locationPathField = new Text(parent, SWT.BORDER);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
......@@ -39,33 +56,47 @@ public class DataFileOption extends TemplateOption {
browseButton = new Button(parent, SWT.PUSH);
browseButton.setText("Browse");
browseButton.addSelectionListener(new SelectionAdapter() {
browseButton.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent event) {
handleLocationBrowseButtonPressed();
fileChooser = new FileDialog(parent.getShell());
fileChooser.setText("Select File");
fileChooser.setFilterExtensions(new String[] {"*.*"});
chosenFile = fileChooser.open();
locationPathField.setText(chosenFile);
setValue(chosenFile);
}
});
}
private void handleLocationBrowseButtonPressed() {
String selectedFile = null;
String dirName = getPathFromLocationField();
@Override public void widgetDefaultSelected(SelectionEvent e) {}
});
}
private String getPathFromLocationField() {
/**
* Get the path from the location path field
*
* @return the path
*/
public String getDataFilePath() {
URI fieldURI;
try {
fieldURI = new URI(locationPathField.getText());
fieldURI = new URI(chosenFile);
} catch (URISyntaxException e) {
return locationPathField.getText();
return chosenFile;
}
String path = fieldURI.getPath();
return path != null ? path : locationPathField.getText();
return path != null ? path : chosenFile;
}
@Override
public boolean isEmpty() {
return getValue() == null;
}
/**
* Set whether the option is usable
*/
@Override
public void setEnabled(boolean enabled) {
locationLabel.setEnabled(enabled);
......
......@@ -13,12 +13,21 @@
package org.eclipse.ice.projectgeneration.templates;
import java.io.File;
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.ArrayList;
import java.util.Arrays;
import java.util.Locale;
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.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.ice.io.serializable.IReader;
......@@ -29,10 +38,13 @@ 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.AbstractChoiceOption;
import org.eclipse.pde.ui.templates.OptionTemplateSection;
import org.eclipse.pde.ui.templates.StringOption;
import org.eclipse.pde.ui.templates.TemplateOption;
/**
......@@ -52,11 +64,8 @@ public class ICEItemTemplate extends OptionTemplateSection {
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_SET_DEFAULT_FILE = "defaultFile";
protected static final String KEY_DEFAULT_FILE_NAME = "defaultFileName";
private ArrayList<TemplateOption> options;
/**
* Constructor
*/
......@@ -87,9 +96,7 @@ public class ICEItemTemplate extends OptionTemplateSection {
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);
addOption(KEY_SET_DEFAULT_FILE, "Include a default dataset?", false, 0);
addOption(KEY_DEFAULT_FILE_NAME, "Choose a default dataset:", new File(""), 0);
setOptionEnabled(KEY_DEFAULT_FILE_NAME, false);
addDataOption(KEY_DEFAULT_FILE_NAME, "Choose a default dataset (leave blank for none):", "", 0);
}
@Override
......@@ -162,15 +169,54 @@ public class ICEItemTemplate extends OptionTemplateSection {
).trim();
}
protected TemplateOption addOption(String name, String label, File value, int pageIndex) {
/**
*
*/
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();
}
}
}
/**
*
* @param name
* @param label
* @param value
* @param pageIndex
* @return
*/
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;
}
/**
*
*/
@Override
protected void updateModel(IProgressMonitor monitor) throws CoreException {
IPluginBase plugin;
......@@ -209,9 +255,5 @@ public class ICEItemTemplate extends OptionTemplateSection {
if (!extension.isInTheModel())
plugin.add(extension);
}
if (getBooleanOption(KEY_SET_DEFAULT_FILE)) {
setOptionEnabled(KEY_DEFAULT_FILE_NAME,true);
}
}
}
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,15 +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 ioFormat;
private String outputName;
// End required variables
private String exportString;
private IIOService ioService;
......@@ -59,7 +65,7 @@ public class $className$Model extends Model {
outputName = "$className$DefaultOutputName";
exportString = "Export to $className$ input format";
allowedActions.add(0, exportString);
ioFormat = $ioFormat$;
ioFormat = "$ioFormat$";
reader = ioService.getReader(ioFormat);
writer = ioService.getWriter(ioFormat);
}
......@@ -165,14 +171,57 @@ public class $className$Model extends Model {
*/
@Override
public void loadInput(String fileName) {
IFile inputFile = null;
File temp = null;
if (fileName == null) {
try {
// Create a filepath for the default file
fileName = Paths.get("$defaultFileName$").getFileName().toString();
if (fileName.isEmpty()) {
return;
}
String defaultFilePath = project.getLocation().toOSString()
+ System.getProperty("file.separator")
+ fileName;
// Create a temporary location to load the default file
temp = new File(defaultFilePath);
if (!temp.exists()) {
temp.createNewFile();
}
// Pull the default file from inside the plugin
URI uri = new URI(
"platform:/plugin/$packageName$/data/" + fileName);
InputStream reader = uri.toURL().openStream();
FileOutputStream outStream = new FileOutputStream(temp);
// Read in the file and set up the form
IFile inputFile = project.getFile(fileName);
// Write out the default file from the plugin to the temp location
int fileByte;
while ((fileByte = reader.read()) != -1) {
outStream.write(fileByte);
}
outStream.close();
project.refreshLocal(IResource.DEPTH_INFINITE, null);
inputFile = project.getFile(fileName);
} catch (URISyntaxException e) {
logger.error(getClass().getName() + " Exception!",e);
} catch (MalformedURLException e) {
logger.error(getClass().getName() + " Exception!",e);
} catch (IOException e) {
logger.error(getClass().getName() + " Exception!",e);
} catch (CoreException e) {
logger.error(getClass().getName() + " Exception!",e);
}
} else {
// Get the file
inputFile = project.getFile(fileName);
}
form = reader.read(inputFile);
form.setName(getName());
form.setDescription(getDescription());
form.setId(getId());
form.setItemID(getId());
}
}
......@@ -217,7 +217,7 @@ public class PROTEUSModel extends Item {
if (components.size() > 0) {
// create a new IPSWriter with the output file
INIWriter writer = (INIWriter) ioService.getWriter("INIWriter");
INIWriter writer = (INIWriter) ioService.getWriter("INI");
writer.setSectionPattern("!", " ");
try {
// Write the output file
......@@ -273,7 +273,7 @@ public class PROTEUSModel extends Item {
+ inputFile.getLocation().toOSString());
// Set up the reader to use the template if it exists
ITemplatedReader reader = ioService.getTemplatedReader("INIReader");
ITemplatedReader reader = ioService.getTemplatedReader("INI");
reader.setCommentString("!");
reader.setAssignmentPattern("\\s\\s\\s+");
if (new File(templateFile.getLocation().toOSString()).exists()) {
......
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