Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a6c5f0160b4204645664370d1f9e1e72efb6d0ba (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
package org.eclipse.net4j.util.ui.container;

import org.eclipse.net4j.util.container.IManagedContainer;
import org.eclipse.net4j.util.container.IPluginContainer;
import org.eclipse.net4j.util.internal.ui.bundle.OM;
import org.eclipse.net4j.util.ui.actions.LongRunningAction;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Shell;

/**
 * @author Eike Stepper
 * @since 3.2
 */
public class ElementWizardAction extends LongRunningAction
{
  private Shell shell;

  private String title;

  private String toolTip;

  private String productGroup;

  private String factoryType;

  private String description;

  private String defaultFactoryType;

  private IManagedContainer container;

  public ElementWizardAction(Shell shell, String title, String toolTip, ImageDescriptor image, String productGroup)
  {
    this(shell, title, toolTip, image, productGroup, IPluginContainer.INSTANCE);
  }

  public ElementWizardAction(Shell shell, String title, String toolTip, ImageDescriptor image, String productGroup,
      IManagedContainer container)
  {
    this(shell, title, toolTip, image, productGroup, container, null);
  }

  public ElementWizardAction(Shell shell, String title, String toolTip, ImageDescriptor image, String productGroup,
      IManagedContainer container, String defaultFactoryType)
  {
    super(title, toolTip, image);
    this.shell = shell;
    this.title = title;
    this.toolTip = toolTip;
    this.productGroup = productGroup;
    this.container = container;
    this.defaultFactoryType = defaultFactoryType;
  }

  public String getDefaultFactoryType()
  {
    return defaultFactoryType;
  }

  /**
   * Can be overridden by subclasses.
   */
  public String getDefaultDescription(String factoryType)
  {
    return null;
  }

  @Override
  protected void preRun() throws Exception
  {
    ElementWizardDialog dialog = new ElementWizardDialog(shell, title, toolTip, productGroup, defaultFactoryType)
    {
      @Override
      protected IManagedContainer getContainer()
      {
        return container;
      }

      @Override
      protected String getDefaultDescription(String factoryType)
      {
        return ElementWizardAction.this.getDefaultDescription(factoryType);
      }
    };

    if (dialog.open() == ElementWizardDialog.OK)
    {
      factoryType = dialog.getFactoryType();
      description = dialog.getDescription();
    }
    else
    {
      cancel();
    }
  }

  @Override
  protected void doRun(IProgressMonitor progressMonitor) throws Exception
  {
    try
    {
      container.getElement(productGroup, factoryType, description);
    }
    catch (final RuntimeException ex)
    {
      OM.LOG.error(ex);
      getDisplay().asyncExec(new Runnable()
      {
        public void run()
        {
          MessageDialog.openError(shell, title, "An error occured: " + ex.getMessage());
        }
      });
    }
  }
}

Back to the top