1863
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
1869
|
3 local raw_set = Luan.raw_set or error()
|
1882
|
4 local stringify = Luan.stringify or error()
|
1884
|
5 local pairs = Luan.pairs or error()
|
1882
|
6 local Table = require "luan:Table.luan"
|
|
7 local is_empty = Table.is_empty or error()
|
1893
|
8 require "java"
|
|
9 local Dimension = require "java:java.awt.Dimension"
|
1914
|
10 local Point = require "java:java.awt.Point"
|
1863
|
11
|
|
12
|
|
13 local Utils = {}
|
|
14
|
|
15 local fail = {}
|
|
16 Utils.fail = fail
|
|
17
|
|
18 function Utils.make_metatable(class)
|
|
19 local __index = class.__index or error()
|
|
20 local __new_index = class.__new_index or error()
|
|
21
|
|
22 local mt = {}
|
|
23
|
|
24 function mt.__index(t,key)
|
|
25 local rtn = __index(t,key)
|
|
26 rtn ~= fail or error("'"..key.."' not defined")
|
|
27 return rtn
|
|
28 end
|
|
29
|
|
30 function mt.__new_index(t,key,value)
|
|
31 local rtn = __new_index(t,key,value)
|
1872
|
32 rtn ~= fail or error("'"..key.."' not defined")
|
1863
|
33 end
|
|
34
|
|
35 return mt
|
|
36 end
|
|
37
|
1944
|
38 function Utils.remove(t,key)
|
1882
|
39 return raw_set(t,key,nil)
|
|
40 end
|
|
41
|
|
42 function Utils.check_empty(props)
|
1884
|
43 if not is_empty(props) then
|
|
44 local keys = {}
|
|
45 for key in pairs(props) do
|
|
46 keys[#keys+1] = key
|
|
47 end
|
|
48 error("unrecognized keys "..stringify(keys))
|
|
49 end
|
|
50 end
|
|
51
|
|
52 function Utils.check_not_nil(props)
|
|
53 props or error "missing required properties table"
|
1882
|
54 end
|
|
55
|
1893
|
56 function Utils.to_dimension(tbl)
|
|
57 local width = tbl.width or error "missing width"
|
|
58 local height = tbl.height or error "missing height"
|
|
59 return Dimension.new(width,height)
|
|
60 end
|
|
61
|
1914
|
62 function Utils.from_dimension(dim)
|
|
63 return { width=dim.width, height=dim.height }
|
|
64 end
|
|
65
|
|
66 function Utils.to_point(tbl)
|
|
67 local x = tbl.x or error "missing x"
|
|
68 local y = tbl.y or error "missing y"
|
|
69 return Point.new(x,y)
|
|
70 end
|
|
71
|
|
72 function Utils.from_point(point)
|
|
73 return { x=point.x, y=point.y }
|
|
74 end
|
|
75
|
1863
|
76 return Utils
|