File: schema_editor_fe.cpp

package info (click to toggle)
mysql-workbench 5.2.40%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 53,880 kB
  • sloc: cpp: 419,850; yacc: 74,784; xml: 54,510; python: 31,455; sh: 9,423; ansic: 4,736; makefile: 2,442; php: 529; java: 237
file content (212 lines) | stat: -rw-r--r-- 6,430 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212


#include "linux_utilities/plugin_editor_base.h"
#include "../backend/mysql_schema_editor.h"
#include "grtdb/db_object_helpers.h"
#include "linux_utilities/image_cache.h"
#include <gtkmm/image.h>
#include <gtkmm/comboboxtext.h>
#include <gtkmm/textview.h>
#include <gtkmm/liststore.h>
#include <gtkmm/box.h>
#include <gtk/gtkversion.h>

class SchemaEditor : public PluginEditorBase
{
  MySQLSchemaEditorBE *_be;
  std::string          _old_name;
  #if ((GTK_MAJOR_VERSION >= 2) && (GTK_MINOR_VERSION < 16))
  Gtk::Button *_refactor_button;
  #endif
  
  virtual bec::BaseEditor *get_be()
  {
    return _be;
  }

public:
  virtual ~SchemaEditor()
  {
    delete _be;
    _be = 0;
  }
  
  SchemaEditor(grt::Module *m, bec::GRTManager *grtm, const grt::BaseListRef &args)
    : PluginEditorBase(m, grtm, args, "modules/data/editor_schema.glade")
    , _be(new MySQLSchemaEditorBE(grtm, db_SchemaRef::cast_from(args[0]), get_rdbms_for_db_object(args[0])))
  {
    xml()->get_widget("mysql_schema_editor_notebook", _editor_notebook);
    
    Gtk::Widget *widget;
    xml()->get_widget("base_table", widget);

    Gtk::Image *image;
    xml()->get_widget("image", image);
    image->set(ImageCache::get_instance()->image_from_filename("db.Schema.editor.48x48.png", false));

    bind_entry_and_be_setter("name_entry", this, &SchemaEditor::set_name);
    if (_be->is_editing_live_object() && _be->get_schema()->oldName() != "")
    {
      Gtk::Entry *entry;
      xml()->get_widget("name_entry", entry);
      entry->set_sensitive(false);
    }

    Gtk::ComboBox *combo;
    xml()->get_widget("collation_combo", combo);
    Glib::RefPtr<Gtk::ListStore> store(Glib::RefPtr<Gtk::ListStore>::cast_dynamic(xml()->get_object("collation_store")));
    setup_combo_for_string_list(combo);
    fill_combo_from_string_list(combo, _be->get_charset_collation_list());
    add_option_combo_change_handler(combo, "CHARACTER SET - COLLATE", sigc::mem_fun(this, &SchemaEditor::set_schema_option_by_name));

    Gtk::TextView *tview;
    xml()->get_widget("text_view", tview);
    add_text_change_timer(tview, sigc::mem_fun(this, &SchemaEditor::set_comment));
    
    //!widget->reparent(*this);
    add(*_editor_notebook);
    _editor_notebook->show();

    if (!is_editing_live_object())
    {
      #if (GTK_MAJOR_VERSION >= 2) && (GTK_MINOR_VERSION >= 16)
      Gtk::Entry* entry(0);
      xml()->get_widget("name_entry", entry);
      entry->set_icon_from_pixbuf(ImageCache::get_instance()->image_from_filename("execute_script.png", false), Gtk::ENTRY_ICON_SECONDARY);
      entry->set_icon_tooltip_text(_("Refactor SQL in the schema"), Gtk::ENTRY_ICON_SECONDARY);
      entry->set_icon_activatable(true, Gtk::ENTRY_ICON_SECONDARY);
      entry->set_icon_sensitive(Gtk::ENTRY_ICON_SECONDARY, false);
      #else
      _refactor_button = new Gtk::Button();
      image = new Gtk::Image(ImageCache::get_instance()->image_from_filename("execute_script.png", false));
      image->set_size_request(16,16);
      _refactor_button->set_image(*Gtk::manage(image));
      Gtk::HBox *hbox(0);
      xml()->get_widget("name_entry_hbox", hbox);
      hbox->pack_start(*_refactor_button, false, false);
      _refactor_button->set_sensitive(false);
      hbox->show_all();
      #endif
    }

    show_all();
    
    refresh_form_data();
  }
  
