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