File: wb_context_model.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 (1601 lines) | stat: -rw-r--r-- 52,875 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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
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
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
900
901
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
960
961
962
963
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
994
995
996
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
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
/* 
 * Copyright (c) 2009, 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 "base/file_utilities.h"
#include "base/file_functions.h"
#include "base/string_utilities.h"
#include "base/log.h"

#include "wb_context_model.h"

#include "workbench/wb_context.h"
#include "workbench/wb_context_ui.h"
#include "workbench/wb_command_ui.h"

#include "wb_component.h"
#include "wb_component_basic.h"
#include "wb_component_physical.h"
#include "wb_component_logical.h"
#include "model/wb_model_diagram_form.h"
#include "wb_overview_physical.h"
#include "wb_overview_physical_schema.h"
#include "wb_catalog_tree_view.h"

#include "objimpl/wrapper/mforms_ObjectReference_impl.h"
#include "workbench/wb_model_file.h"
#include "model/wb_overview_physical.h"
#include "model/wb_user_datatypes.h"
#include "model/wb_history_tree.h"
#include "wbcanvas/model_diagram_impl.h"
#include "wbcanvas/workbench_physical_model_impl.h"
#include "workbench/wb_model_file.h"
#include "grtdb/db_helpers.h"
#include "grtdb/db_object_helpers.h"
#include "grt/clipboard.h"
#include "grt/common.h"

#include "grtpp_notifications.h"

#include "user_defined_type_editor.h"
#include "wb_template_list.h"

#include "mforms/tabview.h"
#include "mforms/tabview_dock.h"
#include "mforms/utilities.h"
#include "mforms/menubar.h"
#include "mforms/dockingpoint.h"

using namespace base;
using namespace bec;
using namespace wb;

DEFAULT_LOG_DOMAIN("ModelContext");

static std::map<std::string, std::string> auto_save_files;


WBContextModel::WBContextModel(WBContextUI *wbui)
: _wbui(wbui), _file(0), _current_user_type_editor(0), _locked_view_for_plugin_exec(0), _auto_save_point(0), _last_auto_save_time(0), _auto_save_timer(NULL)

{  
  _overview= new PhysicalOverviewBE(_wbui->get_wb());

  scoped_connect(_wbui->get_wb()->get_grt_manager()->get_clipboard()->signal_changed(),boost::bind(&WBContextModel::selection_changed, this));
  scoped_connect(_overview->signal_selection_changed(),boost::bind(&WBContextModel::selection_changed, this)); // make edit menu captions to update

  CommandUI *cmdui = wbui->get_command_ui();
  boost::function<bool ()> validate = boost::bind(&WBContextModel::has_selected_schema, this);
  cmdui->add_builtin_command("addModelDiagram",
                             boost::bind(&WBContextModel::add_model_diagram, this),
                             boost::bind(&WBContextModel::has_selected_model, this));
  cmdui->add_builtin_command("addModelSchema",
                             boost::bind(&WBContextModel::add_model_schema, this),
                             boost::bind(&WBContextModel::has_selected_model, this));
  cmdui->add_builtin_command("addModelTable",
                             boost::bind(&WBContextModel::add_model_table, this),
                             validate);
  cmdui->add_builtin_command("addModelView",
                             boost::bind(&WBContextModel::add_model_view, this),
                             validate);
  cmdui->add_builtin_command("addModelRoutine",
                             boost::bind(&WBContextModel::add_model_rgroup, this),
                             validate);

  cmdui->add_builtin_command("removeFigure",
                             boost::bind(&WBContextModel::remove_figure, this),
                             boost::bind(&WBContextModel::has_selected_figures, this));
  
  base::NotificationCenter::get()->add_observer(this, "GNMainFormChanged");

  // Setup auto-save for model, only full seconds.
  int interval = (int)_wbui->get_wb()->get_root()->options()->options().get_int("workbench:AutoSaveModelInterval", 60);
  if (interval > 0)
    _auto_save_timer = _wbui->get_wb()->get_grt_manager()->run_every(boost::bind(&WBContextModel::auto_save_document, this), interval);
  _auto_save_interval = interval;

  // DON'T set up any UI here. This is running on a background thread!
  _secondary_sidebar = NULL;
  _sidebar_dockpoint = NULL;
  _template_panel = NULL;

  scoped_connect(_wbui->get_wb()->get_root()->options()->signal_dict_changed(),boost::bind(&WBContextModel::option_changed, this, _1, _2, _3));

  setup_secondary_sidebar();
}


WBContextModel::~WBContextModel()
{
  _grtmodel_panel.clear();

  if (_secondary_sidebar != NULL)
    _secondary_sidebar->release();
  if (_sidebar_dockpoint != NULL)
    _sidebar_dockpoint->release();

  delete _template_panel;

  base::NotificationCenter::get()->remove_observer(this);

  if (_doc.is_valid() && _doc->physicalModels().is_valid() && _doc->physicalModels().count() > 0)
    _doc->physicalModels().get(0)->get_data()->set_delegate(NULL);

  if (_auto_save_timer)
    _wbui->get_wb()->get_grt_manager()->cancel_timer(_auto_save_timer);
  CommandUI *cmdui = _wbui->get_command_ui();
  cmdui->remove_builtin_command("addModelDiagram");
  cmdui->remove_builtin_command("addModelSchema");
  cmdui->remove_builtin_command("addModelTable");
  cmdui->remove_builtin_command("addModelView");
  cmdui->remove_builtin_command("addModelRoutine");
  cmdui->remove_builtin_command("removeFigure");
  _file= 0;
  delete _overview;
}


void WBContextModel::setup_secondary_sidebar()
{
  // Setup the secondary model sidebar, which should be shared between all model tabs
  _secondary_sidebar = mforms::manage(new mforms::TabView(mforms::TabViewSelectorSecondary));
  _template_panel = new TableTemplatePanel(_wbui->get_wb()->get_grt(), this);
  _secondary_sidebar->add_page(_template_panel, _("Templates"));
}

void WBContextModel::notify_catalog_tree_view(const CatalogNodeNotificationType &notify_type, grt::ValueRef value, const std::string &diagram_id)
{
  std::map<std::string, ModelDiagramForm*>::iterator it;
  if (diagram_id.empty())
  {
    for (it = _model_forms.begin(); it != _model_forms.end(); ++it)
      it->second->notify_catalog_tree(notify_type, value);
  }
  else
  {
    it = _model_forms.find(diagram_id);
    if (it != _model_forms.end())
    {
      it->second->notify_catalog_tree(notify_type, value);
    }
  }
}

void WBContextModel::refill_catalog_tree()
{
  std::map<std::string, ModelDiagramForm*>::iterator it;
  for (it = _model_forms.begin(); it != _model_forms.end(); ++it)
    it->second->refill_catalog_tree();

}


mforms::TreeNodeView *WBContextModel::create_user_type_list()
{
  UserDatatypeList *type_list;

  type_list = new UserDatatypeList(_wbui->get_wb());
  type_list->set_catalog(_wbui->get_wb()->get_document()->physicalModels()[0]->catalog());
  type_list->refresh();

  type_list->scoped_connect(&_udt_list_changed, boost::bind(&UserDatatypeList::refresh, type_list));

  return type_list;
}

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

mforms::TreeNodeView* WBContextModel::create_history_tree()
{
  HistoryTree *history_tree = new HistoryTree(_wbui->get_wb()->get_grt_manager(), _wbui->get_wb()->get_grt()->get_undo_manager());
  history_tree->refresh();
  return history_tree;
}

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

grt::GRT *WBContextModel::get_grt()
{
  return _wbui->get_wb()->get_grt();
}


void WBContextModel::option_changed(grt::internal::OwnedDict*dict, bool, const std::string&key)
{
  if (key == "workbench:AutoSaveModelInterval" && dict == _wbui->get_wb()->get_wb_options().valueptr())
  {
    auto_save_document();
  }
}


bool WBContextModel::auto_save_document()
{
  WBContext *wb= _wbui->get_wb();
  ssize_t interval= wb->get_root()->options()->options().get_int("workbench:AutoSaveModelInterval", 60);
  if (interval <= 0)
    return false;
  
  workbench_DocumentRef doc(wb->get_document());
    
  mdc::Timestamp now= mdc::get_time();
  if (now - _last_auto_save_time > interval
      && _file
      && doc.is_valid() 
      && !wb->get_grt_manager()->get_dispatcher()->get_busy()
      && wb->get_grt()->get_undo_manager()->get_latest_closed_undo_action() != _auto_save_point)
  {
    _auto_save_point = wb->get_grt()->get_undo_manager()->get_latest_closed_undo_action();
    _last_auto_save_time = now;
    try
    {
      // save the document in the same directory containing the expanded mwb file
      _file->store_document_autosave(wb->get_grt(), doc);
    } 
    catch (std::exception &exc)
    {
      wb->show_exception(_("Could not store document data to autosave file."), exc);
    }
  }
  
  if (interval != _auto_save_interval)
  {
    if (_auto_save_timer)
        wb->get_grt_manager()->cancel_timer(_auto_save_timer);
    // schedule new interval
    _auto_save_timer = wb->get_grt_manager()->run_every(boost::bind(&WBContextModel::auto_save_document, this), (double)interval);
    return false;
  }
  
  return true;
}


void WBContextModel::detect_auto_save_files(const std::string &autosave_dir)
{
  std::map<std::string, std::string> files;

  // look for .mwbd folders with autosave files
  std::list<std::string> autosaves;
  
  try
  {
    autosaves = base::scan_for_files_matching(bec::make_path(autosave_dir, "*.mwbd*"));
  }
  catch (const std::runtime_error &)
  {
    return;
  }

  for (std::list<std::string>::const_iterator d = autosaves.begin(); d != autosaves.end(); ++d)
  { 
    if (!g_file_test(d->c_str(), G_FILE_TEST_IS_DIR))
      continue;

    if (base::LockFile::check(bec::make_path(*d, ModelFile::lock_filename.c_str())) != base::LockFile::NotLocked)
      continue;
    
    if (g_file_test(bec::make_path(*d, MAIN_DOCUMENT_AUTOSAVE_NAME).c_str(), G_FILE_TEST_EXISTS))
    {
      std::string path = bec::make_path(*d, "real_path");
      gchar *orig_path;
      gsize length;
      if (g_file_test(path.c_str(), (GFileTest)(G_FILE_TEST_IS_REGULAR|G_FILE_TEST_EXISTS))
          && g_file_get_contents(path.c_str(), &orig_path, &length, NULL))
      {
        files[std::string(orig_path, length)] = *d;
        g_free(orig_path);
      }
      else
      {
        std::string fname = base::basename(*d);
        fname = fname.substr(0, fname.rfind('.')).append(".mwb");
        // if no real_path file in the autosave, this could be an autosave from an older version
        files[fname] = *d;
      }
    }
    else
    {
      log_info("Found model auto-save %s, but it is empty. Deleting it...\n", d->c_str());
      base_rmdir_recursively(d->c_str());
    }
  }
  ::auto_save_files = files;
}

std::map<std::string, std::string> WBContextModel::auto_save_files()
{
  return ::auto_save_files;
}


void WBContextModel::unrealize()
{
  _page_settings_conn.disconnect();
  
  // unrealize all models
  if (_doc.is_valid() && _doc->physicalModels().is_valid())
  {
    for (size_t c= _doc->physicalModels().count(), i= 0; i < c; i++)
    {
      _doc->physicalModels().get(i)->get_data()->unrealize();
    }
  }  
}


model_DiagramRef WBContextModel::get_active_model_diagram(bool main_form)
{
  bec::UIForm *form= main_form ? get_wbui()->get_active_main_form() : get_wbui()->get_active_form();
  
  if (dynamic_cast<ModelDiagramForm*>(form))
    return dynamic_cast<ModelDiagramForm*>(form)->get_model_diagram();
  
  return model_DiagramRef();
}


model_ModelRef WBContextModel::get_active_model(bool main_form)
{
  bec::UIForm *form= main_form ? get_wbui()->get_active_main_form() : get_wbui()->get_active_form();
  
  if (dynamic_cast<OverviewBE*>(form))
    return dynamic_cast<OverviewBE*>(form)->get_model();
  else if (dynamic_cast<ModelDiagramForm*>(form))
    return dynamic_cast<ModelDiagramForm*>(form)->get_model_diagram()->owner();
  return model_ModelRef();
}



void WBContextModel::model_created(ModelFile *file, workbench_DocumentRef doc)
{
  _file= file;
  _doc= doc;

  std::string target_version = _wbui->get_wb()->get_grt_manager()->get_app_option_string("DefaultTargetMySQLVersion");
  if (target_version.empty())
    target_version = "5.6.1";
  
  _wbui->get_wb()->get_component<WBComponentLogical>()->setup_logical_model(doc.get_grt(), _doc);
  _wbui->get_wb()->get_component<WBComponentPhysical>()->setup_physical_model(doc.get_grt(), _doc, "Mysql", target_version);
  
  _wbui->get_wb()->foreach_component(boost::bind(&WBComponent::reset_document, _1));
  
  _doc->physicalModels().get(0)->get_data()->set_delegate(this);
  
  _doc->physicalModels()[0]->get_data()->realize();

  _wbui->get_wb()->request_refresh(RefreshNewModel, "", 0);

  // setup GRT proxy object
  _grtmodel_panel = ui_ModelPanelRef(_doc.get_grt());
  _grtmodel_panel->model(_doc->physicalModels()[0]);

  if (_sidebar_dockpoint == NULL)
    _sidebar_dockpoint = mforms::manage(new mforms::DockingPoint(new mforms::TabViewDockingPoint(_secondary_sidebar, MODEL_DOCKING_POINT), true));
  _grtmodel_panel->commonSidebar(mforms_to_grt(_doc.get_grt(), _sidebar_dockpoint));

  grt::DictRef info(_doc.get_grt());
  grt::GRTNotificationCenter::get()->send_grt("GRNModelCreated", _grtmodel_panel, info);
}


void WBContextModel::model_loaded(ModelFile *file, workbench_DocumentRef doc)
{
  _file= file;
  _doc= doc;
  
  _wbui->get_wb()->foreach_component(boost::bind(&WBComponent::reset_document, _1));
  
  _wbui->get_wb()->foreach_component(boost::bind(&WBComponent::document_loaded, _1));
  
  _doc->physicalModels().get(0)->get_data()->set_delegate(this);
    
  _wbui->get_wb()->request_refresh(RefreshNewModel, "", 0);
  
  std::string temp_dir = _file->get_tempdir_path();
  for (std::map<std::string, std::string>::iterator iter = ::auto_save_files.begin();
       iter != ::auto_save_files.end(); ++iter)
  {
    if (iter->second == temp_dir)
    {
      ::auto_save_files.erase(iter);
      _wbui->refresh_home_documents();
      break;
    }
  }

  // setup GRT proxy object
  _grtmodel_panel = ui_ModelPanelRef(get_grt());
  _grtmodel_panel->model(_doc->physicalModels()[0]);

  if (_sidebar_dockpoint == NULL)
    _sidebar_dockpoint = mforms::manage(new mforms::DockingPoint(new mforms::TabViewDockingPoint(_secondary_sidebar, MODEL_DOCKING_POINT), true));
  _grtmodel_panel->commonSidebar(mforms_to_grt(_doc.get_grt(), _sidebar_dockpoint));

  grt::DictRef info(get_grt());
  grt::GRTNotificationCenter::get()->send_grt("GRNModelOpened", _grtmodel_panel, info);
}


void WBContextModel::model_closed()
{
  grt::DictRef info(get_grt());
  grt::GRTNotificationCenter::get()->send_grt("GRNModelClosed", _grtmodel_panel, info);
}


void WBContextModel::realize()
{
  _page_settings_conn = _doc->pageSettings()->signal_changed()->connect(boost::bind(&WBContextModel::page_settings_changed, this, _1, _2));
  
  _doc->physicalModels()[0]->get_data()->realize();
}


void WBContextModel::page_settings_changed(const std::string &field, const grt::ValueRef &value)
{
  if (field == "paperType")
  {
    update_page_settings();
  }
}




/** 
 ****************************************************************************
 * @brief Update the canvas view according to app.PageSettings 
 *
 * This will update the page size and total view size to reflect changes
 * to the page/print settings.
 ****************************************************************************
 */
