Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dc978aa399e8f8fb4351f12191b3bf5802cc0d25 (plain) (blame)
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
/***************************************************************************
 * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
 * 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:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.net4j.util.ui.widgets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
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;

public abstract class SashComposite extends Composite
{
  private boolean vertical;

  private FormLayout form;

  private Sash sash;

  private Control control1;

  private Control control2;

  private FormData sashData;

  private FormData control1Data;

  private FormData control2Data;

  private int limit;

  private int percent;

  public SashComposite(Composite parent, int style, int limit, int percent)
  {
    this(parent, style, limit, percent, true);
  }

  public SashComposite(Composite parent, int style, int limit, int percent, boolean vertical)
  {
    super(parent, style);
    this.vertical = vertical;
    this.limit = limit;
    this.percent = percent;

    form = new FormLayout();
    setLayout(form);

    control1Data = new FormData();
    control1 = createControl1(this);
    control1.setLayoutData(control1Data);

    sashData = new FormData();
    sash = createSash(this);
    sash.setLayoutData(sashData);

    control2Data = new FormData();
    control2 = createControl2(this);
    control2.setLayoutData(control2Data);

    init();
    if (!vertical)
    {
      swap();
    }
  }

  public boolean isVertical()
  {
    return vertical;
  }

  public void setVertical(boolean vertical)
  {
    if (this.vertical != vertical)
    {
      this.vertical = vertical;
      swap();
      layout();
    }
  }

  public Sash getSash()
  {
    return sash;
  }

  public Control getControl1()
  {
    return control1;
  }

  public Control getControl2()
  {
    return control2;
  }

  protected void init()
  {
    control1Data.left = new FormAttachment(0, 0);
    control1Data.right = new FormAttachment(sash, 0);
    control1Data.top = new FormAttachment(0, 0);
    control1Data.bottom = new FormAttachment(100, 0);

    sashData.left = new FormAttachment(percent, 0);
    sashData.right = null;
    sashData.top = new FormAttachment(0, 0);
    sashData.bottom = new FormAttachment(100, 0);

    control2Data.left = new FormAttachment(sash, 0);
    control2Data.right = new FormAttachment(100, 0);
    control2Data.top = new FormAttachment(0, 0);
    control2Data.bottom = new FormAttachment(100, 0);
  }

  protected void swap()
  {
    swap(control1Data);
    swap(sashData);
    swap(control2Data);
  }

  protected void swap(FormData formData)
  {
    FormAttachment tmp = formData.left;
    formData.left = formData.top;
    formData.top = tmp;

    tmp = formData.right;
    formData.right = formData.bottom;
    formData.bottom = tmp;
  }

  protected Sash createSash(Composite parent)
  {
    Sash sash = new Sash(parent, vertical ? SWT.VERTICAL : SWT.HORIZONTAL);
    sash.addListener(SWT.Selection, new SashListener());
    return sash;
  }

  protected abstract Control createControl1(Composite parent);

  protected abstract Control createControl2(Composite parent);

  /**
   * @author Eike Stepper
   */
  private final class SashListener implements Listener
  {
    public SashListener()
    {
    }

    public void handleEvent(Event e)
    {
      Rectangle sashRect = sash.getBounds();
      Rectangle shellRect = SashComposite.this.getClientArea();
      if (vertical)
      {
        int right = shellRect.width - sashRect.width - limit;
        e.x = Math.max(Math.min(e.x, right), limit);
        if (e.x != sashRect.x)
        {
          sashData.left = new FormAttachment(0, e.x);
          SashComposite.this.layout();
        }
      }
      else
      {
        int bottom = shellRect.height - sashRect.height - limit;
        e.y = Math.max(Math.min(e.y, bottom), limit);
        if (e.y != sashRect.y)
        {
          sashData.top = new FormAttachment(0, e.y);
          SashComposite.this.layout();
        }
      }
    }
  }
}

Back to the top