class Relation

this file is part of manqod manqod is distributed under the CDDL licence the author of manqod is Dobai-Pataky Balint(dpblnt@gmail.com)

Attributes

handle[R]
parentM[R]
rel_custom[R]
rel_name[R]
rel_type[R]
relation_id[R]
rfield1[R]
rfield2[R]

Public Class Methods

new(parentM,rel_data) click to toggle source
Calls superclass method
# File lib/RelationBuilder/Relation.rb, line 8
def initialize(parentM,rel_data)
        super()
        @parentM=parentM
        reload(rel_data)
        @shown=false
end

Public Instance Methods

active?() click to toggle source
# File lib/RelationBuilder/Relation.rb, line 60
def active?
        @handle.widget.active?
end
edit() click to toggle source
# File lib/RelationBuilder/Relation.rb, line 99
        def edit
                dialog=Gtk::Dialog.new("relation properties",
                        @handle.get_ancestor(Gtk::Window),
                        Gtk::Dialog::MODAL | Gtk::Dialog::DESTROY_WITH_PARENT,
                        [Gtk::Stock::OK, Gtk::Dialog::RESPONSE_ACCEPT] #,
#                       [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_REJECT]
                        )
                dialog.vbox.
                        pack_start_defaults(many2one=Gtk::ToggleButton.new("Many to One")).
                        pack_start_defaults(rel_dir=Gtk::Button.new("change direction")).
                        pack_start_defaults(description=Gtk::Label.new).
                        pack_start_defaults(Gtk::HBox.new.pack_start_defaults(Gtk::Label.new("alias")).pack_start_defaults(rel_name_widget=Gtk::Entry.new.set_text(@rel_name))).
                        pack_start_defaults(Gtk::HBox.new.pack_start_defaults(Gtk::Label.new("and join condition")).pack_start_defaults(rel_custom_widget=Gtk::Entry.new.set_text(@rel_custom))).
                        pack_start_defaults(remove_rel=Gtk::ToggleButton.new("Remove"))
                        
                rel_dir.signal_connect('pressed'){|me|
                        a=@rfield1
                        @rfield1=@rfield2
                        @rfield2=a
                        description.set_label("#{if many2one.active? then "Many" else "One" end} #{@rfield1.table_name} to One #{@rfield2.table_name}") unless @rfield1.nil? or @rfield2.nil?
                }
                many2one.signal_connect("toggled"){|me|
                        description.set_label("#{if many2one.active? then "Many" else "One" end} #{@rfield1.table_name} to One #{@rfield2.table_name}") unless @rfield1.nil? or @rfield2.nil?
                }
                many2one.set_active(rel_type=="m")

                description.set_label("#{if many2one.active? then "Many" else "One" end} #{@rfield1.table_name} to One #{@rfield2.table_name}") unless @rfield1.nil? or @rfield2.nil?
                dialog.signal_connect('response'){|dialog,response|
#                       rfield1.set_active(false)
#                       rfield2.set_active(false)
                        if response == Gtk::Dialog::RESPONSE_ACCEPT
                                if remove_rel.active?
                                        remove
                                        else
                                        @rel_name=rel_name_widget.text
                                        @rel_custom=rel_custom_widget.text
                                        set_rel_type(if many2one.active? then "m"; else "o";end)
                                        save
                                        show_me
                                end
                        end
                        dialog.destroy
                }
                dialog.show_all
        end
hide_me() click to toggle source
# File lib/RelationBuilder/Relation.rb, line 43
def hide_me
        parentM.widget.remove(@handle) if @shown
        @shown=false
        self
end
inspect() click to toggle source
# File lib/RelationBuilder/Relation.rb, line 161
def inspect
        "relation #{@rel_name}(#{relation_id}) #{rfield1.table.table}.#{rfield1.field_name} <-> #{rfield2.table.table}.#{rfield2.field_name}"
end
other_field(rfield) click to toggle source
# File lib/RelationBuilder/Relation.rb, line 145
def other_field(rfield)
        if rfield == rfield1
                rfield2
                else
                if rfield == rfield2
                        rfield1
                        else
                        nil
                end
        end