void WBContextModel::update_page_settings()
{
  if (!_doc.is_valid() || !_doc->logicalModel().is_valid())
    return;
  
  grt::ListRef<model_Diagram> views(grt::ListRef<model_Diagram>::cast_from(_doc->logicalModel()->diagrams()));
  for (size_t vc= views.count(), v= 0; v < vc; v++)
  {
    views[v]->get_data()->update_size();
  }

  grt::ListRef<workbench_physical_Model> models(_doc->physicalModels());
  for (size_t c= models.count(), i= 0; i < c; i++)
  {
    views= grt::ListRef<model_Diagram>::cast_from(models[i]->diagrams());
    for (size_t vc= views.count(), v= 0; v < vc; v++)
    {
      views[v]->get_data()->update_from_page_size();
    }
  }
}


cairo_surface_t *WBContextModel::fetch_image(const std::string &file)
{
  return _wbui->get_wb()->get_file()->get_image(file);
}


std::string WBContextModel::attach_image(const std::string &file)
{
  return _wbui->get_wb()->get_file()->add_image_file(file);
}


void WBContextModel::release_image(const std::string &file)
{
  // QQQ
  // _wbui->get_wb()->get_file()->release_image(file);
}

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

/** 
 * @brief Delegate method for creating canvas views
 *
 * This is called by the GRT bridge when a new view object is created.
 * It will in turn, call a frontend supplied callback which should create
 * the canvas view (and its viewer) and then return the canvas view.
 *
 * The free_canvas_view() method will be called once the view can be freed.
 * 
 * @param name the name of the canvas. Will be the object-id of the view.
 */
