Class | JSON::Editor::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 357 357: def append_new_node(item) 358: if parent = selection.selected 359: parent_type = parent.type 360: case parent_type 361: when 'Hash' 362: key, type, content = ask_for_hash_pair(parent) 363: key or return 364: iter = create_node(parent, 'Key', key) 365: iter = create_node(iter, type, content) 366: toplevel.display_status( 367: "Added a (key, value)-pair to '#{parent_type}'.") 368: window.change 369: when 'Array' 370: type, content = ask_for_element(parent) 371: type or return 372: iter = create_node(parent, type, content) 373: window.change 374: toplevel.display_status("Appendend an element to '#{parent_type}'.") 375: else 376: toplevel.display_status("Cannot append to '#{parent_type}'!") 377: end 378: else 379: type, content = ask_for_element 380: type or return 381: iter = create_node(nil, type, content) 382: window.change 383: end 384: end
Change the type or content of the selected node.
# File lib/json/editor.rb, line 234 234: def change_node(item) 235: if current = selection.selected 236: parent = current.parent 237: old_type, old_content = current.type, current.content 238: if ALL_TYPES.include?(old_type) 239: @clipboard_data = Editor.model2data(current) 240: type, content = ask_for_element(parent, current.type, 241: current.content) 242: if type 243: current.type, current.content = type, content 244: current.remove_subtree(model) 245: toplevel.display_status("Changed a node in tree.") 246: window.change 247: end 248: else 249: toplevel.display_status( 250: "Cannot change node of type #{old_type} in tree!") 251: end 252: end 253: end
Recursively collapse/expand a subtree starting from the selected node.
# File lib/json/editor.rb, line 413 413: def collapse_expand(item) 414: if current = selection.selected 415: if row_expanded?(current.path) 416: collapse_row(current.path) 417: else 418: expand_row(current.path, true) 419: end 420: else 421: toplevel.display_status("Append a node into the root first!") 422: end 423: end
Copy the selected node and its subtree, and save it into the clipboard.
# File lib/json/editor.rb, line 274 274: def copy_node(item) 275: if current = selection.selected 276: if current and current.type == 'Key' 277: @clipboard_data = { 278: current.content => Editor.model2data(current.first_child) 279: } 280: else 281: @clipboard_data = Editor.model2data(current) 282: end 283: window.change 284: toplevel.display_status("Copied a node from tree.") 285: end 286: end
Create the menu.
# File lib/json/editor.rb, line 426 426: def create 427: add_item("Change node", &method(:change_node)) 428: add_separator 429: add_item("Cut node", &method(:cut_node)) 430: add_item("Copy node", &method(:copy_node)) 431: add_item("Paste node (appending)", &method(:paste_node_appending)) 432: add_item("Paste node (inserting before)", 433: &method(:paste_node_inserting_before)) 434: add_separator 435: add_item("Append new node", &method(:append_new_node)) 436: add_item("Insert new node before", &method(:insert_new_node)) 437: add_separator 438: add_item("Collapse/Expand node (recursively)", 439: &method(:collapse_expand)) 440: 441: menu.show_all 442: signal_connect(:button_press_event) do |widget, event| 443: if event.kind_of? Gdk::EventButton and event.button == 3 444: menu.popup(nil, nil, event.button, event.time) 445: end 446: end 447: signal_connect(:popup_menu) do 448: menu.popup(nil, nil, 0, Gdk::Event::CURRENT_TIME) 449: end 450: end
Cut the selected node and its subtree, and save it into the clipboard.
# File lib/json/editor.rb, line 257 257: def cut_node(item) 258: if current = selection.selected 259: if current and current.type == 'Key' 260: @clipboard_data = { 261: current.content => Editor.model2data(current.first_child) 262: } 263: else 264: @clipboard_data = Editor.model2data(current) 265: end 266: model.remove(current) 267: window.change 268: toplevel.display_status("Cut a node from tree.") 269: end 270: end
Insert a new node into an Array before the selected element.
# File lib/json/editor.rb, line 387 387: def insert_new_node(item) 388: if current = selection.selected 389: parent = current.parent or return 390: parent_parent = parent.parent 391: parent_type = parent.type 392: if parent_type == 'Array' 393: selected_index = parent.each_with_index do |c, i| 394: break i if c == current 395: end 396: type, content = ask_for_element(parent) 397: type or return 398: iter = model.insert_before(parent, current) 399: iter.type, iter.content = type, content 400: toplevel.display_status("Inserted an element to " + 401: "'#{parent_type}' before index #{selected_index}.") 402: window.change 403: else 404: toplevel.display_status( 405: "Cannot insert node below '#{parent_type}'!") 406: end 407: else 408: toplevel.display_status("Append a node into the root first!") 409: end 410: end
Paste the data in the clipboard into the selected Array or Hash by appending it.
# File lib/json/editor.rb, line 290 290: def paste_node_appending(item) 291: if current = selection.selected 292: if @clipboard_data 293: case current.type 294: when 'Array' 295: Editor.data2model(@clipboard_data, model, current) 296: expand_collapse(current) 297: when 'Hash' 298: if @clipboard_data.is_a? Hash 299: parent = current.parent 300: hash = Editor.model2data(current) 301: model.remove(current) 302: hash.update(@clipboard_data) 303: Editor.data2model(hash, model, parent) 304: if parent 305: expand_collapse(parent) 306: elsif @expanded 307: expand_all 308: end 309: window.change 310: else 311: toplevel.display_status( 312: "Cannot paste non-#{current.type} data into '#{current.type}'!") 313: end 314: else 315: toplevel.display_status( 316: "Cannot paste node below '#{current.type}'!") 317: end 318: else 319: toplevel.display_status("Nothing to paste in clipboard!") 320: end 321: else 322: toplevel.display_status("Append a node into the root first!") 323: end 324: end
Paste the data in the clipboard into the selected Array inserting it before the selected element.
# File lib/json/editor.rb, line 328 328: def paste_node_inserting_before(item) 329: if current = selection.selected 330: if @clipboard_data 331: parent = current.parent or return 332: parent_type = parent.type 333: if parent_type == 'Array' 334: selected_index = parent.each_with_index do |c, i| 335: break i if c == current 336: end 337: Editor.data2model(@clipboard_data, model, parent) do |m| 338: m.insert_before(parent, current) 339: end 340: expand_collapse(current) 341: toplevel.display_status("Inserted an element to " + 342: "'#{parent_type}' before index #{selected_index}.") 343: window.change 344: else 345: toplevel.display_status( 346: "Cannot insert node below '#{parent_type}'!") 347: end 348: else 349: toplevel.display_status("Nothing to paste in clipboard!") 350: end 351: else 352: toplevel.display_status("Append a node into the root first!") 353: end 354: end