From 4c379d0d99d5ba91548db1c21525cbf489271205 Mon Sep 17 00:00:00 2001 From: Kasper Gammeltoft Date: Thu, 18 Jun 2015 15:55:31 -0400 Subject: [PATCH] Fixed add dialog for add material wizard. Added MaterialStack class for better implementation of composites. Updated UI to work with MaterialStack in the Materials Database Editor. Signed-off-by: Kasper Gammeltoft --- .../ice/datastructures/form/Material.java | 71 +++++++- .../datastructures/form/MaterialStack.java | 168 ++++++++++++++++++ .../materials/ui/AddMaterialWizardPage.java | 9 +- .../ice/materials/ui/MaterialDetailsPage.java | 36 +++- .../ui/MaterialsDatabaseContentProvider.java | 13 +- .../ui/MaterialsDatabaseLabelProvider.java | 2 +- .../MaterialsDatabaseMasterDetailsBlock.java | 80 ++++++--- .../MaterialStackWritableTableFormat.java | 144 +++++++++++++++ .../datastructures/test/MaterialTester.java | 7 +- .../test/TestMaterialFactory.java | 18 +- .../materials/test/TestMaterialFactory.java | 2 + 11 files changed, 488 insertions(+), 62 deletions(-) create mode 100644 src/org.eclipse.ice.datastructures/src/org/eclipse/ice/datastructures/form/MaterialStack.java create mode 100644 src/org.eclipse.ice.materials/src/org/eclipse/ice/materials/MaterialStackWritableTableFormat.java diff --git a/src/org.eclipse.ice.datastructures/src/org/eclipse/ice/datastructures/form/Material.java b/src/org.eclipse.ice.datastructures/src/org/eclipse/ice/datastructures/form/Material.java index db7d0d6f5..34f766244 100644 --- a/src/org.eclipse.ice.datastructures/src/org/eclipse/ice/datastructures/form/Material.java +++ b/src/org.eclipse.ice.datastructures/src/org/eclipse/ice/datastructures/form/Material.java @@ -22,6 +22,8 @@ import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; +import org.eclipse.ice.datastructures.form.MaterialStack; + /** * This class represents physical Materials. * @@ -117,8 +119,8 @@ public class Material implements Cloneable, Comparable { /** * The list of components that comprise this material. */ - @XmlElement(name = "Material") - private List components; + //@XmlElement(name = "Material") + private List components; /** * The constructor. @@ -126,7 +128,7 @@ public class Material implements Cloneable, Comparable { public Material() { name = ""; properties = new HashMap(); - components = new ArrayList(); + components = new ArrayList(); } /** @@ -205,19 +207,70 @@ public Map getProperties() { * @return The list of Materials that make up this one. Changing this list * will not affect list stored by the Material. */ - public List getComponents() { - return new ArrayList(components); + public List getComponents() { + return new ArrayList(components); } /** * This operation adds a component to this material, effectively marking - * this Materials as being a composite of others. + * this Material as being a composite of others. It tries to see if the + * material added already exists in this material's components, and if so + * just increments the amount. Otherwise, adds the new material as a + * material stack with an amount of 1. * * @param component - * The material that is a component of this material. + * The material that is a component of this material to be added. */ public void addComponent(Material component) { - components.add(component); + // Do not want to make a new stack if the material already is a + // component + // of this one. + boolean added = false; + // Iterate over stacks to try to find the material + for (MaterialStack stack : components) { + Material m = stack.getMaterial(); + if (component.equals(m)) { + // Add the material + stack.setAmount(stack.getAmount() + 1); + added = true; + break; + } + } + // Not found already, create a new material stack and add to components + if (!added) { + components.add(new MaterialStack(component, 1)); + } + } + + /** + * This operation adds a component to this material, effectivly marking this + * Material as being a composite of others. It tries to see if the material + * stack given is a stack of an existing compoenent of this material, and if + * so updates the amount for that stack. Otherwise, adds the new stack to + * the list of components. + * + * @param stack + */ + public void addComponent(MaterialStack stack) { + // Do not want to add a new stack if the material in the stack already + // is a component of this one. + boolean added = false; + Material mat = stack.getMaterial(); + // Iterate over stacks to try to find the material that fits the new + // stack + for (MaterialStack matStack : components) { + Material m = matStack.getMaterial(); + if (mat.equals(m)) { + // Add the stack amount to the existing stack + matStack.setAmount(matStack.getAmount() + stack.getAmount()); + added = true; + break; + } + } + // Not found already, then just add the stack to the components + if (!added) { + components.add(stack); + } } /** @@ -280,7 +333,7 @@ public void copy(Material material) { if (material != null && material != this) { this.name = material.name; this.properties = new HashMap(material.properties); - this.components = new ArrayList(material.components); + this.components = new ArrayList(material.components); } } diff --git a/src/org.eclipse.ice.datastructures/src/org/eclipse/ice/datastructures/form/MaterialStack.java b/src/org.eclipse.ice.datastructures/src/org/eclipse/ice/datastructures/form/MaterialStack.java new file mode 100644 index 000000000..2e2567012 --- /dev/null +++ b/src/org.eclipse.ice.datastructures/src/org/eclipse/ice/datastructures/form/MaterialStack.java @@ -0,0 +1,168 @@ +/******************************************************************************* + * Copyright (c) 2013, 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 - + * Kasper Gammeltoft + *******************************************************************************/ +package org.eclipse.ice.datastructures.form; + +/** + * A class to encapsulate a multitude of the same material. This is a much + * better solution than having a material hold information about its own amount + * in a stack. + * + * + * @author Kasper Gammeltoft + * + */ +public class MaterialStack implements Comparable { + + /** + * The material in this material stack + */ + private Material material; + + /** + * The amount in this material stack + */ + private int number; + + /** + * Null constructor. + */ + public MaterialStack() { + material = null; + number = 0; + } + + /** + * Create a new material stack for holding a material. + * + * @param material + * The material to hold. + * @param amount + * How many to hold. + */ + public MaterialStack(Material material, int amount) { + this.material = material; + number = amount; + + } + + /** + * Gets the material held in this stack. + * + * @return The material + */ + public Material getMaterial() { + return material; + } + + /** + * Sets the material that should be held in this stack. Note- does not reset + * the amount held. + * + * @param material + * The material to take over the stack. + */ + public void setMaterial(Material material) { + this.material = material; + } + + /** + * The amount of the material held in this stack. + * + * @return The amount in the stack + */ + public int getAmount() { + return number; + } + + /** + * Sets the amount of the material in the stack. + * + * @param amount + * The amount + */ + public void setAmount(int amount) { + number = amount; + } + + /** + * Adds one to the amount on the stack. A convenience method. The same + * effect could be achieved by writing MaterialStack.add(1). + */ + public void incrimentAmount() { + number++; + } + + /** + * Adds the amount specified to the stacks existing amount. + * + * @param amount + * The amount to add + */ + public void addAmount(int amount) { + number += amount; + } + + /** + * Compares two material stacks to tell if they are equal to one another. + * + * @param stack + * The stack to compare this one with + * @return Returns true if the stacks are equal (same material, same amount) + * and false if otherwise. + */ + public boolean equals(MaterialStack stack) { + return number == stack.getAmount() + && material.equals(stack.getMaterial()); + } + + /** + * Compares two material stacks to see which should be first in a list + * containing material stacks. Compares based on the same logic for the + * materials, but will list lesser (in amount) material stacks before + * greater if the materials are the same when compared. + * + * @param + * stack The material stack to compare to this one. + */ + @Override + public int compareTo(MaterialStack stack) { + int retVal = 0; + // The material names are the same. Order from less amount to greater + // amount. + if (material.getName().toLowerCase() + .equals(stack.getMaterial().getName().toLowerCase())) { + if (number < stack.getAmount()) { + retVal = -1; + } else if (number == stack.getAmount()) { + retVal = 0; + } else { + retVal = 1; + } + } else { + retVal = material.compareTo(stack.getMaterial()); + } + + return retVal; + } + + /** + * Gets this material stack as a string. In the format [Material Stack:[ of + * Material[some material name] and amount[#]] + */ + @Override + public String toString() { + String str = "Material Stack:[ of Material[" + material.getName() + + "] and amount[" + number + "]"; + return str; + } + +} diff --git a/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/AddMaterialWizardPage.java b/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/AddMaterialWizardPage.java index 5f24a1818..e31f1613d 100644 --- a/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/AddMaterialWizardPage.java +++ b/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/AddMaterialWizardPage.java @@ -18,7 +18,7 @@ import org.eclipse.ice.datastructures.ICEObject.IElementSource; import org.eclipse.ice.datastructures.ICEObject.ListComponent; import org.eclipse.ice.datastructures.form.Material; -import org.eclipse.ice.materials.MaterialStack; +import org.eclipse.ice.datastructures.form.MaterialStack; import org.eclipse.ice.materials.MaterialStackWritableTableFormat; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.window.Window; @@ -156,16 +156,15 @@ public Material getMaterial() { // Add the stoichiometric components as components of the new material. for (MaterialStack stack : list) { - Material m = stack.getMaterial(); - matList.add(m); - material.addComponent(m); + material.addComponent(stack); } // Following John Ankner's code ported from Visual Basic int molMass = 0; for(MaterialStack stack: list){ - //molMass+=stack.getAmount()*(stack.getMaterial().getProperty(key)) + molMass+=stack.getAmount()*(stack.getMaterial().getProperty(Material.ATOMIC_MASS)); + } return material; diff --git a/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/MaterialDetailsPage.java b/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/MaterialDetailsPage.java index d2b890f39..fa78ec4ad 100644 --- a/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/MaterialDetailsPage.java +++ b/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/MaterialDetailsPage.java @@ -17,6 +17,7 @@ import org.eclipse.ice.client.widgets.ListComponentNattable; import org.eclipse.ice.datastructures.ICEObject.ListComponent; import org.eclipse.ice.datastructures.form.Material; +import org.eclipse.ice.datastructures.form.MaterialStack; import org.eclipse.ice.materials.IMaterialsDatabase; import org.eclipse.ice.materials.SingleMaterialWritableTableFormat; import org.eclipse.jface.dialogs.MessageDialog; @@ -199,14 +200,22 @@ public void selectionChanged(IFormPart part, ISelection selection) { // Grab the selection Object structuredSelection = ((IStructuredSelection) selection) .getFirstElement(); - if (structuredSelection instanceof Material) { - // Updates the material to the new selection - material = (Material) structuredSelection; + // Make sure that the selection can be handled by this details page + if (structuredSelection instanceof Material + || structuredSelection instanceof MaterialStack) { - // Creates new table if this is the first selection of a material. + if (structuredSelection instanceof Material) { + material = (Material) structuredSelection; + } else { + material = ((MaterialStack) structuredSelection).getMaterial(); + } + + // Creates new table if the selections change from one form to + // another. if (natTable == null) { + System.out.println("Making table"); // Creates new listComponent for the table data. list = new ListComponent(); @@ -241,6 +250,7 @@ public void selectionChanged( }); } else { + System.out.println("Not making table"); // Clears out any existing entries in the list list.clear(); // Gets the property names or column names for the table. @@ -269,9 +279,10 @@ public void selectionChanged( */ @Override public void createContents(Composite parent) { + // Make sure that we are not re-creating our contents! // Get the shell for the add property dialog shell = parent.getShell(); - + System.out.println("Creating contents"); // Set the layout for the parent GridLayout parentGridLayout = new GridLayout(1, true); parent.setLayout(parentGridLayout); @@ -301,6 +312,10 @@ public void createContents(Composite parent) { // Finally tell the section about its client section.setClient(sectionClient); + if(natTable!=null){ + natTable.getTable().setParent(sectionClient); + natTable.getTable().refresh(); + } // Sets the sectionClient color to overrule the table's background sectionClient.setBackground(Display.getCurrent().getSystemColor( SWT.COLOR_WHITE)); @@ -310,8 +325,8 @@ public void createContents(Composite parent) { // or removing properties Composite buttonComposite = new Composite(sectionClient, SWT.NONE); buttonComposite.setLayout(new GridLayout(1, false)); - buttonComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, - true, 1, 1)); + buttonComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, + false, true, 1, 1)); // Create the Add button Button addMaterialButton = new Button(buttonComposite, SWT.PUSH); @@ -356,13 +371,15 @@ public void widgetDefaultSelected(SelectionEvent arg0) { public void widgetSelected(SelectionEvent arg0) { // gets the selected property - String property = (String) natTable.getSelectedObjects().get(0); + String property = (String) natTable.getSelectedObjects() + .get(0); // Removes the property from the material. material.removeProperty(property); database.updateMaterial(material); - // Finally, removes the property string from the list so that it + // Finally, removes the property string from the list so + // that it // will // update on screen for the user. // Lock the list to avoid concurrent modifications @@ -386,4 +403,5 @@ public void widgetDefaultSelected(SelectionEvent arg0) { return; } + } \ No newline at end of file diff --git a/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/MaterialsDatabaseContentProvider.java b/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/MaterialsDatabaseContentProvider.java index 0cf406102..6673a7b1f 100644 --- a/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/MaterialsDatabaseContentProvider.java +++ b/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/MaterialsDatabaseContentProvider.java @@ -13,6 +13,7 @@ package org.eclipse.ice.materials.ui; import org.eclipse.ice.datastructures.form.Material; +import org.eclipse.ice.datastructures.form.MaterialStack; import org.eclipse.ice.materials.IMaterialsDatabase; import org.eclipse.jface.viewers.ITreeContentProvider; import org.eclipse.jface.viewers.TreeViewer; @@ -80,7 +81,10 @@ public Object[] getChildren(Object parentElement) { // This changes depending on whether it is the initial input or one of // the Materials under it - if (parentElement instanceof Material) { + if(parentElement instanceof MaterialStack){ + Material material = ((MaterialStack)parentElement).getMaterial(); + retVal = material.getComponents().toArray(); + } else if (parentElement instanceof Material) { Material material = (Material) parentElement; // Only return the children of this Material retVal = material.getComponents().toArray(); @@ -115,9 +119,14 @@ public boolean hasChildren(Object element) { // This changes depending on whether it is the initial input or one of // the Materials under it + if (element instanceof MaterialStack){ + Material material = ((MaterialStack)element).getMaterial(); + List children = material.getComponents(); + retVal = !children.isEmpty(); + } if (element instanceof Material) { Material material = (Material) element; - List children = material.getComponents(); + List children = material.getComponents(); retVal = !children.isEmpty(); } else if (element instanceof ArrayList) { ArrayList materials = (ArrayList) element; diff --git a/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/MaterialsDatabaseLabelProvider.java b/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/MaterialsDatabaseLabelProvider.java index e3f47d240..2d7ada00d 100644 --- a/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/MaterialsDatabaseLabelProvider.java +++ b/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/MaterialsDatabaseLabelProvider.java @@ -13,7 +13,7 @@ package org.eclipse.ice.materials.ui; import org.eclipse.ice.datastructures.form.Material; -import org.eclipse.ice.materials.MaterialStack; +import org.eclipse.ice.datastructures.form.MaterialStack; import org.eclipse.jface.viewers.LabelProvider; /** diff --git a/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/MaterialsDatabaseMasterDetailsBlock.java b/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/MaterialsDatabaseMasterDetailsBlock.java index 444a7decb..8574f0f3b 100644 --- a/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/MaterialsDatabaseMasterDetailsBlock.java +++ b/src/org.eclipse.ice.materials.ui/src/org/eclipse/ice/materials/ui/MaterialsDatabaseMasterDetailsBlock.java @@ -22,6 +22,7 @@ import org.eclipse.e4.ui.workbench.IWorkbench; import org.eclipse.ice.datastructures.ICEObject.ListComponent; import org.eclipse.ice.datastructures.form.Material; +import org.eclipse.ice.datastructures.form.MaterialStack; import org.eclipse.ice.materials.IMaterialsDatabase; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.ISelectionChangedListener; @@ -70,7 +71,7 @@ public class MaterialsDatabaseMasterDetailsBlock extends MasterDetailsBlock { /** * The list that holds the materials database information. */ - List materials; + ListComponent materials; /** * The managed form for the block. @@ -153,12 +154,14 @@ protected void createMasterPart(IManagedForm managedForm, Composite parent) { // Create a sorted final list from the database for pulling the database // information - materials = materialsDatabase.getMaterials(); + materials = new ListComponent(); + materials.addAll(materialsDatabase.getMaterials()); + // Sorts the list according to the material compareTo operator Collections.sort(materials); // Create a copy of the master list for the table to display. - List editableCopy = new ArrayList(); + List editableCopy = new ArrayList(); for (int i = 0; i < materials.size(); i++) { editableCopy.add(materials.get(i)); } @@ -188,7 +191,7 @@ public void modifyText(ModifyEvent arg0) { int numRemoved = 0; for (int i = 0; i < materials.size(); i++) { - Material mat = materials.get(i); + Material mat = (Material) materials.get(i); String matName = ""; if (useElementName) { @@ -197,8 +200,7 @@ public void modifyText(ModifyEvent arg0) { matName = mat.getName(); } // Finally, if the material fits the filter, make sure it is - // in the list. Otherwise, - // take it out of the list. + // in the list. Otherwise, take it out of the list. if (matName.toLowerCase().startsWith(filterText)) { // make sure material is in list if (!listFromTree.contains(mat)) { @@ -223,9 +225,8 @@ public void modifyText(ModifyEvent arg0) { treeViewer.getTree().setLayout(new GridLayout(1, true)); // Sets the gridData to grab the available space, but to have only the - // treeview have the scrolling. - // This allows for the master tree to scroll without moving the details - // page out of the viewport. + // treeview have the scrolling. This allows for the master tree to + // scroll without moving the details page out of the viewport. GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1); data.widthHint = sectionClient.getClientArea().width; data.heightHint = sectionClient.getClientArea().height; @@ -250,32 +251,41 @@ public void selectionChanged(SelectionChangedEvent event) { // Create the Add button Button addMaterialButton = new Button(buttonComposite, SWT.PUSH); addMaterialButton.setText("Add"); - + // Add a listener to the add button to open the Add Material Wizard addMaterialButton.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { - // Create a wizard dialog to hold the AddMaterialWizard that will be + // Create a wizard dialog to hold the AddMaterialWizard that + // will be // used to create new materials. IWorkbenchWindow window = PlatformUI.getWorkbench() .getActiveWorkbenchWindow(); - AddMaterialWizard addMaterialWizard = new AddMaterialWizard(window, materialsDatabase); + AddMaterialWizard addMaterialWizard = new AddMaterialWizard( + window, materialsDatabase); addMaterialWizard.setWindowTitle("Create a new material"); - WizardDialog addMaterialDialog = new WizardDialog( - window.getShell(), addMaterialWizard); - + WizardDialog addMaterialDialog = new WizardDialog(window + .getShell(), addMaterialWizard); + // Get the new material to add - if(addMaterialDialog.open() == Window.OK){ + if (addMaterialDialog.open() == Window.OK) { Material newMaterial = addMaterialWizard.getMaterial(); materialsDatabase.addMaterial(newMaterial); - materials.add(newMaterial); + materials.getReadWriteLock().writeLock().lock(); + try{ + materials.add(newMaterial); + Collections.sort(materials); + } finally{ + materials.getReadWriteLock().writeLock().unlock(); + } + List listFromTree = (List) treeViewer .getInput(); listFromTree.add(newMaterial); Collections.sort(listFromTree); treeViewer.refresh(); } - + } @Override @@ -317,8 +327,14 @@ public void widgetSelected(SelectionEvent e) { Material toDelete = (Material) it.next(); // Remove the material from the user's database materialsDatabase.deleteMaterial(toDelete); - // Remove from the master materials list - materials.remove(toDelete); + + materials.getReadWriteLock().writeLock().lock(); + try { + // Remove from the master materials list + materials.remove(toDelete); + } finally { + materials.getReadWriteLock().writeLock().unlock(); + } // Remove the material from the tree viewer listFromTree.remove(toDelete); } @@ -361,14 +377,20 @@ public void widgetSelected(SelectionEvent e) { materialsDatabase.restoreDefaults(); // Create a sorted final list from the database for pulling // the database information - materials = materialsDatabase.getMaterials(); - // Sorts the list according to the material compareTo - // operator - Collections.sort(materials); + List newList = materialsDatabase.getMaterials(); + materials.getReadWriteLock().writeLock().lock(); + try { + materials.clear(); + materials.addAll(newList); + // Sorts the list according to the material compareTo + // operator + Collections.sort(materials); + } finally { + materials.getReadWriteLock().writeLock().unlock(); + } // Get the model from the treeViewer - List listFromTree = (List) treeViewer - .getInput(); + List listFromTree = (List) treeViewer.getInput(); // Refresh the list from the reset materials database listFromTree.clear(); for (int i = 0; i < materials.size(); i++) { @@ -401,8 +423,10 @@ public void widgetDefaultSelected(SelectionEvent e) { */ @Override protected void registerPages(DetailsPart detailsPart) { - detailsPart.registerPage(Material.class, new MaterialDetailsPage( - materialsDatabase)); + MaterialDetailsPage detailsPage = new MaterialDetailsPage(materialsDatabase); + MaterialDetailsPage stackDetailsPage = new MaterialDetailsPage(materialsDatabase); + detailsPart.registerPage(Material.class, detailsPage); + detailsPart.registerPage(MaterialStack.class, stackDetailsPage); } diff --git a/src/org.eclipse.ice.materials/src/org/eclipse/ice/materials/MaterialStackWritableTableFormat.java b/src/org.eclipse.ice.materials/src/org/eclipse/ice/materials/MaterialStackWritableTableFormat.java new file mode 100644 index 000000000..01bcce282 --- /dev/null +++ b/src/org.eclipse.ice.materials/src/org/eclipse/ice/materials/MaterialStackWritableTableFormat.java @@ -0,0 +1,144 @@ +/******************************************************************************* + * Copyright (c) 2013, 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 - + * Kasper Gammeltoft + *******************************************************************************/ +package org.eclipse.ice.materials; + +import org.eclipse.ice.datastructures.form.MaterialStack; + +import ca.odell.glazedlists.gui.WritableTableFormat; + +/** + * Implements the WritableTableFormat for use with glazed lists to display data + * about material stacks. Mainly used for the stoichiometry calculator at the + * moment. + * + * + * @author Kasper Gammeltoft + * + */ +public class MaterialStackWritableTableFormat implements + WritableTableFormat { + + /** + * The constructor + */ + public MaterialStackWritableTableFormat() { + // Nothing TODO + } + + /** + * Gets the column count, should be just 2 columns. The first displays the + * name and the second the amount in the stack. + * + * @return The number of columns + */ + @Override + public int getColumnCount() { + return 2; + } + + /** + * Gets the column name for the specified column. + * + * @param column + * The column to get the name for. Should either be 0 or 1 + * @return Returns "Material" if column is 0, and "Amount" if otherwise. + */ + @Override + public String getColumnName(int column) { + String colName; + if (column == 0) { + colName = "Material"; + } else { + colName = "Amount"; + } + return colName; + } + + /** + * Gets the value for the specified material stack and column. + * + * @param stack + * The material stack + * @param column + * The column (either 0 or 1). + * @return Returns either the name of the material (if column is 0) or the + * amount in the stack. + */ + @Override + public Object getColumnValue(MaterialStack stack, int column) { + Object value; + if (column == 0) { + value = stack.getMaterial().getName(); + } else { + value = stack.getAmount(); + } + return value; + } + + /** + * Should allow only for editing the amount, not the name of the material + * for the specified material stack. + * + * @param stack + * The material stack + * @param column + * The column to edit. Ignores if this is 0, otherwise returns + * true. + * @return Returns true if the stack is editable at that column, false if + * otherwise. + */ + @Override + public boolean isEditable(MaterialStack stack, int column) { + boolean val; + if (column == 0) { + val = false; + } else { + val = true; + } + val = true; + return val; + } + + /** + * Sets the column value for the specified material stack and column. Will + * only allow for the amount in the stack to be set. + * + * @param stack + * The material stack + * @param newValue + * The new value for the amount in the material stack. Should + * accept int, double, and String values. + */ + @Override + public MaterialStack setColumnValue(MaterialStack stack, Object newValue, + int column) { + if (column > 0) { + int value = stack.getAmount(); + if (newValue instanceof String) { + try { + value = (int) Double.parseDouble((String) newValue); + } catch (Exception e) { + + } + } else { + try { + value = (Integer) newValue; + } catch (Exception e) { + + } + } + stack.setAmount(value); + } + return stack; + } + +} diff --git a/tests/org.eclipse.ice.datastructures.test/src/org/eclipse/ice/datastructures/test/MaterialTester.java b/tests/org.eclipse.ice.datastructures.test/src/org/eclipse/ice/datastructures/test/MaterialTester.java index 421d368ae..e6012329e 100644 --- a/tests/org.eclipse.ice.datastructures.test/src/org/eclipse/ice/datastructures/test/MaterialTester.java +++ b/tests/org.eclipse.ice.datastructures.test/src/org/eclipse/ice/datastructures/test/MaterialTester.java @@ -25,6 +25,7 @@ import org.eclipse.ice.datastructures.ICEObject.ICEJAXBHandler; import org.eclipse.ice.datastructures.form.Material; +import org.eclipse.ice.datastructures.form.MaterialStack; import org.junit.Test; /** @@ -81,12 +82,12 @@ public void checkComponents() { // Check its components assertNotNull(testMaterial.getComponents()); - List components = testMaterial.getComponents(); + List components = testMaterial.getComponents(); assertEquals(2, components.size()); // Get the Materials - Material firstMaterial = components.get(0); - Material secondMaterial = components.get(1); + Material firstMaterial = components.get(0).getMaterial(); + Material secondMaterial = components.get(1).getMaterial(); // Check them in an order independent way. It is enough to check that // the components are there. There is no need to check their sizes. diff --git a/tests/org.eclipse.ice.datastructures.test/src/org/eclipse/ice/datastructures/test/TestMaterialFactory.java b/tests/org.eclipse.ice.datastructures.test/src/org/eclipse/ice/datastructures/test/TestMaterialFactory.java index 69fb9439c..8ea4a09b7 100644 --- a/tests/org.eclipse.ice.datastructures.test/src/org/eclipse/ice/datastructures/test/TestMaterialFactory.java +++ b/tests/org.eclipse.ice.datastructures.test/src/org/eclipse/ice/datastructures/test/TestMaterialFactory.java @@ -8,17 +8,18 @@ * 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 + * Claire Saunders, Matthew Wang, Anna Wojtowicz, Kasper Gammeltoft *******************************************************************************/ package org.eclipse.ice.datastructures.test; import org.eclipse.ice.datastructures.form.Material; +import org.eclipse.ice.datastructures.form.MaterialStack; /** * This is a utility class used by the Material tests to create materials with a * known initial configuration. * - * @author Jay Jay Billings + * @author Jay Jay Billings, Kasper Gammeltoft * */ public class TestMaterialFactory { @@ -40,16 +41,21 @@ public static Material createCO2() { // Create component 1 - Carbon Material carbon = new Material(); carbon.setName("C"); + testMaterial.addComponent(carbon); // Create component 2 - Oxygen Material oxygen = new Material(); oxygen.setName("O"); - // Note that the "size" of O is 2 since we need O2 + // Note that we need a stack because there is more than one. + // The same effect could be achieved by adding oxygen twice to + // the test material. + testMaterial.addComponent(new MaterialStack(oxygen, 2)); // Add them as components testMaterial.addComponent(carbon); testMaterial.addComponent(oxygen); - + testMaterial.addComponent(oxygen); + return testMaterial; } @@ -77,10 +83,12 @@ public static Material createH2O() { oxygen.setName("O"); // Add them as components + // We need two hydrogens for H2O + testMaterial.addComponent(hydrogen); testMaterial.addComponent(hydrogen); testMaterial.addComponent(oxygen); return testMaterial; } - + } diff --git a/tests/org.eclipse.ice.materials.test/src/org/eclipse/ice/materials/test/TestMaterialFactory.java b/tests/org.eclipse.ice.materials.test/src/org/eclipse/ice/materials/test/TestMaterialFactory.java index 97264e2f1..ad51df84b 100644 --- a/tests/org.eclipse.ice.materials.test/src/org/eclipse/ice/materials/test/TestMaterialFactory.java +++ b/tests/org.eclipse.ice.materials.test/src/org/eclipse/ice/materials/test/TestMaterialFactory.java @@ -49,6 +49,7 @@ public static Material createCO2() { // Add them as components testMaterial.addComponent(carbon); testMaterial.addComponent(oxygen); + testMaterial.addComponent(oxygen); return testMaterial; } @@ -78,6 +79,7 @@ public static Material createH2O() { // Add them as components testMaterial.addComponent(hydrogen); + testMaterial.addComponent(hydrogen); testMaterial.addComponent(oxygen); return testMaterial;