mdc::CanvasView *WBContextModel::create_diagram(const model_DiagramRef &view)
{
  return _wbui->get_wb()->execute_in_main_thread<mdc::CanvasView*>("create_diagram", boost::bind(&WBContextModel::create_diagram_main, this, view));
}

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

/**
 * Notification trigger for canvas view destructions. Can be called via two different paths.
 * If called from the front end (because the UI caused closing the editor) then the diagram form
 * is already unregistered and we don't need to call the UI again (it was the trigger after all).
 */
void WBContextModel::free_canvas_view(mdc::CanvasView *view)
{
  ModelDiagramForm* diagram = get_diagram_form(view);
  if (diagram != NULL)
  {
    // This function is expected to be called from the main thread.
    notify_diagram_destroyed(diagram);

    // Notify front end so it can close its editor for this view.
    if (_wbui->get_wb()->get_grt_manager()->in_main_thread())
      _wbui->get_wb()->destroy_view(view);
    else
      _wbui->get_wb()->execute_in_main_thread<void>("destroy view", 
      boost::bind(_wbui->get_wb()->destroy_view, view));
  }
}

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

mdc::CanvasView *WBContextModel::create_diagram_main(const model_DiagramRef &diagram_reference)
{
  ModelDiagramForm *diagram= 0;
  WBContext *wb= _wbui->get_wb();
  
  FOREACH_COMPONENT(wb->_components, iter)
  {
    if (diagram_reference.is_instance((*iter)->get_diagram_class_name())
        && (*iter)->get_diagram_class_name() != model_Diagram::static_class_name())
    {
      diagram= new ModelDiagramForm(*iter, diagram_reference);
      break;
    }
  }
  
  // fallback
  if (!diagram)
    diagram= new ModelDiagramForm(wb->get_component_named("basic"), diagram_reference);
  
  if (!diagram)
  {
    mforms::Utilities::show_error("Internal error adding a new diagram.", "Unknown diagram type.", _("Close"));
    return 0;
  }
  
  scoped_connect(diagram_reference->signal_objectActivated(),(boost::bind(&WBContextModel::activate_canvas_object, this, _1, _2)));
    
  scoped_connect(diagram_reference->signal_list_changed(),boost::bind(&WBContextModel::diagram_object_list_changed, this, _1, _2, _3, diagram));
  
  register_diagram_form(diagram);
  
  // Forward creation to front end.
  mdc::CanvasView *view= wb->create_diagram(diagram_reference);
  if (view)
  {
    diagram->attach_canvas_view(view);
    
    notify_diagram_created(diagram);
    
    // use this signal instead of selection_change from canvas so that when the callback is called
    // the grt diagram object already reflects the selection changes
    scoped_connect(diagram_reference->get_data()->signal_selection_changed(),boost::bind(&WBContextModel::selection_changed, this));

    wb->request_refresh(RefreshNewDiagram, diagram_reference.id(), (NativeHandle)view->get_user_data());
  }
  else
  {
    delete diagram;
    mforms::Utilities::show_error("Internal error adding a new diagram.", "Frontend did not return a diagram.", _("Close"));
  }
  
  if (getenv("DEBUG_CANVAS"))
    view->enable_debug(true);
  
  return view;
}



