Class: JSON::Editor::JSONTreeView

Inherits:
Gtk::TreeView
  • Object
show all
Includes:
Gtk
Defined in:
lib/json/editor.rb

Overview

This class inherits from Gtk::TreeView, to configure it and to add a lot of behaviour to it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window) ⇒ JSONTreeView

Creates a JSONTreeView instance, the parameter window is a MainWindow instance and used for self delegation.



647
648
649
650
651
652
653
654
655
656
# File 'lib/json/editor.rb', line 647

def initialize(window)
  @window = window
  super(TreeStore.new(Gdk::Pixbuf, String, String))
  self.selection.mode = SELECTION_BROWSE

  @expanded = false
  self.headers_visible = false
  add_columns
  add_popup_menu
end

Instance Attribute Details

#expandedObject

Returns true, if nodes are autoexpanding, false otherwise.



662
663
664
# File 'lib/json/editor.rb', line 662

def expanded
  @expanded
end

#windowObject (readonly)

Returns the MainWindow instance of this JSONTreeView.



659
660
661
# File 'lib/json/editor.rb', line 659

def window
  @window
end

Instance Method Details

#ask_for_element(parent = nil, default_type = nil, value_text = @content) ⇒ Object

Ask for an element to be appended parent.



839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
# File 'lib/json/editor.rb', line 839

def ask_for_element(parent = nil, default_type = nil, value_text = @content)
  type_input = value_input = nil

  dialog = Dialog.new(
    "New element into #{parent ? parent.type : 'root'}",
    nil, nil,
    [ Stock::OK, Dialog::RESPONSE_ACCEPT ],
    [ Stock::CANCEL, Dialog::RESPONSE_REJECT ]
  )
  hbox = HBox.new(false, 5)
  hbox.add(Label.new("Type:"))
  hbox.pack_start(type_input = ComboBox.new(true))
  default_active = 0
  ALL_TYPES.each_with_index do |t, i|
    type_input.append_text(t)
    if t == default_type
      default_active = i
    end
  end
  type_input.active = default_active
  dialog.vbox.add(hbox)
  type_input.signal_connect(:changed) do
    configure_value(value_input, ALL_TYPES[type_input.active])
  end

  hbox = HBox.new(false, 5)
  hbox.add(Label.new("Value:"))
  hbox.pack_start(value_input = Entry.new)
  value_input.text = value_text if value_text
  configure_value(value_input, ALL_TYPES[type_input.active])

  dialog.vbox.add(hbox)

  dialog.show_all
  dialog.run do |response| 
    if response == Dialog::RESPONSE_ACCEPT
      type = ALL_TYPES[type_input.active]
      @content = case type
      when 'Numeric'
        Integer(value_input.text) rescue Float(value_input.text) rescue 0
      else
        value_input.text
      end.to_s
      return type, @content
    end
  end
  return
ensure
  dialog.destroy if dialog
end

#ask_for_find_termObject

Ask for a find term to search for in the tree. Returns the term as a string.



923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
# File 'lib/json/editor.rb', line 923

def ask_for_find_term
  dialog = Dialog.new(
    "Find a node matching regex in tree.",
    nil, nil,
    [ Stock::OK, Dialog::RESPONSE_ACCEPT ],
    [ Stock::CANCEL, Dialog::RESPONSE_REJECT ]
  )
  hbox = HBox.new(false, 5)

  hbox.add(Label.new("Regex:"))
  hbox.pack_start(regex_input = Entry.new)
  regex_input.text = @regex || ''

  dialog.vbox.add(hbox)

  dialog.show_all
  dialog.run do |response| 
    if response == Dialog::RESPONSE_ACCEPT
      return @regex = regex_input.text
    end
  end
  return
ensure
  dialog.destroy if dialog
end

#ask_for_hash_pair(parent) ⇒ Object

Ask for a hash key, value pair to be added to the Hash node parent.



772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
# File 'lib/json/editor.rb', line 772

