summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/internal/core/model/property/table/PropertyTable.java
blob: 20233e95e49a743818ff76c54bd2b414061474db (plain)
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
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.wb.internal.core.model.property.table;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.wb.draw2d.IColorConstants;
import org.eclipse.wb.draw2d.ICursorConstants;
import org.eclipse.wb.internal.core.DesignerPlugin;
import org.eclipse.wb.internal.core.EnvironmentUtils;
import org.eclipse.wb.internal.core.model.property.Property;
import org.eclipse.wb.internal.core.model.property.category.PropertyCategory;
import org.eclipse.wb.internal.core.model.property.category.PropertyCategoryProvider;
import org.eclipse.wb.internal.core.model.property.category.PropertyCategoryProviders;
import org.eclipse.wb.internal.core.model.property.editor.PropertyEditor;
import org.eclipse.wb.internal.core.model.property.editor.complex.IComplexPropertyEditor;
import org.eclipse.wb.internal.core.model.property.editor.presentation.ButtonPropertyEditorPresentation;
import org.eclipse.wb.internal.core.model.property.editor.presentation.PropertyEditorPresentation;
import org.eclipse.wb.internal.core.utils.check.Assert;
import org.eclipse.wb.internal.core.utils.ui.DrawUtils;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * Control that can display {@link Property}'s and edit them using {@link PropertyEditor}'s.
 *
 * @author scheglov_ke
 * @author lobas_av
 * @coverage core.model.property.table
 */
public class PropertyTable extends Canvas implements ISelectionProvider {
  ////////////////////////////////////////////////////////////////////////////
  //
  // Colors
  //
  ////////////////////////////////////////////////////////////////////////////
  private static final Color COLOR_BACKGROUND = IColorConstants.listBackground;
  private static final Color COLOR_NO_PROPERTIES = IColorConstants.gray;
  private static final Color COLOR_LINE = IColorConstants.lightGray;
  private static final Color COLOR_COMPLEX_LINE = DrawUtils.getShiftedColor(
      IColorConstants.lightGray,
      -32);
  private static final Color COLOR_PROPERTY_BG = DrawUtils.getShiftedColor(COLOR_BACKGROUND, -12);
  private static final Color COLOR_PROPERTY_BG_MODIFIED = COLOR_BACKGROUND;
  private static final Color COLOR_PROPERTY_FG_TITLE = IColorConstants.listForeground;
  private static final Color COLOR_PROPERTY_FG_VALUE =
      DrawUtils.isDarkColor(IColorConstants.listBackground)
          ? IColorConstants.lightBlue
          : IColorConstants.darkBlue;
  private static final Color COLOR_PROPERTY_BG_SELECTED = IColorConstants.listSelection;
  private static final Color COLOR_PROPERTY_FG_SELECTED = IColorConstants.listSelectionText;
  private static final Color COLOR_PROPERTY_FG_ADVANCED = IColorConstants.gray;
  // BEGIN ADT MODIFICATIONS
  public static final Color COLOR_PROPERTY_FG_DEFAULT = IColorConstants.darkGray;
  // END ADT MODIFICATIONS
  ////////////////////////////////////////////////////////////////////////////
  //
  // Sizes
  //
  ////////////////////////////////////////////////////////////////////////////
  private static final int MIN_COLUMN_WIDTH = 75;
  private static final int MARGIN_LEFT = 2;
  private static final int MARGIN_RIGHT = 1;
  private static final int STATE_IMAGE_MARGIN_RIGHT = 4;
  ////////////////////////////////////////////////////////////////////////////
  //
  // Images
  //
  ////////////////////////////////////////////////////////////////////////////
  private static final Image m_plusImage = DesignerPlugin.getImage("properties/plus.gif");
  private static final Image m_minusImage = DesignerPlugin.getImage("properties/minus.gif");
  private static int m_stateWidth = 9;
  ////////////////////////////////////////////////////////////////////////////
  //
  // Instance fields
  //
  ////////////////////////////////////////////////////////////////////////////
  private final PropertyTableTooltipHelper m_tooltipHelper;
  private boolean m_showAdvancedProperties;
  private Property[] m_rawProperties;
  private List<PropertyInfo> m_properties;
  private final Set<String> m_expandedIds = Sets.newTreeSet();
  // BEGIN ADT MODIFICATIONS
  // Add support for "expand by default" : If you specify ids to be collapsed by
  // default, then any *other* ids will be expanded by default.
  private Set<String> m_collapsedIds = null;

    /**
     * Sets a set of ids that should be collapsed by default. Everything else
     * will be expanded by default. If this method is not called, ids are
     * collapsed by default instead.
     *
     * @param names set of ids to be collapsed
     */
  public void setDefaultCollapsedNames(Collection<String> names) {
      m_collapsedIds = Sets.newTreeSet();
      for (String name : names) {
          m_collapsedIds.add("|" + name); // See PropertyInfo constructor for id syntax
      }
  }
  // END ADT MODIFICATIONS

  private Image m_bufferedImage;
  private int m_rowHeight;
  private int m_selection;
  private int m_page;
  private int m_splitter = -1;

