| Class | PopUpMenu |
| In: |
lib/json/editor.rb
|
| Parent: | Object |
This class creates the popup menu, that opens when clicking onto the treeview.
Append a new node to the selected Hash or Array.
# File lib/json/editor.rb, line 388
388: def append_new_node(item)
389: if parent = selection.selected
390: parent_type = parent.type
391: case parent_type
392: when 'Hash'
393: key, type, content = ask_for_hash_pair(parent)
394: key or return
395: iter = create_node(parent, 'Key', key)
396: iter = create_node(iter, type, content)
397: toplevel.display_status(
398: "Added a (key, value)-pair to '#{parent_type}'.")
399: window.change
400: when 'Array'
401: type, content = ask_for_element(parent)
402: type or return
403: iter = create_node(parent, type, content)
404: window.change
405: toplevel.display_status("Appendend an element to '#{parent_type}'.")
406: else
407: toplevel.display_status("Cannot append to '#{parent_type}'!")
408: end
409: else
410: type, content = ask_for_element
411: type or return
412: iter = create_node(nil, type, content)
413: window.change
414: end
415: end
Change the type or content of the selected node.
# File lib/json/editor.rb, line 265
265: def change_node(item)
266: if current = selection.selected
267: parent = current.parent
268: old_type, old_content = current.type, current.content
269: if ALL_TYPES.include?(old_type)
270: @clipboard_data = Editor.model2data(current)
271: type, content = ask_for_element(parent, current.type,
272: current.content)
273: if type
274: current.type, current.content = type, content
275: current.remove_subtree(model)
276: toplevel.display_status("Changed a node in tree.")
277: window.change
278: end
279: else
280: toplevel.display_status(
281: "Cannot change node of type #{old_type} in tree!")
282: end
283: end
284: end
Recursively collapse/expand a subtree starting from the selected node.
# File lib/json/editor.rb, line 444
444: def collapse_expand(item)
445: if current = selection.selected
446: if row_expanded?(current.path)
447: collapse_row(current.path)
448: else
449: expand_row(current.path, true)
450: end
451: else
452: toplevel.display_status("Append a node into the root first!")
453: end
454: end
Copy the selected node and its subtree, and save it into the clipboard.
# File lib/json/editor.rb, line 305
305: def copy_node(item)
306: if current = selection.selected
307: if current and current.type == 'Key'
308: @clipboard_data = {
309: current.content => Editor.model2data(current.first_child)
310: }
311: else
312: @clipboard_data = Editor.model2data(current)
313: end
314: window.change
315: toplevel.display_status("Copied a node from tree.")
316: end
317: end
Create the menu.
# File lib/json/editor.rb, line 457
457: def create
458: add_item("Change node", ?n, &method(:change_node))
459: add_separator
460: add_item("Cut node", ?X, &method(:cut_node))
461: add_item("Copy node", ?C, &method(:copy_node))
462: add_item("Paste node (appending)", ?A, &method(:paste_node_appending))
463: add_item("Paste node (inserting before)", ?I,
464: &method(:paste_node_inserting_before))
465: add_separator
466: add_item("Append new node", ?a, &method(:append_new_node))
467: add_item("Insert new node before", ?i, &method(:insert_new_node))
468: add_separator
469: add_item("Collapse/Expand node (recursively)", ?e,
470: &method(:collapse_expand))
471:
472: menu.show_all
473: signal_connect(:button_press_event) do |widget, event|
474: if event.kind_of? Gdk::EventButton and event.button == 3
475: menu.popup(nil, nil, event.button, event.time)
476: end
477: end
478: signal_connect(:popup_menu) do
479: menu.popup(nil, nil, 0, Gdk::Event::CURRENT_TIME)
480: end
481: end
Cut the selected node and its subtree, and save it into the clipboard.
# File lib/json/editor.rb, line 288
288: def cut_node(item)
289: if current = selection.selected
290: if current and current.type == 'Key'
291: @clipboard_data = {
292: current.content => Editor.model2data(current.first_child)
293: }
294: else
295: @clipboard_data = Editor.model2data(current)
296: end
297: model.remove(current)
298: window.change
299: toplevel.display_status("Cut a node from tree.")
300: end
301: end
Insert a new node into an Array before the selected element.
# File lib/json/editor.rb, line 418
418: def insert_new_node(item)
419: if current = selection.selected
420: parent = current.parent or return
421: parent_parent = parent.parent
422: parent_type = parent.type
423: if parent_type == 'Array'
424: selected_index = parent.each_with_index do |c, i|
425: break i if c == current
426: end
427: type, content = ask_for_element(parent)
428: type or return
429: iter = model.insert_before(parent, current)
430: iter.type, iter.content = type, content
431: toplevel.display_status("Inserted an element to " +
432: "'#{parent_type}' before index #{selected_index}.")
433: window.change
434: else
435: toplevel.display_status(
436: "Cannot insert node below '#{parent_type}'!")
437: end
438: else
439: toplevel.display_status("Append a node into the root first!")
440: end
441: end
Paste the data in the clipboard into the selected Array or Hash by appending it.
# File lib/json/editor.rb, line 321
321: def paste_node_appending(item)
322: if current = selection.selected
323: if @clipboard_data
324: case current.type
325: when 'Array'
326: Editor.data2model(@clipboard_data, model, current)
327: expand_collapse(current)
328: when 'Hash'
329: if @clipboard_data.is_a? Hash
330: parent = current.parent
331: hash = Editor.model2data(current)
332: model.remove(current)
333: hash.update(@clipboard_data)
334: Editor.data2model(hash, model, parent)
335: if parent
336: expand_collapse(parent)
337: elsif @expanded
338: expand_all
339: end
340: window.change
341: else
342: toplevel.display_status(
343: "Cannot paste non-#{current.type} data into '#{current.type}'!")
344: end
345: else
346: toplevel.display_status(
347: "Cannot paste node below '#{current.type}'!")
348: end
349: else
350: toplevel.display_status("Nothing to paste in clipboard!")
351: end
352: else
353: toplevel.display_status("Append a node into the root first!")
354: end
355: end
Paste the data in the clipboard into the selected Array inserting it before the selected element.
# File lib/json/editor.rb, line 359
359: def paste_node_inserting_before(item)
360: if current = selection.selected
361: if @clipboard_data
362: parent = current.parent or return
363: parent_type = parent.type
364: if parent_type == 'Array'
365: selected_index = parent.each_with_index do |c, i|
366: break i if c == current
367: end
368: Editor.data2model(@clipboard_data, model, parent) do |m|
369: m.insert_before(parent, current)
370: end
371: expand_collapse(current)
372: toplevel.display_status("Inserted an element to " +
373: "'#{parent_type}' before index #{selected_index}.")
374: window.change
375: else
376: toplevel.display_status(
377: "Cannot insert node below '#{parent_type}'!")
378: end
379: else
380: toplevel.display_status("Nothing to paste in clipboard!")
381: end
382: else
383: toplevel.display_status("Append a node into the root first!")
384: end
385: end