summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/core/controls/flyout/FlyoutControlComposite.java
blob: 23d2f83afa952191e89efb4657e06ad3eb4a29e9 (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
/*******************************************************************************
 * 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.core.controls.flyout;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.MouseTrackAdapter;
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.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Sash;
import org.eclipse.swt.widgets.Tracker;
import org.eclipse.wb.core.controls.Messages;
import org.eclipse.wb.draw2d.IColorConstants;
import org.eclipse.wb.draw2d.ICursorConstants;
import org.eclipse.wb.internal.core.utils.ui.DrawUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * {@link FlyoutControlComposite} is container for two {@link Control}'s. One (client control) is
 * used to fill client area. Second (flyout control) can be docked to any enabled position or
 * temporary hidden.
 *
 * @author scheglov_ke
 * @coverage core.control
 */
public final class FlyoutControlComposite extends Composite {
  private static final int RESIZE_WIDTH = 5;
  private static final int TITLE_LINES = 30;
  private static final int TITLE_MARGIN = 5;
  private static final Font TITLE_FONT = JFaceResources.getFontRegistry().getBold(
      JFaceResources.DEFAULT_FONT);
  ////////////////////////////////////////////////////////////////////////////
  //
  // Images
  //
  ////////////////////////////////////////////////////////////////////////////
  private static final Image PIN = loadImage("icons/pin.gif");
  private static final Image ARROW_LEFT = loadImage("icons/arrow_left.gif");
  private static final Image ARROW_RIGHT = loadImage("icons/arrow_right.gif");
  private static final Image ARROW_TOP = loadImage("icons/arrow_top.gif");
  private static final Image ARROW_BOTTOM = loadImage("icons/arrow_bottom.gif");

  private static Image loadImage(String path) {
    return DrawUtils.loadImage(FlyoutControlComposite.class, path);
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Instance fields
  //
  ////////////////////////////////////////////////////////////////////////////
  private final IFlyoutPreferences m_preferences;
  private final FlyoutContainer m_flyoutContainer;
  private int m_minWidth = 150;
  private int m_validDockLocations = -1;
  private final List<IFlyoutMenuContributor> m_menuContributors =
      new ArrayList<IFlyoutMenuContributor>();

  ////////////////////////////////////////////////////////////////////////////
  //
  // Constructor
  //
  ////////////////////////////////////////////////////////////////////////////
  public FlyoutControlComposite(Composite parent, int style, IFlyoutPreferences preferences) {
    super(parent, style);
    m_preferences = preferences;
    // add listeners
    addListener(SWT.Resize, new Listener() {
      @Override
    public void handleEvent(Event event) {
        if (getShell().getMinimized()) {
          return;
        }
        layout();
      }
    });
    // create container for flyout control
    m_flyoutContainer = new FlyoutContainer(this, SWT.NO_BACKGROUND);
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Parents
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * @return the parent {@link Composite} for flyout {@link Control}.
   */
  public Composite getFlyoutParent() {
    return m_flyoutContainer;
  }

  /**
   * @return the parent {@link Composite} for client {@link Control}.
   */
  public Composite getClientParent() {
    return this;
  }

  /**
   * Sets the bit set with valid docking locations.
   */
  public void setValidDockLocations(int validDockLocations) {
    m_validDockLocations = validDockLocations;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Access
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Sets the minimal width of flyout.
   */
  public void setMinWidth(int minWidth) {
    m_minWidth = minWidth;
  }

  /**
   * Sets the text of title.
   */
  public void setTitleText(String text) {
    m_flyoutContainer.setTitleText(text);
  }

  /**
   * Adds new {@link IFlyoutMenuContributor}.
   */
  public void addMenuContributor(IFlyoutMenuContributor contributor) {
    if (!m_menuContributors.contains(contributor)) {
      m_menuContributors.add(contributor);
    }
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Layout
  //
  ////////////////////////////////////////////////////////////////////////////
  @Override
  public void layout() {
    Rectangle clientArea = getClientArea();
    int state = m_preferences.getState();
    Control client = getChildren()[1];
    // check, may be "clientArea" is empty, for example because CTabFolder page is not visible
    if (clientArea.width == 0 || clientArea.height == 0) {
      return;
    }
    // check, maybe flyout has no Control, so "client" should fill client area
    if (m_flyoutContainer.getControl() == null
            // BEGIN ADT MODIFICATIONS
            || !m_flyoutContainer.getControl().getVisible()
            // END ADT MODIFICATIONS
            ) {
      m_flyoutContainer.setBounds(0, 0, 0, 0);
      client.setBounds(clientArea);
      return;
    }
    // prepare width to display
    int width;
    int offset;
    if (state == IFlyoutPreferences.STATE_OPEN) {
      width = m_preferences.getWidth();
      // limit maximum value
      if (isHorizontal()) {
        width = Math.min(clientArea.width / 2, width);
      } else {
        width = Math.min(clientArea.height / 2, width);
      }
      // limit minimum value
      width = Math.max(width, m_minWidth);
      width = Math.max(width, 2 * m_flyoutContainer.m_titleHeight + m_flyoutContainer.m_titleWidth);
      // remember actual width
      m_preferences.setWidth(width);
      //
      offset = width;
    } else if (state == IFlyoutPreferences.STATE_EXPANDED) {
      offset = m_flyoutContainer.m_titleHeight;
      width = m_preferences.getWidth();
    } else {
      width = m_flyoutContainer.m_titleHeight;
      offset = width;
    }
    // change bounds for flyout container and client control
    {
      if (isWest()) {
        m_flyoutContainer.setBounds(0, 0, width, clientArea.height);
        client.setBounds(offset, 0, clientArea.width - offset, clientArea.height);
      } else if (isEast()) {
        m_flyoutContainer.setBounds(clientArea.width - width, 0, width, clientArea.height);
        client.setBounds(0, 0, clientArea.width - offset, clientArea.height);
      } else if (isNorth()) {
        m_flyoutContainer.setBounds(0, 0, clientArea.width, width);
        client.setBounds(0, offset, clientArea.width, clientArea.height - offset);
      } else if (isSouth()) {
        m_flyoutContainer.setBounds(0, clientArea.height - width, clientArea.width, width);
        client.setBounds(0, 0, clientArea.width, clientArea.height - offset);
      }
    }
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Internal utils
  //
  ////////////////////////////////////////////////////////////////////////////
  private boolean isHorizontal() {
    return isWest() || isEast();
  }

  private boolean isWest() {
    return getDockLocation() == IFlyoutPreferences.DOCK_WEST;
  }

  private boolean isEast() {
    return getDockLocation() == IFlyoutPreferences.DOCK_EAST;
  }

  private boolean isNorth() {
    return getDockLocation() == IFlyoutPreferences.DOCK_NORTH;
  }

  private boolean isSouth() {
    return getDockLocation() == IFlyoutPreferences.DOCK_SOUTH;
  }

  /**
   * @return <code>true</code> if given docking location is valid.
   */
  private boolean isValidDockLocation(int location) {
    return (location & m_validDockLocations) == location;
  }

  /**
   * @return current docking location.
   */
  private int getDockLocation() {
    return m_preferences.getDockLocation();
  }

  /**
   * Sets new docking location.
   */
  private void setDockLocation(int dockLocation) {
    m_preferences.setDockLocation(dockLocation);
    layout();
  }

  // BEGIN ADT MODIFICATIONS
  /** If the flyout hover is showing, dismiss it */
  public void dismissHover() {
      if (m_flyoutContainer != null) {
          m_flyoutContainer.dismissHover();
      }
  }

  /** Sets a listener to be modified when windows are opened, collapsed and expanded */
  public void setListener(IFlyoutListener listener) {
      assert m_listener == null; // Only one listener supported
      m_listener = listener;
  }
  private IFlyoutListener m_listener;
  // END ADT MODIFICATIONS

  ////////////////////////////////////////////////////////////////////////////
  //
  // FlyoutContainer
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Container for flyout {@link Control}.
   *
   * @author scheglov_ke
   */
  private final class FlyoutContainer extends Composite {
    ////////////////////////////////////////////////////////////////////////////
    //
    // Container
    //
    ////////////////////////////////////////////////////////////////////////////
    public FlyoutContainer(Composite parent, int style) {
      super(parent, style);
      configureMenu();
      updateTitleImage("Flyout");
      // add listeners
      addListener(SWT.Dispose, new Listener() {
        @Override
        public void handleEvent(Event event) {
          if (m_titleImage != null) {
            m_titleImage.dispose();
            m_titleImageRotated.dispose();
            m_titleImage = null;
            m_titleImageRotated = null;
          }
          if (m_backImage != null) {
            m_backImage.dispose();
            m_backImage = null;
          }
        }
      });
      {
        Listener listener = new Listener() {
          @Override
        public void handleEvent(Event event) {
            layout();
          }
        };
        addListener(SWT.Move, listener);
        addListener(SWT.Resize, listener);
      }
      addListener(SWT.Paint, new Listener() {
        @Override
        public void handleEvent(Event event) {
          handlePaint(event.gc);
        }
      });
      // mouse listeners
      addMouseListener(new MouseAdapter() {
        @Override
        public void mouseDown(MouseEvent event) {
          if (event.button == 1) {
            handle_mouseDown(event);
          }
        }

        @Override
        public void mouseUp(MouseEvent event) {
          if (event.button == 1) {
            handle_mouseUp(event);
          }
        }
      });
      addMouseTrackListener(new MouseTrackAdapter() {
        @Override
        public void mouseExit(MouseEvent e) {
          m_stateHover = false;
          redraw();
          setCursor(null);
        }

        @Override
        public void mouseHover(MouseEvent e) {
          handle_mouseHover();
        }
      });
      addMouseMoveListener(new MouseMoveListener() {
        @Override
        public void mouseMove(MouseEvent event) {
          handle_mouseMove(event);
        }
      });
    }

    // BEGIN ADT MODIFICATIONS
    private void dismissHover() {
      int state = m_preferences.getState();
      if (state == IFlyoutPreferences.STATE_EXPANDED) {
        state = IFlyoutPreferences.STATE_COLLAPSED;
        m_preferences.setState(state);
        redraw();
        FlyoutControlComposite.this.layout();
        if (m_listener != null) {
            m_listener.stateChanged(IFlyoutPreferences.STATE_EXPANDED, state);
        }
      }
    }
    // END END MODIFICATIONS

    ////////////////////////////////////////////////////////////////////////////
    //
    // Events: mouse
    //
    ////////////////////////////////////////////////////////////////////////////
    private boolean m_resize;
    private boolean m_stateHover;

    /**
     * Handler for {@link SWT#MouseDown} event.
     */
    private void handle_mouseDown(MouseEvent event) {
      if (m_stateHover) {
        int state = m_preferences.getState();
        // BEGIN ADT MODIFICATIONS
        int oldState = state;
        // END ADT MODIFICATIONS
        if (state == IFlyoutPreferences.STATE_OPEN) {
          state = IFlyoutPreferences.STATE_COLLAPSED;
        } else {
          state = IFlyoutPreferences.STATE_OPEN;
        }
        m_preferences.setState(state);
        redraw();
        FlyoutControlComposite.this.layout();
        // BEGIN ADT MODIFICATIONS
        if (m_listener != null) {
          m_listener.stateChanged(oldState, state);
        }
        // END ADT MODIFICATIONS
      } else if (getCursor() == ICursorConstants.SIZEWE || getCursor() == ICursorConstants.SIZENS) {
        m_resize = true;
      } else if (getCursor() == ICursorConstants.SIZEALL) {
        handleDocking();
      }
    }

    /**
     * Handler for {@link SWT#MouseUp} event.
     */
    private void handle_mouseUp(MouseEvent event) {
      if (m_resize) {
        m_resize = false;
        handle_mouseMove(event);
      }
    }

    /**
     * Handler for {@link SWT#MouseMove} event.
     */
    private void handle_mouseMove(MouseEvent event) {
      final FlyoutControlComposite container = FlyoutControlComposite.this;
      if (m_resize) {
        // prepare width
        int width;
        if (isHorizontal()) {
          width = getSize().x;
        } else {
          width = getSize().y;
        }
        // prepare new width
        int newWidth = width;
        if (isWest()) {
          newWidth = event.x + RESIZE_WIDTH / 2;
        } else if (isEast()) {
          newWidth = width - event.x + RESIZE_WIDTH / 2;
        } else if (isNorth()) {
          newWidth = event.y + RESIZE_WIDTH / 2;
        } else if (isSouth()) {
          newWidth = width - event.y + RESIZE_WIDTH / 2;
        }
        // update width
        if (newWidth != width) {
          m_preferences.setWidth(newWidth);
          redraw();
          container.layout();
        }
      } else {
        Rectangle clientArea = getClientArea();
        boolean inside = clientArea.contains(event.x, event.y);
        int x = event.x;
        int y = event.y;
        if (inside) {
          // check for state
          {
            boolean oldStateHover = m_stateHover;
            if (isEast()) {
              m_stateHover = x > clientArea.width - m_titleHeight && y < m_titleHeight;
            } else {
              m_stateHover = x < m_titleHeight && y < m_titleHeight;
            }
            if (m_stateHover != oldStateHover) {
              redraw();
            }
            if (m_stateHover) {
              setCursor(null);
              return;
            }
          }
          // check for resize band
          if (isOpenExpanded()) {
            if (isWest() && x >= clientArea.width - RESIZE_WIDTH) {
              setCursor(ICursorConstants.SIZEWE);
            } else if (isEast() && x <= RESIZE_WIDTH) {
              setCursor(ICursorConstants.SIZEWE);
            } else if (isNorth() && y >= clientArea.height - RESIZE_WIDTH) {
              setCursor(ICursorConstants.SIZENS);
            } else if (isSouth() && y <= RESIZE_WIDTH) {
              setCursor(ICursorConstants.SIZENS);
            } else {
              setCursor(null);
            }
          }
          // check for docking
          if (getCursor() == null) {
            setCursor(ICursorConstants.SIZEALL);
          }
        } else {
          setCursor(null);
        }
      }
    }

    /**
     * Handler for {@link SWT#MouseHover} event - temporary expands flyout and collapse again when
     * mouse moves above client.
     */
    private void handle_mouseHover() {
      if (m_preferences.getState() == IFlyoutPreferences.STATE_COLLAPSED && !m_stateHover) {
        m_preferences.setState(IFlyoutPreferences.STATE_EXPANDED);
        //
        final FlyoutControlComposite container = FlyoutControlComposite.this;
        container.layout();
        // BEGIN ADT MODIFICATIONS
        if (m_listener != null) {
            m_listener.stateChanged(IFlyoutPreferences.STATE_COLLAPSED,
                    IFlyoutPreferences.STATE_EXPANDED);
        }
        // END ADT MODIFICATIONS
        // add listeners
        Listener listener = new Listener() {
          @Override
        public void handleEvent(Event event) {
            if (event.type == SWT.Dispose) {
              getDisplay().removeFilter(SWT.MouseMove, this);
            } else {
              Point p = ((Control) event.widget).toDisplay(event.x, event.y);
              // during resize mouse can be temporary outside of flyout - ignore
              if (m_resize) {
                return;
              }
              // mouse in in flyout container - ignore
              if (getClientArea().contains(toControl(p.x, p.y))) {
                return;
              }
              // mouse is in full container - collapse
              if (container.getClientArea().contains(container.toControl(p.x, p.y))) {
                getDisplay().removeFilter(SWT.MouseMove, this);
                // it is possible, that user restored (OPEN) flyout, so collapse only if we still in expand state
                if (m_preferences.getState() == IFlyoutPreferences.STATE_EXPANDED) {
                  m_preferences.setState(IFlyoutPreferences.STATE_COLLAPSED);
                  container.layout();
                  // BEGIN ADT MODIFICATIONS
                  if (m_listener != null) {
                      m_listener.stateChanged(IFlyoutPreferences.STATE_EXPANDED,
                              IFlyoutPreferences.STATE_COLLAPSED);
                  }
                  // END ADT MODIFICATIONS
                }
              }
            }
          }
        };
        addListener(SWT.Dispose, listener);
        getDisplay().addFilter(SWT.MouseMove, listener);
      }
    }

    /**
     * Handler for docking.
     */
    private void handleDocking() {
      final FlyoutControlComposite container = FlyoutControlComposite.this;
      final int width = m_preferences.getWidth();
      final int oldDockLocation = getDockLocation();
      final int[] newDockLocation = new int[]{oldDockLocation};
      final Tracker dockingTracker = new Tracker(container, SWT.NONE);
      dockingTracker.setRectangles(new Rectangle[]{getBounds()});
      dockingTracker.setStippled(true);
      dockingTracker.addListener(SWT.Move, new Listener() {
        @Override
        public void handleEvent(Event event2) {
          Rectangle clientArea = container.getClientArea();
          Point location = container.toControl(event2.x, event2.y);
          int h3 = clientArea.height / 3;
          // check locations
          if (location.y < h3 && isValidDockLocation(IFlyoutPreferences.DOCK_NORTH)) {
            dockingTracker.setRectangles(new Rectangle[]{new Rectangle(0,
                0,
                clientArea.width,
                width)});
            newDockLocation[0] = IFlyoutPreferences.DOCK_NORTH;
          } else if (location.y > 2 * h3 && isValidDockLocation(IFlyoutPreferences.DOCK_SOUTH)) {
            dockingTracker.setRectangles(new Rectangle[]{new Rectangle(0,
                clientArea.height - width,
                clientArea.width,
                width)});
            newDockLocation[0] = IFlyoutPreferences.DOCK_SOUTH;
          } else if (location.x < clientArea.width / 2
              && isValidDockLocation(IFlyoutPreferences.DOCK_WEST)) {
            dockingTracker.setRectangles(new Rectangle[]{new Rectangle(0,
                0,
                width,
                clientArea.height)});
            newDockLocation[0] = IFlyoutPreferences.DOCK_WEST;
          } else if (isValidDockLocation(IFlyoutPreferences.DOCK_EAST)) {
            dockingTracker.setRectangles(new Rectangle[]{new Rectangle(clientArea.width - width,
                0,
                width,
                clientArea.height)});
            newDockLocation[0] = IFlyoutPreferences.DOCK_EAST;
          } else {
            dockingTracker.setRectangles(new Rectangle[]{getBounds()});
            newDockLocation[0] = oldDockLocation;
          }
        }
      });
      // start tracking
      if (dockingTracker.open()) {
        setDockLocation(newDockLocation[0]);
      }
      // dispose tracker
      dockingTracker.dispose();
    }

    ////////////////////////////////////////////////////////////////////////////
    //
    // Access
    //
    ////////////////////////////////////////////////////////////////////////////
    /**
     * @return the {@link Control} installed on this {@link FlyoutControlComposite}, or
     *         <code>null</code> if there are no any {@link Control}.
     */
    private Control getControl() {
      Control[] children = getChildren();
      return children.length == 1 ? children[0] : null;
    }

    /**
     * Sets the text of title.
     */
    public void setTitleText(String text) {
      updateTitleImage(text);
    }

    ////////////////////////////////////////////////////////////////////////////
    //
    // Layout
    //
    ////////////////////////////////////////////////////////////////////////////
    @Override
    public void layout() {
      Control control = getControl();
      if (control == null) {
        return;
      }
      // OK, we have control, so can continue layout
      Rectangle clientArea = getClientArea();
      if (isOpenExpanded()) {
        if (isWest()) {
          int y = m_titleHeight;
          control.setBounds(0, y, clientArea.width - RESIZE_WIDTH, clientArea.height - y);
        } else if (isEast()) {
          int y = m_titleHeight;
          control.setBounds(RESIZE_WIDTH, y, clientArea.width - RESIZE_WIDTH, clientArea.height - y);
        } else if (isNorth()) {
          int y = m_titleHeight;
          control.setBounds(0, y, clientArea.width, clientArea.height - y - RESIZE_WIDTH);
        } else if (isSouth()) {
          int y = RESIZE_WIDTH + m_titleHeight;
          control.setBounds(0, y, clientArea.width, clientArea.height - y);
        }
      } else {
        control.setBounds(0, 0, 0, 0);
      }
    }

    ////////////////////////////////////////////////////////////////////////////
    //
    // Paint
    //
    ////////////////////////////////////////////////////////////////////////////
    private Image m_backImage;

    /**
     * Handler for {@link SWT#Paint} event.
     */
    private void handlePaint(GC paintGC) {
      Rectangle clientArea = getClientArea();
      // prepare back image
      GC gc;
      {
        if (m_backImage == null || !m_backImage.getBounds().equals(clientArea)) {
          if (m_backImage != null) {
            m_backImage.dispose();
          }
          m_backImage = new Image(getDisplay(), clientArea.width, clientArea.height);
        }
        // prepare GC
        gc = new GC(m_backImage);
        gc.setBackground(paintGC.getBackground());
        gc.setForeground(paintGC.getForeground());
        gc.fillRectangle(clientArea);
      }
      //
      if (isOpenExpanded()) {
        // draw header
        {
          // draw title
          if (isWest()) {
            drawStateImage(gc, 0, 0);
            gc.drawImage(m_titleImage, m_titleHeight, 0);
          } else if (isEast()) {
            int x = clientArea.width - m_titleHeight;
            drawStateImage(gc, x, 0);
            gc.drawImage(m_titleImage, x - m_titleWidth, 0);
          } else if (isNorth()) {
            drawStateImage(gc, 0, 0);
            gc.drawImage(m_titleImage, m_titleHeight, 0);
          } else if (isSouth()) {
            int y = RESIZE_WIDTH;
            drawStateImage(gc, 0, y);
            gc.drawImage(m_titleImage, m_titleHeight, y);
          }
        }
        // draw resize band
        drawResizeBand(gc);
      } else {
        if (isHorizontal()) {
          drawStateImage(gc, 0, 0);
          gc.drawImage(m_titleImageRotated, 0, m_titleHeight);
        } else {
          drawStateImage(gc, 0, 0);
          gc.drawImage(m_titleImage, m_titleHeight, 0);
        }
        DrawUtils.drawHighlightRectangle(gc, 0, 0, clientArea.width, clientArea.height);
      }
      // flush back image
      {
        gc.dispose();
        paintGC.drawImage(m_backImage, 0, 0);
      }
    }

    /**
     * Draws the state image (arrow) at given location.
     */
    private void drawStateImage(GC gc, int x, int y) {
      DrawUtils.drawImageCHCV(gc, getStateImage(), x, y, m_titleHeight, m_titleHeight);
      if (m_stateHover) {
        DrawUtils.drawHighlightRectangle(gc, x, y, m_titleHeight, m_titleHeight);
      }
    }

    /**
     * @return the {@link Image} corresponding to current state (open or collapsed).
     */
    private Image getStateImage() {
      int location = getDockLocation();
      int state = m_preferences.getState();
      if (state == IFlyoutPreferences.STATE_OPEN) {
        switch (location) {
          case IFlyoutPreferences.DOCK_WEST :
            return ARROW_LEFT;
          case IFlyoutPreferences.DOCK_EAST :
            return ARROW_RIGHT;
          case IFlyoutPreferences.DOCK_NORTH :
            return ARROW_TOP;
          case IFlyoutPreferences.DOCK_SOUTH :
            return ARROW_BOTTOM;
        }
      } else if (state == IFlyoutPreferences.STATE_EXPANDED) {
        return PIN;
      } else {
        switch (location) {
          case IFlyoutPreferences.DOCK_WEST :
            return ARROW_RIGHT;
          case IFlyoutPreferences.DOCK_EAST :
            return ARROW_LEFT;
          case IFlyoutPreferences.DOCK_NORTH :
            return ARROW_BOTTOM;
          case IFlyoutPreferences.DOCK_SOUTH :
            return ARROW_TOP;
        }
      }
      //
      return null;
    }

    /**
     * Draws that resize band, {@link Sash} like.
     */
    private void drawResizeBand(GC gc) {
      Rectangle clientArea = getClientArea();
      // prepare locations
      int x, y, width, height;
      if (isHorizontal()) {
        if (isWest()) {
          x = clientArea.width - RESIZE_WIDTH;
        } else {
          x = 0;
        }
        y = 0;
        width = RESIZE_WIDTH;
        height = clientArea.height;
      } else {
        x = 0;
        if (isNorth()) {
          y = clientArea.height - RESIZE_WIDTH;
        } else {
          y = 0;
        }
        width = clientArea.width;
        height = RESIZE_WIDTH;
      }
      // draw band
      DrawUtils.drawHighlightRectangle(gc, x, y, width, height);
    }

    /**
     * @return <code>true</code> if flyout is open or expanded.
     */
    private boolean isOpenExpanded() {
      int state = m_preferences.getState();
      return state == IFlyoutPreferences.STATE_OPEN || state == IFlyoutPreferences.STATE_EXPANDED;
    }

    ////////////////////////////////////////////////////////////////////////////
    //
    // Title image
    //
    ////////////////////////////////////////////////////////////////////////////
    private int m_titleWidth;
    private int m_titleHeight;
    private Image m_titleImage;
    private Image m_titleImageRotated;

    /**
     * Creates {@link Image} for given title text.
     */
    private void updateTitleImage(String text) {
      // prepare size of text
      Point textSize;
      {
        GC gc = new GC(this);
        gc.setFont(TITLE_FONT);
        textSize = gc.textExtent(text);
        gc.dispose();
      }
      // dispose existing image
      if (m_titleImage != null) {
        m_titleImage.dispose();
        m_titleImageRotated.dispose();
      }
      // prepare new image
      {
        m_titleWidth = textSize.x + 2 * TITLE_LINES + 4 * TITLE_MARGIN;
        m_titleHeight = textSize.y;
        m_titleImage = new Image(getDisplay(), m_titleWidth, m_titleHeight);
        GC gc = new GC(m_titleImage);
        try {
          gc.setBackground(getBackground());
          gc.fillRectangle(0, 0, m_titleWidth, m_titleHeight);
          int x = 0;
          // draw left lines
          {
            x += TITLE_MARGIN;
            drawTitleLines(gc, x, m_titleHeight, TITLE_LINES);
            x += TITLE_LINES + TITLE_MARGIN;
          }
          // draw text
          {
            gc.setForeground(IColorConstants.black);
            gc.setFont(TITLE_FONT);
            gc.drawText(text, x, 0);
            x += textSize.x;
          }
          // draw right lines
          {
            x += TITLE_MARGIN;
            drawTitleLines(gc, x, m_titleHeight, TITLE_LINES);
          }
        } finally {
          gc.dispose();
        }
      }
      // prepare rotated image
      m_titleImageRotated = DrawUtils.createRotatedImage(m_titleImage);
    }

    /**
     * Draws two title lines.
     */
    private void drawTitleLines(GC gc, int x, int height, int width) {
      drawTitleLine(gc, x, height / 3, width);
      drawTitleLine(gc, x, 2 * height / 3, width);
    }

    /**
     * Draws single title line.
     */
    private void drawTitleLine(GC gc, int x, int y, int width) {
      int right = x + TITLE_LINES;
      //
      gc.setForeground(IColorConstants.buttonLightest);
      gc.drawLine(x, y, right - 2, y);
      gc.drawLine(x, y + 1, right - 2, y + 1);
      //
      gc.setForeground(IColorConstants.buttonDarker);
      gc.drawLine(right - 2, y, right - 1, y);
      gc.drawLine(x + 2, y + 1, right - 2, y + 1);
    }

    ////////////////////////////////////////////////////////////////////////////
    //
    // Menu
    //
    ////////////////////////////////////////////////////////////////////////////
    private void configureMenu() {
      final MenuManager manager = new MenuManager();
      manager.setRemoveAllWhenShown(true);
      manager.addMenuListener(new IMenuListener() {
        @Override
        public void menuAboutToShow(IMenuManager menuMgr) {
          addDockActions();
          for (IFlyoutMenuContributor contributor : m_menuContributors) {
            contributor.contribute(manager);
          }
        }

        private void addDockActions() {
          MenuManager dockManager = new MenuManager(Messages.FlyoutControlComposite_dockManager);
          addDockAction(
              dockManager,
              Messages.FlyoutControlComposite_dockLeft,
              IFlyoutPreferences.DOCK_WEST);
          addDockAction(
              dockManager,
              Messages.FlyoutControlComposite_dockRight,
              IFlyoutPreferences.DOCK_EAST);
          addDockAction(
              dockManager,
              Messages.FlyoutControlComposite_dockTop,
              IFlyoutPreferences.DOCK_NORTH);
          addDockAction(
              dockManager,
              Messages.FlyoutControlComposite_dockBottom,
              IFlyoutPreferences.DOCK_SOUTH);
          manager.add(dockManager);
        }

        private void addDockAction(MenuManager dockManager, String text, int location) {
          if ((m_validDockLocations & location) != 0) {
            dockManager.add(new DockAction(text, location));
          }
        }
      });
      // set menu
      setMenu(manager.createContextMenu(this));
      // dispose it later
      addDisposeListener(new DisposeListener() {
        @Override
        public void widgetDisposed(DisposeEvent e) {
          manager.dispose();
        }
      });
    }
  }
  ////////////////////////////////////////////////////////////////////////////
  //
  // DockAction
  //
  ////////////////////////////////////////////////////////////////////////////
  private class DockAction extends Action {
    private final int m_location;

    ////////////////////////////////////////////////////////////////////////////
    //
    // Constructor
    //
    ////////////////////////////////////////////////////////////////////////////
    public DockAction(String text, int location) {
      super(text, AS_RADIO_BUTTON);
      m_location = location;
    }

    ////////////////////////////////////////////////////////////////////////////
    //
    // Action
    //
    ////////////////////////////////////////////////////////////////////////////
    @Override
    public boolean isChecked() {
      return getDockLocation() == m_location;
    }

    @Override
    public void run() {
      setDockLocation(m_location);
    }
  }
}