Class: Doodl::PluggableRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/jruby/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePluggableRenderer

Returns a new instance of PluggableRenderer.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jruby/renderer.rb', line 30

def initialize
  super
  @paint_iterations = true
  @transform = AffineTransform.new
  @node_shapes, @edge_shapes = [], []
  set_defaults
  setBackground(Color::black)
  setDoubleBuffered(true)
  @tooltip = TooltipMouseListener.new
  addMouseMotionListener(@tooltip)
end

Instance Attribute Details

#edge_shapesObject (readonly)

Returns the value of attribute edge_shapes.



28
29
30
# File 'lib/jruby/renderer.rb', line 28

def edge_shapes
  @edge_shapes
end

#node_shapesObject (readonly)

Returns the value of attribute node_shapes.



28
29
30
# File 'lib/jruby/renderer.rb', line 28

def node_shapes
  @node_shapes
end

#paint_iterations=(value) ⇒ Object (writeonly)

Sets the attribute paint_iterations

Parameters:

  • value

    the value to set the attribute paint_iterations to.



27
28
29
# File 'lib/jruby/renderer.rb', line 27

def paint_iterations=(value)
  @paint_iterations = value
end

Instance Method Details

#centerObject



203
204
205
# File 'lib/jruby/renderer.rb', line 203

def center
  Location.new(width/2.0, height/2.0)
end

#center_on_node(node) ⇒ Object



182
183
184
185
186
187
# File 'lib/jruby/renderer.rb', line 182

def center_on_node(node)
  @transform.translate(-@transform.getTranslateX, -@transform.getTranslateY)
  location = @layout[node]
  @transform.translate(-location.x + center.x, -location.y + center.y)
  repaint
end

#edge_label_position(edge, label_layout) ⇒ Object



270
271
272
# File 'lib/jruby/renderer.rb', line 270

def edge_label_position(edge, label_layout)
  @edge_label_position_function.call(@graph, @layout, edge, label_layout)
end

#edge_label_position_function(&block) ⇒ Object

Raises:

  • (ArgumentError)


265
266
267
268
# File 'lib/jruby/renderer.rb', line 265

def edge_label_position_function(&block)
  raise ArgumentError unless block_given?
  @edge_label_position_function = block
end

#edge_label_position_function=(function) ⇒ Object



261
262
263
# File 'lib/jruby/renderer.rb', line 261

def edge_label_position_function=(function)
  @edge_label_position_function = function
end

#heightObject



211
212
213
# File 'lib/jruby/renderer.rb', line 211

def height
  return self.getHeight
end

#move(x, y, smooth = true) ⇒ Object



178
179
180
# File 'lib/jruby/renderer.rb', line 178

def move(x, y, smooth = true)
  repaint
end

#node_label_position(node, label_layout) ⇒ Object



257
258
259
# File 'lib/jruby/renderer.rb', line 257

def node_label_position(node, label_layout)
  @node_label_position_function.call(@graph, @layout, node, label_layout)
end

#node_label_position_function(&block) ⇒ Object

Raises:

  • (ArgumentError)


252
253
254
255
# File 'lib/jruby/renderer.rb', line 252

def node_label_position_function(&block)
  raise ArgumentError unless block_given?
  @node_label_position_function = block
end

#node_label_position_function=(function) ⇒ Object



248
249
250
# File 'lib/jruby/renderer.rb', line 248

def node_label_position_function=(function)
  @node_label_position_function = function
end

#paint_arrow_head(g, edge) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/jruby/renderer.rb', line 103

def paint_arrow_head(g, edge)
  at = g.getTransform
  loc_and_angle = arrow_head_properties(g, edge)
  if loc_and_angle
    g.translate(loc_and_angle[0].x, loc_and_angle[0].y)
    g.rotate(loc_and_angle[1])
    g.setColor arrow_head_fill_color(edge)
    shape = arrow_head_shape(edge)
    g.fill(shape)
    g.setStroke arrow_head_stroke(edge)
    g.setColor arrow_head_stroke_color(edge)
    g.draw(shape)
    g.setTransform(at)
  end
