annotate src/samer/core_/util/BaseViewer.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 * BaseViewer.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.util;
samer@0 13 import samer.core.*;
samer@0 14 import java.util.*;
samer@0 15 import java.awt.*;
samer@0 16
samer@0 17 /**
samer@0 18 Basic Viewer that appears as panel with an optional
samer@0 19 label. It is actually an envelope class for the real Viewer
samer@0 20 provided by Shell.createViewerPanel(), but in addition, it:
samer@0 21 - handles a reference to an Observable.
samer@0 22 - handles attach() and detach() in response to
samer@0 23 underlying panel's visibility.
samer@0 24 - responds to DISPOSE message from Observable by attempting
samer@0 25 to destroy itself using Shell.releaseViewer()
samer@0 26 */
samer@0 27
samer@0 28 public class BaseViewer implements Observer, Viewer
samer@0 29 {
samer@0 30 private Container panel;
samer@0 31 private Viewer viewer;
samer@0 32 protected Observable obs;
samer@0 33 private Component label;
samer@0 34
samer@0 35 /** Construct a BaseViewer for the given Viewable. If the Viewable
samer@0 36 has an Agent (ie returns something from getAgent() ) then
samer@0 37 the Agent's commands are exposed on the Viewer using
samer@0 38 this.exposeCommmands()
samer@0 39 */
samer@0 40 public BaseViewer(Viewable v) {
samer@0 41 this((Observable)v);
samer@0 42 Agent a=v.getAgent();
samer@0 43 if (a!=null) exposeCommands(a);
samer@0 44 }
samer@0 45
samer@0 46 /** Construct a BaseViewer for the given Observable. The only difference
samer@0 47 between this constructor and the BaseViewer(Viewable) constructor
samer@0 48 is that no agent is exposed.
samer@0 49 */
samer@0 50 public BaseViewer(Observable o) {
samer@0 51 obs=o;
samer@0 52 viewer=Shell.createViewerPanel(this);
samer@0 53 panel=(Container)viewer.getComponent();
samer@0 54 }
samer@0 55
samer@0 56 /** Returns the Container to which Components can be added to
samer@0 57 be exposed in this Viewer.
samer@0 58 */
samer@0 59 public Container panel() { return panel; }
samer@0 60
samer@0 61 /** Attempts to display a label or name for this viewer.
samer@0 62 <ul>
samer@0 63 <li>If no label exists, then one is created via Shell.createLabel()
samer@0 64 <li>If a label exists, it's text is changed to match the <code>txt</code>
samer@0 65 <li>If <code>txt==null</code> then any label is removed
samer@0 66 </ul>
samer@0 67 */
samer@0 68 public void setText(String txt) {
samer@0 69 if (txt==null) {
samer@0 70 if (label!=null) panel.remove(label);
samer@0 71 label=null;
samer@0 72 } else if (label==null) {
samer@0 73 panel.add(label=Shell.createLabel(txt));
samer@0 74 } else {
samer@0 75 label.setName(txt);
samer@0 76 }
samer@0 77 }
samer@0 78
samer@0 79 /** Equivalent to panel().setLayout(layout) - See java.awt.Container.setLayout() */
samer@0 80 public void setLayout(LayoutManager l) { panel.setLayout(l); }
samer@0 81
samer@0 82 /** Equivalent to panel().add(component) - See java.awt.Container.add() */
samer@0 83 public void add(Component c) { panel.add(c); }
samer@0 84
samer@0 85 /** Add a default viewer for the given Viewable
samer@0 86 Equivalent to panel().add(vbl.getViewer().getComponent())
samer@0 87 See java.awt.Container.add()
samer@0 88 */
samer@0 89 public void add(Viewable vbl) { panel.add(vbl.getViewer().getComponent()); }
samer@0 90
samer@0 91 /** Implementation of Viewer.getComponent(): returns the
samer@0 92 AWT Component for this Viewer. */
samer@0 93 public Component getComponent() { return panel; }
samer@0 94
samer@0 95 /** Delegated to viewer returned frp, Shell.createViewerPanel() -
samer@0 96 Generally, this will add an Agent's commands to a right-click popup menu */
samer@0 97 public void exposeCommands(Agent agent) { viewer.exposeCommands(agent); }
samer@0 98
samer@0 99 /** A call to this means that the Viewer should begin observing and
samer@0 100 reflecting changes in its Viewable */
samer@0 101 public void attach() { obs.addObserver(this); }
samer@0 102
samer@0 103 /** This ought to be the reverse of attach(). Not entirely clear who
samer@0 104 should be dropping references to whom here. Mainly, the Viewer
samer@0 105 should become disposable, so Viewer should arrange for any references
samer@0 106 to it (esp from the Viewable) to be dropped. This implementation
samer@0 107 deletes this Viewer (Observer) from the Viewable (Observable),
samer@0 108 but does not drop the Viewer's reference to the Observable incase
samer@0 109 it needs to reattach. This will be a problem for Viewers that
samer@0 110 observe more than one Viewable, so let's assume that BaseViewer
samer@0 111 is specifically a monogamous Viewer!
samer@0 112 */
samer@0 113 public void detach() { obs.deleteObserver(this); } // obs=null;
samer@0 114
samer@0 115 /** Respond to changes in Observable. This implementation tests
samer@0 116 to see if the Observable is being destroyed and if so, attempts to
samer@0 117 destroy the Viewer.
samer@0 118 */
samer@0 119 public void update(Observable o, Object arg) { disposeFilter(o,arg); }
samer@0 120
samer@0 121 /** implementation of update method is here so that it can be called
samer@0 122 easily by sub-classes more than one generation removed. */
samer@0 123 protected final boolean disposeFilter(Observable o, Object arg)
samer@0 124 {
samer@0 125 if (obs.equals(o) && Viewable.DISPOSING==arg) {
samer@0 126 Shell.releaseViewer(this);
samer@0 127 return true;
samer@0 128 }
samer@0 129 return false;
samer@0 130 }
samer@0 131 }