jrubyfx-fxmlloader-master 0.4.master.2015.9.30-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,158 @@
1
+ # * Copyright (c) 2013 Patrick Plenefisch
2
+ # * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3
+ # *
4
+ # * This code is free software; you can redistribute it and/or modify it
5
+ # * under the terms of the GNU General Public License version 2 only, as
6
+ # * published by the Free Software Foundation. Oracle designates this
7
+ # * particular file as subject to the "Classpath" exception as provided
8
+ # * by Oracle in the LICENSE file that accompanied this code.
9
+ # *
10
+ # * This code is distributed in the hope that it will be useful, but WITHOUT
11
+ # * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
+ # * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
+ # * version 2 for more details (a copy is included in the LICENSE file that
14
+ # * accompanied this code).
15
+ # *
16
+ # * You should have received a copy of the GNU General Public License version
17
+ # * 2 along with this work; if not, write to the Free Software Foundation,
18
+ # * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
+ # *
20
+
21
+ require 'digest/sha1'
22
+ require 'fileutils'
23
+
24
+ def javafx
25
+ Java::javafx
26
+ end
27
+
28
+ Infinity = 1.0/0.0
29
+
30
+ class FxmlJitInfo
31
+ include JRubyFX
32
+ def self.hash(file)
33
+ Digest::SHA1.hexdigest(File.read file)
34
+ end
35
+ def self.load_aot(file, cache, validate = true)
36
+ if validate
37
+ hash = hash(file)
38
+ f_hash = File.open(cache, "r", &:readline).strip
39
+ return false if "# #{hash} encoding: utf-8" != f_hash
40
+ end
41
+ hash = Digest::SHA1.hexdigest(File.basename file)
42
+ require cache
43
+ JRubyFX::GeneratedAssets.const_get("AOT#{hash}").new rescue nil
44
+ end
45
+ # TODO: store jit settings in here instead of $RB_* variables
46
+ attr_accessor :file_name, :raw_code, :jit_settings
47
+ def initialize(file_name, jit_settings=1, outfile=nil, cache_dir=nil,validate = true, opts = nil)
48
+ @file_name = file_name
49
+ @no_write = (opts && opts[:no_write]) || false
50
+ if @file_name.start_with? "file:"
51
+ @file_name = @file_name.gsub(/^file\:/, '')
52
+ elsif @file_name.start_with? "jar:"
53
+ @no_write = true
54
+ elsif @file_name.start_with? "compoundjar:"
55
+ @no_write = true
56
+ end
57
+ @jit_settings = jit_settings
58
+ @run_count = 0
59
+ @opts = opts
60
+ @outfile = if @outfile
61
+ outfile
62
+ else
63
+ cache_dir = cache_dir || File.join(File.dirname(@file_name), ".jrubyfx_cache")
64
+ FileUtils.mkpath(cache_dir) unless File.directory?(cache_dir) or @no_write
65
+ @f_hash = Digest::SHA1.hexdigest(File.basename @file_name)
66
+ File.join(cache_dir, "#{@f_hash}.rb")
67
+ end
68
+ if File.exist?(@outfile) && !(opts && opts[:force])
69
+ @compiled = self.class.load_aot(@file_name, @outfile, validate)
70
+ if @compiled
71
+ dputs "got #{file_name} from cache"
72
+ end
73
+ end
74
+ end
75
+ def hash
76
+ FxmlJitInfo.hash(@file_name)
77
+ end
78
+ def should_jit?
79
+ return false if @jit_settings == :no_jit || compiled?
80
+ return true if (@run_count += 1) >= @jit_settings
81
+ end
82
+ def compiled?
83
+ !!@compiled
84
+ end
85
+ def decompile
86
+ @compiled = nil
87
+ end
88
+ def __build_via_jit(__local_fxml_controller, __local_namespace, __local_jruby_ext)
89
+ @compiled.__build_via_jit(__local_fxml_controller, __local_namespace, __local_jruby_ext)
90
+ end
91
+ def compile(code=@raw_code)
92
+ @raw_code = code
93
+ # TODO: begin rescue end
94
+ full_code = <<METHOD_DEF
95
+ def __build_via_jit(__local_fxml_controller, __local_namespace, __local_jruby_ext)
96
+ __local_fx_id_setter = lambda do |name, __i|
97
+ __local_namespace[name] = __i
98
+ __local_fxml_controller.instance_variable_set(("@\#{name}").to_sym, __i)
99
+ end
100
+ #{code}
101
+ end
102
+ METHOD_DEF
103
+ ;#)
104
+ unless @no_write
105
+ begin
106
+ jit_aot_cache(full_code)
107
+ rescue
108
+ p $!
109
+ jit_no_cache(full_code)
110
+ end
111
+ else
112
+ jit_no_cache(full_code)
113
+ end
114
+ if @opts && @opts[:compiled_hook]
115
+ @opts[:compiled_hook].call(@outfile)
116
+ end
117
+
118
+ end
119
+
120
+ def jit_no_cache(full_code)
121
+ dputs "JIT only, no aot for #{@file_name}"
122
+ self.instance_eval full_code
123
+ @compiled = true
124
+ end
125
+
126
+ def jit_aot_cache(full_code)
127
+ File.open(@outfile, "w") do |f|
128
+ hash = hash()
129
+ f << <<AOT
130
+ # #{hash} encoding: utf-8
131
+ # @@ 1
132
+
133
+ ########################### DO NOT MODIFY THIS FILE ###########################
134
+ # This file was automatically generated by JRubyFX-fxmlloader on #
135
+ # #{Time.now} for #{@file_name}
136
+ ########################### DO NOT MODIFY THIS FILE ###########################
137
+
138
+ module JRubyFX
139
+ module GeneratedAssets
140
+ class AOT#{@f_hash}
141
+ include JRubyFX
142
+ #{full_code}
143
+ def hash
144
+ #{hash.inspect}
145
+ end
146
+ def compiled?
147
+ true
148
+ end
149
+ end
150
+ end
151
+ end
152
+ AOT
153
+ end
154
+ puts "saved #{@outfile} to cache"
155
+ require @outfile
156
+ @compiled = JRubyFX::GeneratedAssets.const_get("AOT#{@f_hash}").new rescue nil
157
+ end
158
+ end
@@ -0,0 +1,290 @@
1
+ #/*
2
+ # * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3
+ # * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
+ # *
5
+ # * This code is free software; you can redistribute it and/or modify it
6
+ # * under the terms of the GNU General Public License version 2 only, as
7
+ # * published by the Free Software Foundation. Oracle designates this
8
+ # * particular file as subject to the "Classpath" exception as provided
9
+ # * by Oracle in the LICENSE file that accompanied this code.
10
+ # *
11
+ # * This code is distributed in the hope that it will be useful, but WITHOUT
12
+ # * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
+ # * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
+ # * version 2 for more details (a copy is included in the LICENSE file that
15
+ # * accompanied this code).
16
+ # *
17
+ # * You should have received a copy of the GNU General Public License version
18
+ # * 2 along with this work; if not, write to the Free Software Foundation,
19
+ # * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
+ # *
21
+ # * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
+ # * or visit www.oracle.com if you need additional information or have any
23
+ # * questions.
24
+ # */
25
+
26
+
27
+ #/**
28
+ # * Class representing an observable expression value.
29
+ # */
30
+ class RRExpressionValue < Java::javafx.beans.value.ObservableValueBase
31
+ #// Monitors a namespace for changes along a key path
32
+
33
+
34
+
35
+ def initialize(namespace, expression, type)
36
+ super()
37
+ if (namespace == nil)
38
+ raise "NullPointerException.new();"
39
+ end
40
+
41
+ if (expression == nil)
42
+ raise "NullPointerException.new();"
43
+ end
44
+
45
+ if (type == nil)
46
+ raise "NullPointerException.new();"
47
+ end
48
+
49
+ @listenerCount = 0;
50
+
51
+ @namespace = namespace;
52
+ @expression = expression;
53
+ @type = type;
54
+
55
+ arguments = expression.getArguments();
56
+ @argumentMonitors = java.util.ArrayList.new(arguments.size());
57
+
58
+ for argument in arguments
59
+ @argumentMonitors.add(KeyPathMonitor.new(self, argument.iterator()));
60
+ end
61
+ end
62
+
63
+
64
+ def getValue()
65
+ return RubyWrapperBeanAdapter.coerce(@expression.evaluate(@namespace), @type);
66
+ end
67
+
68
+ def addListener( listener)
69
+ if (@listenerCount == 0)
70
+ monitorArguments();
71
+ end
72
+
73
+ super(listener);
74
+ @listenerCount += 1
75
+ end
76
+
77
+ def removeListener( listener)
78
+ super(listener);
79
+ @listenerCount-=1
80
+
81
+ if (@listenerCount == 0)
82
+ unmonitorArguments();
83
+ end
84
+ end
85
+
86
+ def monitorArguments()
87
+ for argumentMonitor in @argumentMonitors
88
+ argumentMonitor.monitor(@namespace);
89
+ end
90
+ end
91
+
92
+ def unmonitorArguments()
93
+ for argumentMonitor in @argumentMonitors
94
+ argumentMonitor.unmonitor();
95
+ end
96
+ end
97
+ end
98
+
99
+
100
+ class KeyPathMonitor
101
+ @key = nil;
102
+ @next = nil
103
+
104
+ @namespace = nil;
105
+
106
+
107
+ class ListChangeImpl
108
+ include ListChangeListener
109
+
110
+ def initialize(this)
111
+ @this = this
112
+ end
113
+
114
+ def onChanged(change)
115
+ @this.list_changed(change)
116
+ end
117
+ end
118
+
119
+
120
+ class MapChangeImpl
121
+ include MapChangeListener
122
+
123
+ def initialize(this)
124
+ @this = this
125
+ end
126
+
127
+ def onChanged(change)
128
+ @this.map_changed(change)
129
+ end
130
+ end
131
+
132
+
133
+
134
+ class ChangeListenerImpl
135
+ include ChangeListener
136
+
137
+ def initialize(this)
138
+ @this = this
139
+ end
140
+
141
+ def changed(ov, old, new)
142
+ @this.normal_changed(ov, old, new)
143
+ end
144
+ end
145
+
146
+ def initialize(this, keyPathIterator)
147
+ @key = keyPathIterator.next();
148
+ @this = this
149
+
150
+
151
+ @listChangeListener = ListChangeImpl.new(self)
152
+
153
+
154
+ @mapChangeListener = MapChangeImpl.new(self)
155
+
156
+
157
+ @propertyChangeListener = ChangeListenerImpl.new(self)
158
+
159
+ if (keyPathIterator.hasNext())
160
+ @next = KeyPathMonitor.new(this, keyPathIterator);
161
+ else
162
+ @next = nil;
163
+ end
164
+ end
165
+
166
+ def list_changed(change)
167
+ while (change.next())
168
+ index = @key.to_i
169
+
170
+ if (index >= change.getFrom() && index < change.getTo())
171
+ @this.fireValueChangedEvent();
172
+ remonitor();
173
+ end
174
+ end
175
+ end
176
+
177
+ def map_changed(change)
178
+ if (@key == (change.getKey()))
179
+ @this.fireValueChangedEvent();
180
+ remonitor();
181
+ end
182
+ end
183
+
184
+ def normal_changed(observable, oldValue, newValue)
185
+ if (@key == (observable.getName()))
186
+
187
+ @this.fireValueChangedEvent();
188
+ remonitor();
189
+ end
190
+ end
191
+
192
+ def monitor(namespace)
193
+ if (namespace.is_a? ObservableList)
194
+ old_verbose = $VERBOSE
195
+ begin
196
+ $VERBOSE = nil
197
+ namespace.addListener @listChangeListener
198
+ ensure
199
+ # always re-set to old value, even if block raises an exception
200
+ $VERBOSE = old_verbose
201
+ end
202
+ elsif (namespace.is_a? ObservableMap)
203
+ old_verbose = $VERBOSE
204
+ begin
205
+ $VERBOSE = nil
206
+ namespace.addListener @mapChangeListener
207
+ ensure
208
+ # always re-set to old value, even if block raises an exception
209
+ $VERBOSE = old_verbose
210
+ end
211
+ else
212
+ namespaceAdapter = RubyWrapperBeanAdapter.for(namespace);
213
+ propertyModel = namespaceAdapter.getPropertyModel(@key).to_java
214
+ if (propertyModel != nil)
215
+ old_verbose = $VERBOSE
216
+ begin
217
+ $VERBOSE = nil
218
+ propertyModel.addListener @propertyChangeListener
219
+ ensure
220
+ # always re-set to old value, even if block raises an exception
221
+ $VERBOSE = old_verbose
222
+ end
223
+ end
224
+
225
+ @namespace = namespaceAdapter;
226
+ end
227
+
228
+ @namespace = namespace;
229
+
230
+ if (@next != nil)
231
+ value = Expression.get(@namespace, @key)
232
+ if (value != nil)
233
+ @next.monitor(value);
234
+ end
235
+ end
236
+ end
237
+
238
+ def unmonitor()
239
+ if (@namespace.is_a? ObservableList)
240
+ old_verbose = $VERBOSE
241
+ begin
242
+ $VERBOSE = nil
243
+ @namespace.removeListener @listChangeListener
244
+ ensure
245
+ # always re-set to old value, even if block raises an exception
246
+ $VERBOSE = old_verbose
247
+ end
248
+ elsif (@namespace.is_a? ObservableMap)
249
+ old_verbose = $VERBOSE
250
+ begin
251
+ $VERBOSE = nil
252
+ @namespace.removeListener @mapChangeListener
253
+ ensure
254
+ # always re-set to old value, even if block raises an exception
255
+ $VERBOSE = old_verbose
256
+ end
257
+ elsif (@namespace != nil)
258
+ namespaceAdapter = @namespace;
259
+ propertyModel = namespaceAdapter.getPropertyModel(@key);
260
+
261
+ if (propertyModel != nil)
262
+ old_verbose = $VERBOSE
263
+ begin
264
+ $VERBOSE = nil
265
+ propertyModel.removeListener @propertyChangeListener
266
+ ensure
267
+ # always re-set to old value, even if block raises an exception
268
+ $VERBOSE = old_verbose
269
+ end
270
+ end
271
+ end
272
+
273
+ @namespace = nil;
274
+
275
+ if (@next != nil)
276
+ @next.unmonitor();
277
+ end
278
+ end
279
+
280
+ def remonitor()
281
+ if (@next != nil)
282
+ @next.unmonitor();
283
+ value = Expression.get(@namespace, @key);
284
+ if (value != nil)
285
+ @next.monitor(value);
286
+ end
287
+ end
288
+ end
289
+ end
290
+