Eclipseplugins
WizardMainPage.java
1 package com.proalpha.pds.ui.wizards.wizardpages;
2 
3 import java.net.URL;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.LinkedHashMap;
7 import java.util.List;
8 import java.util.Map;
9 
10 import org.eclipse.core.resources.IFolder;
11 import org.eclipse.core.resources.IResource;
12 import org.eclipse.core.resources.ResourcesPlugin;
13 import org.eclipse.core.runtime.FileLocator;
14 import org.eclipse.core.runtime.Path;
15 import org.eclipse.core.runtime.Platform;
16 import org.eclipse.jface.dialogs.IDialogPage;
17 import org.eclipse.jface.layout.GridDataFactory;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.jface.wizard.WizardPage;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.custom.CCombo;
22 import org.eclipse.swt.events.FocusAdapter;
23 import org.eclipse.swt.events.FocusEvent;
24 import org.eclipse.swt.events.ModifyEvent;
25 import org.eclipse.swt.events.ModifyListener;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Combo;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Text;
35 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 
39 import com.proalpha.pds.paconnector.PaProject;
40 import com.proalpha.pds.paconnector.parameter.StartparameterReader;
41 import com.proalpha.pds.paconnector.repository.Instance;
42 import com.proalpha.pds.paconnector.repository.RepositoryDelegate;
43 import com.proalpha.pds.paconnector.repository.RepositoryInformations;
44 import com.proalpha.pds.templates.data.GeneratorSettingsConstans;
45 import com.proalpha.pds.ui.Activator;
46 
54 public class WizardMainPage extends WizardPage implements IpaWizardPage {
55 
56  private static final Logger logger = LoggerFactory.getLogger(WizardMainPage.class);
57 
58  private PaProject paProject;
59  private RepositoryDelegate repositoryDelegate;
60  private RepositoryInformations repositoryInformations;
61  private StartparameterReader startparameterReader;
62  private CCombo programVariant;
63  private Text containerText;
64  private Text fileText;
65  private Text relativePathText;
66  private String currConfig;
67  private Map<String, WizardMainPageConfiguration> configList;
68  private boolean checkRepository;
69  private boolean supportRepositoryCreateButton;
70  private boolean checkFileNameLength;
71  private boolean checkLowerCase;
72  private Combo parentClassCombo;
73  private String rootClass;
74  private Text shortDescriptionText;
75  private Text descriptionText;
76  private Button buttonCreateObject;
77  private Text productText;
78  private Text versionText;
79  private Text authorText;
80  private List<String> extensionList;
81  private Text productDescriptionText;
82  private Text moduleDescriptionText;
83  private Text moduleText;
84  private String thirdDigit;
85  private HashMap<String, String> classGroupCode;
86  private Boolean isFileExisting = false;
87 
88  private static final String OO_ABL = "OOABL";
89 
100  public WizardMainPage(PaProject paProject, Map<String, WizardMainPageConfiguration> configs, RepositoryDelegate repositoryDelegate,
101  RepositoryInformations repositoryInformations, StartparameterReader startparameterReader) {
102  super("WizardMainPage");
103 
104  URL url = FileLocator.find(Platform.getBundle(Activator.PLUGIN_ID), new Path("icons/pA-logox72.png"),
105  Collections.emptyMap());
106 
107  setImageDescriptor(ImageDescriptor.createFromURL(url));
108 
109  this.paProject = paProject;
110  this.configList = configs;
111  currConfig = configs.entrySet().iterator().next().getKey();
112  this.repositoryDelegate = repositoryDelegate;
113  this.repositoryInformations = repositoryInformations;
114  this.startparameterReader = startparameterReader;
115 
116  repositoryDelegate.refreshChache();
117 
118  }
119 
120  private class CreateInRepositorySelectionAdapter extends SelectionAdapter {
121  @Override
122  public void widgetSelected(SelectionEvent event) {
123  String fileName = getFileName();
124  String description = getFileDescription();
125  String shortDescription = getShortDescription();
126  String parentClass = parentClassCombo.getText();
127 
128  Instance instance = repositoryDelegate.getEmptyInstance();
129  instance.setName(fileName);
130  instance.setDescription(description);
131  instance.setShortDescription(shortDescription);
132  instance.setParentClass(parentClass);
133  repositoryDelegate.createRepositoryEntryforInstance(instance);
134 
135  checkFileExisting(getFileName());
136 
137  validateInput();
138 
139  }
140 
141  private String getFileDescription() {
142  return descriptionText.getText();
143  }
144 
145  }
146 
147  private class BrowseButtonSelectionAdapter extends SelectionAdapter {
148 
153  @Override
154  public void widgetSelected(SelectionEvent event) {
155  ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(),
156  ResourcesPlugin.getWorkspace().getRoot(), false, "Select new file container");
157 
158  if (dialog.open() == ContainerSelectionDialog.OK) {
159  Object[] result = dialog.getResult();
160  if (result.length == 1) {
161  containerText.setText(((Path) result[0]).toString());
162  }
163  }
164  }
165  }
166 
167  private class FileNameFocusAdapter extends FocusAdapter {
168  @Override
169  public void focusLost(FocusEvent event) {
170  validateFileName();
171 
172  }
173 
174  }
175 
176  private class ValidateModifyListener implements ModifyListener {
177  public void modifyText(ModifyEvent e) {
178  if (programVariant.isVisible())
179  validateInput();
180  }
181  }
182 
186  public void createControl(Composite parent) {
187 
188  Composite container;
189  container = new Composite(parent, SWT.NULL);
190 
191  GridLayout glContainer = new GridLayout(3, false);
192  container.setLayout(glContainer);
193 
194  ValidateModifyListener modifyListener = new ValidateModifyListener();
195 
196  createLabel(container, "Program &Variant:", SWT.NULL);
197  programVariant = new CCombo(container, SWT.SINGLE | SWT.BORDER);
198  programVariant.setEditable(false);
199  GridData gdProgramVariant = new GridData(GridData.FILL, GridData.FILL, true, false, 1, 1);
200  gdProgramVariant.widthHint = convertWidthInCharsToPixels(10);
201  programVariant.setLayoutData(gdProgramVariant);
202  programVariant.addSelectionListener(new SelectionAdapter() {
203  @Override
204  public void widgetSelected(SelectionEvent e) {
205  if (!currConfig.equals(programVariant.getText())) {
206  currConfig = programVariant.getText();
207  initialize();
208  }
209  }
210  });
211  createEmptyLabel(container, SWT.NONE);
212 
213  createLabel(container, "&File name:", SWT.NULL);
214  fileText = createTextField(container, "Physical File Name");
215  fileText.addModifyListener(modifyListener);
216  fileText.addFocusListener(new FileNameFocusAdapter());
217  createEmptyLabel(container, SWT.NONE);
218 
219  createLabel(container, "&Parent Class:", SWT.NONE);
220  parentClassCombo = new Combo(container, SWT.NONE);
221  GridData gdParentClass = new GridData(GridData.FILL, GridData.FILL, true, false, 1, 1);
222  gdParentClass.widthHint = convertWidthInCharsToPixels(10);
223  parentClassCombo.setLayoutData(gdParentClass);
224  parentClassCombo.addModifyListener(modifyListener);
225 
226  buttonCreateObject = createButton(container, "Create in Repository");
227  buttonCreateObject.addSelectionListener(new CreateInRepositorySelectionAdapter());
228 
229  createLabel(container, "&Container:", SWT.NONE);
230  containerText = createTextField(container, "Parent Directory");
231  containerText.addModifyListener(modifyListener);
232 
233  Button buttonBrowseForContainer = createButton(container, "Browse...");
234  buttonBrowseForContainer.setToolTipText("Browse Directories in current Project");
235  buttonBrowseForContainer.addSelectionListener(new BrowseButtonSelectionAdapter());
236 
237  createLabel(container, "Author:", SWT.NONE);
238  authorText = createTextField(container, "Author");
239  createEmptyLabel(container, SWT.NONE);
240 
241  createLabel(container, "Version:", SWT.NONE);
242  versionText = createTextField(container, "Current proALPHA Version");
243  createEmptyLabel(container, SWT.NONE);
244 
245  createLabel(container, "Product:", SWT.NONE);
246  productText = createReadOnlyTextField(container, "proALPHA Product Key");
247  productDescriptionText = createReadOnlyTextField(container, "proALPHA Product Description");
248 
249  createLabel(container, "Module:", SWT.NONE);
250  moduleText = createReadOnlyTextField(container, "proALPHA Sub Module Key");
251  moduleDescriptionText = createReadOnlyTextField(container, "proALPHA Sub Module Description");
252 
253  createLabel(container, "Relative Path:", SWT.NONE);
254  relativePathText = createReadOnlyTextField(container, "Relative Path of File");
255  // Override default layout data
256  GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).span(2, 1)
257  .applyTo(relativePathText);
258 
259  createLabel(container, "Short Description:", SWT.NONE);
260  shortDescriptionText = createTextField(container, "Short Description");
261  GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).span(2, 1)
262  .applyTo(shortDescriptionText);
263  shortDescriptionText.addModifyListener(modifyListener);
264  shortDescriptionText.setTextLimit(80);
265 
266  Label descriptionLabel = createLabel(container, "Description:", SWT.NONE);
267  GridDataFactory.fillDefaults().align(SWT.FILL, SWT.TOP).applyTo(descriptionLabel);
268  descriptionText = createTextField(container, "Description", SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
269  GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).span(2, 1).applyTo(descriptionText);
270  descriptionText.addModifyListener(modifyListener);
271  descriptionText.setTextLimit(800);
272 
273  setControl(container);
274  new Label(container, SWT.NONE);
275  new Label(container, SWT.NONE);
276 
277  }
278 
279  @Override
280  public void setVisible(boolean visible) {
281  programVariant.setItems(getConfigKeys((LinkedHashMap<String, WizardMainPageConfiguration>) this.configList));
282  programVariant.setText(currConfig);
283  initialize();
284  super.setVisible(visible);
285  }
286 
287  private Button createButton(Composite composite, String text) {
288  Button newButton = new Button(composite, SWT.PUSH);
289  newButton.setText(text);
290  return newButton;
291  }
292 
293  private Text createReadOnlyTextField(Composite composite, String tooltip) {
294  return createTextField(composite, tooltip, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
295  }
296 
297  private Text createTextField(Composite composite, String tooltip) {
298  return createTextField(composite, tooltip, SWT.BORDER | SWT.SINGLE);
299  }
300 
301  private Text createTextField(Composite composite, String tooltip, int style) {
302  Text newText = new Text(composite, style);
303  newText.setToolTipText(tooltip);
304 
305  GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(newText);
306 
307  return newText;
308  }
309 
310  private Label createEmptyLabel(Composite composite, int style) {
311  return createLabel(composite, "", style);
312  }
313 
314  private Label createLabel(Composite composite, String text, int style) {
315  Label newLabel = new Label(composite, style);
316  newLabel.setText(text);
317 
318  return newLabel;
319  }
320 
325  private void initialize() {
326 
327  authorText.setText(System.getProperty("user.name"));
328  versionText.setText(this.paProject.getVersion());
329 
330  WizardMainPageConfiguration configuration = configList.get(currConfig);
331 
332  setTitle(configuration.getWizardTitle());
333  setDescription(configuration.getWizardDescription());
334  this.checkRepository = configuration.isCheckRepository();
335  this.extensionList = configuration.getExtensionList();
336  this.thirdDigit = configuration.getThirdDigit();
337  this.rootClass = configuration.getRootClass();
338  this.extensionList = configuration.getExtensionList();
339  this.checkFileNameLength = configuration.isCheckFileNameLength();
340  this.checkLowerCase = configuration.isCheckLowerCase();
341  this.supportRepositoryCreateButton = configuration.isSupportRepositoryCreateButton();
342 
343  IFolder workingDirectory = startparameterReader.getWorkingDirectory();
344 
345  containerText.setText(workingDirectory.getFullPath().toString());
346 
347  buttonCreateObject.setEnabled(false);
348 
349  // Initialize Parent Class Combo
350  // Items are the given parent class plus all subclasses
351  // Initialize the screen value with parent class
352  if (!checkRepository) {
353  parentClassCombo.setEnabled(false);
354  } else {
355 
356  String subClasses = repositoryInformations.getListOFSubClasses(this.rootClass);
357 
358  if (subClasses != null && !subClasses.isEmpty()) {
359  String[] subClassesArray = subClasses.split(",");
360 
361  parentClassCombo.setItems(subClassesArray);
362  parentClassCombo.setText(rootClass);
363 
364  // Fill the HashMap with each className and the correspondent
365  // objectClassCode
366  classGroupCode = new HashMap<>();
367 
368  if (rootClass.equals(OO_ABL)) {
369  for (String className : subClassesArray) {
370  String objectClassCode = repositoryInformations.getClassGroupOf(className);
371  classGroupCode.put(className, objectClassCode);
372  }
373  }
374  }
375  }
376 
377  setPageComplete(false);
378  }
379 
383  private void validateInput() {
384 
385  try {
386 
387  if (!this.paProject.isAvailable()) {
388  return;
389  }
390 
391  String fileName = getFileName();
392 
393  buttonCreateObject.setEnabled((checkRepository) && (supportRepositoryCreateButton)
394  && (!shortDescriptionText.getText().equals("")) && (!isFileExisting)
395  // never in customlevel Standard but in all other Levels for UserExits
396  && (!this.paProject.getCustomLevel().equals("")));
397 
398  parentClassCombo
399  .setEnabled((checkRepository) && (parentClassCombo.getItemCount() != 1) && (!isFileExisting));
400 
401  shortDescriptionText.setEnabled(!((checkRepository) && (isFileExisting)));
402 
403  IResource container = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(getContainerName()));
404 
405  int dotLoc = fileName.lastIndexOf('.');
406  if (dotLoc == -1) {
407  updateStatus("File extension must be specified");
408  buttonCreateObject.setEnabled(Boolean.FALSE);
409  return;
410  } else {
411  String ext = fileName.substring(dotLoc + 1);
412  if (!(this.extensionList.contains(ext))) {
413  updateStatus("Invalid File Extension \"" + ext + "\"");
414  buttonCreateObject.setEnabled(Boolean.FALSE);
415  return;
416  }
417  }
418 
419  if (getContainerName().length() == 0) {
420  updateStatus("File container must be specified");
421  return;
422  }
423  if (container == null || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
424  updateStatus("File container must exist");
425  return;
426  }
427  if (!container.isAccessible()) {
428  updateStatus("Project must be writable");
429  return;
430  }
431  if (fileName.length() == 0) {
432  updateStatus("File name must be specified");
433  return;
434  }
435  if (fileName.replace('\\', '/').indexOf('/', 1) > 0) {
436  updateStatus("File name must be valid");
437  return;
438  }
439  if ((this.checkLowerCase) && !fileName.toLowerCase().equals(fileName)) {
440  updateStatus("File name must be lower case");
441  return;
442  }
443 
444  String fileBody = fileName.substring(0, dotLoc);
445  if ((this.checkFileNameLength) && (fileBody.length() != 8)) {
446  updateStatus("File name must have a length of 8 digits");
447  return;
448 
449  }
450  if (this.thirdDigit.length() > 0 && !fileName.substring(2, 3).equals(this.thirdDigit)) {
451  updateStatus("Thirdt Digit of File name must be \"" + this.thirdDigit + "\"");
452  return;
453 
454  }
455 
456  // Exclusion: _* (Update program)
457  if (!fileName.startsWith("_") && moduleText.getText().equals("")) {
458  updateStatus("The file must be assigned to a module.");
459  return;
460  }
461 
462  if (shortDescriptionText.getText().length() == 0) {
463  updateStatus("Short description must be specified");
464  return;
465  }
466 
467  /*
468  * Check if the fileName is valid before we create the object within the
469  * repository
470  */
471 
472  if (fileName.endsWith(".cls")) {
473  String parentClass = parentClassCombo.getText();
474  String objectClassCode = this.classGroupCode.get(parentClass);
475 
476  for (int i = 0; i < 2; i++) {
477 
478  if (Character.isLowerCase(fileName.charAt(i))) {
479  updateStatus("The first three characters must be uppercase.");
480  return;
481  }
482 
483  }
484 
485  // There is no class group code defined for the parentClass
486 
487  if (objectClassCode == null || objectClassCode.contains("?")) {
488  objectClassCode = "";
489  }
490 
491  if ((!fileName.contains(objectClassCode + ".cls") && (!parentClassCombo.getText().equals(OO_ABL)))) {
492 
493  updateStatus("You selected the following parent Class: " + parentClass
494  + "Your Classname does not contain the dedicated Class Group Code: *" + objectClassCode
495  + ".cls");
496  buttonCreateObject.setEnabled(false);
497  return;
498  }
499  if ((!fileName.contains(objectClassCode + ".cls") && (parentClassCombo.getText().equals(OO_ABL)))) {
500  fileText.setText(fileName.replace(objectClassCode, ""));
501  return;
502  }
503  }
504 
505  if (this.checkRepository && parentClassCombo.getText().equals("") && (!isFileExisting)) {
506  updateStatus("Object should be member of Class \"" + this.rootClass + "\"");
507  return;
508  }
509 
510  if (checkRepository && (!isFileExisting)) {
511  // in standard level the hash code is auto generated. create is not supported
512 
513  if (this.paProject.getCustomLevel().equals("")) {
514  updateStatus("File unknown to Repository Manager. Please create manually in a seperate pA Session");
515  }
516  else {
517  updateStatus("File unknown to Repository Manager.");
518  }
519 
520  return;
521  }
522  updateStatus(null);
523  } catch (Exception e) {
524  logger.error(e.getMessage(), e);
525  }
526  }
527 
528  private void updateStatus(String message) {
529  setErrorMessage(message);
530  setPageComplete(message == null);
531  }
532 
533  public String getContainerName() {
534  return containerText.getText();
535  }
536 
537  public String getFileName() {
538  return fileText.getText();
539  }
540 
541  public void setFileName(String fileName) {
542  this.fileText.setText(fileName);
543  validateFileName();
544  }
545 
546  public String getAuthor() {
547  return authorText.getText();
548  }
549 
550  public String getShortDescription() {
551  return shortDescriptionText.getText();
552  }
553 
554  public String getProduct() {
555  return productText.getText();
556  }
557 
558  public String getProductDescription() {
559  return productDescriptionText.getText();
560  }
561 
562  public String getRelativePath() {
563  return relativePathText.getText();
564  }
565 
566  public String getVersion() {
567  return versionText.getText();
568  }
569 
570  public String getModule() {
571  return moduleText.getText();
572  }
573 
574  public String getModuleDescription() {
575  return moduleDescriptionText.getText();
576  }
577 
578  public Map<String, Object> getSettings() {
579  HashMap<String, Object> wizardSettings = new HashMap<>();
580  wizardSettings.put(GeneratorSettingsConstans.AUTHOR, this.getAuthor());
581  wizardSettings.put(GeneratorSettingsConstans.CONTAINER_NAME, this.getContainerName());
582  wizardSettings.put(GeneratorSettingsConstans.DESCRIPTION, this.getShortDescription());
583  wizardSettings.put("FileName", this.getFileName());
584  wizardSettings.put("Product", this.getProduct());
585  wizardSettings.put("ProductDescription", this.getProductDescription());
586  wizardSettings.put("RelativePath", this.getRelativePath());
587  wizardSettings.put("Module", this.getModule());
588  wizardSettings.put("ModuleDescription", this.getModuleDescription());
589  wizardSettings.put("Version", this.getVersion());
590  wizardSettings.put("ParentObject", this.getParentClass());
591  wizardSettings.put(GeneratorSettingsConstans.TEMPLATE_NAME, this.configList.get(currConfig).getTemplateName());
592  return wizardSettings;
593  }
594 
595  private Object getParentClass() {
596 
597  return parentClassCombo.getText();
598  }
599 
600  public void setFileDescriptionText(String description) {
601  if (shortDescriptionText != null) {
602  shortDescriptionText.setText(description);
603  }
604  }
605 
609  public boolean isCheckFileNameLength() {
610  return checkFileNameLength;
611  }
612 
616  public void setCheckFileNameLength(boolean checkFileNameLength) {
617  this.checkFileNameLength = checkFileNameLength;
618  }
619 
623  public boolean isCheckLowerCase() {
624  return checkLowerCase;
625  }
626 
630  public void setCheckLowerCase(boolean checkLowerCase) {
631  this.checkLowerCase = checkLowerCase;
632  }
633 
634  private void checkFileExisting(String fileName) {
635  if (checkRepository) {
636  isFileExisting = repositoryInformations.exitsInstanceInRepository(fileName);
637  } else {
638  isFileExisting = false;
639  }
640  }
641 
642  private void validateFileName() {
643  if (!this.paProject.isAvailable())
644  return;
645 
646  String fileName = getFileName();
647 
648  checkFileExisting(fileName);
649 
650  // Derive Product Key and Description
651  // Derive Module Key and Description
652  // Derive relative Path
653  if (fileName.length() > 0) {
654 
655  productText.setText(repositoryInformations.getProductName(fileName));
656  productDescriptionText.setText(repositoryInformations.getProductDescription(fileName));
657 
658  moduleText.setText(repositoryInformations.getModule(fileName));
659  moduleDescriptionText.setText(repositoryInformations.getModuleDescription(fileName));
660 
661  relativePathText.setText(repositoryInformations.getRelativePath(fileName));
662  } else {
663 
664  productText.setText("");
665  productDescriptionText.setText("");
666  moduleText.setText("");
667  moduleDescriptionText.setText("");
668  relativePathText.setText("");
669 
670  }
671 
672  // Derive Short Description from Repository
673 
674  if (isFileExisting) {
675 
676  shortDescriptionText.setText(repositoryInformations.getProgramShortDesc(fileName));
677 
678  }
679 
680  if (checkRepository) {
681 
682  String parentClass = "";
683  if (isFileExisting) {
684  parentClass = repositoryInformations.getClassName(repositoryInformations.getClassOBJ(fileName));
685 
686  } else if (fileName.contains(".cls")) {
687  parentClass = retrieveParentClass();
688  }
689 
690  if (!parentClass.isEmpty()) {
691  parentClassCombo.setText(parentClass);
692  }
693 
694  }
695 
696  validateInput();
697  }
698 
699  private String retrieveParentClass() {
700 
701  String fileName = getFileName();
702 
703  for (Map.Entry<String, String> entry : classGroupCode.entrySet()) {
704 
705  String classCode = entry.getValue();
706 
707  if ((classCode != null) && (!classCode.isEmpty()) && (classCode.length() == 3)) {
708  classCode = classCode + ".cls";
709  if (fileName.contains(classCode)) {
710  return entry.getKey();
711  }
712  }
713  }
714 
715  return OO_ABL;
716  }
717 
724  private String[] getConfigKeys(LinkedHashMap<String, WizardMainPageConfiguration> configList) {
725  return configList.keySet().stream().toArray(String[]::new);
726  }
727 }
WizardMainPage(PaProject paProject, Map< String, WizardMainPageConfiguration > configs, RepositoryDelegate repositoryDelegate, RepositoryInformations repositoryInformations, StartparameterReader startparameterReader)
void setCheckFileNameLength(boolean checkFileNameLength)
void setShortDescription(String shortDescription)