Mercurial > hg > jslab
view src/samer/core_/Viewable.java @ 0:bf79fb79ee13
Initial Mercurial check in.
author | samer |
---|---|
date | Tue, 17 Jan 2012 17:50:20 +0000 |
parents | |
children |
line wrap: on
line source
/* * Viewable.java * * Copyright (c) 2000, Samer Abdallah, King's College London. * All rights reserved. * * This software is provided AS iS and WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. */ package samer.core; import java.util.*; /** <p> A Viewable is a named Observable object, for which can be created a Viewer. A Viewer is meant to allow user interaction with the object. The name is managed by a Node object, so they can form a heirarchy. A Viewable can also have an associated Agent, so that Viewers can automatically expose commands for the Agent. */ public class Viewable extends Observable { /** sent to Obervable.update() when Viewable is * being disposed of. */ public final static Object DISPOSING = new Object(); public final Node getNode() { return node; } public final void setNode(Node n) { node=n; } public Agent getAgent() { return agent; } public void setAgent(Agent a) { agent=a; } public void addAgent(Agent a) { setAgent(new CompoundAgent(agent,a)); } public String getLabel() { String str = Shell.getString(node.fullNameFor("label"),null); return str==null ? node.fullName().substring(1) : str; } /** <p>Should leave the Viewable in such a state that the * garbage collector can get it as soon as any direct * references go out of scope. In particular, any attached * Viewers or Observers should drop their references to * this Viewable, and if necessary, dispose of themselves. * <p> * The default implementation deregisters the Viewable, * notifies Obervers and detaches the node. */ public void dispose() { Shell.deregisterViewable(this); changed(DISPOSING); } /** This should return a suitable Viewer for this object. Default implementation asks the <code>samer.core.Registry</code> to create one. */ public Viewer getViewer() { return Registry.createViewer(this); } protected Viewable(String nm) { node = new Node(nm); } protected Viewable(Node nd) { node = nd; } protected Viewable() {} protected Node node; protected Agent agent=null; // protected Vector agents=new Vector(); public String toString() { return Node.lastPart(getClass().getName())+":"+getLabel(); } public void finalize() { Shell.trace("Viewable finalizing: "+toString()); } /* ------- override implementation of Observable here -----------*/ private static class Vector extends java.util.Vector { public Object[] getArray() { return elementData; } } private Vector observers=new Vector(); public synchronized void addObserver(Observer o) { if (!observers.contains(o)) observers.addElement(o); } public synchronized void deleteObserver(Observer o) { observers.removeElement(o); } public synchronized void deleteObservers() { observers.removeAllElements(); } public void notifyObservers(Object arg) { Object [] arr=observers.getArray(); for (int i=observers.size()-1; i>=0; i--) { ((Observer)arr[i]).update(this, arg); } } /** This will notify all observers that this object may have changed The second argument to Observer.update() will be null */ public final void changed() { notifyObservers(null); } /** This will notify all observers that this object may have changed. The second argument to Observer.update() will be the given object */ public final void changed(Object o) { notifyObservers(o); } }