Skip to main content
summaryrefslogtreecommitdiffstats
blob: 056e3e351c75e58aa11a7a0c044b67950e88f176 (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
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
/*
 * Copyright (c) 2004 - 2011 Eike Stepper (Berlin, Germany) and others.
 * 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.net4j.util.event.IListener;
import org.eclipse.net4j.util.event.INotifier;
import org.eclipse.net4j.util.event.Notifier;

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;

/**
 * @author Eike Stepper
 */
public abstract class SashComposite extends Composite implements INotifier
{
  private final SashListener sashListener = new SashListener();

  private final Notifier notifier = new Notifier();

  private int limit;

  private int percent;

  private boolean borders;

  private boolean vertical;

  private Sash sash;

  private Control control1;

  private Control control2;

  private FormData sashData;

  private FormData control1Data;

  private FormData control2Data;

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

  public SashComposite(Composite parent, int style, int limit, int percent, boolean borders)
  {
    super(parent, style);
    setLayout(new FormLayout());
    this.limit = limit;
    this.percent = percent;
    this.borders = borders;

    control1Data = new FormData();
    control1 = borders ? new OneBorderComposite(this)
    {
      @Override
      protected Control createUI(Composite parent)
      {
        return createControl1(parent);
      }
    } : createControl1(this);

    control1.setLayoutData(control1Data);

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

    control2Data = new FormData();
    control2 = borders ? new OneBorderComposite(this)
    {
      @Override
      protected Control createUI(Composite parent)
      {
        return createControl2(parent);
      }
    } : createControl2(this);

    control2.setLayoutData(control2Data);

    init();
  }

  /**
   * @since 2.0
   */
  @Override
  public void dispose()
  {
    sash.removeListener(SWT.Selection, sashListener);
    super.dispose();
  }

  /**
   * @since 2.0
   */
  public void addListener(IListener listener)
  {
    notifier.addListener(listener);
  }

  /**
   * @since 2.0
   */
  public IListener[] getListeners()
  {
    return notifier.getListeners();
  }

  /**
   * @since 2.0
   */
  public boolean hasListeners()
  {
    return notifier.hasListeners();
  }

  /**
   * @since 2.0
   */
  public void removeListener(IListener listener)
  {
    notifier.removeListener(listener);
  }

  public boolean isVertical()
  {
    return vertical;
  }

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

      Sash newSash = createSash(this);
      newSash.moveBelow(control1);
      newSash.setLayoutData(sash.getLayoutData());

      sash.removeListener(SWT.Selection, sashListener);
      sash.setLayoutData(null);
      sash.dispose();
      sash = newSash;

      init();
      layout();
      IListener[] listeners = notifier.getListeners();
      if (listeners != null)
      {
        notifier.fireEvent(new OrientationChangedEvent(vertical), listeners);
      }
    }
  }

  public Sash getSash()
  {
    return sash;
  }

  public Control getControl1()
  {
    return borders ? ((OneBorderComposite)control1).getClientControl() : control1;
  }

  public Control getControl2()
  {
    return borders ? ((OneBorderComposite)control2).getClientControl() : control2;
  }

  protected void init()
  {
    if (borders)
    {
      ((OneBorderComposite)control1).setBorderPosition(SWT.RIGHT);
      ((OneBorderComposite)control2).setBorderPosition(SWT.LEFT);
    }

    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);

    if (vertical)
    {
      swap();
    }
  }

  protected void swap()
  {
    if (borders)
    {
      ((OneBorderComposite)control1).swapBorderPosition();
      ((OneBorderComposite)control2).swapBorderPosition();
    }

    swap(control1Data);
    swap(sashData);
    swap(control2Data);
  }

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

    FormAttachment tmp2 = formData.right;
    formData.right = formData.bottom;
    formData.bottom = tmp2;
  }

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

  protected abstract Control createControl1(Composite parent);

  protected abstract Control createControl2(Composite parent);

  /**
   * @author Eike Stepper
   * @since 2.0
   */
  public class OrientationChangedEvent extends org.eclipse.net4j.util.event.Event
  {
    private static final long serialVersionUID = 1L;

    private boolean vertical;

    public OrientationChangedEvent(boolean vertical)
    {
      super(SashComposite.this);
    }

    @Override
    public SashComposite getSource()
    {
      return (SashComposite)super.getSource();
    }

    public boolean isVertical()
    {
      return vertical;
    }
  }

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

    public void handleEvent(Event e)
    {
      Rectangle sashRect = sash.getBounds();
      Rectangle shellRect = 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);
          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);
          layout();
        }
      }
    }
  }
}

Back to the top