annotate src/samer/core_/Viewable.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
rev   line source
samer@0 1 /*
samer@0 2 * Viewable.java
samer@0 3 *
samer@0 4 * Copyright (c) 2000, Samer Abdallah, King's College London.
samer@0 5 * All rights reserved.
samer@0 6 *
samer@0 7 * This software is provided AS iS and WITHOUT ANY WARRANTY;
samer@0 8 * without even the implied warranty of MERCHANTABILITY or
samer@0 9 * FITNESS FOR A PARTICULAR PURPOSE.
samer@0 10 */
samer@0 11
samer@0 12 package samer.core;
samer@0 13 import java.util.*;
samer@0 14
samer@0 15
samer@0 16 /**
samer@0 17 <p>
samer@0 18 A Viewable is a named Observable object, for which
samer@0 19 can be created a Viewer. A Viewer is meant to allow user
samer@0 20 interaction with the object. The name is managed
samer@0 21 by a Node object, so they can form a heirarchy.
samer@0 22 A Viewable can also have an associated Agent, so that
samer@0 23 Viewers can automatically expose commands
samer@0 24 for the Agent.
samer@0 25 */
samer@0 26
samer@0 27
samer@0 28 public class Viewable extends Observable
samer@0 29 {
samer@0 30 /** sent to Obervable.update() when Viewable is
samer@0 31 * being disposed of.
samer@0 32 */
samer@0 33 public final static Object DISPOSING = new Object();
samer@0 34
samer@0 35 public final Node getNode() { return node; }
samer@0 36 public final void setNode(Node n) { node=n; }
samer@0 37 public Agent getAgent() { return agent; }
samer@0 38 public void setAgent(Agent a) { agent=a; }
samer@0 39 public void addAgent(Agent a) { setAgent(new CompoundAgent(agent,a)); }
samer@0 40
samer@0 41 public String getLabel() {
samer@0 42 String str = Shell.getString(node.fullNameFor("label"),null);
samer@0 43 return str==null ? node.fullName().substring(1) : str;
samer@0 44 }
samer@0 45
samer@0 46 /** <p>Should leave the Viewable in such a state that the
samer@0 47 * garbage collector can get it as soon as any direct
samer@0 48 * references go out of scope. In particular, any attached
samer@0 49 * Viewers or Observers should drop their references to
samer@0 50 * this Viewable, and if necessary, dispose of themselves.
samer@0 51 * <p>
samer@0 52 * The default implementation deregisters the Viewable,
samer@0 53 * notifies Obervers and detaches the node.
samer@0 54 */
samer@0 55
samer@0 56 public void dispose()
samer@0 57 {
samer@0 58 Shell.deregisterViewable(this);
samer@0 59 changed(DISPOSING);
samer@0 60 }
samer@0 61
samer@0 62 /** This should return a suitable Viewer for this object.
samer@0 63 Default implementation asks the <code>samer.core.Registry</code>
samer@0 64 to create one. */
samer@0 65 public Viewer getViewer() { return Registry.createViewer(this); }
samer@0 66
samer@0 67 protected Viewable(String nm) { node = new Node(nm); }
samer@0 68 protected Viewable(Node nd) { node = nd; }
samer@0 69 protected Viewable() {}
samer@0 70
samer@0 71 protected Node node;
samer@0 72 protected Agent agent=null;
samer@0 73 // protected Vector agents=new Vector();
samer@0 74
samer@0 75 public String toString() {
samer@0 76 return Node.lastPart(getClass().getName())+":"+getLabel();
samer@0 77 }
samer@0 78
samer@0 79 public void finalize() { Shell.trace("Viewable finalizing: "+toString()); }
samer@0 80
samer@0 81 /* ------- override implementation of Observable here -----------*/
samer@0 82
samer@0 83 private static class Vector extends java.util.Vector {
samer@0 84 public Object[] getArray() { return elementData; }
samer@0 85 }
samer@0 86
samer@0 87 private Vector observers=new Vector();
samer@0 88
samer@0 89 public synchronized void addObserver(Observer o) {
samer@0 90 if (!observers.contains(o)) observers.addElement(o);
samer@0 91 }
samer@0 92
samer@0 93 public synchronized void deleteObserver(Observer o) {
samer@0 94 observers.removeElement(o);
samer@0 95 }
samer@0 96
samer@0 97 public synchronized void deleteObservers() {
samer@0 98 observers.removeAllElements();
samer@0 99 }
samer@0 100
samer@0 101 public void notifyObservers(Object arg)
samer@0 102 {
samer@0 103 Object [] arr=observers.getArray();
samer@0 104
samer@0 105 for (int i=observers.size()-1; i>=0; i--) {
samer@0 106 ((Observer)arr[i]).update(this, arg);
samer@0 107 }
samer@0 108 }
samer@0 109
samer@0 110 /** This will notify all observers that this object may have changed
samer@0 111 The second argument to Observer.update() will be null */
samer@0 112 public final void changed() { notifyObservers(null); }
samer@0 113
samer@0 114 /** This will notify all observers that this object may have changed.
samer@0 115 The second argument to Observer.update() will be the given object */
samer@0 116 public final void changed(Object o) { notifyObservers(o); }
samer@0 117 }
samer@0 118