Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aa86fe910a05db6c9a2d622b840f529d3c653b66 (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
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
/*
 * Copyright (c) 2014-2016 Eike Stepper (Loehne, 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.oomph.ui;

import org.eclipse.oomph.internal.ui.UIPlugin;
import org.eclipse.oomph.ui.HelpSupport.HelpProvider;
import org.eclipse.oomph.util.PropertyFile;
import org.eclipse.oomph.util.ReflectUtil;

import org.eclipse.jface.dialogs.DialogTray;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Sash;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @author Eike Stepper
 */
public abstract class OomphDialog extends TitleAreaDialog implements HelpProvider
{
  private static final PropertyFile HISTORY = new PropertyFile(UIPlugin.INSTANCE.getUserLocation().append("dialog-help-shown.properties").toFile());

  private static final String SETTING_DIALOG_WIDTH = "dialogWidth";

  private static final String SETTING_DIALOG_HEIGHT = "dialogHeight";

  private String title;

  private int width;

  private int height;

  private OomphUIPlugin plugin;

  private HelpSupport helpSupport;

  protected OomphDialog(Shell parentShell, String title, int defaultWidth, int defaultHeight, OomphUIPlugin plugin, boolean helpAvailable)
  {
    super(parentShell);
    setShellStyle(SWT.SHELL_TRIM | SWT.BORDER | SWT.APPLICATION_MODAL);

    this.title = title;
    this.plugin = plugin;

    width = defaultWidth;
    height = defaultHeight;

    IDialogSettings settings = getDialogSettings();

    try
    {
      int dialogWidth = settings.getInt(SETTING_DIALOG_WIDTH);
      width = dialogWidth;
    }
    catch (NumberFormatException ex)
    {
      //$FALL-THROUGH$
    }

    try
    {
      int dialogHeight = settings.getInt(SETTING_DIALOG_HEIGHT);
      height = dialogHeight;
    }
    catch (NumberFormatException ex)
    {
      //$FALL-THROUGH$
    }

    if (helpAvailable)
    {
      helpSupport = new HelpSupport(this)
      {
        @Override
        protected void handleInactivity(Display display, boolean inactive)
        {
          OomphDialog.this.handleInactivity(display, inactive);
        }
      };
    }
  }

  @Override
  public boolean isHelpAvailable()
  {
    return helpSupport != null;
  }

  public final HelpSupport getHelpSupport()
  {
    return helpSupport;
  }

  public String getHelpPath()
  {
    return null;
  }

  @Override
  public boolean close()
  {
    if (helpSupport != null)
    {
      helpSupport.dispose();
      helpSupport = null;
    }

    if (getTray() != null)
    {
      closeTray();
    }

    Shell shell = getShell();
    if (shell != null)
    {
      Point size = shell.getSize();
      IDialogSettings settings = getDialogSettings();
      settings.put(SETTING_DIALOG_WIDTH, size.x);
      settings.put(SETTING_DIALOG_HEIGHT, size.y);
    }

    return super.close();
  }

  public String getTitle()
  {
    return title;
  }

  @Override
  public void openTray(DialogTray tray) throws IllegalStateException, UnsupportedOperationException
  {
    super.openTray(tray);
    hookTray(this);
  }

  @Override
  public void setTitleImage(Image newTitleImage)
  {
    super.setTitleImage(newTitleImage);
    fixTitleImageLayout(this);
  }

  @Override
  protected Control createContents(Composite parent)
  {
    Control contents = super.createContents(parent);
    fixTitleImageLayout(this);
    return contents;
  }

  @Override
  protected Control createDialogArea(Composite parent)
  {
    Shell shell = getShell();
    shell.setText(getShellText());

    setTitle(title);
    setTitleImage(getDefaultImage(getImagePath()));
    setMessage(getDefaultMessage());

    Composite area = (Composite)super.createDialogArea(parent);

    GridLayout layout = new GridLayout(getContainerColumns(), false);
    layout.marginWidth = getContainerMargin();
    layout.marginHeight = getContainerMargin();
    layout.verticalSpacing = 0;

    Composite container = new Composite(area, SWT.NONE);
    container.setLayout(layout);
    container.setLayoutData(new GridData(GridData.FILL_BOTH));
    container.setBackground(container.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));

    createUI(container);

    if (getContainerMargin() == 0)
    {
      createSeparator(container);
    }

    shell.setActive();
    return area;
  }

  protected Control createButtonBarWithControls(Composite parent)
  {
    GridLayout layout = new GridLayout();
    layout.marginRight = 0;
    layout.marginLeft = 0;
    layout.marginHeight = 0;
    layout.horizontalSpacing = 5;

    Composite controlArea = new Composite(parent, SWT.NONE);
    controlArea.setLayout(layout);
    controlArea.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
    controlArea.setFont(parent.getFont());

    boolean helpAvailable = isHelpAvailable();
    if (helpAvailable)
    {
      Control helpControl = createHelpControl(controlArea);
      ((GridData)helpControl.getLayoutData()).horizontalIndent = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
    }

    createControlsForButtonBar(controlArea);

    HelpSupport oldHelpSupport = helpSupport;
    helpSupport = null;
    super.createButtonBar(controlArea);
    helpSupport = oldHelpSupport;

    return controlArea;
  }

  protected void createControlsForButtonBar(Composite parent)
  {
    throw new UnsupportedOperationException("At least one control must be added when createButtonBarWithControls is called.");
  }

  public CCombo createCCombo(Composite parent)
  {
    GridLayout layout = (GridLayout)parent.getLayout();
    if (layout.numColumns == 1)
    {
      layout.marginLeft = 10;
    }

    layout.numColumns++;

    final CCombo combo = new CCombo(parent, SWT.BORDER | SWT.READ_ONLY | SWT.FLAT);
    combo.addControlListener(new ControlAdapter()
    {
      boolean firstTime = true;

      @Override
      public void controlResized(ControlEvent e)
      {
        Button button = getButton(IDialogConstants.CANCEL_ID);
        if (firstTime)
        {
          button.addControlListener(this);
          firstTime = false;
        }

        Rectangle buttonBounds = button.getBounds();
        if (buttonBounds.height != 0)
        {
          int borderCompensation = button.getBorderWidth() - combo.getBorderWidth();

          Rectangle bounds = combo.getBounds();
          bounds.y = buttonBounds.y - borderCompensation / 2;
          bounds.height = buttonBounds.height + borderCompensation;

          combo.setBounds(bounds);
        }
      }
    });

    GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
    int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
    data.grabExcessHorizontalSpace = true;
    data.widthHint = widthHint;
    combo.setLayoutData(data);

    return combo;
  }

  public Combo createCombo(Composite parent)
  {
    GridLayout layout = (GridLayout)parent.getLayout();
    if (layout.numColumns == 1)
    {
      layout.marginLeft = 10;
    }

    layout.numColumns++;

    Combo combo = new Combo(parent, SWT.READ_ONLY);

    GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
    int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
    data.grabExcessHorizontalSpace = true;
    data.widthHint = widthHint;
    combo.setLayoutData(data);

    return combo;
  }

  public Label createLabel(Composite parent, String text)
  {
    GridLayout layout = (GridLayout)parent.getLayout();
    if (layout.numColumns == 1 && parent.getChildren().length == 0)
    {
      layout.marginLeft = 10;
    }

    layout.numColumns++;

    Label label = new Label(parent, SWT.NONE);
    label.setText(text);

    GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
    label.setLayoutData(data);

    return label;
  }

  protected Button createCheckbox(Composite parent, String label)
  {
    ((GridLayout)parent.getLayout()).numColumns++;

    Button button = new Button(parent, SWT.CHECK);
    button.setText(label);
    button.setFont(JFaceResources.getDialogFont());

    setButtonLayoutData(button);
    return button;
  }

  @Override
  protected Control createHelpControl(Composite parent)
  {
    ToolBar toolBar = (ToolBar)super.createHelpControl(parent);
    if (helpSupport != null)
    {
      ToolItem helpButton = toolBar.getItems()[0];
      helpSupport.hook(helpButton);
    }

    createToolItemsForToolBar(toolBar);
    return toolBar;
  }

  protected void createToolItemsForToolBar(ToolBar toolBar)
  {
  }

  protected final ToolItem createToolItem(ToolBar toolBar, String label)
  {
    return createToolItem(toolBar, null, label);
  }

  protected final ToolItem createToolItem(ToolBar toolBar, String iconPath, String toolTip)
  {
    ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
    if (iconPath == null)
    {
      toolItem.setText(toolTip);
    }
    else
    {
      Image image = getDefaultImage(iconPath);
      toolItem.setImage(image);
      toolItem.setToolTipText(toolTip);
    }

    return toolItem;
  }

  protected Label createSeparator(Composite parent)
  {
    Label separator = new Label(parent, SWT.HORIZONTAL | SWT.SEPARATOR);
    separator.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, getContainerColumns(), 1));
    return separator;
  }

  protected int getContainerColumns()
  {
    return 1;
  }

  protected int getContainerMargin()
  {
    return 0;
  }

  protected abstract String getImagePath();

  protected IDialogSettings getDialogSettings()
  {
    String sectionName = getClass().getName();
    OomphUIPlugin plugin = this.plugin != null ? this.plugin : UIPlugin.INSTANCE;
    return plugin.getDialogSettings(sectionName);
  }

  protected void handleInactivity(Display display, boolean inactive)
  {
  }

  @Override
  protected final Point getInitialSize()
  {
    return new Point(width, height);
  }

  protected final Image getDefaultImage(String path)
  {
    return plugin.getSWTImage(path);
  }

  protected abstract String getDefaultMessage();

  protected abstract String getShellText();

  protected abstract void createUI(Composite parent);

  public static boolean showFirstTimeHelp(final TrayDialog dialog)
  {
    try
    {
      if (dialog.isHelpAvailable())
      {
        String key = dialog.getClass().getName();
        String value = new Date().toString();

        if (HISTORY.compareAndSetProperty(key, value))
        {
          UIUtil.asyncExec(dialog.getShell(), new Runnable()
          {
            public void run()
            {
              try
              {
                Method method = ReflectUtil.getMethod(TrayDialog.class, "helpPressed");
                ReflectUtil.invokeMethod(method, dialog);
              }
              catch (Throwable ex)
              {
                UIPlugin.INSTANCE.log(ex);
              }
            }
          });

          return true;
        }
      }
    }
    catch (Throwable ex)
    {
      UIPlugin.INSTANCE.log(ex);
    }

    return false;
  }

  public static void hookTray(final TrayDialog dialog) throws IllegalStateException, UnsupportedOperationException
  {
    final Control trayControl = getFieldValue(dialog, "trayControl");
    final Label rightSeparator = getFieldValue(dialog, "rightSeparator");
    final Sash sash = getFieldValue(dialog, "sash");
    if (trayControl == null || rightSeparator == null || sash == null)
    {
      return;
    }

    final GridData data = (GridData)trayControl.getLayoutData();
    sash.addListener(SWT.Selection, new Listener()
    {
      public void handleEvent(Event event)
      {
        if (event.detail == SWT.DRAG)
        {
          Shell shell = dialog.getShell();
          Rectangle clientArea = shell.getClientArea();
          int newWidth = clientArea.width - event.x - (sash.getSize().x + rightSeparator.getSize().x);
          if (newWidth != data.widthHint)
          {
            data.widthHint = newWidth;
            shell.layout();
          }
        }
      }
    });
  }

  public static void fixTitleImageLayout(TitleAreaDialog dialog)
  {
    try
    {
      Field titleImageLargestField = ReflectUtil.getField(TitleAreaDialog.class, "titleImageLargest");
      boolean titleImageLargest = (Boolean)ReflectUtil.getValue(titleImageLargestField, dialog);

      Field workAreaField = ReflectUtil.getField(TitleAreaDialog.class, "workArea");
      Composite workArea = (Composite)ReflectUtil.getValue(workAreaField, dialog);

      Field titleImageLabelField = ReflectUtil.getField(TitleAreaDialog.class, "titleImageLabel");
      Label titleImageLabel = (Label)ReflectUtil.getValue(titleImageLabelField, dialog);

      FormData layoutData = (FormData)titleImageLabel.getLayoutData();

      if (titleImageLargest)
      {
        layoutData.top = new FormAttachment(0, 0);
        layoutData.bottom = null;
      }
      else
      {
        layoutData.top = null;
        layoutData.bottom = new FormAttachment(workArea);
      }
    }
    catch (Throwable t)
    {
      // Ignore.
    }
  }

  /**
   * This is a prototype, maybe worth showing to people later...
   */
  @SuppressWarnings("unused")
  private static void animateMessage(TitleAreaDialog dialog)
  {
    try
    {
      final Text messageLabel = ReflectUtil.getValue("messageLabel", dialog);
      final Display display = messageLabel.getDisplay();

      final Color[] colors = new Color[8];
      for (int color = 0; color < colors.length; color++)
      {
        colors[color] = new Color(display, 0, color * 32, 0);
      }

      display.asyncExec(new Runnable()
      {
        private int loop;

        private int color;

        private boolean reverse;

        public void run()
        {
          // System.out.println(loop + ", " + color);
          messageLabel.setForeground(colors[color]);

          if (reverse)
          {
            if (--color == -1)
            {
              if (++loop == 5)
              {
                for (int i = 0; i < colors.length; i++)
                {
                  colors[i].dispose();
                }

                return;
              }

              color = 1;
              reverse = false;
            }
          }
          else
          {
            if (++color == colors.length)
            {
              color = colors.length - 2;
              reverse = true;
            }
          }

          display.timerExec(80, this);
        }
      });
    }
    catch (Throwable ex)
    {
      ex.printStackTrace();
    }
  }

  private static <T> T getFieldValue(TrayDialog dialog, String name)
  {
    try
    {
      Field field = ReflectUtil.getField(TrayDialog.class, name);
      if (field != null)
      {
        @SuppressWarnings("unchecked")
        T value = (T)ReflectUtil.getValue(field, dialog);
        return value;
      }
    }
    catch (Exception ex)
    {
      //$FALL-THROUGH$
    }

    return null;
  }
}

Back to the top