def ask_for_hash_pair(parent)
  key_input = type_input = value_input = nil

  dialog = Dialog.new("New (key, value) pair for Hash", nil, nil,
    [ Stock::OK, Dialog::RESPONSE_ACCEPT ],
    [ Stock::CANCEL, Dialog::RESPONSE_REJECT ]
  )

  hbox = HBox.new(false, 5)
  hbox.pack_start(Label.new("Key:"))
  hbox.pack_start(key_input = Entry.new)
  key_input.text = @key || ''
  dialog.vbox.add(hbox)
  key_input.signal_connect(:activate) do
    if parent.any? { |c| c.content == key_input.text }
      toplevel.display_status('Key already exists in Hash!')
      key_input.text = ''
    else
      toplevel.display_status('Key has been changed.')
    end
  end

  hbox = HBox.new(false, 5)
  hbox.add(Label.new("Type:"))
  hbox.pack_start(type_input = ComboBox.new(true))
  ALL_TYPES.each { |t| type_input.append_text(t) }
  type_input.active = @type || 0
  dialog.vbox.add(hbox)

  type_input.signal_connect(:changed) do
    value_input.editable = false
    case ALL_TYPES[type_input.active]
    when 'Array', 'Hash'
      value_input.text = ''
    when 'TrueClass'
      value_input.text = 'true'
    when 'FalseClass'
      value_input.text = 'false'
    when 'NilClass'
      value_input.text = 'null'
    else
      value_input.text = ''
      value_input.editable = true
    end
  end

  hbox = HBox.new(false, 5)
  hbox.add(Label.new("Value:"))
  hbox.pack_start(value_input = Entry.new)
  value_input.text = @value || ''
  dialog.vbox.add(hbox)

  dialog.show_all
  dialog.run do |response| 
    if response == Dialog::RESPONSE_ACCEPT
      @key = key_input.text
      type = ALL_TYPES[@type = type_input.active]
      content = value_input.text
      return @key, type, content
    end
  end
  return
ensure
  dialog.destroy
end

#ask_for_orderObject

Ask for an order criteria for sorting, using x for the element in question. Returns the order criterium, and true/false for reverse sorting.



893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
# File 'lib/json/editor.rb', line 893

def ask_for_order
  dialog = Dialog.new(
    "Give an order criterium for 'x'.",
    nil, nil,
    [ Stock::OK, Dialog::RESPONSE_ACCEPT ],
    [ Stock::CANCEL, Dialog::RESPONSE_REJECT ]
  )
  hbox = HBox.new(false, 5)

  hbox.add(Label.new("Order:"))
  hbox.pack_start(order_input = Entry.new)
  order_input.text = @order || 'x'

  hbox.pack_start(reverse_checkbox = CheckButton.new('Reverse'))

  dialog.vbox.add(hbox)

  dialog.show_all
  dialog.run do |response| 
    if response == Dialog::RESPONSE_ACCEPT
      return @order = order_input.text, reverse_checkbox.active?
    end
  end
  return
ensure
  dialog.destroy if dialog
end

#create_node(parent, type, content) ⇒ Object

Create a type node with content content, and add it to parent in the model. If parent is nil, create a new model and put it into the editor treeview.



758
759
760
761
762
763
764
765
766
767
768
769
# File 'lib/json/editor.rb', line 758

def create_node(parent, type, content)
  iter = if parent
    model.append(parent)
  else
    new_model = Editor.data2model(nil)
    toplevel.view_new_model(new_model)
    new_model.iter_first
  end
  iter.type, iter.content = type, content
  expand_collapse(parent) if parent
  iter
end

#expand_collapse(iter) ⇒ Object

Expand or collapse row pointed to by iter according to the #expanded attribute.



951
952
953
954
955
956
957
# File 'lib/json/editor.rb', line 951

def expand_collapse(iter)
  if expanded
    expand_row(iter.path, true)
  else
    collapse_row(iter.path)
  end
end