end
reload(rel) click to toggle source
# File lib/RelationBuilder/Relation.rb, line 23
def reload(rel)
        @rfield1=parentM.tables[rel["src_table"]].fields[rel["src_field"]] if parentM.tables.has_key?(rel["src_table"])
        @rfield2=parentM.tables[rel["dst_table"]].fields[rel["dst_field"]] if parentM.tables.has_key?(rel["dst_table"])
        @rel_type=rel["rel_type"]
        @rel_name=rel['rel_name']
        @rel_custom=rel['rel_custom']
        @relation_id=if rel["id"] then rel["id"].to_i else nil;end
        if @handle then
                @handle.move_me(rel["rbx"].to_f,rel["rby"].to_f)
        else
                @handle=RelationHandle.new(self,rel["rbx"].to_f,rel["rby"].to_f)
        end
end
remove() click to toggle source
# File lib/RelationBuilder/Relation.rb, line 91
def remove
        query("delete from relations where id='#{@relation_id}'") if !@relation_id.nil?
        parentM.relations.delete(self)
        parentM.clear
        @handle.destroy unless @handle.destroyed?
        @parentM.reload_server_rb(true,nil,@relation_id)
end
save() click to toggle source
# File lib/RelationBuilder/Relation.rb, line 71
def save
        if @relation_id.nil?
                query("insert into relations set rel_type='#{@rel_type}'")
                @relation_id=qrow("select id from `relations` order by id desc limit 1")["id"]
                parentM.clear
                einfo("inserted","relation-builder")
        end
        if rfield1.nil? or rfield2.nil?
                ewarn("id:#{@relation_id} has a lost table","relation-builder")
        else
                query("update relations set src_table = '#{@rfield1.table.table}', dst_table = '#{@rfield2.table.table}', src_field='#{@rfield1.field["Field"]}', dst_field='#{@rfield2.field["Field"]}', rel_type='#{@rel_type}', rbx='#{@handle.x}', rby='#{@handle.y}', rel_name='#{@rel_name}', rel_custom='#{@rel_custom}' where id='#{@relation_id}'")
                einfo("updated","relation-builder")
                parentM.reload_server_rb(true,nil,@relation_id)
        end
end
set_active(new_a) click to toggle source
# File lib/RelationBuilder/Relation.rb, line 63
def set_active(new_a)
        @handle.widget.set_active(new_a)
end
set_rel_type(rel_type) click to toggle source
# File lib/RelationBuilder/Relation.rb, line 67
def set_rel_type(rel_type)
        @rel_type=rel_type
end
set_rfield2(new_rfield2=rfield2) click to toggle source
# File lib/RelationBuilder/Relation.rb, line 87
def set_rfield2(new_rfield2=rfield2)
        @rfield2=new_rfield2
end
set_visibility(vis) click to toggle source
# File lib/RelationBuilder/Relation.rb, line 48
def set_visibility(vis)
        if vis
                show_me
                else
                hide_me
        end
end
show_me() click to toggle source
# File lib/RelationBuilder/Relation.rb, line 37
def show_me
        parentM.widget.put(@handle,@handle.x,@handle.y) unless @shown
        @handle.show_all
        @shown=true
        self
end
shown?() click to toggle source
# File lib/RelationBuilder/Relation.rb, line 56
def shown?
        @shown
end
to_s() click to toggle source
# File lib/RelationBuilder/Relation.rb, line 164
def to_s
        "relation #{rel_type} of #{rfield1} and #{rfield2}"
end
visible?() click to toggle source
# File lib/RelationBuilder/Relation.rb, line 157
def visible?
        @handle && !@handle.destroyed? && @handle.visible?
end
x() click to toggle source
# File lib/RelationBuilder/Relation.rb, line 16
def x
        @handle.x
end
y() click to toggle source
# File lib/RelationBuilder/Relation.rb, line 19
def y
        @handle.y
end