File: wb_catalog_tree_view.cpp

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (514 lines) | stat: -rw-r--r-- 16,065 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/* 
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the
 * License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "mforms/menubar.h"

#include "workbench/wb_context.h"
#include "grt/icon_manager.h"
#include "wb_model_diagram_form.h"
#include "wb_catalog_tree_view.h"
#include <boost/unordered_set.hpp>

using namespace wb;

//--------------------------------------------------------------------------------------------------

// Simple mapper of a tree node and a model node.
class CatalogData : public mforms::TreeNodeData
{
public:
  bec::NodeId id;

  CatalogData(bec::NodeId theId)
    : mforms::TreeNodeData()
  {
    id = theId;
  }
};

//--------------------------------------------------------------------------------------------------
static std::string get_node_icon_path(NodeIcons icon)
{
  bec::IconId iconid;

  switch(icon)
  {
  case IconTablesMany:
    iconid = bec::IconManager::get_instance()->get_icon_id("db.Table.many.$.png", bec::Icon16);
    break;
  case IconTable:
      iconid = bec::IconManager::get_instance()->get_icon_id("db.Table.$.png", bec::Icon16);
      break;
  case IconViewsMany:
      iconid = bec::IconManager::get_instance()->get_icon_id("db.View.many.$.png", bec::Icon16);
      break;
  case IconView:
      iconid = bec::IconManager::get_instance()->get_icon_id("db.View.$.png", bec::Icon16);
      break;
  case IconRoutineGroupsMany:
      iconid = bec::IconManager::get_instance()->get_icon_id("db.Routine.many.$.png", bec::Icon16);
      break;
  case IconRoutineGroup:
      iconid = bec::IconManager::get_instance()->get_icon_id("db.Routine.$.png", bec::Icon16);
      break;
  case IconSchema:
      iconid = bec::IconManager::get_instance()->get_icon_id("db.Schema.$.png", bec::Icon16);
      break;
  default:
    return "";
  }

  return bec::IconManager::get_instance()->get_icon_file(iconid);
}


CatalogTreeView::ObjectNodeData::ObjectNodeData(grt::ObjectRef obj_ref) : mforms::TreeNodeData(), _ref(obj_ref)
{

}
grt::ObjectRef CatalogTreeView::ObjectNodeData::get_object_ref()
{
  return _ref;
}

bool CatalogTreeView::get_drag_data(mforms::DragDetails &details, void **data, std::string &format)
{
  std::list<mforms::TreeNodeRef> selection = get_selection();

  _dragged_objects.clear();
  for (std::list<mforms::TreeNodeRef>::const_iterator iterator = selection.begin();
    iterator != selection.end(); ++iterator)
  {
    ObjectNodeData *odata = dynamic_cast<ObjectNodeData *>((*iterator)->get_data());
    if (odata != NULL)
    {
      GrtObjectRef object;
      grt::ValueRef value = odata->get_object_ref();
      if (value.is_valid() &&
          (db_TableRef::can_wrap(value) || db_ViewRef::can_wrap(value) || db_RoutineGroupRef::can_wrap(value)))
        object = GrtObjectRef::cast_from(value);
      if (object.is_valid())
        _dragged_objects.push_back(object);
    }
  }

  if (_dragged_objects.empty())
    return false;

  details.allowedOperations = mforms::DragOperationCopy;
  *data = &_dragged_objects;
  format = WB_DBOBJECT_DRAG_TYPE;

  return true;
}

void CatalogTreeView::menu_action(const std::string &name, grt::ValueRef val)
{
  if (name == "edit" && _activate_callback)
    _activate_callback(val);
}

mforms::TreeNodeRef CatalogTreeView::create_new_node(const ObjectType &otype, mforms::TreeNodeRef parent, const std::string &name, grt::ObjectRef obj)
{
  mforms::TreeNodeRef new_node;
  if (parent.is_valid())
  {
    std::string icon_path;
    switch(otype)
    {
    case ObjTable:
    {
      new_node = parent->get_child(0)->add_child();
      icon_path = get_node_icon_path(IconTable);
      break;
    }
    case ObjView:
    {
      new_node = parent->get_child(1)->add_child();
      icon_path = get_node_icon_path(IconView);
      break;
    }
    case ObjRoutineGrp:
    {
      new_node = parent->get_child(2)->add_child();
      icon_path = get_node_icon_path(IconRoutineGroup);
      break;
    }
    case ObjSchema:
      new_node = parent->add_child();
      icon_path = get_node_icon_path(IconSchema);
      break;
    default:
      break;
    }

    if (new_node.is_valid())
    {
      new_node->set_string(0, name);
      new_node->set_icon_path(0, icon_path);
      new_node->set_data(new ObjectNodeData(obj));
      new_node->set_tag(obj.id());
      if (otype == ObjSchema) //it's different we need to also create catalog nodes
      {
        mforms::TreeNodeRef child = new_node->add_child();
        new_node->expand();
        child->set_string(0, _("Tables"));
        child->set_icon_path(0, get_node_icon_path(IconTablesMany));
        child = new_node->add_child();
        child->set_string(0, _("Views"));
        child->set_icon_path(0, get_node_icon_path(IconViewsMany));
        child = new_node->add_child();
        child->set_string(0, _("Routine Groups"));
        child->set_icon_path(0, get_node_icon_path(IconRoutineGroupsMany));
      }
    }
  }

  return new_node;
}

CatalogTreeView::CatalogTreeView(ModelDiagramForm *owner)
  : mforms::TreeNodeView(mforms::TreeNoBorder | mforms::TreeSizeSmall | mforms::TreeCanBeDragSource | mforms::TreeIndexOnTag
#ifndef _WIN32
  | mforms::TreeNoHeader
#endif
)
, _owner(owner)

{
  _initialized = false;
  bool showHeaderText = true;
  set_selection_mode(mforms::TreeSelectMultiple);
#ifdef _WIN32
  set_row_height(19);
  showHeaderText = false;
#else
  set_row_height(17);
#endif
  add_column(mforms::IconStringColumnType, showHeaderText ? "Name" : "", 200);
  add_column(mforms::StringColumnType, showHeaderText ? "Presence" : "", 20);
  end_columns();

  _menu = new mforms::ContextMenu();
  _menu->signal_will_show()->connect(boost::bind(&CatalogTreeView::context_menu_will_show, this, _1));
  set_context_menu(_menu);
}

//--------------------------------------------------------------------------------------------------

CatalogTreeView::~CatalogTreeView()
{
  delete _menu;
}

void CatalogTreeView::node_activated(mforms::TreeNodeRef row, int column)
{
  ObjectNodeData *data = dynamic_cast<ObjectNodeData *>(row->get_data());
  if (data != NULL)
    _activate_callback(data->get_object_ref());
}

static bool compare_db_object(db_DatabaseObjectRef a, db_DatabaseObjectRef b)
{
  return base::string_compare(a->name(), b->name()) < 0 ? true : false;
}

template <typename T> static std::vector<grt::Ref<T> > sort_db_object (grt::ListRef<T> list)
{

  std::vector<grt::Ref<T> > vec;
  for (size_t i = 0; i < list.count(); ++i)
  {
    vec.push_back(list[i]);
  }
  std::sort(vec.begin(), vec.end(), compare_db_object);
  return vec;
}


void CatalogTreeView::refill(bool force)
{
  if (_initialized && !force)
      return;

  clear();
  model_ModelRef model = _owner->get_model_diagram()->owner();

  boost::unordered_set<grt::internal::Value*> uset;
  grt::ListRef<model_Figure> figures(_owner->get_model_diagram()->figures());
  for (size_t c= figures.count(), i= 0; i < c; i++)
  {
    model_FigureRef f(figures[i]);

    if (f.has_member("table"))
      uset.insert(f.get_member("table").valueptr());
    else if (f.has_member("view"))
      uset.insert(f.get_member("view").valueptr());
    else if (f.has_member("routine"))
      uset.insert(f.get_member("routine").valueptr());
    else if (f.has_member("routineGroup"))
      uset.insert(f.get_member("routineGroup").valueptr());
  }

  freeze_refresh();
  grt::ListRef<db_Schema> schema_list = workbench_physical_ModelRef::cast_from(model)->catalog()->schemata();
  for (size_t i = 0; i < schema_list.count(); ++i)
  {
    mforms::TreeNodeRef node = add_node();
    node->set_string(0, schema_list[i]->name().c_str());
    node->set_icon_path(0, get_node_icon_path(IconSchema));;
    node->set_tag(schema_list[i].id());
    node->set_data(new ObjectNodeData(schema_list[i]));
    mforms::TreeNodeRef child = node->add_child();
    if (i == 0) //we expand by default only first schema on the list
      node->expand();
    child->set_string(0, _("Tables"));
    child->set_icon_path(0, get_node_icon_path(IconTablesMany));

    std::vector<db_TableRef> table_list = sort_db_object<db_Table>(schema_list[i]->tables());

    for (size_t j = 0; j < table_list.size(); ++j)
    {
      db_TableRef table = table_list[j];
      mforms::TreeNodeRef subchild = child->add_child();
      subchild->set_string(0, table->name().c_str());
      subchild->set_icon_path(0, get_node_icon_path(IconTable));
      subchild->set_tag(table.id());
      subchild->set_data(new ObjectNodeData(table));
      if (uset.find(table.valueptr()) != uset.end())
        subchild->set_string(1, "\xe2\x97\x8f");
    }

    child = node->add_child();
    child->set_string(0, _("Views"));
    child->set_icon_path(0, get_node_icon_path(IconViewsMany));
    std::vector<db_ViewRef> view_list = sort_db_object<db_View>(schema_list[i]->views());
    for (size_t j = 0; j < view_list.size(); ++j)
    {
      db_ViewRef view = view_list[j];
      mforms::TreeNodeRef subchild = child->add_child();
      subchild->set_string(0, view->name().c_str());
      subchild->set_icon_path(0, get_node_icon_path(IconView));
      subchild->set_tag(view.id());
      subchild->set_data(new ObjectNodeData(view));
      if (uset.find(view.valueptr()) != uset.end())
        subchild->set_string(1, "\xe2\x97\x8f");
    }

    child = node->add_child();
    child->set_string(0, _("Routine Groups"));
    child->set_icon_path(0, get_node_icon_path(IconRoutineGroupsMany));
    std::vector<db_RoutineGroupRef> routine_group_list = sort_db_object<db_RoutineGroup>(schema_list[i]->routineGroups());
    for (size_t j = 0; j < routine_group_list.size(); ++j)
    {
      db_RoutineGroupRef routineGrp = routine_group_list[j];
      mforms::TreeNodeRef subchild = child->add_child();
      subchild->set_string(0, routineGrp->name().c_str());
      subchild->set_icon_path(0, get_node_icon_path(IconRoutineGroup));
      subchild->set_tag(routineGrp.id());
      subchild->set_data(new ObjectNodeData(routineGrp));
      if (uset.find(routineGrp.valueptr()) != uset.end())
        subchild->set_string(1, "\xe2\x97\x8f");
    }
  }
  thaw_refresh();
  _initialized = true;
}

void CatalogTreeView::set_activate_callback(const boost::function<void (grt::ValueRef)> &active_callback)
{
  _activate_callback= active_callback;
}

//--------------------------------------------------------------------------------------------------

void CatalogTreeView::context_menu_will_show(mforms::MenuItem *parent_item)
{
  std::list<mforms::TreeNodeRef> selection = get_selection();

  mforms::MenuBase *parent;
  if (parent_item)
    parent = parent_item;
  else
    parent = _menu;

  parent->remove_all();

  if (!selection.empty())
  {
    ObjectNodeData *odata = dynamic_cast<ObjectNodeData *>((*selection.begin())->get_data());
    if (odata != NULL)
    {
      grt::ValueRef value(odata->get_object_ref());
      std::string caption = "";
      if (value.is_valid())
      {
        if (db_SchemaRef::can_wrap(value))
          caption = _("Edit Schema...");
        else if (db_TableRef::can_wrap(value))
          caption = _("Edit Table...");
        else if (db_ViewRef::can_wrap(value))
          caption = _("Edit View...");
        else if (db_RoutineRef::can_wrap(value))
          caption = _("Edit Routine...");
        else if (db_RoutineGroupRef::can_wrap(value))
          caption = _("Edit Routine Group...");
      }

      if (!caption.empty())
        parent->add_item_with_title(caption, boost::bind(&CatalogTreeView::menu_action, this, "edit", value));
    }
  }


}
//--------------------------------------------------------------------------------------------------
void CatalogTreeView::mark_node(grt::ValueRef val, bool mark)
{
  db_DatabaseObjectRef obj;
  if (db_DatabaseObjectRef::can_wrap(val))
    obj = db_DatabaseObjectRef::cast_from(val);

  if (!obj.is_valid())
    return;

  mforms::TreeNodeRef node = node_with_tag(obj.id());
  if (node.is_valid())
    node->set_string(1, mark ? "\xe2\x97\x8f" : "");
}

void CatalogTreeView::add_update_node_caption(grt::ValueRef val)
{
  ObjectType otype = ObjNone;

  db_DatabaseObjectRef obj;
  if (db_DatabaseObjectRef::can_wrap(val))
    obj = db_DatabaseObjectRef::cast_from(val);

  if (!obj.is_valid())
    return;

  std::string new_name = obj->name().c_str();
  if (db_TableRef::can_wrap(val))
    otype = ObjTable;
  else if (db_RoutineGroupRef::can_wrap(val))
    otype = ObjRoutineGrp;
  else if (db_ViewRef::can_wrap(val))
    otype = ObjView;
  else if (db_SchemaRef::can_wrap(val))
    otype = ObjSchema;
  else
    return;


  mforms::TreeNodeRef node = node_with_tag(obj.id());
  if (node.is_valid())
  {
    node->set_string(0, new_name);
  }
  else // if node is invalid it means that new object was added on diagram
  {
    node = node_with_tag(obj->owner().id());
    if (node.is_valid())
    {
      mforms::TreeNodeRef prnt = node;
      node = create_new_node(otype, prnt, new_name, obj);
      workbench_physical_DiagramRef view(workbench_physical_DiagramRef::cast_from(_owner->get_model_diagram()));
      if (view->getFigureForDBObject(obj).is_valid())
        node->set_string(1, "\xe2\x97\x8f");

    }
    else if (db_SchemaRef::can_wrap(obj)) //check if it's schemaref
      node = create_new_node(otype, root_node(), new_name, obj);
  }


  if (node.is_valid() && node->get_parent()->count() > 1)// We check if node is valid, and if so we need to rearrange this into different position.
  {
    mforms::TreeNodeRef next_node = node->next_sibling();
    mforms::TreeNodeRef parent = node->get_parent();
    mforms::TreeNodeRef prev_node = node->previous_sibling();
    bool moved = false;
    if (next_node.is_valid())
    {
      int new_idx = -1;
      while (next_node.is_valid())
      {
        int ret = base::string_compare(node->get_string(0), next_node->get_string(0), false);
        if (ret > 0)
          new_idx = parent->get_child_index(next_node);
        else
        {
          next_node = next_node->previous_sibling(); // wee need to get back to the prev node
          break;
        }

        if (next_node->next_sibling().is_valid())
          next_node = next_node->next_sibling();
        else
          break;
      }
      if (new_idx > -1 && next_node.is_valid())
      {
        node->move_node(next_node, false);
        moved = true;
      }
    }
    if (prev_node.is_valid() && !moved)
    {
      int new_idx = -1;
      while (prev_node.is_valid())
      {
        int ret = base::string_compare(node->get_string(0), prev_node->get_string(0), false);
        if (ret < 0)
          new_idx = parent->get_child_index(prev_node);
        else
        {
          prev_node = prev_node->next_sibling(); // wee need to get back to the prev node
          break;
        }

        if (prev_node->previous_sibling().is_valid())
          prev_node = prev_node->previous_sibling();
        else
          break;
      }
      if (new_idx > -1 && prev_node.is_valid())
      {
        node->move_node(prev_node, true);
        moved = true;
      }
    }
  }
}

void CatalogTreeView::remove_node(grt::ValueRef val)
{
  db_DatabaseObjectRef obj;
  if (db_DatabaseObjectRef::can_wrap(val))
    obj = db_DatabaseObjectRef::cast_from(val);

  if (!obj.is_valid())
    return;

  mforms::TreeNodeRef node = node_with_tag(obj.id());
  if (node.is_valid())
    node->remove_from_parent();
}
//--------------------------------------------------------------------------------------------------