Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / java / beans / ChangeListenerMap.js
1 Clazz.declarePackage ("java.beans");
2 Clazz.load (null, "java.beans.ChangeListenerMap", ["java.util.ArrayList", "$.Collections", "$.EventListenerProxy", "$.HashMap"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.map = null;
5 Clazz.instantialize (this, arguments);
6 }, java.beans, "ChangeListenerMap");
7 Clazz.defineMethod (c$, "add", 
8 function (name, listener) {
9 if (this.map == null) {
10 this.map =  new java.util.HashMap ();
11 }var array = this.map.get (name);
12 var size = (array != null) ? array.length : 0;
13 var clone = this.newArray (size + 1);
14 clone[size] = listener;
15 if (array != null) {
16 System.arraycopy (array, 0, clone, 0, size);
17 }this.map.put (name, clone);
18 }, "~S,~O");
19 Clazz.defineMethod (c$, "remove", 
20 function (name, listener) {
21 if (this.map != null) {
22 var array = this.map.get (name);
23 if (array != null) {
24 for (var i = 0; i < array.length; i++) {
25 if (listener.equals (array[i])) {
26 var size = array.length - 1;
27 if (size > 0) {
28 var clone = this.newArray (size);
29 System.arraycopy (array, 0, clone, 0, i);
30 System.arraycopy (array, i + 1, clone, i, size - i);
31 this.map.put (name, clone);
32 } else {
33 this.map.remove (name);
34 if (this.map.isEmpty ()) {
35 this.map = null;
36 }}break;
37 }}
38 }}}, "~S,~O");
39 Clazz.defineMethod (c$, "get", 
40 function (name) {
41 return (this.map != null) ? this.map.get (name) : null;
42 }, "~S");
43 Clazz.defineMethod (c$, "set", 
44 function (name, listeners) {
45 if (listeners != null) {
46 if (this.map == null) {
47 this.map =  new java.util.HashMap ();
48 }this.map.put (name, listeners);
49 } else if (this.map != null) {
50 this.map.remove (name);
51 if (this.map.isEmpty ()) {
52 this.map = null;
53 }}}, "~S,~A");
54 Clazz.defineMethod (c$, "getListeners", 
55 function () {
56 if (this.map == null) {
57 return this.newArray (0);
58 }var list =  new java.util.ArrayList ();
59 var listeners = this.map.get (null);
60 if (listeners != null) {
61 for (var listener, $listener = 0, $$listener = listeners; $listener < $$listener.length && ((listener = $$listener[$listener]) || true); $listener++) {
62 list.add (listener);
63 }
64 }for (var entry, $entry = this.map.entrySet ().iterator (); $entry.hasNext () && ((entry = $entry.next ()) || true);) {
65 var name = entry.getKey ();
66 if (name != null) {
67 for (var listener, $listener = 0, $$listener = entry.getValue (); $listener < $$listener.length && ((listener = $$listener[$listener]) || true); $listener++) {
68 list.add (this.newProxy (name, listener));
69 }
70 }}
71 return list.toArray (this.newArray (list.size ()));
72 });
73 Clazz.defineMethod (c$, "getListeners", 
74 function (name) {
75 if (name != null) {
76 var listeners = this.get (name);
77 if (listeners != null) {
78 return listeners.clone ();
79 }}return this.newArray (0);
80 }, "~S");
81 Clazz.defineMethod (c$, "hasListeners", 
82 function (name) {
83 if (this.map == null) {
84 return false;
85 }var array = this.map.get (null);
86 return (array != null) || ((name != null) && (null != this.map.get (name)));
87 }, "~S");
88 Clazz.defineMethod (c$, "getEntries", 
89 function () {
90 return (this.map != null) ? this.map.entrySet () : java.util.Collections.emptySet ();
91 });
92 Clazz.defineMethod (c$, "extract", 
93 function (listener) {
94 while (Clazz.instanceOf (listener, java.util.EventListenerProxy)) {
95 var proxy = listener;
96 listener = proxy.getListener ();
97 }
98 return listener;
99 }, "~O");
100 });