end

#paint_edge(g, edge) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/jruby/renderer.rb', line 92

def paint_edge(g, edge)
  if edge_predicate(edge) and node_predicate(edge.source) and node_predicate(edge.target)
    g.setColor edge_stroke_color(edge)
    shape = edge_shape(edge)
    @edge_shapes << [edge, shape]
    g.setStroke edge_stroke(edge)
    g.draw(shape)
    paint_arrow_head(g, edge) if arrow_head_predicate(edge)
  end
end

#paint_edge_label(g, edge) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/jruby/renderer.rb', line 128

def paint_edge_label(g, edge)
  if edge_label_predicate(edge) and text = edge_label_string(edge)
    g.setColor edge_label_color(edge)
    layout = TextLayout.new(text, g.getFont, g.getFontRenderContext)
    position = edge_label_position(edge, layout)
    layout.draw(g, position.x, position.y)
  end
end

#paint_edge_labels(g) ⇒ Object



170
171
172
# File 'lib/jruby/renderer.rb', line 170

def paint_edge_labels(g)
  @graph.each_edge { |edge| paint_edge_label(g, edge) }
end

#paint_edges(g) ⇒ Object



162
163
164
# File 'lib/jruby/renderer.rb', line 162

def paint_edges(g)
  @graph.each_edge { |edge| paint_edge(g, edge) }
end

#paint_graph(g) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/jruby/renderer.rb', line 68

def paint_graph(g)
  if @graph and @layout
    @node_shapes.clear
    @edge_shapes.clear
    paint_edges(g)
    paint_nodes(g)
    paint_node_labels(g)
    paint_edge_labels(g)
    paint_tooltip(g)
  end
end

#paint_iterations?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/jruby/renderer.rb', line 174

def paint_iterations?
  @paint_iterations
end

#paint_node(g, node) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jruby/renderer.rb', line 80

def paint_node(g, node)
  if node_predicate(node)
    shape = node_shape(node)
    @node_shapes << [node, shape]
    g.setColor node_fill_color(node)
    g.fill(shape)
    g.setColor node_stroke_color(node)
    g.setStroke node_stroke(node)
    g.draw(shape)
  end
end

#paint_node_label(g, node) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/jruby/renderer.rb', line 119

def paint_node_label(g, node)
  if node_label_predicate(node) and text = node_label_string(node)
    g.setColor node_label_color(node)
    layout = TextLayout.new(text, g.getFont, g.getFontRenderContext)
    position = node_label_position(node, layout)
    layout.draw(g, position.x, position.y)
  end
end

#paint_node_labels(g) ⇒ Object



166
167
168
# File 'lib/jruby/renderer.rb', line 166

def paint_node_labels(g)
  @graph.each_node { |node| paint_node_label(g, node) }
end

#paint_nodes(g) ⇒ Object



158
159
160
# File 'lib/jruby/renderer.rb', line 158

def paint_nodes(g)
  @graph.each_node { |node| paint_node(g, node) }
end

#paint_tooltip(g) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/jruby/renderer.rb', line 137

def paint_tooltip(g)
  element = @tooltip.element
  if element and tooltip_predicate(element)
    temp = g.getTransform
    layout = TextLayout.new(tooltip_string(element), g.getFont, g.getFontRenderContext)
    rect = layout.getBounds
    rect.x -= 3
    rect.y -= 3
    rect.width += 5
    rect.height += 5
    g.translate(@tooltip.position.x + 15, @tooltip.position.y - 15)
    g.setColor(tooltip_fill_color(element))
    g.fill(rect)
    g.setColor(tooltip_stroke_color(element))
    g.setStroke(tooltip_stroke(element))
    g.draw(rect)
    layout.draw(g, 0, 0)
    g.setTransform(temp)
  end
end

#paintComponent(g) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/jruby/renderer.rb', line 57

