Skip to content

Commit

Permalink
Bug 548113 provide ability to create user from issue license wizard
Browse files Browse the repository at this point in the history
Add RootClassifierWizard

Signed-off-by: Alexander Fedorov <alexander.fedorov@arsysop.ru>
  • Loading branch information
ruspl-afed committed Feb 20, 2020
1 parent 8dfa0a1 commit f6a4620
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class WorkbenchMessages extends NLS {
public static String LocWokbench_e_saving;
public static String RedoHandler_label_base;
public static String RedoHandler_label_pattern;
public static String RootClassifierWizard_message_e_create;
public static String RootClassifierWizard_title_e_create;
public static String SelectFromDialog_e_null_appearance;
public static String SelectFromDialog_e_null_initial;
public static String SelectFromDialog_e_null_shell;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ LocWokbench_e_nothing_to_select=Nothing to select from
LocWokbench_e_saving=Error saving resource
RedoHandler_label_base=Redo
RedoHandler_label_pattern={0} {1}
RootClassifierWizard_message_e_create=Create operation failed
RootClassifierWizard_title_e_create=Create
SelectFromDialog_e_null_appearance=Appearance must not be null
SelectFromDialog_e_null_initial=Initial selection must not be null
SelectFromDialog_e_null_shell=Shell must not be null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
* Creates new licensing object, either root of resource or not. Can be asked
* for a reference to a created instance.
*
* @param <N> a sub-type of {@link BaseClassifierWizardPage} to be used
* @since 0.6
*
*/
public abstract class BaseClassifierWizard extends Wizard {
public abstract class BaseClassifierWizard<N extends BaseClassifierWizardPage> extends Wizard {

protected final BaseClassifierWizardPage newClassifierPage;
protected final N newClassifierPage;

/**
* Creates a new wizard with given metadata and initializer
Expand Down Expand Up @@ -58,8 +59,7 @@ protected BaseClassifierWizard(ClassifierMetadata metadata, ClassifierInitialize
* not be <code>null</code>
* @return a just created instance of the {@link WizardPage}
*/
protected abstract BaseClassifierWizardPage createNewClassifierPage(ClassifierMetadata metadata,
ClassifierInitializer initializer);
protected abstract N createNewClassifierPage(ClassifierMetadata metadata, ClassifierInitializer initializer);

/**
* An optional reference to a created instance, may be empty in case of errors
Expand All @@ -68,7 +68,8 @@ protected abstract BaseClassifierWizardPage createNewClassifierPage(ClassifierMe
* @return created {@link EObject} or {@link Optional#empty()}
*/
public Optional<EObject> created() {
return newClassifierPage.created();
return newClassifierPage.candidate().eResource() != null ? Optional.of(newClassifierPage.candidate())
: Optional.empty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
package org.eclipse.passage.loc.internal.workbench.wizards;

import java.util.Objects;
import java.util.Optional;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
Expand All @@ -32,7 +31,7 @@

/**
* Provides UI to to fulfill the field values for an object to be created,
* either root of resource or not. Can be asked for a reference to a created
* either root of resource or not. Can be asked for a reference to a candidate
* instance.
*
* @since 0.6
Expand Down Expand Up @@ -143,7 +142,7 @@ public void setVisible(boolean visible) {
*
* @see BaseClassifierWizard#created()
*/
protected Optional<EObject> created() {
return eObject.eResource() != null ? Optional.of(eObject) : Optional.empty();
protected EObject candidate() {
return eObject;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.eclipse.passage.loc.internal.workbench.wizards;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.passage.lic.base.LicensingResults;
import org.eclipse.passage.lic.emf.ecore.EditingDomainRegistry;
import org.eclipse.passage.lic.emf.edit.ClassifierInitializer;
import org.eclipse.passage.lic.jface.dialogs.LicensingResultDialogs;
import org.eclipse.passage.loc.internal.workbench.ClassifierMetadata;
import org.eclipse.passage.loc.internal.workbench.i18n.WorkbenchMessages;
import org.eclipse.passage.loc.workbench.LocWokbench;

/**
* Creates new root licensing object. Can be asked for a reference to a created
* instance.
*
* @since 0.6
*
*/
public final class RootClassifierWizard extends BaseClassifierWizard<RootClassifierWizardPage> {

private final EditingDomainRegistry<?> domainRegistry;

public RootClassifierWizard(ClassifierMetadata metadata, ClassifierInitializer initializer,
EditingDomainRegistry<?> registry) {
super(metadata, initializer);
this.domainRegistry = registry;
}

@Override
protected RootClassifierWizardPage createNewClassifierPage(ClassifierMetadata metadata,
ClassifierInitializer initializer) {
return new RootClassifierWizardPage(metadata, initializer, domainRegistry.getFileExtension());
}

@Override
public boolean performFinish() {
try {
getContainer().run(false, false, m -> store(newClassifierPage.path(), newClassifierPage.candidate()));
return true;
} catch (Exception exception) {
process(exception);
return false;
}
}

protected void store(String path, EObject candidate) {
URI fileURI = URI.createFileURI(path);
Resource resource = resourceSet().createResource(fileURI);
resource.getContents().add(candidate);
LocWokbench.save(resource);
domainRegistry.registerSource(fileURI.toFileString());
}

protected ResourceSet resourceSet() {
if (domainRegistry instanceof IEditingDomainProvider) {
IEditingDomainProvider edProvider = (IEditingDomainProvider) domainRegistry;
return edProvider.getEditingDomain().getResourceSet();
}
return new ResourceSetImpl();
}

protected void process(Exception exception) {
LicensingResultDialogs.openMessageDialog(getShell(), WorkbenchMessages.RootClassifierWizard_title_e_create, //
LicensingResults.createError(WorkbenchMessages.RootClassifierWizard_message_e_create,
getClass().getName(), exception));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.eclipse.passage.loc.internal.workbench.wizards;

import java.io.File;

import org.eclipse.passage.lic.emf.edit.ClassifierInitializer;
import org.eclipse.passage.loc.internal.workbench.ClassifierMetadata;
import org.eclipse.passage.loc.internal.workbench.i18n.WorkbenchMessages;
import org.eclipse.passage.loc.workbench.LocWokbench;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

/**
* Provides UI to to fulfill the field values for a root classifier to be
* created, including path to store the new resource.
*
* @since 0.6
*
* @see BaseClassifierWizardPage
*
*/
public final class RootClassifierWizardPage extends BaseClassifierWizardPage {

private final String extension;

protected Text textPath;
private Button buttonPath;

protected RootClassifierWizardPage(ClassifierMetadata metadata, ClassifierInitializer initializer,
String extension) {
super(RootClassifierWizardPage.class.getSimpleName(), metadata, initializer);
this.extension = extension;
}

@Override
protected void createFieldControls(Composite composite) {
super.createFieldControls(composite);
textPath = new Text(composite, SWT.BORDER);
{
GridData data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.grabExcessHorizontalSpace = true;
data.horizontalSpan = 1;
textPath.setLayoutData(data);
}
buttonPath = new Button(composite, SWT.PUSH);
buttonPath.setText(WorkbenchMessages.CreateFileWizardPage_button_browse);
textPath.addModifyListener(validator);
buttonPath.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> selectPath()));
}

protected void selectPath() {
String selected = LocWokbench.selectSavePath(getShell(), extension);
if (selected != null) {
textPath.setText(selected);
}
}

@Override
protected void initControls(ClassifierInitializer initializer) {
super.initControls(initializer);
String basePath = basePath();
String fileName = initializer.newFileName();
String resourceURI = basePath + File.separator + fileName + '.' + extension;
textPath.setText(resourceURI);
}

protected String basePath() {
return System.getProperty("user.home"); //$NON-NLS-1$
}

protected String path() {
String text = textPath.getText();
if (!text.endsWith('.' + extension)) {
text = text + '.' + extension;
}
return text;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
Expand All @@ -41,11 +40,12 @@
import org.eclipse.passage.lic.emf.edit.ComposedAdapterFactoryProvider;
import org.eclipse.passage.lic.emf.edit.EditingDomainRegistryAccess;
import org.eclipse.passage.lic.jface.resource.LicensingImages;
import org.eclipse.passage.loc.internal.workbench.ClassifierMetadata;
import org.eclipse.passage.loc.internal.workbench.i18n.WorkbenchMessages;
import org.eclipse.passage.loc.internal.workbench.wizards.RootClassifierWizard;
import org.eclipse.passage.loc.jface.dialogs.FilteredSelectionDialog;
import org.eclipse.passage.loc.jface.dialogs.LabelSearchFilter;
import org.eclipse.passage.loc.workbench.viewers.DomainRegistryLabelProvider;
import org.eclipse.passage.loc.workbench.wizards.CreateFileWizard;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
Expand Down Expand Up @@ -91,18 +91,17 @@ public static void createDomainResource(IEclipseContext context, String domain,
EditingDomainRegistryAccess registryAccess = context.get(EditingDomainRegistryAccess.class);
ClassifierInitializer initializer = registryAccess.getClassifierInitializer(domain);
EditingDomainRegistry<?> registry = registryAccess.getDomainRegistry(domain);
EClass eClass = registry.getContentClassifier();

Wizard wizard = new CreateFileWizard(context, domain);
ClassifierMetadata metadata = new ClassifierMetadata(registry.getContentClassifier(),
registry.getContentIdentifierAttribute(), registry.getContentNameAttribute());
Wizard wizard = new RootClassifierWizard(metadata, initializer, registry);
Shell shell = context.get(Shell.class);
WizardDialog dialog = new WizardDialog(shell, wizard);
dialog.create();
dialog.setTitle(initializer.newObjectTitle());
dialog.setMessage(initializer.newFileMessage());

Shell createdShell = dialog.getShell();
createdShell.setText(initializer.newObjectMessage());
createdShell.setImage(LicensingImages.getImage(eClass.getName()));
createdShell.setImage(LicensingImages.getImage(registry.getContentClassifier().getName()));
int open = dialog.open();
if (open == Window.OK) {
LocWokbench.switchPerspective(context, perspectiveId);
Expand Down

0 comments on commit f6a4620

Please sign in to comment.