1 /*
  2  * core.js
  3  *
  4  * Copyright (c) 2015-2018 Emmanuel PUYBARET / eTeks <info@eteks.com>
  5  * 
  6  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
  7  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  8  *
  9  * This code is free software; you can redistribute it and/or modify it
 10  * under the terms of the GNU General Public License version 2 only, as
 11  * published by the Free Software Foundation.  Oracle designates this
 12  * particular file as subject to the "Classpath" exception as provided
 13  * by Oracle in the LICENSE file that accompanied OpenJDK 8 source code.
 14  *
 15  * This code is distributed in the hope that it will be useful, but WITHOUT
 16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 17  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 18  * version 2 for more details (a copy is included in the LICENSE file that
 19  * accompanied this code).
 20  *
 21  * You should have received a copy of the GNU General Public License version
 22  * 2 along with this work; if not, write to the Free Software Foundation,
 23  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 24  */
 25 
 26 // Various classes of OpenJDK 8 translated to Javascript
 27 
 28 /**
 29  * Creates an IllegalArgumentException instance.
 30  * Adapted from java.lang.IllegalArgumentException
 31  * @constructor
 32  */
 33 function IllegalArgumentException(message) {
 34   this.message = message;
 35 }
 36 
 37 
 38 /**
 39  * Creates an NullPointerException instance.
 40  * Adapted from java.lang.NullPointerException
 41  * @constructor
 42  */
 43 function NullPointerException(message) {
 44   this.message = message;
 45 }
 46 
 47 
 48 /**
 49  * Creates an IllegalStateException instance.
 50  * Adapted from java.lang.IllegalStateException
 51  * @constructor
 52  */
 53 function IllegalStateException(message) {
 54   this.message = message;
 55 }
 56 
 57 
 58 /**
 59  * Creates an UnsupportedOperationException instance.
 60  * Adapted from java.lang.UnsupportedOperationException
 61  * @constructor
 62  */
 63 function UnsupportedOperationException(message) {
 64   this.message = message;
 65 }
 66 
 67 
 68 /**
 69  * Creates an InternalError instance.
 70  * Adapted from java.lang.InternalError
 71  * @constructor
 72  * @ignore
 73  */
 74 function InternalError(message) {
 75   this.message = message;
 76 }
 77 
 78 
 79 /**
 80  * Creates an NoSuchElementException instance.
 81  * Adapted from java.util.NoSuchElementException
 82  * @constructor
 83  */
 84 function NoSuchElementException(message) {
 85   this.message = message;
 86 }
 87 
 88 
 89 /**
 90  * System class.
 91  * @class
 92  * @ignore
 93  */
 94 var System = {}
 95 
 96 System.arraycopy = function(srcPts, srcOff, dstPts, dstOff, size) {
 97   if (srcPts !== dstPts
 98       || dstOff >= srcOff + size) {
 99     while (--size >= 0) {
100       dstPts[dstOff++] = srcPts[srcOff++];
101     }
102   } else {
103     // In case copied items overlap  
104     var tmp = srcPts.slice(srcOff, srcOff + size);
105     for (var i = 0; i < size; i++) {
106       dstPts[dstOff++] = tmp[i];
107     }
108   }
109 }
110 
111 
112 /**
113  * Creates an EventObject instance.
114  * Adapted from java.util.EventObject
115  * @constructor
116  */
117 function EventObject(source) {
118   this.source = source;
119 }
120 
121 /**
122  * Returns the source of this event.
123  * @return {Object}
124  */
125 EventObject.prototype.getSource = function() {
126   return this.source;
127 }
128 
129 
130 /**
131  * Creates a PropertyChangeEvent instance.
132  * Adapted from java.beans.PropertyChangeEvent
133  * @constructor
134  */
135 function PropertyChangeEvent(source, propertyName, oldValue, newValue) {
136   EventObject.call(this, source);
137   this.propertyName = propertyName;
138   this.newValue = newValue;
139   this.oldValue = oldValue;
140 }
141 PropertyChangeEvent.prototype = Object.create(EventObject.prototype);
142 PropertyChangeEvent.prototype.constructor = PropertyChangeEvent;
143 
144 /**
145  * Returns the name of the modified property.
146  * @return {string}
147  */
148 PropertyChangeEvent.prototype.getPropertyName = function() {
149   return this.propertyName;
150 }
151   
152 /**
153  * Returns the new value of the property.
154  */
155 PropertyChangeEvent.prototype.getNewValue = function() {
156   return this.newValue;
157 }
158 
159 /**
160  * Returns the old value of the property.
161  */
162 PropertyChangeEvent.prototype.getOldValue = function() {
163   return this.oldValue;
164 }
165 
166 
167 /**
168  * Creates a PropertyChangeSupport instance.
169  * Adapted from java.beans.PropertyChangeSupport
170  * @constructor
171  */
172 function PropertyChangeSupport(source) {
173   this.source = source;
174   this.listeners = [];
175 }
176 
177 /**
178  * Adds the <code>listener</code> in parameter to the list of listeners that may be notified.
179  * @param {string} [propertyName] the name of an optional property to listen
180  * @param listener  a callback that will be called with a {@link PropertyChangeEvent} instance
181  */
182 PropertyChangeSupport.prototype.addPropertyChangeListener = function(propertyName, listener) {
183   if (listener === undefined) {
184     // One parameter
185     listener = propertyName;
186     propertyName = null;
187   }
188   if (listener) {
189     if (propertyName) {
190       this.listeners.push({"listener":listener, "propertyName":propertyName});
191     } else {
192       this.listeners.push({"listener":listener});
193     }
194   }
195 }
196 
197 /**
198  * Removes the <code>listener</code> in parameter to the list of listeners that may be notified.
199  * @param listener the listener to remove. If it doesn't exist, it's simply ignored.
200  */
201 PropertyChangeSupport.prototype.removePropertyChangeListener = function(propertyName, listener) {
202   if (listener === undefined) {
203     // One parameter
204     listener = propertyName;
205     propertyName = undefined;
206   }
207   if (listener) {
208     for (var i = this.listeners.length - 1; i >= 0; i--) {
209       if (this.listeners [i].propertyName == propertyName
210           && this.listeners [i].listener === listener) {
211         this.listeners.splice(i, 1);
212       }
213     }
214   }
215 }
216 
217 /**
218  * Returns an array of all the listeners that were added to the
219  * PropertyChangeSupport object with addPropertyChangeListener().
220  * @param {string} [propertyName] the name of an optional property to listen
221  * @return [Array]
222  */
223 PropertyChangeSupport.prototype.getPropertyChangeListeners = function(propertyName) {
224   var listeners = [];
225   for (var i = this.listeners.length - 1; i >= 0; i--) {
226     if (this.listeners [i].propertyName == propertyName
227         && this.listeners [i].listener === listener) {
228       listeners.push(this.listeners [i]);
229     }
230   }
231   return listeners;
232 }
233 
234 /**
235  * Fires a property change event.
236  * @param propertyName {string} the name of the modified property
237  * @param oldValue old value
238  * @param newValue new value
239  */
240 PropertyChangeSupport.prototype.firePropertyChange = function(propertyName, oldValue, newValue) {
241   if (oldValue != newValue) {
242     var ev = new PropertyChangeEvent(this.source, propertyName, oldValue, newValue);
243     for (var i = 0; i < this.listeners.length; i++) {
244       if (!("propertyName" in this.listeners [i])
245           || this.listeners [i].propertyName == propertyName) {
246         if (typeof(this.listeners [i].listener) === "function") {
247           this.listeners [i].listener(ev);
248         } else {
249           this.listeners [i].listener.propertyChange(ev);
250         }
251       }
252     }
253   }
254 }
255