/* 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 . */ /* * PrimitiveNull.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.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.HashMap; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JPanel; /** * * @author Brandon */ public class PrimitiveNull extends Primitive { public static String pseudocodeTypeName() { return "Null"; } public String getType() { return pseudocodeTypeName(); }// String name; // // public boolean isNullable() { return true; } public Primitive copy() { PrimitiveNull n = new PrimitiveNull(); return n; } 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); alreadyCopiedAddresses.put(p.getAddress(), pp.getAddress()); PrimitiveNull n = (PrimitiveNull) pp.dereference(); super.deepCopyDefinedProperties(n, alreadyCopiedAddresses); return n; } public boolean deepEquals(Primitive p) throws DynamicMVCException { if (!(p instanceof PrimitiveNull)) { DynamicController.errorMessage = "Expecting Null, but found " + p.getType(); return false; } return super.deepEqualsDefinedProperties(p); } /** Creates a new instance of PrimitiveString */ public PrimitiveNull() { super(); } // // public String getName() { return name; } public void setName(String name) { this.name = name; } // // public void populateFromEditComponentWrapper(JComponent editComponentWrapper) { // nothing } public String getEditComponentWrapperName() { return "pnl"; } public JComponent getEditComponent() { // wrap everything in a panel JPanel wrapper = new JPanel(); wrapper.setName(getEditComponentWrapperName()); // create a label to display the empty symbol JLabel input = new JLabel(); input.setText("\u03D5"); /* JPanel changeDataTypeWrapper = new JPanel(); changeDataTypeWrapper.setBorder(javax.swing.BorderFactory.createTitledBorder("Change Data Type")); // also, add a button to allow new inputs newDataType = new javax.swing.JComboBox(); newDataType.setModel(new javax.swing.DefaultComboBoxModel(PrimitiveFactory.getTypeNames().toArray())); JButton changeButton = new JButton(); changeButton.setText("Change"); changeButton.addActionListener(new ChangeHandler()); changeDataTypeWrapper.add(newDataType); changeDataTypeWrapper.add(changeButton); wrapper.add(changeDataTypeWrapper); */ wrapper.add(input); return wrapper; } javax.swing.JComboBox newDataType; class ChangeHandler implements ActionListener { public void actionPerformed(ActionEvent e) { try { // assume the actionCommand tells what type of item to add String dataType = (String) newDataType.getSelectedItem(); PrimitivePointer p = PrimitiveFactory.create(dataType, name); PrimitivePointer oldP = getPointer(); PrimitiveFactory.setInMemory(oldP.getAddress(), p.dereference()); View.updateAsk(); } catch (DynamicMVCException ex) { ex.printStackTrace(); } } } public JComponent getReadOnlyComponent() { // wrap everything in a panel JPanel wrapper = new JPanel(); // create a label to display the empty symbol JLabel input = new JLabel(); input.setText("\u03D5"); wrapper.add(input); return wrapper; } // public PrimitivePointer get(String name) throws DynamicMVCException { throw new DynamicMVCException(" does not contain a property named:'" + name + "'."); } public Object execute(String methodName, ArrayList arguments) throws DynamicMVCException { // is this a method of mine? if (methodName.equalsIgnoreCase("equals")) { return equals(arguments); } else { throw new DynamicMVCException("Invalid method name:'" + methodName + "' on object '" + "" + "'."); } } 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 PrimitiveNull) { return true; } else { return false; } } public boolean valueEquals(Primitive p) { if (p instanceof PrimitiveNull) { return valueEquals((PrimitiveNull) p); } return false; } public boolean valueEquals(PrimitiveNull s) { if (s.hasDefinedProperties(this.getDefinedProperties())) { return true; } else { return false; } } public String toString() { return "\u03D5"; } public PrimitiveString toPrimitiveString() { PrimitiveString s = (PrimitiveString) (PrimitiveFactory.create(PrimitiveString.pseudocodeTypeName(), "s").dereference()); s.setValue("\u03D5"); return s; } }