def paintComponent(g)
  g.setColor(getBackground)
  g.fill(Rectangle2D::Double.new(0, 0, width, height))
  # g.setFont(Font.new("Gill Sans", Font::BOLD, 14))
  rh =  RenderingHints.new(
    RenderingHints.new(RenderingHints::KEY_ANTIALIASING, RenderingHints::VALUE_ANTIALIAS_ON))
  g.setTransform(@transform)
  g.setRenderingHints(rh)
  paint_graph(g)
end

#set_defaultsObject



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/jruby/renderer.rb', line 274

def set_defaults
  node_shape_function { |g, l, n| Ellipse2D::Double.new(l[n].x-10, l[n].y-10, 20, 20) }
  node_stroke_function { BasicStroke.new(2) }
  node_predicate_function { true }
  node_stroke_color_function { Color::white }
  node_fill_color_function { Color::black }
  node_label_predicate_function { false }
  node_label_string_function { |g, l, n| n.to_s }
  node_label_position_function do |g, l, node, layout|
    nb = node_shape(node).getBounds2D
    lb = layout.getBounds
    Point2D::Double.new(nb.getCenterX-lb.getCenterX, nb.getCenterY-lb.getCenterY)
  end
  node_label_color_function { Color::white }

  edge_shape_function { |g, l, e| Line2D::Double.new(l[e.source].x, l[e.source].y, l[e.target].x, l[e.target].y) }
  edge_predicate_function { true }
  edge_stroke_function { |g, l, e| BasicStroke.new(2) }
  edge_stroke_color_function { Color::white }
  edge_fill_color_function { Color::white }

  edge_label_predicate_function { true }
  edge_label_string_function { |g, l, e| g.edge_data(:text)[e] if g.has_edge_data_key?(:text) }
  edge_label_color_function { Color::white }
  edge_label_position_function do |g, l, edge, layout|
    shape = edge_shape(edge)
    edge_bounds = shape.getBounds2D
    label_bounds = layout.getBounds
    ex = edge_bounds.getCenterX
    ey = edge_bounds.getCenterY
    lx = label_bounds.getCenterX
    ly = label_bounds.getCenterY
    v = Location.new(l[edge.source], l[edge.target])
    v.normalize!
    v.rotate!(90)
    v.mul!(15)
    v.add!(ex-lx, ey-ly)
  end

  arrow_head_shape_function { Polygon.new([0, 4, 0, -4, 0].to_java(:int), [0, 14, 11, 14, 0].to_java(:int), 5) }
  arrow_head_predicate_function { |g, l, e| e.directed? }
  arrow_head_stroke_function { BasicStroke.new(2) }
  arrow_head_stroke_color_function { Color::white }
  arrow_head_fill_color_function { Color::white }

  tooltip_string_function { |g, l, element| element.to_s }
  tooltip_stroke_function { |g, l, element| BasicStroke.new(1) }
  tooltip_stroke_color_function { |g, l, element| Color::WHITE }
  tooltip_fill_color_function { |g, l, element| Color::BLACK }
  tooltip_predicate_function { true }
end

#update_layout(layout) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jruby/renderer.rb', line 42

def update_layout(layout)
  if (paint_iterations? or layout.finished?)
    @layout, @graph = layout, layout.graph
    @transform = AffineTransform.new
    if SwingUtilities.isEventDispatchThread
      repaint
    else
      SwingUtilities.invokeAndWait(java.lang.Runnable.impl do
        repaint
        java.lang.Thread.sleep(20)
      end)
    end
  end
end

#widthObject



207
208
209
# File 'lib/jruby/renderer.rb', line 207

def width
  return self.getWidth
end

#zoom(f) ⇒ Object



189
190
191
192
193
194
# File 'lib/jruby/renderer.rb', line 189

def zoom(f)
  @transform.translate(center.x, center.y)
  @transform.scale(f, f)
  @transform.translate(-center.x, -center.y)
  repaint
end

#zoom_absolute(f) ⇒ Object



196
197
198
199
200
201
# File 'lib/jruby/renderer.rb', line 196

def zoom_absolute(f)
  @transform.translate(center.x, center.y)
  @transform.scale(f/@transform.getScaleX, f/@transform.getScaleY)
  @transform.translate(-center.x, -center.y)
  repaint
end