/*
* Copyright 2007 Daniel McCarney, Kevin Roe
* This file is part of JRiver.
*
* JRiver 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.
*
* JRiver 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.jriver.core;
import javax.swing.tree.*;
import java.util.*;
public class MudObject
{
private DefaultMutableTreeNode treeNode;
private boolean isContainer = true;
private String idString;
public String name;
public String shortDesc = "";
public String longDesc = "";
public MudObject(boolean containerFlag)
{
isContainer = containerFlag;
treeNode = new DefaultMutableTreeNode(this, containerFlag);
setID();
System.out.println("Object ["+idString+"] created");
}
public MudObject()
{
this(true);
}
public boolean isContainer() { return isContainer; }
public DefaultMutableTreeNode getRootNode()
{
return treeNode;
}
public DefaultMutableTreeNode environment()
{
return (DefaultMutableTreeNode) treeNode.getParent();
}
public Enumeration inventory()
{
return treeNode.children();
}
/* find an object in this object by name */
public MudObject present(String objName)
{
Enumeration e = this.inventory();
MudObject retObj = null;
while(e.hasMoreElements()) {
DefaultMutableTreeNode n = (DefaultMutableTreeNode) e.nextElement();
MudObject thisObj = (MudObject) n.getUserObject();
if(thisObj.queryName().equals(objName)) {
retObj = thisObj;
/* found the object so quit searching */
break;
}
}
return retObj;
}
public void insertContent(MudObject content) throws IllegalStateException
{
treeNode.add(content.getRootNode());
}
/* assign the object a unique id based on the class name */
private void setID()
{
idString = this.getClass().getName() + "#" + hashCode();
}
public void setName(String name) {
this.name = name;
}
public String queryName()
{
return name;
}
public String toString()
{
return idString;
}
public String getID()
{
return idString;
}
/* tell this object a message */
/* abstract */
public void tellObject(String message)
{
/* process incoming message */
}
/* send a tell to all the objects in this object */
public void announce(String message)
{
Enumeration inv = this.inventory();
while(inv.hasMoreElements()) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) inv.nextElement();
MudObject thisObj = (MudObject) node.getUserObject();
thisObj.tellObject(message);
}
}
public void announce(String message, MudObject exclude)
{
Enumeration inv = this.inventory();
while(inv.hasMoreElements()) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) inv.nextElement();
MudObject thisObj = (MudObject) node.getUserObject();
if(!thisObj.equals(exclude))
thisObj.tellObject(message);
}
}
public int drop()
{
if(treeNode.getParent() != null && treeNode.getParent().getParent() != null)
{
((DefaultMutableTreeNode) treeNode.getParent().getParent()).add(treeNode);
return 1;
}
return 0;
}
public int move(DefaultMutableTreeNode destination)
{
destination.add(treeNode);
MudObject thisObj = (MudObject)treeNode.getUserObject();
MudObject targetObj = (MudObject)destination.getUserObject();
System.out.println(thisObj.queryName()+"["+thisObj.toString()+"]"+
" moved into "+targetObj.queryName()+"["+targetObj.toString()+"]");
return 1;
}
}