  void set_name(const std::string& name)
  {
    if (_be)
    {
      _be->set_name(name);
      #if (GTK_MAJOR_VERSION >= 2) && (GTK_MINOR_VERSION >= 16)
      Gtk::Entry* entry(0);
      xml()->get_widget("name_entry", entry);
      if (entry)
      {
        entry->set_icon_sensitive(Gtk::ENTRY_ICON_SECONDARY, true);
        entry->signal_icon_release().connect(sigc::bind(sigc::mem_fun(this, &SchemaEditor::refactor_schema), name));
      }
      #else
      _refactor_button->set_sensitive(true);
      _refactor_button->signal_clicked().connect(sigc::bind(sigc::mem_fun(this, &SchemaEditor::refactor_schema), name));
      #endif
    }
  }

  #if (GTK_MAJOR_VERSION >= 2) && (GTK_MINOR_VERSION >= 16)
  void refactor_schema(Gtk::EntryIconPosition pos, const GdkEventButton* btn, const std::string& new_name)
  #else
  void refactor_schema(const std::string& new_name)
  #endif
  {
    if (_be)
    {
      #if (GTK_MAJOR_VERSION >= 2) && (GTK_MINOR_VERSION >= 16)
      Gtk::Entry* entry(0);
      xml()->get_widget("name_entry", entry);
      entry->set_icon_sensitive(Gtk::ENTRY_ICON_SECONDARY, false);
      #else
      _refactor_button->set_sensitive(true);
      #endif
      
      _be->refactor_catalog_upon_schema_rename(_old_name, new_name);
      _old_name = new_name;
    }
  }

  void set_comment(const std::string& text)
  {
    if (_be)
      _be->set_comment(text);
  }
  
  void set_schema_option_by_name(const std::string &name, const std::string &value)
  {
    if (_be)
      _be->set_schema_option_by_name(name, value);
  }
  
  virtual void do_refresh_form_data()
  {    
    Gtk::Entry *entry;
    xml()->get_widget("name_entry", entry);

    Gtk::TextView *tview;
    xml()->get_widget("text_view", tview);

    Gtk::ComboBox *combo;
    xml()->get_widget("collation_combo", combo);

    if (_be)
    {
      set_selected_combo_item(combo, _be->get_schema_option_by_name("CHARACTER SET - COLLATE"));

      _old_name = _be->get_name();
      entry->set_text(_old_name);

      tview->get_buffer()->set_text(_be->get_comment());

      bool is_editing_live_obj= is_editing_live_object();
      tview->set_sensitive(!is_editing_live_obj);
      Gtk::Label *tlabel;
      xml()->get_widget("label5", tlabel);
      tlabel->set_sensitive(!is_editing_live_obj);
    }
  }
  
  virtual std::string get_title()
  {
    return "Schema";
  }

  virtual bool switch_edited_object(bec::GRTManager *grtm, const grt::BaseListRef &args);
};

//------------------------------------------------------------------------------
bool SchemaEditor::switch_edited_object(bec::GRTManager *grtm, const grt::BaseListRef &args)
{
  MySQLSchemaEditorBE *old_be = _be;
  _be = new MySQLSchemaEditorBE(grtm, db_SchemaRef::cast_from(args[0]), get_rdbms_for_db_object(args[0]));
  
  if (_be)
  {
    do_refresh_form_data();
  
    delete old_be;
    old_be = 0;
  }
  else
    _be = old_be;

  return true;
}


extern "C" 
{
  GUIPluginBase *createDbMysqlSchemaEditor(grt::Module *m,  bec::GRTManager *grtm, const grt::BaseListRef &args)
  {
    return Gtk::manage(new SchemaEditor(m, grtm, args));
  }
};