void WBContextModel::activate_canvas_object(const model_ObjectRef &object, ssize_t flags)
{
  bool newwindow= flags & 1;
  
  FOREACH_COMPONENT(_wbui->get_wb()->_components, iter)
  {
    if ((*iter)->handles_figure(object))
      (*iter)->activate_canvas_object(object, newwindow);
  }
}


void WBContextModel::register_diagram_form(ModelDiagramForm *view)
{
  _model_forms[view->get_model_diagram().id()]= view;
  view->refill_catalog_tree();
}


ModelDiagramForm *WBContextModel::get_diagram_form(mdc::CanvasView *view)
{
  for (std::map<std::string,ModelDiagramForm*>::const_iterator iter= _model_forms.begin();
       iter != _model_forms.end(); ++iter)
  {
    if (iter->second->get_view() == view)
      return iter->second;
  }
  return 0;
}


void WBContextModel::notify_diagram_created(ModelDiagramForm *view)
{
   view->scoped_connect(view->get_model_diagram()->signal_changed(),
        boost::bind(&WBContextModel::diagram_object_changed, this, _1, _2, view));
    
  // now called from wb_component_physical.cpp:model_list_changed
  //_wbui->get_physical_overview()->send_refresh_diagram(model_DiagramRef());
}


void WBContextModel::notify_diagram_destroyed(ModelDiagramForm *diagram)
{
  if (diagram != NULL)
  {
    std::string id= diagram->get_model_diagram().id();
    delete diagram;
    _model_forms.erase(id);
  }

  // now called from wb_component_physical.cpp:model_list_changed
  //_wbui->get_physical_overview()->send_refresh_diagram(model_DiagramRef());
}


void WBContextModel::handle_notification(const std::string &name, void *sender, base::NotificationInfo &info)
{
  if (name == "GNMainFormChanged")
    update_current_diagram(_wbui->get_active_main_form());
}


void WBContextModel::update_current_diagram(bec::UIForm *form)
{
  ModelDiagramForm* dform= dynamic_cast<ModelDiagramForm*>(form);
  if (dform)
  {
    model_DiagramRef diagram(dform->get_model_diagram());
    if (diagram.is_valid() && diagram->owner().is_valid())
      diagram->owner()->currentDiagram(diagram);    
    
    _wbui->get_command_ui()->revalidate_edit_menu_items();
  }
}


void WBContextModel::diagram_object_changed(const std::string &member, const grt::ValueRef &ovalue, ModelDiagramForm *view)
{
  if (member == "name")
  {
    if (view->get_model_diagram().is_valid())
    {
      base::NotificationInfo info;
      info["form"] = view->form_id();
      info["title"] = view->get_title();
      base::NotificationCenter::get()->send("GNFormTitleDidChange", view, info);
      _wbui->get_physical_overview()->send_refresh_diagram(view->get_model_diagram());
    }
  }
  else if (member == "zoom")
  {
    _wbui->get_wb()->request_refresh(RefreshZoom, "");
  }
}


void WBContextModel::diagram_object_list_changed(grt::internal::OwnedList *list, bool added, const grt::ValueRef &value, ModelDiagramForm *vform)
{
  if (vform == _wbui->get_active_main_form())
  {
    if (vform->get_model_diagram()->selection().valueptr() == list)
      _wbui->get_wb()->request_refresh(RefreshSelection, "", reinterpret_cast<NativeHandle>(vform->get_frontend_data()));
  }
}


bool WBContextModel::has_selected_schema()
{
  PhysicalOverviewBE *active_form = dynamic_cast<PhysicalOverviewBE*>(_wbui->get_active_main_form());
  if (active_form == _overview && _overview->get_active_schema_node())
    return true;
  
  return false;
}

bool WBContextModel::has_selected_figures()
{
  ModelDiagramForm *view;
  model_DiagramRef diagram(get_active_model_diagram(false));
  if (!diagram.is_valid()) // in case an editor in a diagram tab is active
  {
    diagram = get_active_model_diagram(true);
    view = dynamic_cast<ModelDiagramForm*>(_wbui->get_active_main_form());
  }
  else
    view= dynamic_cast<ModelDiagramForm*>(_wbui->get_active_form());
  if (view && view->has_selection())
    return true;
  return false;
}

bool WBContextModel::has_selected_model()
{
  if (_wbui->get_active_main_form() == _overview)
    return true;
  return false;
}

void WBContextModel::add_model_schema()
{
  _wbui->get_wb()->get_component<WBComponentPhysical>()->add_new_db_schema(workbench_physical_ModelRef::cast_from(get_active_model(true)));
}

void WBContextModel::add_model_diagram()
{
  add_new_diagram(get_active_model(true));
}

void WBContextModel::add_model_table()
{
  if (_overview->get_active_schema_node())
    _overview->get_active_schema_node()->add_new_db_table(_wbui->get_wb());
}


void WBContextModel::add_model_view()
{
  if (_overview->get_active_schema_node())
    _overview->get_active_schema_node()->add_new_db_view(_wbui->get_wb());
}


void WBContextModel::add_model_rgroup()
{
  if (_overview->get_active_schema_node())
    _overview->get_active_schema_node()->add_new_db_routine(_wbui->get_wb());
}

void WBContextModel::remove_figure()
{
  ModelDiagramForm *view;
  model_DiagramRef diagram(get_active_model_diagram(false));
  if (!diagram.is_valid()) // in case an editor in a diagram tab is active
  {
    diagram = get_active_model_diagram(true);
    view = dynamic_cast<ModelDiagramForm*>(_wbui->get_active_main_form());
  }
  else
    view= dynamic_cast<ModelDiagramForm*>(_wbui->get_active_form());
  if (view)
    view->remove_selection();
}


