Eclipseplugins
PaWizard.java
1 package com.proalpha.pds.ui.wizards.controller;
2 
3 import java.lang.reflect.Method;
4 import java.util.HashMap;
5 import java.util.LinkedHashMap;
6 import java.util.Map;
7 
8 import org.eclipse.core.runtime.CoreException;
9 import org.eclipse.core.runtime.IStatus;
10 import org.eclipse.core.runtime.Status;
11 import org.eclipse.jface.dialogs.MessageDialog;
12 import org.eclipse.jface.viewers.ISelection;
13 import org.eclipse.jface.viewers.IStructuredSelection;
14 import org.eclipse.jface.window.Window;
15 import org.eclipse.jface.wizard.IWizardPage;
16 import org.eclipse.jface.wizard.Wizard;
17 import org.eclipse.ui.IWorkbench;
18 import org.eclipse.ui.IWorkbenchWizard;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 
22 import com.proalpha.pds.exception.ProALPHANotAvailableException;
23 import com.proalpha.pds.generator.ContentGenerator;
24 import com.proalpha.pds.paconnector.Activator;
25 import com.proalpha.pds.paconnector.PaProject;
26 import com.proalpha.pds.paconnector.RunProgramInPa;
27 import com.proalpha.pds.paconnector.parameter.ParameterFactory;
28 import com.proalpha.pds.paconnector.parameter.StartparameterReader;
29 import com.proalpha.pds.paconnector.repository.RepositoryDelegate;
30 import com.proalpha.pds.paconnector.repository.RepositoryFactory;
31 import com.proalpha.pds.paconnector.repository.RepositoryInformations;
32 import com.proalpha.pds.paconnector.utils.ExceptionUtils;
33 import com.proalpha.pds.templates.data.DataContainerFactory;
34 import com.proalpha.pds.templates.data.IGeneratorDataContainer;
35 import com.proalpha.pds.ui.PaProjectSelection;
36 import com.proalpha.pds.ui.wizards.wizardpages.WizardMainPage;
37 import com.proalpha.pds.ui.wizards.wizardpages.WizardMainPageConfiguration;
38 import com.proalpha.pds.ui.wizards.wizardpages.WizardpageFactory;
39 
50 public abstract class PaWizard extends Wizard {
51 
52  private final Logger logger = LoggerFactory.getLogger(PaWizard.class);
53 
54  protected ISelection selection;
55 
56  protected WizardMainPage wizardMainPage;
57 
58  private RunProgramInPa bridge;
59 
60  private LinkedHashMap<String, WizardMainPageConfiguration> configList;
61 
62  private RepositoryDelegate repositoryDelegate;
63 
64  private RepositoryInformations repositoryInformations;
65 
66  private StartparameterReader startparameterReader;
67 
68  protected Map<String, Object> wizardSettings;
69 
70  PaProject paProject;
71 
72  public PaWizard() {
73  super();
74  setNeedsProgressMonitor(true);
75 
76  this.paProject = Activator.getDefault().getProjectManager().getPaProjectByProjectExplorer();
77 
78  if (this.paProject == null || !this.paProject.getOeProject().getProject().isOpen()
79  || !this.paProject.isAvailable()) {
80  PaProjectSelection dialog = new PaProjectSelection(getShell());
81  dialog.create();
82  if (dialog.open() == Window.OK) {
83  this.paProject = dialog.getSelectedProject();
84  }
85  }
86 
87  if (this.paProject == null || !this.paProject.getOeProject().getProject().isOpen()
88  || !this.paProject.isAvailable()) {
89  this.dispose();
90  return;
91  }
92 
93  logger.debug("Running wizard for {}", this.paProject);
94 
95  try {
96  RepositoryFactory repositoryFactory = new RepositoryFactory();
97  this.repositoryDelegate = repositoryFactory.createRepositoryDelegate(this.paProject);
98  this.repositoryInformations = repositoryFactory.createRepositoryInformations(this.paProject);
99  ParameterFactory parameterFactory = new ParameterFactory();
100  this.startparameterReader = parameterFactory.createStartparameterReader(this.paProject);
101  } catch (ProALPHANotAvailableException e) {
103  }
104 
105  if (!this.paProject.isAvailable()) {
106  MessageDialog.openError(getShell(), "Connect to version control failed",
107  "The version control is not available.");
108  throw new IllegalStateException("Version control is not available");
109  }
110 
111  wizardSettings = new HashMap<>();
112  }
113 
123  protected boolean doFinish() {
124 
125  try {
127  IGeneratorDataContainer dataContainer = factory.createDataContainer(this.paProject, wizardSettings);
128  new ContentGenerator("", dataContainer, wizardSettings.get("TemplateName").toString()).schedule();
129  } catch (Exception e) {
131  return false;
132  }
133 
134  return true;
135  }
136 
137  protected void throwCoreException(String message) throws CoreException {
138  IStatus status = new Status(IStatus.ERROR, "com.proalpha.java.plugins.templates", IStatus.OK, message, null);
139  throw new CoreException(status);
140  }
141 
148  public void init(IWorkbench wb, IStructuredSelection selection) {
149  this.selection = selection;
150  }
151 
156  @SuppressWarnings("unchecked")
157  public boolean performFinish() {
158 
159  IWizardPage[] wizardPages = getPages();
160 
161  for (IWizardPage page : wizardPages) {
162  Class<?>[] interfaces = page.getClass().getInterfaces();
163  for (Class<?> c : interfaces) {
164  if (c.getName().contains("IpaWizardPage")) {
165  try {
166  Method method = page.getClass().getMethod("getSettings");
167  wizardSettings.putAll((HashMap<String, Object>) method.invoke(page));
168  } catch (Exception e) {
170  return false;
171  }
172  }
173  }
174  }
175  return doFinish();
176  }
177 
182  @Override
183  public void addPages() {
184 
185  wizardMainPage = WizardpageFactory.createWizardMainPage(this.paProject, this.configList, this.repositoryDelegate,
186  this.repositoryInformations, this.startparameterReader);
187 
188  addPage(wizardMainPage);
189  }
190 
191  public RunProgramInPa getbridge() {
192  return this.bridge;
193  }
194 
195  public RepositoryDelegate getRepositoryDelegate() {
196  return repositoryDelegate;
197  }
198 
199  public RepositoryInformations getRepositoryInformations() {
200  return repositoryInformations;
201  }
202 
203  public StartparameterReader getStartparameterReader() {
204  return startparameterReader;
205  }
206 
207  public LinkedHashMap<String, WizardMainPageConfiguration> getConfigList() {
208  return configList;
209  }
210 
211  public void setConfigList(LinkedHashMap<String, WizardMainPageConfiguration> configList) {
212  this.configList = configList;
213  }
214 }
static void showAndLogErrorMessage(AbstractUIPlugin plugin, Throwable exception)
void init(IWorkbench wb, IStructuredSelection selection)
Definition: PaWizard.java:148