Class: JSON::Editor::JSONTreeView

Inherits:
Gtk::TreeView
  • Object
show all
Includes:
Gtk
Defined in:
lib/vendor/json_pure/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.



701
702
703
704
705
706
707
708
709
710
# File 'lib/vendor/json_pure/lib/json/editor.rb', line 701

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.



716
717
718
# File 'lib/vendor/json_pure/lib/json/editor.rb', line 716

def expanded
  @expanded
end

#windowObject (readonly)

Returns the MainWindow instance of this JSONTreeView.



713
714
715
# File 'lib/vendor/json_pure/lib/json/editor.rb', line 713

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.



902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
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
948
949
950
951
952
953
954
955
956
957
958
959
# File 'lib/vendor/json_pure/lib/json/editor.rb', line 902

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.pack_start(Label.new("Type:"), false)
  hbox.pack_start(type_input = ComboBox.new(true))
  default_active = 0
  types = parent ? ALL_TYPES : CONTAINER_TYPES
  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.pack_start(hbox, false)
  type_input.signal_connect(:changed) do
    configure_value(value_input, types[type_input.active])
  end

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

  dialog.vbox.pack_start(hbox, false)

  dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER)
  dialog.show_all
  self.focus = dialog
  dialog.run do |response| 
    if response == Dialog::RESPONSE_ACCEPT
      type = types[type_input.active]
      @content = case type
      when 'Numeric'
        if (t = value_input.text) == 'Infinity'
          1 / 0.0
        else
          Integer(t) rescue Float(t) rescue 0
        end
      else
        value_input.text
      end.to_s
      return type, @content
    end
  end
  return
ensure
  dialog.destroy if dialog
end

#ask_for_find_term(search = nil) ⇒ Object

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



997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
# File 'lib/vendor/json_pure/lib/json/editor.rb', line 997

def ask_for_find_term(search = nil)
  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.pack_start(Label.new("Regex:"), false)
  hbox.pack_start(regex_input = Entry.new)
  hbox.pack_start(icase_checkbox = CheckButton.new('Icase'), false)
  regex_input.width_chars = 60
  if search
    regex_input.text = search.source
    icase_checkbox.active = search.casefold?
  end

  dialog.vbox.pack_start(hbox, false)

  dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER)
  dialog.show_all
  self.focus = dialog
  dialog.run do |response| 
    if response == Dialog::RESPONSE_ACCEPT
      begin
        return Regexp.new(regex_input.text, icase_checkbox.active? ? Regexp::IGNORECASE : 0)
      rescue => e
        Editor.error_dialog(self, "Evaluation of regex /#{regex_input.text}/ failed: #{e}!")
        return
      end
    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.



831
832
833
834
835
836
837
838
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
889
890
891
892
893
894
895
896
897
898
899
# File 'lib/vendor/json_pure/lib/json/editor.rb', line 831

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 ]
  )
  dialog.width_request = 640

  hbox = HBox.new(false, 5)
  hbox.pack_start(Label.new("Key:"), false)
  hbox.pack_start(key_input = Entry.new)
  key_input.text = @key || ''
  dialog.vbox.pack_start(hbox, false)
  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.pack_start(Label.new("Type:"), false)
  hbox.pack_start(type_input = ComboBox.new(true))
  ALL_TYPES.each { |t| type_input.append_text(t) }
  type_input.active = @type || 0
  dialog.vbox.pack_start(hbox, false)

  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.pack_start(Label.new("Value:"), false)
  hbox.pack_start(value_input = Entry.new)
  value_input.width_chars = 60
  value_input.text = @value || ''
  dialog.vbox.pack_start(hbox, false)

  dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER)
  dialog.show_all
  self.focus = dialog
  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.



964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
# File 'lib/vendor/json_pure/lib/json/editor.rb', line 964

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.pack_start(Label.new("Order:"), false)
  hbox.pack_start(order_input = Entry.new)
  order_input.text = @order || 'x'
  order_input.width_chars = 60

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

  dialog.vbox.pack_start(hbox, false)

  dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER)
  dialog.show_all
  self.focus = dialog
  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.



817
818
819
820
821
822
823
824
825
826
827
828
# File 'lib/vendor/json_pure/lib/json/editor.rb', line 817

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.



1037
1038
1039
1040
1041
1042
1043
# File 'lib/vendor/json_pure/lib/json/editor.rb', line 1037

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