GrtObjectRef WBContextModel::duplicate_object(const db_DatabaseObjectRef &object, grt::CopyContext &copy_context)
{
  std::set<std::string> skip;
  skip.insert("oldName");
  
  if (object.is_instance(db_Table::static_class_name()))
  {
    db_TableRef table(db_TableRef::cast_from(object));
    
    // copy table
    db_TableRef dbtable(db_TableRef::cast_from(copy_context.copy(table, skip)));
    
    copy_context.update_references();
    
    // post-processing
    // - Make foreign key names unique.
    ssize_t max_fk_len = workbench_physical_ModelRef::cast_from(dbtable->owner()->owner()->owner())->rdbms()->maximumIdentifierLength();
    grt::ListRef<db_ForeignKey> fks(dbtable->foreignKeys());
    std::set<std::string> used_fk_names = bec::SchemaHelper::get_foreign_key_names(db_SchemaRef::cast_from(dbtable->owner()));
    for (size_t c= fks.count(), i = 0; i < c; i++)
    {
      db_ForeignKeyRef fk(fks[i]);
      fk->name(bec::SchemaHelper::get_unique_foreign_key_name(used_fk_names, fk->name(), (int)max_fk_len));
    }
    
    if (grt::find_named_object_in_list(db_SchemaRef::cast_from(dbtable->owner())->tables(), dbtable->name()).is_valid())
      dbtable->name(grt::get_name_suggestion_for_list_object(db_SchemaRef::cast_from(dbtable->owner())->tables(),
                                                             *dbtable->name()+"_copy"));
    grt::AutoUndo undo(get_grt());
    
    db_SchemaRef::cast_from(dbtable->owner())->tables().insert(dbtable);
    
    undo.end(strfmt(_("Duplicate Table '%s'"), dbtable->name().c_str()));
    
    return dbtable;
  }
  else if (object.is_instance(db_View::static_class_name()))
  {
    db_ViewRef view(db_ViewRef::cast_from(object));
    
    // copy view
    db_ViewRef dbview(db_ViewRef::cast_from(copy_context.copy(view)));
    
    if (grt::find_named_object_in_list(db_SchemaRef::cast_from(dbview->owner())->views(), dbview->name()).is_valid())
      dbview->name(grt::get_name_suggestion_for_list_object(db_SchemaRef::cast_from(dbview->owner())->views(),
                                                            *dbview->name()+"_copy"));
    grt::AutoUndo undo(get_grt());
    
    db_SchemaRef::cast_from(dbview->owner())->views().insert(dbview);
    
    undo.end(strfmt(_("Duplicate View '%s'"), dbview->name().c_str()));

    return dbview;
  }
  else if (object.is_instance(db_RoutineGroup::static_class_name()))
  {
    db_RoutineGroupRef routineGroup(db_RoutineGroupRef::cast_from(object));
    
    // copy routineGroup
    db_RoutineGroupRef dbroutineGroup(db_RoutineGroupRef::cast_from(copy_context.copy(routineGroup)));
    
    if (grt::find_named_object_in_list(db_SchemaRef::cast_from(dbroutineGroup->owner())->routineGroups(), dbroutineGroup->name()).is_valid())
      dbroutineGroup->name(grt::get_name_suggestion_for_list_object(db_SchemaRef::cast_from(dbroutineGroup->owner())->routineGroups(),
                                                                    *dbroutineGroup->name()+"_copy"));
    grt::AutoUndo undo(get_grt());
    
    db_SchemaRef::cast_from(dbroutineGroup->owner())->routineGroups().insert(dbroutineGroup);
    
    undo.end(strfmt(_("Duplicate Routine Group '%s'"), dbroutineGroup->name().c_str()));
    return dbroutineGroup;
  }
  return GrtObjectRef();
}


void WBContextModel::update_plugin_arguments_pool(ArgumentPool &args)
{
  model_ModelRef model(get_active_model(true));

  if (!model.is_valid())
    return;

  args.add_entries_for_object("", model, "model.Model");
  args.add_entries_for_object("activeModel", model, "model.Model");

  if (workbench_physical_ModelRef::can_wrap(model))
  {
    workbench_physical_ModelRef pmodel(workbench_physical_ModelRef::cast_from(model));
    args.add_entries_for_object("", pmodel->catalog(), "db.Catalog");
    args.add_entries_for_object("activeCatalog", pmodel->catalog(), "db.Catalog");
  }
  
  ModelDiagramForm *view;
  model_DiagramRef diagram(get_active_model_diagram(false));
  if (!diagram.is_valid()) // in case an editor in a diagram tab is active
  {
    diagram = get_active_model_diagram(true);
    view = dynamic_cast<ModelDiagramForm*>(_wbui->get_active_main_form());
  }
  else
    view= dynamic_cast<ModelDiagramForm*>(_wbui->get_active_form());
  
  if (diagram.is_valid())
  {
    args.add_entries_for_object("", diagram, "model.Diagram");
    args.add_entries_for_object("activeDiagram", diagram, "model.Diagram");
    
    if (view)
    {
      // diagram selection
      grt::ListRef<model_Object> selection(view->get_selection());

      args.add_list_for_selection("activeDiagram", selection);
      
      if (selection.count() == 1)
      {
        model_ObjectRef object(selection[0]);
      
        //args.add_entries_for_object("", object, "model.Object");
        args.add_entries_for_object("", object, "GrtObject");
        
        // check if object represented by this is wanted
        FOREACH_COMPONENT(_wbui->get_wb()->_components, iter)
        {
          if ((*iter)->handles_figure(object))
          {
            grt::ObjectRef fobject((*iter)->get_object_for_figure(object));
            if (fobject.is_valid())
              args.add_entries_for_object("", fobject, "db.DatabaseObject");
          }
        }
      }
    }
  }

  // overview selection
  OverviewBE *overview= dynamic_cast<OverviewBE*>(_wbui->get_active_form());
  if (overview)
  {
    grt::ListRef<GrtObject> selection(overview->get_selection());
    
    if (selection.count() == 1)
    {
      GrtObjectRef object(selection[0]);
      
      args.add_entries_for_object("", object, "GrtObject");
    }
  }
}


/**
 * Adds a list of common menu entries to the menu item list. Returns the number of items added.
 */
