Mercurial > hg > jslab
comparison src/samer/core_/util/VMap.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 * Copyright (c) 2000, Samer Abdallah, King's College London. | |
3 * All rights reserved. | |
4 * | |
5 * This software is provided AS iS and WITHOUT ANY WARRANTY; | |
6 * without even the implied warranty of MERCHANTABILITY or | |
7 * FITNESS FOR A PARTICULAR PURPOSE. | |
8 */ | |
9 | |
10 package samer.core.util; | |
11 | |
12 import samer.core.*; | |
13 import samer.core.types.*; | |
14 import java.util.*; | |
15 import java.awt.*; | |
16 | |
17 /** | |
18 An Agent and Viewable that manages a IMap object - provides | |
19 a user interface for adjusting the domain. | |
20 */ | |
21 | |
22 public class VMap extends Viewable implements Agent | |
23 { | |
24 IMap map; | |
25 boolean symmetric; | |
26 | |
27 public static final Object NEW_MAP = new Object(); | |
28 | |
29 public VMap(IMap m) { this(m,true); } | |
30 public VMap(IMap m, Node node) { this(m,true,node); } | |
31 public VMap(IMap m, boolean init) { this(m,init,new Node("map")); } | |
32 | |
33 /** Construct a VMap from a given IMap and node. The boolean init flag specifies | |
34 whether or not the VMap should reinitialise the IMap from the current Environment | |
35 */ | |
36 | |
37 public VMap(IMap m, boolean init, Node node) | |
38 { | |
39 super(node); | |
40 setAgent(this); | |
41 | |
42 map = m; | |
43 symmetric = false; | |
44 | |
45 if (init) { | |
46 Shell.push(getNode()); | |
47 try { | |
48 boolean logmap = Shell.getBoolean("log", false); | |
49 | |
50 if (logmap) { map=new LogMap(); symmetric=false; } | |
51 else symmetric = Shell.getBoolean( "symmetric", symmetric); | |
52 | |
53 setDomain( | |
54 Shell.getDouble("minimum",map.getDomainMin()), | |
55 Shell.getDouble("maximum",map.getDomainMax()) | |
56 ); | |
57 } finally { Shell.pop(); } | |
58 } | |
59 } | |
60 | |
61 public void setMap( IMap m) { map=m; changed(NEW_MAP); } | |
62 public IMap getMap() { return map; } | |
63 | |
64 public String toString() { return super.toString()+"="+map.toString(); } | |
65 | |
66 // ........... Viewable bits ................ | |
67 | |
68 public void dispose() | |
69 { | |
70 Shell.deregisterAgent(this); | |
71 super.dispose(); | |
72 } | |
73 | |
74 public Viewer getViewer() { return new Adjuster(); } | |
75 | |
76 // ........... Agent Bits ................... | |
77 | |
78 public void getCommands(Agent.Registry r) { | |
79 r.add("domain").add("symmetric",symmetric); | |
80 r.add("linear").add("log"); | |
81 r.group(); | |
82 r.add("put").add("get"); | |
83 } | |
84 | |
85 public void execute(String c, Environment env) throws Exception | |
86 { | |
87 if (c.equals("domain")) { | |
88 Shell.showDialogFor(getViewer().getComponent(),Node.lastPart(node.fullName())); | |
89 } else if (c.equals("symmetric")) { | |
90 symmetric = X._bool(env.datum(),!symmetric); | |
91 setDomain( map.getDomainMin(), map.getDomainMax()); | |
92 changed(); | |
93 } else if (c.equals("linear")) { | |
94 IMap newmap = new LinearMap( ); | |
95 newmap.setDomain( map.getDomainMin(), map.getDomainMax()); | |
96 newmap.setIntRange( map.getIntRange()); | |
97 setMap(newmap); | |
98 } else if (c.equals("log")) { | |
99 double min=map.getDomainMin(); | |
100 double max=map.getDomainMax(); | |
101 | |
102 if (min<=0) min=max/1000; // default dynamic range | |
103 symmetric=false; | |
104 setMap(new LogMap(min,max,map.getIntRange())); | |
105 } else if (c.equals("put")) Shell.put("map",this); | |
106 else if (c.equals("get")) setMap(((VMap)Shell.get("map")).getMap().copy()); | |
107 /* or | |
108 } else if (c.equals("put")) Shell.put("map",this.getMap().copy()); | |
109 else if (c.equals("get")) setMap((IMap)Shell.get("map")); | |
110 */ | |
111 } | |
112 | |
113 public void setSymmetric(boolean f) { symmetric=f; } | |
114 public void setDomain( double min, double max) | |
115 { | |
116 if (map instanceof LogMap) { | |
117 if (min<=0) min=max/1000; | |
118 } else { | |
119 if (symmetric) { max=Math.max(max,-min); min=-max; } | |
120 } | |
121 // Shell.trace("new domain=["+min+","+max+")"); | |
122 map.setDomain(min,max); | |
123 } | |
124 | |
125 class Adjuster extends BaseViewer | |
126 { | |
127 VBoolean sym; | |
128 VDouble t1, t2; | |
129 | |
130 Adjuster() | |
131 { | |
132 super(VMap.this); | |
133 panel().setName(getLabel()); | |
134 Shell.push(getNode()); | |
135 | |
136 setLayout( new StackLayout()); | |
137 | |
138 t1 = new VDouble("maximum",map.getDomainMax(),Variable.NOINIT); | |
139 t2 = new VDouble("minimum",map.getDomainMin(),Variable.NOINIT); | |
140 sym= new VBoolean("symmetric",symmetric,Variable.NOINIT); | |
141 t1.addObserver(this); | |
142 t2.addObserver(this); | |
143 sym.addObserver(this); | |
144 | |
145 add(t1.getViewer().getComponent()); | |
146 add(t2.getViewer().getComponent()); | |
147 add(sym.getViewer().getComponent()); | |
148 | |
149 Shell.pop(); | |
150 } | |
151 | |
152 public void update(Observable o, Object src) | |
153 { | |
154 if (src==this) return; // &&& | |
155 if (VMap.this.equals(o)) { | |
156 // means someone has changed the map ... | |
157 // ... and if it wasn't us... | |
158 t1.value=map.getDomainMax(); t1.changed(this); | |
159 t2.value=map.getDomainMin(); t2.changed(this); | |
160 if (sym.value!=symmetric) { | |
161 sym.value=symmetric; sym.changed(this); | |
162 } | |
163 } else { | |
164 // Widget adjusted | |
165 // user has adjusted controls... | |
166 // must set map and notify any observers | |
167 if (sym==o) { | |
168 if (symmetric=sym.value) { | |
169 double max=Math.max(t1.value,-t2.value); | |
170 t1.value=max; t2.value=-max; | |
171 t1.changed(this); t2.changed(this); | |
172 } | |
173 } else { | |
174 if (symmetric) { | |
175 if (t1==o) { t2.value=-t1.value; t2.changed(this); } | |
176 else if (t2==o) { t1.value=-t2.value; t1.changed(this); } | |
177 } | |
178 } | |
179 map.setDomain(t2.value,t1.value); | |
180 changed(this); | |
181 // else, WE adjusted the controls to | |
182 // reflect reality, so do nothing | |
183 } | |
184 } | |
185 } | |
186 } | |
187 | |
188 |