Commit dc9c3777 authored by amccaskey's avatar amccaskey
Browse files

Providing way to rename Item



Addressing bug 481426 by providing a cleaner way to rename an Item
through the IPersistenceProvider. Also updating the form editor header
contents to display the type of the Item plus its Id, and the form
editor top tab the item XML file name.  
Signed-off-by: default avataramccaskey <mccaskeyaj@ornl.gov>
parent 529ac8d8
......@@ -175,7 +175,7 @@ public class MOOSETreeCompositeView extends TreeCompositeViewer implements
// Set the MOOSE tree based on the editor's current configuration.
int treeId = MOOSEModel.mooseTreeCompositeId;
setInput((TreeComposite) form.getComponent(treeId), getFormEditor());
setInput((TreeComposite) form.getComponent(treeId), editor);
}
return;
......
......@@ -25,6 +25,7 @@ import org.eclipse.ice.datastructures.ICEObject.Component;
import org.eclipse.ice.datastructures.ICEObject.ICEObject;
import org.eclipse.ice.datastructures.ICEObject.IUpdateable;
import org.eclipse.ice.datastructures.ICEObject.IUpdateableListener;
import org.eclipse.ice.datastructures.ICEObject.Identifiable;
import org.eclipse.ice.datastructures.ICEObject.ListComponent;
import org.eclipse.ice.datastructures.componentVisitor.IComponentVisitor;
import org.eclipse.ice.datastructures.componentVisitor.IReactorComponent;
......@@ -257,7 +258,8 @@ public class ICEFormEditor extends SharedHeaderFormEditor
IMessageProvider.WARNING);
} else {
messageManager.removeMessage("statusUpdate");
//messageManager.addMessage("statusUpdate", "Form Saved", null, IMessageProvider.INFORMATION);
// messageManager.addMessage("statusUpdate", "Form Saved", null,
// IMessageProvider.INFORMATION);
}
}
......@@ -734,7 +736,7 @@ public class ICEFormEditor extends SharedHeaderFormEditor
form.setHeadClient(headClient);
// Set Form name
form.setText(iceDataForm.getName() + " " + iceDataForm.getId());
form.setText(itemName);
return;
}
......@@ -826,6 +828,8 @@ public class ICEFormEditor extends SharedHeaderFormEditor
return sectionPages;
}
private String itemName;
/**
* This operation overrides init so that the ICE Form, passed as an
* IEditorInput, can be stored.
......@@ -838,8 +842,12 @@ public class ICEFormEditor extends SharedHeaderFormEditor
@Override
public void init(IEditorSite site, IEditorInput input) throws RuntimeException {
// Set the part name
setPartName(input.getName());
IClient client = null;
try {
client = IClient.getClient();
} catch (CoreException e1) {
e1.printStackTrace();
}
// Set the site
setSite(site);
......@@ -850,33 +858,50 @@ public class ICEFormEditor extends SharedHeaderFormEditor
if (input instanceof ICEFormInput) {
ICEFormInput = (ICEFormInput) input;
iceDataForm = ICEFormInput.getForm();
// Set the part name to be the file name
setPartName(iceDataForm.getName() + ".xml");
// Set the input
setInput(input);
} else if (input instanceof FileEditorInput) {
} else if (input instanceof FileEditorInput && client != null) {
// Grab the file and load the form
IFile formFile = ((FileEditorInput) input).getFile();
// try {
// IClient client = IClient.getClient();
iceDataForm = client.loadItem(formFile);
logger.info("IClient and Form loaded.");
// Set *correct* input via a little short circuit.
ICEFormInput = new ICEFormInput(iceDataForm);
setInput(ICEFormInput);
// Set the part name to be the file name
setPartName(input.getName());
// Register the client as a listener
// of specific form editor events.
try {
IClient client = IClient.getClient();
iceDataForm = client.loadItem(formFile);
logger.info("IClient and Form loaded.");
// Set *correct* input via a little short circuit.
ICEFormInput = new ICEFormInput(iceDataForm);
setInput(ICEFormInput);
// Register the client as a listener
// of specific form editor events.
registerUpdateListener(IUpdateEventListener.getUpdateEventListener());
registerProcessListener(IProcessEventListener.getProcessEventListener());
registerResourceProvider(ISimpleResourceProvider.getSimpleResourceProvider());
} catch (CoreException e) {
// Complain
logger.error("Unable to get IClient instance!", e);
logger.error("Unable to get register the update, process, or simpleresource implementations!", e);
}
} else {
// Throw errors if the type is wrong
logger.error("Unable to load Form Editor!");
throw new RuntimeException(
"Input passed to ICEFormEditor.init()" + " is not of type ICEFormInput or FileEditorInput.");
throw new RuntimeException("Input passed to ICEFormEditor.init()"
+ " is not of type ICEFormInput or FileEditorInput, or the IClient instance is null.");
}
// Get the Item Name for the Form Header.
for (Identifiable i : client.getItems()) {
if (iceDataForm.getItemID() == i.getId()) {
itemName = i.getClass().getSimpleName() + " Item " + i.getId();
break;
}
}
iceDataForm.register(this);
......@@ -1337,7 +1362,8 @@ public class ICEFormEditor extends SharedHeaderFormEditor
PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
managedForm.getForm().getForm().setText(iceDataForm.getName() + " " + iceDataForm.getId());
// FIXME WANT THIS TO REMAIN THE ITEM TYPE + ID
setPartName(iceDataForm.getName() + ".xml");
managedForm.getForm().getForm().redraw();
}
});
......
......@@ -57,6 +57,8 @@ public class FakePersistenceProvider implements IPersistenceProvider {
*/
private volatile boolean deleted = false;
private volatile boolean renamed = false;
/**
* <p>
* This operation returns true if the Items were loaded, false otherwise.
......@@ -83,6 +85,7 @@ public class FakePersistenceProvider implements IPersistenceProvider {
persisted = false;
updated = false;
deleted = false;
renamed = false;
}
......@@ -232,8 +235,13 @@ public class FakePersistenceProvider implements IPersistenceProvider {
}
@Override
public boolean renameItem(Item item, String newName) {
public void renameItem(Item item, String newName) {
renamed = true;
return;
}
public boolean itemRenamed() {
// TODO Auto-generated method stub
return false;
return renamed;
}
}
\ No newline at end of file
......@@ -267,6 +267,8 @@ public class ItemManagerTester {
int itemId = itemManager.createItem(fakeGeometryBuilder.getItemName(), null);
itemManager.renameItem(itemId, "New Name");
assertTrue("New Name".equals(itemManager.retrieveItem(itemId).getName()));
assertTrue(fakePersistenceProvider.itemRenamed());
fakePersistenceProvider.reset();
}
/**
......
......@@ -706,6 +706,7 @@ public class ItemManager implements ItemListener {
logger.info("ItemManager Message: Updating all Items with "
+ "Persistence Provider.");
for (Item item : itemList.values()) {
logger.info("ItemManager Message: Persisting " + item.getName());
provider.updateItem(item);
}
}
......
......@@ -95,12 +95,13 @@ public interface IPersistenceProvider {
public boolean updateItem(Item item);
/**
* Renames the item in the IPersistenceProvider.
*
* @param item
* @param newName
* @return
* @param item The item to be renamed.
* @param newName The new name of the Item
* @return
*/
public boolean renameItem(Item item, String newName);
public void renameItem(Item item, String newName);
/**
* Returns all the Items in the persistence piece.
......
......@@ -237,6 +237,33 @@ public class XMLPersistenceProviderTester {
return;
}
/**
* Check that we can rename Items with the persistence provider.
*
*/
@Test
public void checkRename() {
// Create a MOOSE item
MOOSEModelBuilder builder = new MOOSEModelBuilder();
Item item = builder.build(project);
String name = item.getName().replace(" ", "_") + ".xml";
// Persist it
assertTrue(xmlpp.persistItem(item));
// Wait while the file is persisted. The MOOSE Model takes about a half
// a second, but lets wait two.
pause(2);
xmlpp.renameItem(item, "testItemName");
pause(2);
assertTrue(project.getFile("testItemName.xml").exists());
assertFalse(project.getFile(name).exists());
}
/**
* This operation checks the ability of the XMLPersistenceProvider to
* persist Items to its project space. It also checks update() since that
......
......@@ -76,14 +76,12 @@ import org.slf4j.LoggerFactory;
* @author Jay Jay Billings
*
*/
public class XMLPersistenceProvider
implements IPersistenceProvider, Runnable, IReader, IWriter {
public class XMLPersistenceProvider implements IPersistenceProvider, Runnable, IReader, IWriter {
/**
* Logger for handling event messages and other information.
*/
private static final Logger logger = LoggerFactory
.getLogger(XMLPersistenceProvider.class);
private static final Logger logger = LoggerFactory.getLogger(XMLPersistenceProvider.class);
/**
* An atomic boolean used to manage the event loop. It is set to true when
......@@ -125,8 +123,7 @@ public class XMLPersistenceProvider
* The blocking queue that holds all of the persistence tasks that are left
* to be processed.
*/
ArrayBlockingQueue<QueuedTask> taskQueue = new ArrayBlockingQueue<QueuedTask>(
1024);
ArrayBlockingQueue<QueuedTask> taskQueue = new ArrayBlockingQueue<QueuedTask>(1024);
/**
* A private thread on which the event loop is run. The runnable for this
......@@ -220,8 +217,8 @@ public class XMLPersistenceProvider
// Only add the resources that are xml files with the format
// that we expect. This uses a regular expression that checks
// for <itemName>_<itemId>.xml.
if (resource.getType() == IResource.FILE && resource.getName()
.matches("^[a-zA-Z0-9_\\-]*_\\d+\\.xml$")) {
if (resource.getType() == IResource.FILE
&& resource.getName().matches("^[a-zA-Z0-9_\\-]*_\\d+\\.xml$")) {
names.add(resource.getName());
}
}
......@@ -229,15 +226,13 @@ public class XMLPersistenceProvider
// Get the ids of all of the items from the file names and map them
// to the names.
for (String name : names) {
logger.info("XMLPersistenceProvider Message: "
+ "Found persisted Item at " + name);
logger.info("XMLPersistenceProvider Message: " + "Found persisted Item at " + name);
// Remove the file extension
String[] nameParts = name.split("\\.");
String nameMinusExt = nameParts[0];
// Get the id from the end
String[] nameMinusExtParts = nameMinusExt.split("_");
String idString = nameMinusExtParts[nameMinusExtParts.length
- 1];
String idString = nameMinusExtParts[nameMinusExtParts.length - 1];
id = Integer.valueOf(idString);
// Put the info in the map
itemIdMap.put(id, name);
......@@ -367,8 +362,7 @@ public class XMLPersistenceProvider
*/
public void addBuilder(ItemBuilder builder) {
logger.info("XMLPersistenceProvider Message: " + "Item "
+ builder.getItemName() + " registered.");
logger.info("XMLPersistenceProvider Message: " + "Item " + builder.getItemName() + " registered.");
// Build an Item from this builder and store it so that we can get its
// class info later.
......@@ -396,14 +390,12 @@ public class XMLPersistenceProvider
Marshaller marshaller;
try {
marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(obj, outputStream);
} catch (JAXBException e) {
// Complain
logger.error(getClass().getName() + " Exception!", e);
logger.info("XMLPersistenceProvider Message: "
+ "Failed to execute persistence task for " + obj);
logger.info("XMLPersistenceProvider Message: " + "Failed to execute persistence task for " + obj);
}
return outputStream;
}
......@@ -420,8 +412,7 @@ public class XMLPersistenceProvider
// Create an output stream containing the XML.
ByteArrayOutputStream outputStream = createXMLStream(obj);
// Convert it to an input stream so it can be pushed to file
ByteArrayInputStream inputStream = new ByteArrayInputStream(
outputStream.toByteArray());
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
try {
// Update the output file if it already exists
if (file.exists()) {
......@@ -453,8 +444,7 @@ public class XMLPersistenceProvider
// Handle the task if it is available
if (currentTask != null) {
// Get the file name if this is a persist or delete
if ("persist".equals(currentTask.task)
|| "delete".equals(currentTask.task)) {
if ("persist".equals(currentTask.task) || "delete".equals(currentTask.task)) {
// Setup the file name
name = currentTask.item.getName().replaceAll("\\s+", "_")
/* + "_" + currentTask.item.getId() */ + ".xml";
......@@ -462,38 +452,54 @@ public class XMLPersistenceProvider
// This may change depending on whether or not this Item was
// created in the default project.
file = currentTask.item.getProject().getFile(name);
System.out.println("Persist: Getting file = " + file.getName());
}
// Process persists
if ("persist".equals(currentTask.task)
&& !(currentTask.item instanceof ReactorAnalyzer)) {
if ("persist".equals(currentTask.task) && !(currentTask.item instanceof ReactorAnalyzer)) {
// Send the Item off to be written to the file
writeFile(currentTask.item, file);
// Update the item id map
itemIdMap.put(currentTask.item.getId(), file.getName());
} else if ("delete".equals(currentTask.task) && file.exists()) {
} else if ("delete".equals(currentTask.task)) {
// Handle deletes
file.delete(true, null);
// Make sure it exists, the platform may have deleted it
// first
if (file.exists()) {
file.delete(true, null);
}
// Update the item id map
itemIdMap.remove(currentTask.item.getId());
} else if ("write".equals(currentTask.task)) {
// Deal with simple Form write requests from the IWriter
// interface.
writeFile(currentTask.form, currentTask.file);
} else if("rename".equals(currentTask.task)) {
itemIdMap.put(currentTask.item.getId(), currentTask.file.getName());
} else if ("rename".equals(currentTask.task)) {
String oldFile = itemIdMap.get(currentTask.item.getId());
if (oldFile != null) {
itemIdMap.put(currentTask.item.getId(), currentTask.file.getName());
// Sleep for a bit to let the platform delete if it wants
Thread.currentThread();
Thread.sleep(1000);
IProject project = currentTask.item.getProject();
IFile oldFileHandle = project.getFile(oldFile);
if (oldFileHandle.exists()) {
oldFileHandle.move(currentTask.file.getProjectRelativePath(), true, null);
}
}
}
} else {
// Otherwise sleep for a bit
Thread.currentThread();
Thread.sleep(1000);
}
} catch (InterruptedException e) {
} catch (InterruptedException | CoreException e) {
// Complain
logger.error(getClass().getName() + " Exception!", e);
} catch (CoreException e) {
// Complain
logger.error(getClass().getName() + " Exception!", e);
}
}
return;
}
......@@ -522,12 +528,20 @@ public class XMLPersistenceProvider
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.ice.item.persistence.IPersistenceProvider#renameItem(org.
* eclipse.ice.item.Item, java.lang.String)
*/
@Override
public boolean renameItem(Item item, String newName) {
IFile newFile = item.getProject().getFile(newName);
return submitTask(item, "rename", item.getForm(), newFile);
public void renameItem(Item item, String newName) {
IFile newFile = item.getProject().getFile(newName+".xml");
submitTask(item, "rename", item.getForm(), newFile);
return;
}
/**
* A private utility operation that submits a persistence task to the queue.
*
......@@ -541,8 +555,7 @@ public class XMLPersistenceProvider
* @return True if the task was submitted, false if there was some exception
* or the Item was null.
*/
private boolean submitTask(Item item, String taskName, Form form,
IFile file) {
private boolean submitTask(Item item, String taskName, Form form, IFile file) {
// Local Declarations
boolean retVal = true;
......@@ -553,7 +566,7 @@ public class XMLPersistenceProvider
// Setup the task
task.item = item;
task.task = taskName;
if (file != null) {
task.file = file;
}
......@@ -650,8 +663,7 @@ public class XMLPersistenceProvider
@Override
public Item loadItem(IResource itemResource) {
// If the IResource is an IFile, load it and otherwise return null.
return (itemResource instanceof IFile) ? loadItem((IFile) itemResource)
: null;
return (itemResource instanceof IFile) ? loadItem((IFile) itemResource) : null;
}
/**
......@@ -733,8 +745,7 @@ public class XMLPersistenceProvider
public void replace(IFile file, String regex, String value) {
try {
throw new OperationNotSupportedException(
"XMLPersistenceProvider Error: "
+ "IWriter.replace() is not supported.");
"XMLPersistenceProvider Error: " + "IWriter.replace() is not supported.");
} catch (OperationNotSupportedException e) {
// TODO Auto-generated catch block
logger.error(getClass().getName() + " Exception!", e);
......@@ -787,8 +798,7 @@ public class XMLPersistenceProvider
public ArrayList<Entry> findAll(IFile file, String regex) {
try {
throw new OperationNotSupportedException(
"XMLPersistenceProvider Error: "
+ "IReader.findAll() is not supported.");
"XMLPersistenceProvider Error: " + "IReader.findAll() is not supported.");
} catch (OperationNotSupportedException e) {
// TODO Auto-generated catch block
logger.error(getClass().getName() + " Exception!", e);
......
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