int WBContextModel::get_object_list_popup_items(bec::UIForm *form, 
                                                const std::vector<bec::NodeId> &nodes,
                                                const grt::ListRef<GrtObject> &objects,
                                                const std::string &label, const std::list<std::string> &groups,
                                                bec::MenuItemList &items)
{
  size_t initial_count= items.size();
  bec::TreeModel* model= dynamic_cast<bec::TreeModel*>(form);
  WBContext *wb= _wbui->get_wb();

  // Start with clipboard commands.
  // First check if all items in the selection list are deletable (used for cut and delete items).
  bec::MenuItem item;
  bool can_delete= objects->count() > 0;
  bool can_copy= can_delete;
  bool can_remove= true;
  if (model != NULL)
    for (std::vector<bec::NodeId>::const_iterator iterator= nodes.begin(); iterator != nodes.end(); iterator++)
    {
      if (can_delete && !model->is_deletable(*iterator))
        can_delete= false;
      if (can_copy && !model->is_copyable(*iterator))
        can_copy= false;
    }

  // can't copy/paste diagrams
  if (nodes.empty() || nodes[0][0] != 0)
  {
    item.checked= false;
    item.enabled= can_copy && can_delete;
    item.caption= label.empty() ? _("Cut") : strfmt(_("Cut %s"), label.c_str());
    item.name= "builtin:cut";
    item.type= MenuAction;
    items.push_back(item);
    
    item.enabled= can_copy;
    item.caption= label.empty() ? _("Copy") : strfmt(_("Copy %s"), label.c_str());
    item.name= "builtin:copy";
    item.type= MenuAction;
    items.push_back(item);
    
    item.enabled= form->can_paste();
    item.caption= strfmt(_("Paste %s"), wb->get_clipboard()->get_content_description().c_str());
    item.name= "builtin:paste";
    item.type= MenuAction;
    items.push_back(item);

  // this is already implemented, but not yet exposed.. uncomment once the whole thing is working
  //  item.enabled= form->can_paste();
  //  item.caption= strfmt(_("Duplicate %s"), wb->get_clipboard()->get_content_description().c_str());
  //  item.name= "builtin:duplicate";
  //  item.type= MenuAction;
  //  items.push_back(item);

    item.type= MenuSeparator;
    items.push_back(item);
  }

  // Plugin dependent menu entries.
  size_t old_count= items.size();
  
  if (objects.count() > 0)
  {
    add_object_plugins_to_popup_menu(objects, groups, items);
    
    bool has_objects = false;
    bool connections_only = true;
    grt::ListRef<GrtObject> model_objects(wb->get_grt());
    for (size_t c= objects.count(), i= 0; i < c; i++)
    {
      if (!objects[i].is_instance(model_Connection::static_class_name()))
        connections_only = false;
      if (objects[i].is_instance(model_Object::static_class_name()))
      {
        has_objects = true;
        FOREACH_COMPONENT(wb->_components, compo)
        {
          GrtObjectRef model_object((*compo)->get_object_for_figure(model_ObjectRef::cast_from(objects[i])));
          
          if (model_object.is_valid())
          {
            model_objects.insert(model_object);
            break;
          }
        }
      }
    }
    if (connections_only || !has_objects)
      can_remove = false;

    if (model_objects.count() > 0)
      add_object_plugins_to_popup_menu(model_objects, groups, items);
    else
      can_remove = false;
  }
  
  // At the end of the list there is the delete command.
  item.checked= false;
  item.enabled= can_delete;
  
  // Add a separator item if we added plugin specific menu commands above.
  if (old_count != items.size())
  {
    item.type= MenuSeparator;
    items.push_back(item);
  }
  item.caption= label.empty() ? _("Delete") : strfmt(_("Delete %s"), label.c_str());
  item.name= "builtin:delete";
  item.type= MenuAction;
  items.push_back(item);

  if ((nodes.empty() || nodes[0][0] != 0) && objects.count() > 0)
  {
    item.caption= label.empty() ? _("Remove Figure") : strfmt(_("Remove Figure %s"), label.c_str());
    item.name= "builtin:removeFigure";
    item.type= MenuAction;
    item.enabled = can_remove;
    items.push_back(item);
  }
  return int(items.size() - initial_count);
}


struct sortplugin
{
  bool operator ()(const app_PluginRef &a, const app_PluginRef &b) const
  {
    return a->rating() < b->rating();
  }
};


int WBContextModel::add_object_plugins_to_popup_menu(const grt::ListRef<GrtObject> &objects,
                                                     const std::list<std::string> &groups,
                                                     bec::MenuItemList &items)
{
  bec::ArgumentPool argpool;
  _wbui->get_wb()->update_plugin_arguments_pool(argpool);
  if (objects.count() > 0)
    argpool.add_entries_for_object("", objects[0], "GrtObject");

  int count= 0;
  // look for plugins that take this object type as input
  std::vector<app_PluginRef> plugins(_wbui->get_wb()->get_plugin_manager()->get_plugins_for_objects(grt::ObjectListRef::cast_from(objects)));

  // sort by rating  
  std::sort(plugins.begin(), plugins.end(), sortplugin());
  
  for (std::vector<app_PluginRef>::const_iterator iter= plugins.begin(); iter != plugins.end(); ++iter)
  {
    bool match= false;
    //    bool matchPrefix= false;
    
    for (size_t c= (*iter)->groups().count(), i= 0; i < c; i++)
    {
      std::string str= (*iter)->groups().get(i);
      for (std::list<std::string>::const_iterator ctx= groups.begin();
           ctx != groups.end(); ++ctx)
      {
        if ((*ctx)[ctx->size()-1] == '*' && g_ascii_strncasecmp(ctx->c_str(), str.c_str(), (guint) ctx->size() - 1) == 0)
        {
          match= true;
          break;
        }
        else if ((*ctx)[ctx->size() - 1] != '*' && g_ascii_strcasecmp(ctx->c_str(), str.c_str()) == 0)
        {
          match= true;
          break;
        }
      }
    }
    if (!match)
      continue;
    
    bec::MenuItem item;
    
    item.type= MenuAction;
    item.caption= *(*iter)->caption() + ((*iter)->pluginType()=="gui"?"...":"");
    item.checked= false;
    item.enabled= _wbui->get_wb()->get_grt_manager()->check_plugin_runnable(*iter, argpool);
    item.shortcut= "";
    item.name= "plugin:"+*(*iter)->name();
    if (item.caption.empty())
      item.caption= item.name;
    items.push_back(item);
    count++;
    
    if ((*iter)->groups().get_index("catalog/Editors") != grt::BaseListRef::npos ||
        (*iter)->groups().get_index("model/Editors") != grt::BaseListRef::npos)
    {
      app_PluginRef plugin(_wbui->get_wb()->get_plugin_manager()->get_plugin("wb.edit.editSelectedFigureInNewWindow"));

      item.caption= _("Edit in New Tab...");
      if (_wbui->get_active_form() && !_wbui->get_active_form()->get_edit_target_name().empty())
      {
        items.back().caption = base::strfmt(_("Edit %s..."), _wbui->get_active_form()->get_edit_target_name().c_str());

        item.caption= base::strfmt(_("Edit %s in New Tab..."), _wbui->get_active_form()->get_edit_target_name().c_str());
      }

      item.name= "plugin:"+*plugin->name();
      item.type= MenuAction;
      // state for this item will be the same as for the previous one
      //item.enabled= check_plugin_runnable(plugin, "", objects);
      items.push_back(item);
      count++;
    }
  }
  
  return count;
}


