/*
PseudoCode Interpreted Language (PCIL): Part of the algoviz@vt collection of algorithm visualizations.
Copyright (C) 2008 Brandon Malone, Frank Hadlock
This file is part of the PseudoCode Interpreted Language.
PseudoCode Interpreted Language is free software: you can redistribute
it and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
PseudoCode Interpreted Language is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
You should have received a copy of the GNU General Public License along with
PseudoCode Interpreted Language. If not, see
.
*/
/*
* PrimitiveString.java
*
* Created on March 6, 2008, 1:27 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package dynamicmvc;
import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
/**
*
* @author Brandon
*/
public class PrimitiveString extends Primitive {
public static String pseudocodeTypeName() {
return "String";
}
public String getType() {
return pseudocodeTypeName();
}
public boolean isResizable() {
return true;
}
//
private String value;
private String name;
//
//
public boolean isNullable() {
return (value.length() == 0);
}
public Primitive copy() {
PrimitiveString i = new PrimitiveString(name, value);
return i;
}
public Primitive deepCopy(HashMap alreadyCopiedAddresses) throws DynamicMVCException {
PrimitivePointer p = getPointer();
if (alreadyCopiedAddresses.containsKey(Integer.valueOf(p.getAddress()))) {
return PrimitiveFactory.getFromMemory(alreadyCopiedAddresses.get(p.getAddress()));
}
PrimitivePointer pp = PrimitiveFactory.create(pseudocodeTypeName(), name);
PrimitiveString i = (PrimitiveString)pp.dereference();
i.setName(name);
i.setValue(value);
alreadyCopiedAddresses.put(p.getAddress(), pp.getAddress());
super.deepCopyDefinedProperties(i, alreadyCopiedAddresses);
return i;
}
public boolean deepEquals(Primitive p) throws DynamicMVCException {
if (!(p instanceof PrimitiveString)) {
DynamicController.errorMessage = "Expecting String, but found " + p.getType();
return false;
}
PrimitiveString s = (PrimitiveString)p;
if(!value.equalsIgnoreCase(s.getValue())) {
DynamicController.errorMessage = "Wrong string. Expecting '" + value + "', but found '" + s.getValue() + "'.";
return false;
}
return super.deepEqualsDefinedProperties(p);
}
/** Creates a new instance of PrimitiveString */
public PrimitiveString(String name, String value) {
super();
setName(name);
setValue(value);
}
public PrimitiveString(String value) {
super();
setName("");
setValue(value);
}
public PrimitiveString() {
super();
setName("");
setValue("");
}
//
//
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public void setValue(PrimitivePointer p) {
setValue(((PrimitiveString)p.dereference()));
}
public void setValue(PrimitiveString s) {
this.value = s.getValue();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//
//
public void populateFromEditComponentWrapper(JComponent editComponentWrapper) {
// find the value from the text box
Component comp = View.getComponent(editComponentWrapper, getEditComponentName());
if (comp instanceof JTextArea) {
JTextArea txtInput = (JTextArea)comp;
// update the variable
setValue(txtInput.getText());
}
}
public String getEditComponentWrapperName() {
return "pnl" + name;
}
public String getEditComponentName() {
return "txt" + name;
}
public JComponent getEditComponent() {
// wrap everything in a panel
JPanel wrapper = new JPanel();
wrapper.setName(getEditComponentWrapperName());
try {
int width = 35;
int height = 20;
if(this.getDefinedProperties().containsKey("__width")) {
PrimitivePointer pWidth = get("__width");
PrimitiveInteger w = (PrimitiveInteger)pWidth.dereference();
width = w.getValue();
}
if(this.getDefinedProperties().containsKey("__height")) {
PrimitivePointer pHeight = get("__height");
PrimitiveInteger h = (PrimitiveInteger)pHeight.dereference();
height = h.getValue();
}
// create a text box to input the number
JTextArea input = new JTextArea();
input.setName(getEditComponentName());
input.setText(String.valueOf(value));
input.setPreferredSize(new Dimension(width, height));
wrapper.add(input);
} catch (DynamicMVCException ex) {
ex.printStackTrace();
}
return wrapper;
}
public JComponent getReadOnlyComponent() {
// wrap everything in a panel
JPanel wrapper = new JPanel();
// create a label to display the number
JLabel input = new JLabel();
input.setName(getEditComponentName());
input.setText(value);
wrapper.add(input);
return wrapper;
}
//
public Object execute(String methodName, ArrayList arguments) throws DynamicMVCException {
// is this a method of mine?
if (methodName.equalsIgnoreCase("equals")) {
return equals(arguments);
} else if (methodName.equalsIgnoreCase("notEqual")) {
return !equals(arguments);
} else if (methodName.equalsIgnoreCase("concat")) {
return concat(arguments);
} else if (methodName.equalsIgnoreCase("charAt")) {
return charAt(arguments);
} else if (methodName.equalsIgnoreCase("length")) {
return length(arguments);
} else {
throw new DynamicMVCException("Invalid method name:'" + methodName + "' on object '" + name + "'.");
}
}
public int compareTo(Object o) throws DynamicMVCException {
if (o instanceof PrimitiveString) {
PrimitiveString i = (PrimitiveString)o;
return compareTo(i);
}
throw new DynamicMVCException("Invalid comparison");
}
public boolean equals(ArrayList arguments) throws DynamicMVCException {
if (arguments.size() == 1) {
PrimitivePointer i = arguments.get(0);
return equals(i);
}
throw new DynamicMVCException("Wrong number of arguments.");
}
public boolean equals(PrimitivePointer i) {
if (i.dereference() instanceof PrimitiveString) {
return (compareTo(i) == 0);
} else {
return false;
}
}
public boolean valueEquals(Primitive p) {
if (p instanceof PrimitiveString) {
return valueEquals((PrimitiveString)p);
}
return false;
}
public boolean valueEquals(PrimitiveString s) {
if(s.hasDefinedProperties(this.getDefinedProperties())) {
return (value.equalsIgnoreCase(s.getValue()));
} else {
return false;
}
}
public int compareTo(ArrayList arguments) throws DynamicMVCException {
if (arguments.size() == 1) {
PrimitivePointer i = arguments.get(0);
return compareTo(i);
}
throw new DynamicMVCException("Wrong number of arguments.");
}
public int compareTo(PrimitivePointer i) {
String otherString = ((PrimitiveString)i.dereference()).getValue();
return value.compareTo(otherString);
}
public String toString() {
return getName() + ": " + getValue();
}
public int compareTo(Primitive p) throws DynamicMVCException {
if (p instanceof PrimitiveString) {
return value.compareTo(((PrimitiveString)p).getValue());
} else {
return -1;
//throw new DynamicMVCException("Invalid comparison.");
}
}
public PrimitivePointer charAt(ArrayList arguments) throws DynamicMVCException {
if (arguments.size() == 1) {
PrimitivePointer i = arguments.get(0);
return charAt(i);
}
throw new DynamicMVCException("Wrong number of arguments.");
}
public PrimitivePointer charAt(PrimitivePointer i) {
PrimitiveInteger pIndex = (PrimitiveInteger)i.dereference();
int index = pIndex.getValue();
PrimitivePointer pp = null;
if (index < value.length()) {
pp = PrimitiveFactory.create(PrimitiveString.pseudocodeTypeName(), "c");
PrimitiveString newString = (PrimitiveString)(pp.dereference());
newString.setValue(String.valueOf(value.charAt(index)));
} else {
pp = PrimitiveFactory.create(PrimitiveNull.pseudocodeTypeName(), "c");
}
return pp;
}
public Primitive concat(ArrayList arguments) throws DynamicMVCException {
if (arguments.size() == 1) {
PrimitivePointer otherString = arguments.get(0);
return concat(otherString);
}
throw new DynamicMVCException("Wrong number of arguments.");
}
public Primitive concat(PrimitivePointer i) {
String otherString = ((PrimitiveString)(i.dereference().toPrimitiveString())).getValue();
PrimitiveString newString = PrimitiveFactory.create(PrimitiveString.pseudocodeTypeName(), "newString").dereference().toPrimitiveString();
newString.setValue(value.concat(otherString));
return newString;
}
public PrimitivePointer length(ArrayList arguments) throws DynamicMVCException {
if (arguments.size() == 0) {
return length();
} else {
throw new DynamicMVCException("Wrong number of arguments.");
}
}
public PrimitivePointer length() {
PrimitivePointer p = PrimitiveFactory.create(PrimitiveInteger.pseudocodeTypeName(), "");
((PrimitiveInteger)p.dereference()).setValue(value.length());
return p;
}
public PrimitiveString toPrimitiveString() {
PrimitiveString s = (PrimitiveString)(PrimitiveFactory.create(PrimitiveString.pseudocodeTypeName(), "s").dereference());
s.setValue(getValue());
return s;
}
}