  ////////////////////////////////////////////////////////////////////////////
  //
  // Constructor
  //
  ////////////////////////////////////////////////////////////////////////////
  public PropertyTable(Composite parent, int style) {
    super(parent, style | SWT.V_SCROLL | SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE);
    hookControlEvents();
    // calculate sizes
    {
      GC gc = new GC(this);
      try {
        m_rowHeight = 1 + gc.getFontMetrics().getHeight() + 1;
      } finally {
        gc.dispose();
      }
    }
    // install tooltip helper
    m_tooltipHelper = new PropertyTableTooltipHelper(this);
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Events
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Adds listeners for events.
   */
  private void hookControlEvents() {
    addListener(SWT.Dispose, new Listener() {
      @Override
    public void handleEvent(Event event) {
        disposeBufferedImage();
      }
    });
    addListener(SWT.Resize, new Listener() {
      @Override
    public void handleEvent(Event event) {
        handleResize();
      }
    });
    addListener(SWT.Paint, new Listener() {
      @Override
    public void handleEvent(Event event) {
        handlePaint(event.gc, event.x, event.y, event.width, event.height);
      }
    });
    getVerticalBar().addListener(SWT.Selection, new Listener() {
      @Override
    public void handleEvent(Event event) {
        handleVerticalScrolling();
      }
    });
    addMouseListener(new MouseAdapter() {
      @Override
      public void mouseDown(MouseEvent event) {
        forceFocus();
        handleMouseDown(event);
      }

      @Override
      public void mouseUp(MouseEvent event) {
        handleMouseUp(event);
      }

      @Override
      public void mouseDoubleClick(MouseEvent event) {
        handleMouseDoubleClick(event);
      }
    });
    addMouseMoveListener(new MouseMoveListener() {
      @Override
    public void mouseMove(MouseEvent event) {
        handleMouseMove(event);
      }
    });
    // keyboard
    addKeyListener(new KeyAdapter() {
      @Override
      public void keyPressed(KeyEvent e) {
        handleKeyDown(e);
      }
    });
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Events: dispose, resize, scroll
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Disposes image used for double buffered painting.
   */
  private void disposeBufferedImage() {
    if (m_bufferedImage != null) {
      m_bufferedImage.dispose();
      m_bufferedImage = null;
    }
  }

  /**
   * Handles {@link SWT#Resize} event.
   */
  private void handleResize() {
    disposeBufferedImage();
    configureScrolling();
    // splitter
    {
      // set default value for splitter
      if (m_splitter <= MIN_COLUMN_WIDTH) {
        m_splitter = Math.max((int) (getClientArea().width * 0.4), MIN_COLUMN_WIDTH);
      }
      configureSplitter();
    }
  }

  /**
   * Handles {@link SWT#Selection} event for vertical {@link ScrollBar}.
   */
  private void handleVerticalScrolling() {
    ScrollBar verticalBar = getVerticalBar();
    if (verticalBar.getEnabled()) {
      // update selection
      m_selection = verticalBar.getSelection();
      // redraw (but not include vertical bar to avoid flashing)
      {
        Rectangle clientArea = getClientArea();
        redraw(clientArea.x, clientArea.y, clientArea.width, clientArea.height, false);
      }
    }
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Keyboard
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Handles {@link SWT#KeyDown} event.
   */
  private void handleKeyDown(KeyEvent e) {
    if (m_activePropertyInfo != null) {
      try {
        Property property = m_activePropertyInfo.getProperty();
        // expand/collapse
        if (m_activePropertyInfo.isComplex()) {
          if (!m_activePropertyInfo.isExpanded()
              && (e.character == '+' || e.keyCode == SWT.ARROW_RIGHT)) {
            m_activePropertyInfo.expand();
            configureScrolling();
            return;
          }
          if (m_activePropertyInfo.isExpanded()
              && (e.character == '-' || e.keyCode == SWT.ARROW_LEFT)) {
            m_activePropertyInfo.collapse();
            configureScrolling();
            return;
          }
        }
        // navigation
        if (navigate(e)) {
          return;
        }
        // editor activation
        if (e.character == ' ' || e.character == SWT.CR) {
          activateEditor(property, null);
          return;
        }
        // DEL
        if (e.keyCode == SWT.DEL) {
          e.doit = false;
          property.setValue(Property.UNKNOWN_VALUE);
          return;
        }
        // send to editor
        property.getEditor().keyDown(this, property, e);
      } catch (Throwable ex) {
        DesignerPlugin.log(ex);
      }
    }
  }

  /**
   * @return <code>true</code> if given {@link KeyEvent} was navigation event, so new
   *         {@link PropertyInfo} was selected.
   */
  public boolean navigate(KeyEvent e) {
    int index = m_properties.indexOf(m_activePropertyInfo);
    Rectangle clientArea = getClientArea();
    //
    int newIndex = index;
    if (e.keyCode == SWT.HOME) {
      newIndex = 0;
    } else if (e.keyCode == SWT.END) {
      newIndex = m_properties.size() - 1;
    } else if (e.keyCode == SWT.PAGE_UP) {
      newIndex = Math.max(index - m_page + 1, 0);
    } else if (e.keyCode == SWT.PAGE_DOWN) {
      newIndex = Math.min(index + m_page - 1, m_properties.size() - 1);
    } else if (e.keyCode == SWT.ARROW_UP) {
      newIndex = Math.max(index - 1, 0);
    } else if (e.keyCode == SWT.ARROW_DOWN) {
      newIndex = Math.min(index + 1, m_properties.size() - 1);
    }
    // activate new property
    if (newIndex != index && newIndex < m_properties.size()) {
      setActivePropertyInfo(m_properties.get(newIndex));
      // check for scrolling
      int y = m_rowHeight * (newIndex - m_selection);
      if (y < 0) {
        m_selection = newIndex;
        configureScrolling();
      } else if (y + m_rowHeight > clientArea.height) {
        m_selection = newIndex - m_page + 1;
        configureScrolling();
      }
      // repaint
      redraw();
      return true;
    }
    // no navigation change
    return false;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Events: mouse
  //
  ////////////////////////////////////////////////////////////////////////////
  private boolean m_splitterResizing;
  /**
   * We do expand/collapse on to events: click on state sign and on double click. But when we double
   * click on state sign, we will have <em>two</em> events, so we should ignore double click.
   */
  private long m_lastExpandCollapseTime;

  /**
   * Handles {@link SWT#MouseDown} event.
   */
  private void handleMouseDown(MouseEvent event) {
    m_splitterResizing = event.button == 1 && m_properties != null && isLocationSplitter(event.x);
    // click in property
    if (!m_splitterResizing && m_properties != null) {
      int propertyIndex = getPropertyIndex(event.y);
      if (propertyIndex >= m_properties.size()) {
        return;
      }
      // prepare property
      setActivePropertyInfo(m_properties.get(propertyIndex));
      Property property = m_activePropertyInfo.getProperty();
      // de-activate current editor
      deactivateEditor(true);
      redraw();
      // activate editor
      if (isLocationValue(event.x)) {
        activateEditor(property, getValueRelativeLocation(event.x, event.y));
      }
    }
  }

  /**
   * Handles {@link SWT#MouseUp} event.
   */
  private void handleMouseUp(MouseEvent event) {
    if (event.button == 1) {
      // resize splitter
      if (m_splitterResizing) {
        m_splitterResizing = false;
        return;
      }
      // if out of bounds, then ignore
      if (!getClientArea().contains(event.x, event.y)) {
        return;
      }
      // update
      if (m_properties != null) {
        int index = getPropertyIndex(event.y);
        if (index < m_properties.size()) {
          PropertyInfo propertyInfo = m_properties.get(index);
          // check for expand/collapse
          if (isLocationState(propertyInfo, event.x)) {
            try {
              m_lastExpandCollapseTime = System.currentTimeMillis();
              propertyInfo.flip();
              configureScrolling();
            } catch (Throwable e) {
              DesignerPlugin.log(e);
            }
          }
        }
      }
    }
  }

  /**
   * Handles {@link SWT#MouseDoubleClick} event.
   */
  private void handleMouseDoubleClick(MouseEvent event) {
    if (System.currentTimeMillis() - m_lastExpandCollapseTime > getDisplay().getDoubleClickTime()) {
      try {
        if (m_activePropertyInfo != null) {
          if (m_activePropertyInfo.isComplex()) {
            m_activePropertyInfo.flip();
            configureScrolling();
          } else {
            Property property = m_activePropertyInfo.getProperty();
            property.getEditor().doubleClick(property, getValueRelativeLocation(event.x, event.y));
          }
        }
      } catch (Throwable e) {
        handleException(e);
      }
    }
  }

  /**
   * Handles {@link SWT#MouseMove} event.
   */
  private void handleMouseMove(MouseEvent event) {
    int x = event.x;
    // resize splitter
    if (m_splitterResizing) {
      m_splitter = x;
      configureSplitter();
      redraw();
      return;
    }
    // if out of bounds, then ignore
    if (!getClientArea().contains(event.x, event.y)) {
      return;
    }
    // update
    if (m_properties != null) {
      // update cursor
      if (isLocationSplitter(x)) {
        setCursor(ICursorConstants.SIZEWE);
      } else {
        setCursor(null);
      }
      // update tooltip helper
      updateTooltip(event);
    } else {
      updateTooltipNoProperty();
    }
  }

  /**
   * Updates {@link PropertyTableTooltipHelper}.
   */
  private void updateTooltip(MouseEvent event) {
    int x = event.x;
    int propertyIndex = getPropertyIndex(event.y);
    //
    if (propertyIndex < m_properties.size()) {
      PropertyInfo propertyInfo = m_properties.get(propertyIndex);
      Property property = propertyInfo.getProperty();
      int y = (propertyIndex - m_selection) * m_rowHeight;
      // check for title
      {
        int titleX = getTitleTextX(propertyInfo);
        int titleRight = m_splitter - 2;
        if (titleX <= x && x < titleRight) {
          m_tooltipHelper.update(property, true, false, titleX, titleRight, y, m_rowHeight);
          return;
        }
      }
      // check for value
      {
        int valueX = m_splitter + 3;
        if (x > valueX) {
          m_tooltipHelper.update(
              property,
              false,
              true,
              valueX,
              getClientArea().width,
              y,
              m_rowHeight);
        }
      }
    } else {
      updateTooltipNoProperty();
    }
  }

  private void updateTooltipNoProperty() {
    m_tooltipHelper.update(null, false, false, 0, 0, 0, 0);
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Editor
  //
  ////////////////////////////////////////////////////////////////////////////
  private PropertyInfo m_activePropertyInfo;
  private String m_activePropertyId;
  private PropertyEditor m_activeEditor;

  /**
   * Tries to activate editor for {@link PropertyInfo} under cursor.
   *
   * @param location
   *          the mouse location, if editor is activated using mouse click, or <code>null</code> if
   *          it is activated using keyboard.
   */
  public void activateEditor(Property property, Point location) {
    try {
      // de-activate old editor
      deactivateEditor(true);
      // activate editor
      PropertyEditor editor = property.getEditor();
      try {
        if (editor.activate(this, property, location)) {
          m_activeEditor = editor;
        }
      } catch (Throwable e) {
        handleException(e);
      }
      // set bounds
      setActiveEditorBounds();
    } catch (NullPointerException e) {
        if (EnvironmentUtils.IS_MAC) {
            // Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=388574
            PropertyEditor editor = property.getEditor();
            PropertyEditorPresentation presentation = editor.getPresentation();
            if (presentation instanceof ButtonPropertyEditorPresentation) {
                ButtonPropertyEditorPresentation button =
                        (ButtonPropertyEditorPresentation) presentation;
                try {
                    button.click(this, property);
                } catch (Exception ex) {
                    deactivateEditor(false);
                    handleException(e);
                }
                return;
            }
        }
        DesignerPlugin.log(e);
    } catch (Throwable e) {
      DesignerPlugin.log(e);
    }
  }

  /**
   * Deactivates current {@link PropertyEditor}.
   */
  public void deactivateEditor(boolean save) {
    if (m_activeEditor != null) {
      PropertyEditor activeEditor = m_activeEditor;
      m_activeEditor = null;
      if (m_activePropertyInfo != null && m_activePropertyInfo.m_property != null) {
        activeEditor.deactivate(this, m_activePropertyInfo.m_property, save);
      }
    }
  }

  /**
   * Sets correct bounds for active editor, for example we need update bounds after scrolling.
   */
  private void setActiveEditorBounds() {
    if (m_activeEditor != null) {
      int index = m_properties.indexOf(m_activePropertyInfo);
      if (index == -1) {
        // it is possible that active property was hidden because its parent was collapsed
        deactivateEditor(true);
      } else {
        // prepare bounds for editor
        Rectangle bounds;
        {
          Rectangle clientArea = getClientArea();
          int x = m_splitter + 1;
          int width = clientArea.width - x - MARGIN_RIGHT;
          int y = m_rowHeight * (index - m_selection) + 1;
          int height = m_rowHeight - 1;
          bounds = new Rectangle(x, y, width, height);
        }
        // update bounds using presentation
        {
          PropertyEditorPresentation presentation = m_activeEditor.getPresentation();
          if (presentation != null) {
            int presentationWidth =
                presentation.show(
                    this,
                    m_activePropertyInfo.m_property,
                    bounds.x,
                    bounds.y,
                    bounds.width,
                    bounds.height);
            bounds.width -= presentationWidth;
          }
        }
        // set editor bounds
        m_activeEditor.setBounds(bounds);
      }
    }
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Exceptions
  //
  ////////////////////////////////////////////////////////////////////////////
  private IPropertyExceptionHandler m_exceptionHandler;

  /**
   * Sets {@link IPropertyExceptionHandler} for handling all exceptions.
   */
  public void setExceptionHandler(IPropertyExceptionHandler exceptionHandler) {
    m_exceptionHandler = exceptionHandler;
  }

  /**
   * Handles given {@link Throwable}.<br>
   * Right now it just logs it, but in future we can open some dialog here.
   */
  public void handleException(Throwable e) {
    m_exceptionHandler.handle(e);
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Scrolling
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Configures vertical {@link ScrollBar}.
   */
  private void configureScrolling() {
    ScrollBar verticalBar = getVerticalBar();
    if (m_properties == null) {
      verticalBar.setEnabled(false);
    } else {
      m_page = getClientArea().height / m_rowHeight;
      m_selection = Math.max(0, Math.min(m_properties.size() - m_page, m_selection));
      verticalBar.setValues(m_selection, 0, m_properties.size(), m_page, 1, m_page);
      // enable/disable scrolling
      if (m_properties.size() <= m_page) {
        verticalBar.setEnabled(false);
      } else {
        verticalBar.setEnabled(true);
      }
    }
    // redraw, we reconfigure scrolling only if list of properties was changed, so we should redraw
    redraw();
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Location/size utils
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * @return the <code>X</code> position for first pixel of {@link PropertyInfo} title (location of
   *         state image).
   */
  private int getTitleX(PropertyInfo propertyInfo) {
    return MARGIN_LEFT + getLevelIndent() * propertyInfo.getLevel();
  }

  /**
   * @return the <code>X</code> position for first pixel of {@link PropertyInfo} title text.
   */
  private int getTitleTextX(PropertyInfo propertyInfo) {
    return getTitleX(propertyInfo) + getLevelIndent();
  }

  /**
   * @return the indentation for single level.
   */
  private int getLevelIndent() {
    return m_stateWidth + STATE_IMAGE_MARGIN_RIGHT;
  }

  /**
   * Checks horizontal splitter value to boundary values.
   */
  private void configureSplitter() {
    Rectangle clientArea = getClientArea();
    // check title width
    if (m_splitter < MIN_COLUMN_WIDTH) {
      m_splitter = MIN_COLUMN_WIDTH;
    }
    // check value width
    if (clientArea.width - m_splitter < MIN_COLUMN_WIDTH) {
      m_splitter = clientArea.width - MIN_COLUMN_WIDTH;
    }
  }

  /**
   * @return the index in {@link #m_properties} corresponding given <code>y</code> location.
   */
  private int getPropertyIndex(int y) {
    return m_selection + y / m_rowHeight;
  }

  /**
   * @return <code>true</code> if given <code>x</code> coordinate is on state (plus/minus) image.
   */
  private boolean isLocationState(PropertyInfo propertyInfo, int x) {
    int levelX = getTitleX(propertyInfo);
    return propertyInfo.isComplex() && levelX <= x && x <= levelX + m_stateWidth;
  }

  /**
   * Returns <code>true</code> if <code>x</code> coordinate is on splitter.
   */
  private boolean isLocationSplitter(int x) {
    return Math.abs(m_splitter - x) < 2;
  }

  /**
   * @return <code>true</code> if given <code>x</code> is on value part of property.
   */
  private boolean isLocationValue(int x) {
    return x > m_splitter + 2;
  }

  /**
   * @param x
   *          the {@link PropertyTable} relative coordinate.
   * @param y
   *          the {@link PropertyTable} relative coordinate.
   *
   * @return the location relative to the value part of property.
   */
  private Point getValueRelativeLocation(int x, int y) {
    return new Point(x - (m_splitter + 2), y - m_rowHeight * getPropertyIndex(y));
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Access
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Shows or hides {@link Property}-s with {@link PropertyCategory#ADVANCED}.
   */
  public void setShowAdvancedProperties(boolean showAdvancedProperties) {
    m_showAdvancedProperties = showAdvancedProperties;
    setInput0();
  }

  /**
   * Sets the array of {@link Property}'s to display/edit.
   */
  public void setInput(Property[] properties) {
    m_rawProperties = properties;
    setInput0();
  }

  private void setInput0() {
    // send "hide" to all PropertyEditorPresentation's
    if (m_properties != null) {
      for (PropertyInfo propertyInfo : m_properties) {
        Property property = propertyInfo.getProperty();
        // hide presentation
        {
          PropertyEditorPresentation presentation = property.getEditor().getPresentation();
          if (presentation != null) {
            presentation.hide(this, property);
          }
        }
      }
    }
    // set new properties
    if (m_rawProperties == null || m_rawProperties.length == 0) {
      deactivateEditor(false);
      m_properties = Lists.newArrayList();
    } else {
      try {
        // add PropertyInfo for each Property
        m_properties = Lists.newArrayList();
        for (Property property : m_rawProperties) {
          if (rawProperties_shouldShow(property)) {
            PropertyInfo propertyInfo = new PropertyInfo(property);
            m_properties.add(propertyInfo);
          }
        }
        // expand properties using history
        while (true) {
          boolean expanded = false;
          List<PropertyInfo> currentProperties = Lists.newArrayList(m_properties);
          for (PropertyInfo propertyInfo : currentProperties) {
            expanded |= propertyInfo.expandFromHistory();
          }
          // stop
          if (!expanded) {
            break;
          }
        }
      } catch (Throwable e) {
        DesignerPlugin.log(e);
      }
    }
    // update active property
    if (m_activePropertyId != null) {
      PropertyInfo newActivePropertyInfo = null;
      // try to find corresponding PropertyInfo
      if (m_properties != null) {
        for (PropertyInfo propertyInfo : m_properties) {
          if (propertyInfo.m_id.equals(m_activePropertyId)) {
            newActivePropertyInfo = propertyInfo;
            break;
          }
        }
      }
      // set new PropertyInfo
      setActivePropertyInfo(newActivePropertyInfo);
    }
    // update scroll bar
    configureScrolling();
  }

  /**
   * @return <code>true</code> if given {@link Property} should be displayed.
   */
  private boolean rawProperties_shouldShow(Property property) throws Exception {
    PropertyCategory category = getCategory(property);
    // filter out hidden properties
    if (category.isHidden()) {
      return false;
    }
    // filter out advanced properties
    if (category.isAdvanced()) {
      if (!m_showAdvancedProperties && !property.isModified()) {
        return false;
      }
    }
    if (category.isAdvancedReally()) {
      return m_showAdvancedProperties;
    }
    // OK
    return true;
  }

  /**
   * Activates given {@link Property}.
   */
  public void setActiveProperty(Property property) {
    for (PropertyInfo propertyInfo : m_properties) {
      if (propertyInfo.m_property == property) {
        setActivePropertyInfo(propertyInfo);
        break;
      }
    }
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Access: only for testing
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * @return the count of properties in "expanded" list.
   */
  public int forTests_getPropertiesCount() {
    return m_properties.size();
  }

  /**
   * @return the {@link Property} from "expanded" list.
   */
  public Property forTests_getProperty(int index) {
    return m_properties.get(index).getProperty();
  }

  /**
   * Expands the {@link PropertyInfo} with given index.
   */
  public void forTests_expand(int index) throws Exception {
    m_properties.get(index).expand();
  }

  /**
   * @return the position of splitter.
   */
  public int forTests_getSplitter() {
    return m_splitter;
  }

  /**
   * @return the location of state image (plus/minus) for given {@link Property}.
   */
  public Point forTests_getStateLocation(Property property) {
    PropertyInfo propertyInfo = getPropertyInfo(property);
    if (propertyInfo != null) {
      int index = m_properties.indexOf(propertyInfo);
      int x = getTitleX(propertyInfo);
      int y = m_rowHeight * (index - m_selection) + 1;
      return new Point(x, y);
    }
    return null;
  }

  /**
   * @return the location of state image (plus/minus) for given {@link Property}.
   */
  public Point forTests_getValueLocation(Property property) {
    PropertyInfo propertyInfo = getPropertyInfo(property);
    if (propertyInfo != null) {
      int index = m_properties.indexOf(propertyInfo);
      int x = m_splitter + 5;
      int y = m_rowHeight * (index - m_selection) + 1;
      return new Point(x, y);
    }
    return null;
  }

  /**
   * @return the active {@link PropertyEditor}.
   */
  public PropertyEditor forTests_getActiveEditor() {
    return m_activeEditor;
  }

  /**
   * @return the {@link PropertyCategory} that is used by this {@link PropertyTable} to display.
   */
  public PropertyCategory forTests_getCategory(Property property) {
    return getCategory(property);
  }

  /**
   * @return the {@link PropertyInfo}for given {@link Property}.
   */
  private PropertyInfo getPropertyInfo(Property property) {
    for (PropertyInfo propertyInfo : m_properties) {
      if (propertyInfo.getProperty() == property) {
        return propertyInfo;
      }
    }
    return null;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // ISelectionProvider
  //
  ////////////////////////////////////////////////////////////////////////////
  private final List<ISelectionChangedListener> m_selectionListeners = Lists.newArrayList();

  @Override
public void addSelectionChangedListener(ISelectionChangedListener listener) {
    if (!m_selectionListeners.contains(listener)) {
      m_selectionListeners.add(listener);
    }
  }

  @Override
public void removeSelectionChangedListener(ISelectionChangedListener listener) {
    m_selectionListeners.add(listener);
  }

  @Override
public ISelection getSelection() {
    if (m_activePropertyInfo != null) {
      return new StructuredSelection(m_activePropertyInfo.getProperty());
    } else {
      return StructuredSelection.EMPTY;
    }
  }

  @Override
  public void setSelection(ISelection selection) {
    throw new UnsupportedOperationException();
  }

  /**
   * Sets the new active {@link PropertyInfo} and sends event to {@link ISelectionChangedListener}
   * 's.
   */
  private void setActivePropertyInfo(PropertyInfo activePropertyInfo) {
    m_activePropertyInfo = activePropertyInfo;
    // update m_activePropertyId only when really select property,
    // not just remove selection because there are no corresponding property for old active
    // so, later for some other component, if we don't select other property, old active will be selected
    if (m_activePropertyInfo != null) {
      m_activePropertyId = m_activePropertyInfo.m_id;
    }
    // make sure that active property is visible
    if (m_activePropertyInfo != null) {
      int row = m_properties.indexOf(m_activePropertyInfo);
      if (m_selection <= row && row < m_selection + m_page) {
      } else {
        m_selection = row;
        configureScrolling();
      }
    }
    // send events
    SelectionChangedEvent selectionEvent = new SelectionChangedEvent(this, getSelection());
    for (ISelectionChangedListener listener : m_selectionListeners) {
      listener.selectionChanged(selectionEvent);
    }
    // re-draw
    redraw();
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Painting
  //
  ////////////////////////////////////////////////////////////////////////////
  private boolean m_painting;
  private Font m_baseFont;
  private Font m_boldFont;
  private Font m_italicFont;

  /**
   * Handles {@link SWT#Paint} event.
   */
  private void handlePaint(GC gc, int x, int y, int width, int height) {
    // sometimes we disable Eclipse Shell to prevent user actions, but we do this for short time
    if (!isEnabled()) {
      return;
    }
    // prevent recursion
    if (m_painting) {
      return;
    }
    m_painting = true;
    //
    try {
      setActiveEditorBounds();
      // prepare buffered image
      if (m_bufferedImage == null || m_bufferedImage.isDisposed()) {
        Point size = getSize();
        m_bufferedImage = new Image(DesignerPlugin.getStandardDisplay(), size.x, size.y);
      }
      // prepare buffered GC
      GC bufferedGC = null;
      try {
        // perform some drawing
        {
          bufferedGC = new GC(m_bufferedImage);
          bufferedGC.setClipping(x, y, width, height);
          bufferedGC.setBackground(gc.getBackground());
          bufferedGC.setForeground(gc.getForeground());
          bufferedGC.setFont(gc.getFont());
          bufferedGC.setLineStyle(gc.getLineStyle());
          bufferedGC.setLineWidth(gc.getLineWidth());
        }
        // fill client area
        {
          Rectangle clientArea = getClientArea();
          bufferedGC.setBackground(COLOR_BACKGROUND);
          bufferedGC.fillRectangle(clientArea);
        }
        // draw content
        if (m_properties == null || m_properties.size() == 0) {
          drawEmptyContent(bufferedGC);
        } else {
          drawContent(bufferedGC);
        }
      } finally {
        // flush image
        if (bufferedGC != null) {
          bufferedGC.dispose();
        }
      }
      gc.drawImage(m_bufferedImage, 0, 0);
    } finally {
      m_painting = false;
    }
  }

  /**
   * Draws content when there are no properties.
   */
  private void drawEmptyContent(GC gc) {
    Rectangle area = getClientArea();
    // draw message
    gc.setForeground(COLOR_NO_PROPERTIES);
    DrawUtils.drawStringCHCV(
        gc,
        "<No properties>",
        0,
        0,
        area.width,
        area.height);
  }

  /**
   * Draws all {@link PropertyInfo}'s, separators, etc.
   */
  private void drawContent(GC gc) {
    Rectangle clientArea = getClientArea();
    // prepare fonts
    m_baseFont = gc.getFont();
    m_boldFont = DrawUtils.getBoldFont(m_baseFont);
    m_italicFont = DrawUtils.getItalicFont(m_baseFont);
    // show presentations
    int[] presentationsWidth = showPresentations(clientArea);
    // draw properties
    {
      int y = clientArea.y - m_rowHeight * m_selection;
      for (int i = 0; i < m_properties.size(); i++) {
        // skip, if not visible yet
        if (y + m_rowHeight < 0) {
          y += m_rowHeight;
          continue;
        }
        // stop, if already invisible
        if (y > clientArea.height) {
          break;
        }
        // draw single property
        {
          PropertyInfo propertyInfo = m_properties.get(i);
          drawProperty(gc, propertyInfo, y + 1, m_rowHeight - 1, clientArea.width
              - presentationsWidth[i]);
          y += m_rowHeight;
        }
        // draw row separator
        gc.setForeground(COLOR_LINE);
        gc.drawLine(0, y, clientArea.width, y);
      }
    }
    // draw expand line
    drawExpandLines(gc, clientArea);
    // draw rectangle around table
    gc.setForeground(COLOR_LINE);
    gc.drawRectangle(0, 0, clientArea.width - 1, clientArea.height - 1);
    // draw splitter
    gc.setForeground(COLOR_LINE);
    gc.drawLine(m_splitter, 0, m_splitter, clientArea.height);
    // dispose font
    m_boldFont.dispose();
    m_italicFont.dispose();
  }

  /**
   * Shows {@link PropertyEditorPresentation}'s for all {@link Property}'s, i.e. updates also their
   * bounds. So, some {@link PropertyEditorPresentation}'s become invisible because they are moved
   * above or below visible client area.
   *
   * @return the array of width for each {@link PropertyEditorPresentation}'s, consumed on the
   *         right.
   */
  private int[] showPresentations(Rectangle clientArea) {
    int[] presentationsWidth = new int[m_properties.size()];
    // prepare value rectangle
    int x = m_splitter + 4;
    int w = clientArea.width - x - MARGIN_RIGHT;
    // show presentation's for all properties
    int y = clientArea.y - m_rowHeight * m_selection;
    for (int i = 0; i < m_properties.size(); i++) {
      PropertyInfo propertyInfo = m_properties.get(i);
      Property property = propertyInfo.getProperty();
      PropertyEditorPresentation presentation = property.getEditor().getPresentation();
      if (presentation != null) {
        presentationsWidth[i] = presentation.show(this, property, x, y + 1, w, m_rowHeight - 1);
      }
      y += m_rowHeight;
    }
    return presentationsWidth;
  }

  /**
   * Draws lines from expanded complex property to its last sub-property.
   */
  private void drawExpandLines(GC gc, Rectangle clientArea) {
    int height = m_rowHeight - 1;
    int xOffset = m_plusImage.getBounds().width / 2;
    int yOffset = (height - m_plusImage.getBounds().width) / 2;
    //
    int y = clientArea.y - m_selection * m_rowHeight;
    gc.setForeground(COLOR_COMPLEX_LINE);
    for (int i = 0; i < m_properties.size(); i++) {
      PropertyInfo propertyInfo = m_properties.get(i);
      //
      if (propertyInfo.isExpanded()) {
        int index = m_properties.indexOf(propertyInfo);
        // prepare index of last sub-property
        int index2 = index;
        for (; index2 < m_properties.size(); index2++) {
          PropertyInfo nextPropertyInfo = m_properties.get(index2);
          if (nextPropertyInfo != propertyInfo
              && nextPropertyInfo.getLevel() <= propertyInfo.getLevel()) {
            break;
          }
        }
        index2--;
        // draw line if there are children
        if (index2 > index) {
          int x = getTitleX(propertyInfo) + xOffset;
          int y1 = y + height - yOffset;
          int y2 = y + m_rowHeight * (index2 - index) + m_rowHeight / 2;
          gc.drawLine(x, y1, x, y2);
          gc.drawLine(x, y2, x + m_rowHeight / 3, y2);
        }
      }
      //
      y += m_rowHeight;
    }
  }

  /**
   * Draws single {@link PropertyInfo} in specified rectangle.
   */
  private void drawProperty(GC gc, PropertyInfo propertyInfo, int y, int height, int width) {
    // remember colors
    Color oldBackground = gc.getBackground();
    Color oldForeground = gc.getForeground();
    // draw property
    try {
      Property property = propertyInfo.getProperty();
      boolean isActiveProperty =
          m_activePropertyInfo != null && m_activePropertyInfo.getProperty() == property;
      // set background
    boolean modified = property.isModified();
    {
        if (isActiveProperty) {
          gc.setBackground(COLOR_PROPERTY_BG_SELECTED);
        } else {
          if (modified) {
            gc.setBackground(COLOR_PROPERTY_BG_MODIFIED);
          } else {
            gc.setBackground(COLOR_PROPERTY_BG);
          }
        }
        gc.fillRectangle(0, y, width, height);
      }
      // draw state image
      if (propertyInfo.isShowComplex()) {
        Image stateImage = propertyInfo.isExpanded() ? m_minusImage : m_plusImage;
        DrawUtils.drawImageCV(gc, stateImage, getTitleX(propertyInfo), y, height);
      }
      // draw title
      {
        // configure GC
        {
          gc.setForeground(COLOR_PROPERTY_FG_TITLE);
          // check category
          if (getCategory(property).isAdvanced()) {
            gc.setForeground(COLOR_PROPERTY_FG_ADVANCED);
            gc.setFont(m_italicFont);
          } else if (getCategory(property).isPreferred() || getCategory(property).isSystem()) {
            gc.setFont(m_boldFont);
          }
          // check for active
          if (isActiveProperty) {
            gc.setForeground(COLOR_PROPERTY_FG_SELECTED);
          }
        }
        // paint title
        int x = getTitleTextX(propertyInfo);
        DrawUtils.drawStringCV(gc, property.getTitle(), x, y, m_splitter - x, height);
      }
      // draw value
      {
        // configure GC
        gc.setFont(m_baseFont);
        if (!isActiveProperty) {
          gc.setForeground(COLOR_PROPERTY_FG_VALUE);
        }
        // prepare value rectangle
        int x = m_splitter + 4;
        int w = width - x - MARGIN_RIGHT;
        // paint value

        // BEGIN ADT MODIFICATIONS
        if (!modified) {
            gc.setForeground(COLOR_PROPERTY_FG_DEFAULT);
        }
        // END ADT MODIFICATIONS

        property.getEditor().paint(property, gc, x, y, w, height);
      }
    } catch (Throwable e) {
      DesignerPlugin.log(e);
    } finally {
      // restore colors
      gc.setBackground(oldBackground);
      gc.setForeground(oldForeground);
    }
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // PropertyCategory
  //
  ////////////////////////////////////////////////////////////////////////////
  private PropertyCategoryProvider m_propertyCategoryProvider =
      PropertyCategoryProviders.fromProperty();

  /**
   * Sets the {@link PropertyCategoryProvider} that can be used to tweak usual
   * {@link PropertyCategory}.
   */
  public void setPropertyCategoryProvider(PropertyCategoryProvider propertyCategoryProvider) {
    m_propertyCategoryProvider = propertyCategoryProvider;
  }

  /**
   * @return the {@link PropertyCategory} that is used by this {@link PropertyTable} to display.
   */
  private PropertyCategory getCategory(Property property) {
    return m_propertyCategoryProvider.getCategory(property);
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // PropertyInfo
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Class with information about single {@link Property}.
   *
   * @author scheglov_ke
   */
  private final class PropertyInfo {
    private final String m_id;
    private final int m_level;
    private final Property m_property;
    private final boolean m_stateComplex;
    private boolean m_stateExpanded;
    private List<PropertyInfo> m_children;

    ////////////////////////////////////////////////////////////////////////////
    //
    // Constructor
    //
    ////////////////////////////////////////////////////////////////////////////
    public PropertyInfo(Property property) {
      this(property, "", 0);
    }

    private PropertyInfo(Property property, String idPrefix, int level) {
      // BEGIN ADT MODIFICATIONS
      //m_id = idPrefix + "|" + property.getTitle();
      m_id = idPrefix + "|" + property.getName();
      // END ADT MODIFICATIONS
      m_level = level;
      m_property = property;
      m_stateComplex = property.getEditor() instanceof IComplexPropertyEditor;
    }

    ////////////////////////////////////////////////////////////////////////////
    //
    // State
    //
    ////////////////////////////////////////////////////////////////////////////
    /**
     * @return <code>true</code> if this property is complex.
     */
    public boolean isComplex() {
      return m_stateComplex;
    }

    public boolean isShowComplex() throws Exception {
      if (m_stateComplex) {
        prepareChildren();
        return m_children != null && !m_children.isEmpty();
      }
      return false;
    }

    /**
     * @return <code>true</code> if this complex property is expanded.
     */
    public boolean isExpanded() {
      return m_stateExpanded;
    }

    ////////////////////////////////////////////////////////////////////////////
    //
    // Access
    //
    ////////////////////////////////////////////////////////////////////////////
    /**
     * @return the level of this property, i.e. on which level of complex property it is located.
     */
    public int getLevel() {
      return m_level;
    }

    /**
     * @return the {@link Property}.
     */
    public Property getProperty() {
      return m_property;
    }

    /**
     * Flips collapsed/expanded state and adds/removes sub-properties.
     */
    public void flip() throws Exception {
      Assert.isTrue(m_stateComplex);
      if (m_stateExpanded) {
        collapse();
      } else {
        expand();
      }
    }

    /**
     * Expands this property.
     */
    public void expand() throws Exception {
      Assert.isTrue(m_stateComplex);
      Assert.isTrue(!m_stateExpanded);
      //
      m_stateExpanded = true;
      m_expandedIds.add(m_id);
      // BEGIN ADT MODIFICATIONS
      if (m_collapsedIds != null) {
          m_collapsedIds.remove(m_id);
      }
      // END ADT MODIFICATIONS
      prepareChildren();
      //
      int index = m_properties.indexOf(this);
      addChildren(index + 1);
    }

    /**
     * Collapses this property.
     */
    public void collapse() throws Exception {
      Assert.isTrue(m_stateComplex);
      Assert.isTrue(m_stateExpanded);
      //
      m_stateExpanded = false;
      m_expandedIds.remove(m_id);
      // BEGIN ADT MODIFICATIONS
      if (m_collapsedIds != null) {
          m_collapsedIds.add(m_id);
      }
      // END ADT MODIFICATIONS
      prepareChildren();
      //
      int index = m_properties.indexOf(this);
      removeChildren(index + 1);
    }

    ////////////////////////////////////////////////////////////////////////////
    //
    // Internal
    //
    ////////////////////////////////////////////////////////////////////////////
    /**
     * Adds children properties.
     *
     * @return the index for new properties to add.
     */
    private int addChildren(int index) throws Exception {
      prepareChildren();
      for (PropertyInfo child : m_children) {
        // skip if should not display raw Property
        if (!rawProperties_shouldShow(child.m_property)) {
          continue;
        }
        // add child
        m_properties.add(index++, child);
        // add children of current child
        if (child.isExpanded()) {
          index = child.addChildren(index);
        }
      }
      return index;
    }

    /**
     * Removes children properties.
     */
    private void removeChildren(int index) throws Exception {
      prepareChildren();
      for (PropertyInfo child : m_children) {
        // skip if should not display raw Property
        if (!rawProperties_shouldShow(child.m_property)) {
          continue;
        }
        // hide presentation
        {
          PropertyEditorPresentation presentation =
              child.getProperty().getEditor().getPresentation();
          if (presentation != null) {
            presentation.hide(PropertyTable.this, child.getProperty());
          }
        }
        // remove child
        m_properties.remove(index);
        // remove children of current child
        if (child.isExpanded()) {
          child.removeChildren(index);
        }
      }
    }

    /**
     * Prepares children {@link PropertyInfo}'s, for sub-properties.
     */
    private void prepareChildren() throws Exception {
      if (m_children == null) {
        m_children = Lists.newArrayList();
        for (Property subProperty : getSubProperties()) {
          PropertyInfo subPropertyInfo = createSubPropertyInfo(subProperty);
          m_children.add(subPropertyInfo);
        }
      }
    }

    private PropertyInfo createSubPropertyInfo(Property subProperty) {
      return new PropertyInfo(subProperty, m_id, m_level + 1);
    }

    private Property[] getSubProperties() throws Exception {
      IComplexPropertyEditor complexEditor = (IComplexPropertyEditor) m_property.getEditor();
      List<Property> subProperties = Lists.newArrayList();
      for (Property subProperty : complexEditor.getProperties(m_property)) {
        if (getCategory(subProperty).isHidden() && !subProperty.isModified()) {
          // skip hidden properties
          continue;
        }
        subProperties.add(subProperty);
      }
      return subProperties.toArray(new Property[subProperties.size()]);
    }

    ////////////////////////////////////////////////////////////////////////////
    //
    // Persistent expanding support
    //
    ////////////////////////////////////////////////////////////////////////////
    /**
     * @return <code>true</code> if this {@link PropertyInfo} was expanded from history.
     */
    public boolean expandFromHistory() throws Exception {
      if (isComplex() && !isExpanded() && m_expandedIds.contains(m_id)) {
        expand();
        return true;
      }
      // BEGIN ADT MODIFICATIONS
      if (m_collapsedIds != null && isComplex() && !isExpanded()
              && !m_collapsedIds.contains(m_id)) {
          expand();
          return true;
      }
      // END ADT MODIFICATIONS
      return false;
    }
  }

  // BEGIN ADT MODIFICATIONS
  /** Collapse all top-level properties */
  public void collapseAll() {
      try {
          m_lastExpandCollapseTime = System.currentTimeMillis();
          if (m_collapsedIds != null) {
              m_collapsedIds.addAll(m_expandedIds);
          }
          m_expandedIds.clear();
          setInput(m_rawProperties);
          redraw();
      } catch (Throwable e) {
          DesignerPlugin.log(e);
      }
  }

  /** Expand all top-level properties */
  public void expandAll() {
      try {
          m_lastExpandCollapseTime = System.currentTimeMillis();
          if (m_collapsedIds != null) {
              m_collapsedIds.clear();
          }
          m_expandedIds.clear();
          for (PropertyInfo info : m_properties) {
              if (info.m_stateComplex) {
                  m_expandedIds.add(info.m_id);
              }
          }
          setInput(m_rawProperties);
          redraw();
      } catch (Throwable e) {
          DesignerPlugin.log(e);
      }
  }
  // END ADT MODIFICATIONS
}