void WBContextModel::history_changed()
{
  std::string undo_description(_wbui->get_wb()->get_grt()->get_undo_manager()->undo_description());
  std::string redo_description(_wbui->get_wb()->get_grt()->get_undo_manager()->redo_description());

  std::list<bec::UIForm*> forms;
  forms.push_back(_overview);
  for (std::map<std::string, ModelDiagramForm*>::const_iterator iter= _model_forms.begin();
       iter != _model_forms.end(); ++iter)
    forms.push_back(iter->second);
  
  mforms::MenuItem *item;
  
  for (std::list<bec::UIForm*>::const_iterator iter = forms.begin(); iter != forms.end(); ++iter)
  {
    bec::UIForm *form = *iter;
    mforms::MenuBar *menu = form->get_menubar();
    if (menu)
    {
      item = menu->find_item("undo");
      if (item)
      {
        if (!undo_description.empty())
          item->set_title(strfmt(_("Undo %s"), undo_description.c_str()));
        else
          item->set_title(_("Undo"));
      }
      item = menu->find_item("redo");
      if (item)
      {
        if (!redo_description.empty())
          item->set_title(strfmt(_("Redo %s"), redo_description.c_str()));
        else
          item->set_title(_("Redo"));
      }
    }      
  }
}


void WBContextModel::selection_changed()
{
  if (!_wbui->get_wb()->get_grt_manager()->in_main_thread())
  {
    _wbui->get_wb()->get_grt_manager()->run_once_when_idle(boost::bind(&WBContextModel::selection_changed, this));
    return;
  }
    
  bec::Clipboard *clip = _wbui->get_wb()->get_clipboard();
    
  std::list<bec::UIForm*> forms;
  forms.push_back(_overview);
  for (std::map<std::string, ModelDiagramForm*>::const_iterator iter= _model_forms.begin();
       iter != _model_forms.end(); ++iter)
    forms.push_back(iter->second);
  
  mforms::MenuItem *item;

  for (std::list<bec::UIForm*>::const_iterator iter = forms.begin(); iter != forms.end(); ++iter)
  {
    bec::UIForm *form = *iter;
    mforms::MenuBar *menu = form->get_menubar();
    if (menu)
    {
      std::string target(form->get_edit_target_name());
      std::string content(clip->get_content_description());
      
      item = menu->find_item("copy");
      if (item)
      {
        if (!target.empty())
          item->set_title(strfmt(_("Copy %s"), target.c_str()));
        else
          item->set_title(_("Copy"));
      }
      
      item = menu->find_item("cut");
      if (item)
      {
        if (!target.empty())
          item->set_title(strfmt(_("Cut %s"), target.c_str()));
        else
          item->set_title(_("Cut"));
      }
      
      item = menu->find_item("delete");
      if (item)
      {
        if (!target.empty())
          item->set_title(strfmt(_("Delete %s"), target.c_str()));
        else
          item->set_title(_("Delete"));
      }

      item = menu->find_item("paste");
      if (item)
      {
        if (!content.empty())
          item->set_title(strfmt(_("Paste %s"), content.c_str()));
        else
          item->set_title(_("Paste"));
      }
    }
  }
  _wbui->get_command_ui()->revalidate_edit_menu_items();
}


GrtVersionRef WBContextModel::get_target_version()
{
  if (get_active_model(true).is_valid())
  {
    db_CatalogRef catalog(workbench_physical_ModelRef::cast_from(get_active_model(true))->catalog());
    if (catalog->version().is_valid())
      return catalog->version();
    else
    {
      std::string target_version = _wbui->get_wb()->get_grt_manager()->get_app_option_string("DefaultTargetMySQLVersion");
      if (target_version.empty())
        target_version = "5.5";

      return bec::parse_version(_wbui->get_wb()->get_grt_manager()->get_grt(), target_version);
    }
  }
  return GrtVersionRef();
}


#ifndef BasicExport____

void WBContextModel::export_png(const std::string &path)
{
  ModelDiagramForm *form= dynamic_cast<ModelDiagramForm*>(_wbui->get_active_main_form());
  if (form)
  {
    _wbui->get_wb()->show_status_text(strfmt(_("Exporting to %s..."), path.c_str()));
    try
    {
      form->get_view()->export_png(path, true);
      _wbui->get_wb()->show_status_text(strfmt(_("Exported diagram image to %s"), path.c_str()));
    }
    catch (const std::exception &exc)
    {
      _wbui->get_wb()->show_status_text(_("Could not export to PNG file."));
      _wbui->get_wb()->show_exception(_("Export to PNG"), exc);
    }
  }
  else
    _wbui->get_wb()->show_error(_("Cannot Export Diagram"), _("Current diagram cannot be exported as image, please select a diagram first."));
}


void WBContextModel::export_pdf(const std::string &path)
{
  ModelDiagramForm *form= dynamic_cast<ModelDiagramForm*>(_wbui->get_active_main_form());
  if (form)
  {
    Size size= form->get_view()->get_total_view_size();
    double scale= _wbui->get_wb()->get_document()->pageSettings()->scale();
    
    size.width= size.width / scale * 2.834;
    size.height= size.height / scale * 2.834;
    
    _wbui->get_wb()->show_status_text(strfmt(_("Exporting full model diagram to %s..."), path.c_str()));
    
    try
    {
      form->get_view()->export_pdf(path, size);
      _wbui->get_wb()->show_status_text(strfmt(_("Exported PDF to %s"), path.c_str()));
    }
    catch (const std::exception &exc)
    {
      _wbui->get_wb()->show_status_text(_("Could not export to PDF"));
      _wbui->get_wb()->show_exception(_("Export to PDF"), exc);
    }
  }
  else
    _wbui->get_wb()->show_error(_("Cannot Export Diagram"), _("Current diagram cannot be exported as image, please select a diagram first."));
}


void WBContextModel::export_svg(const std::string &path)
{
  ModelDiagramForm *form= dynamic_cast<ModelDiagramForm*>(_wbui->get_active_main_form());
  if (form)
  {
    Size size= form->get_view()->get_total_view_size();
    double scale= _wbui->get_wb()->get_document()->pageSettings()->scale();
    
    size.width= MM_TO_PT(size.width / scale);
    size.height= MM_TO_PT(size.height / scale);
    
    _wbui->get_wb()->show_status_text(strfmt(_("Exporting full model diagram to %s..."), path.c_str()));
    
    try
    {
      form->get_view()->export_svg(path, size);
      _wbui->get_wb()->show_status_text(strfmt(_("Exported SVG to %s"), path.c_str()));
    }
    catch (const std::exception &exc)
    {
      _wbui->get_wb()->show_status_text(_("Could not export to SVG"));
      _wbui->get_wb()->show_exception(_("Export to SVG"), exc);
    }
  }
  else
    _wbui->get_wb()->show_error(_("Cannot Export Diagram"), _("Current diagram cannot be exported as image, please select a diagram first."));
}


void WBContextModel::export_ps(const std::string &path)
{
  ModelDiagramForm *form= dynamic_cast<ModelDiagramForm*>(_wbui->get_active_main_form());
  if (form)
  {
    Size size= form->get_view()->get_total_view_size();
    double scale= _wbui->get_wb()->get_document()->pageSettings()->scale();
    
    size.width= MM_TO_PT(size.width / scale);
    size.height= MM_TO_PT(size.height / scale);
    
    _wbui->get_wb()->show_status_text(strfmt(_("Exporting full model diagram to %s..."), path.c_str()));
    
    try
    {
      form->get_view()->export_ps(path, size);
      _wbui->get_wb()->show_status_text(strfmt(_("Exported PS to %s"), path.c_str()));
    }
    catch (std::exception &exc)
    {
      _wbui->get_wb()->show_status_text(_("Could not export to PS file"));
      _wbui->get_wb()->show_exception(_("Export to PS File"), exc);
    }
  }
  else
    _wbui->get_wb()->show_error(_("Cannot Export Diagram"), _("Current diagram cannot be exported as image, please select a diagram first."));
}

#endif // BasicExport____


#ifndef Diagrams_and_Canvas____

//--------------------------------------------------------------------------------
// Canvas Management


void WBContextModel::add_new_diagram(const model_ModelRef &model)
{
  _wbui->get_wb()->show_status_text(_("Creating Diagram..."));
  
 // model_DiagramRef view(model_DiagramRef::cast_from(_wbui->get_wb()->execute_in_grt_thread("Create new diagram",
   //                                                                       boost::bind(&model_Model::addNewDiagram, &model.content(), true))));
  _wbui->get_wb()->lock_gui(true);
  model_DiagramRef view = model->addNewDiagram(true);
  if (view.is_valid())
  {
    model->currentDiagram(view);
    view->get_data()->realize();
  }
  _wbui->get_wb()->lock_gui(false);
  _wbui->get_wb()->show_status_text(_("Diagram added."));
}


void WBContextModel::switch_diagram(const model_DiagramRef &view)
{
  _wbui->get_wb()->switched_view(view->get_data()->get_canvas_view());
}

#endif // Diagrams_and_Canvas____
#ifndef Canvas_Objects____
/** 
 ****************************************************************************
 * @brief Delete a canvas object from the canvas/model
 * 
 * Will remove the figure from the model and delete all known
 * references to it. There may be some references left, which will cause
 * the GRT object and its bridge to stay alive for some more time, so the
 * bridge is marked as invalid to prevent the canvas item from appearing
 * on screen.
 *
 * @param figure to be removed
 ****************************************************************************
 */
bool WBContextModel::delete_object(model_ObjectRef object)
{
  model_DiagramRef view(model_DiagramRef::cast_from(object->owner()));
  
  FOREACH_COMPONENT(_wbui->get_wb()->_components, iter)
  {
    if ((*iter)->handles_figure(object))
    {
      grt::ValueRef value;
      grt::ObjectRef obj;
      if (object.is_instance(model_Figure::static_class_name()))
      {
        obj = (*iter)->get_object_for_figure(model_FigureRef::cast_from(object));
        value = (*iter)->get_object_for_figure(model_FigureRef::cast_from(object));

      }

      if ((*iter)->delete_model_object(object, false))
      {
        notify_catalog_tree_view(NodeDelete, value);
        return true;
      }
      return false;
    }
  }
  return false;
}


bool WBContextModel::remove_figure(model_ObjectRef object)
{
  model_DiagramRef view(model_DiagramRef::cast_from(object->owner()));
  
  FOREACH_COMPONENT(_wbui->get_wb()->_components, iter)
  {
    if ((*iter)->handles_figure(object))
    {
      grt::ValueRef value;
      if (object.is_instance(model_Figure::static_class_name()))
        value = (*iter)->get_object_for_figure(model_FigureRef::cast_from(object));

      if ((*iter)->delete_model_object(object, true))
      {
        notify_catalog_tree_view(NodeUnmark, value, view->id());
        return true;
      }
      return false;
    }
  }
  return false;
}

#endif // Canvas_Objects____

model_DiagramRef WBContextModel::get_view_with_id(const std::string &id)
{
  return model_DiagramRef::cast_from(get_wbui()->get_wb()->get_grt()->find_object_by_id(id, "/wb/doc"));
}

bool WBContextModel::delete_diagram(const model_DiagramRef &view)
{
  grt::AutoUndo undo(get_wbui()->get_wb()->get_grt());
  view->owner()->diagrams().remove_value(view);
  undo.end(strfmt(_("Delete Diagram '%s'"), view->name().c_str()));
  
#ifdef _WIN32
  // in windows, a diagram is released as soon as the tab is closed. That means
  // if a user closes a diagram before deleting it, the diagram is released before 
  // it's removed from the list of diagrams in the list. That will cause the overview
  // to be refreshed too early and with outdated contents. We force another explicit refresh
  // here to workaround that.
  if (get_wbui()->get_physical_overview())
    get_wbui()->get_physical_overview()->send_refresh_diagram(model_DiagramRef());
#endif
  return true;
}


void WBContextModel::begin_plugin_exec()
{
  // lock the canvas so that it doesn't keep refreshing all the time.
  // XXX we have to find some way to allow plugins control refresh freezing
  ModelDiagramForm *view= dynamic_cast<ModelDiagramForm*>(get_wbui()->get_active_main_form());
  _locked_view_for_plugin_exec = 0;
  
  if (view) 
  {
    _locked_view_for_plugin_exec = view->get_view();
    _locked_view_for_plugin_exec->lock_redraw();
  }  
}


void WBContextModel::end_plugin_exec()
{
  // form can get destroyed after a plugin is executed
  if (_locked_view_for_plugin_exec && get_diagram_form(_locked_view_for_plugin_exec))
    _locked_view_for_plugin_exec->unlock_redraw();
  _locked_view_for_plugin_exec = 0;
}


/**
 * Called by the front end when the user data type editor has been closed. We can then remove our reference to it.
 */
static void userTypeEditorClosed(UserDefinedTypeEditor **editor_ptr)
{
  *editor_ptr = NULL;
}

void WBContextModel::show_user_type_editor(workbench_physical_ModelRef model)
{
  if (_current_user_type_editor == NULL)
  {
    _current_user_type_editor = new UserDefinedTypeEditor(get_wbui(), model);
    scoped_connect(_current_user_type_editor->signal_closed(), boost::bind(userTypeEditorClosed, &_current_user_type_editor));
  }
  _current_user_type_editor->show_modal(NULL, NULL);
}

mforms::View *WBContextModel::shared_secondary_sidebar()
{
  return _secondary_sidebar;
}

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

static struct RegisterNotifDocs_wb_context_model
{
  RegisterNotifDocs_wb_context_model()
  {
    base::NotificationCenter::get()->register_notification("GRNModelOpened",
                                                           "modeling",
                                                           "Sent when a model document finishes loading.",
                                                           "ui.ModelPanel instance",
                                                           "");

    base::NotificationCenter::get()->register_notification("GRNModelCreated",
                                                           "modeling",
                                                           "Sent when a new model document is created.",
                                                           "ui.ModelPanel instance",
                                                           "");

    base::NotificationCenter::get()->register_notification("GRNModelClosed",
                                                           "modeling",
                                                           "Sent when a model document is closed.",
                                                           "ui.ModelPanel instance",
                                                           "");
  }
} initdocs_wb_context_model;