From 1d968c19a605863e8581bbf4b33394e3a9b8c1d5 Mon Sep 17 00:00:00 2001 From: Elena Parovyshnaia Date: Wed, 11 Aug 2021 17:23:47 +0300 Subject: [PATCH] Bug 575165 - [Passage][Operator] License Agreement management: gui - custom renderer for License Agreement content file Signed-off-by: eparovyshnaya --- .../META-INF/MANIFEST.MF | 5 +- .../renderers/AgreementFileRenderer.java | 53 +++++++++++++++++-- .../emfforms/renderers/AgreementFormat.java | 32 +++++++---- .../renderers/LocatedAgreementFile.java | 4 +- 4 files changed, 75 insertions(+), 19 deletions(-) diff --git a/bundles/org.eclipse.passage.loc.agreements.emfforms/META-INF/MANIFEST.MF b/bundles/org.eclipse.passage.loc.agreements.emfforms/META-INF/MANIFEST.MF index 7eb57fb1b..d5298617a 100644 --- a/bundles/org.eclipse.passage.loc.agreements.emfforms/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.passage.loc.agreements.emfforms/META-INF/MANIFEST.MF @@ -15,6 +15,8 @@ Require-Bundle: org.eclipse.core.databinding;bundle-version="0.0.0", org.eclipse.emf.databinding;bundle-version="0.0.0", org.eclipse.emf.ecp.edit.swt;bundle-version="0.0.0", org.eclipse.emf.ecp.view.core.swt;bundle-version="0.0.0", + org.eclipse.passage.loc.api;bundle-version="0.0.0", + org.eclipse.emf.ecp.ui.view.swt;bundle-version="0.0.0", org.eclipse.emf.ecp.view.model.common;bundle-version="0.0.0", org.eclipse.emf.ecp.view.model.provider.xmi;bundle-version="0.0.0", org.eclipse.emf.ecp.view.template.model;bundle-version="0.0.0", @@ -29,8 +31,7 @@ Require-Bundle: org.eclipse.core.databinding;bundle-version="0.0.0", org.eclipse.passage.lic.jface;bundle-version="0.0.0", org.eclipse.passage.loc.agreements.core;bundle-version="0.0.0", org.eclipse.passage.loc.agreements.ui;bundle-version="0.0.0", - org.eclipse.passage.loc.workbench.emfforms;bundle-version="0.0.0", - org.eclipse.passage.loc.api;bundle-version="0.0.0" + org.eclipse.passage.loc.workbench.emfforms;bundle-version="0.0.0" Import-Package: javax.inject;version="1.0.0" Export-Package: org.eclipse.passage.loc.agreements.emfforms.parts;x-internal:=true, org.eclipse.passage.loc.agreements.emfforms.renderers;x-friends:="org.eclipse.passage.loc.features.emfforms" diff --git a/bundles/org.eclipse.passage.loc.agreements.emfforms/src/org/eclipse/passage/loc/agreements/emfforms/renderers/AgreementFileRenderer.java b/bundles/org.eclipse.passage.loc.agreements.emfforms/src/org/eclipse/passage/loc/agreements/emfforms/renderers/AgreementFileRenderer.java index 40d3b9ae6..8feee64e9 100644 --- a/bundles/org.eclipse.passage.loc.agreements.emfforms/src/org/eclipse/passage/loc/agreements/emfforms/renderers/AgreementFileRenderer.java +++ b/bundles/org.eclipse.passage.loc.agreements.emfforms/src/org/eclipse/passage/loc/agreements/emfforms/renderers/AgreementFileRenderer.java @@ -18,13 +18,20 @@ import javax.inject.Inject; +import org.eclipse.core.databinding.observable.IDecoratingObservable; +import org.eclipse.core.databinding.observable.IObservable; +import org.eclipse.core.databinding.observable.IObserving; +import org.eclipse.core.databinding.observable.value.IObservableValue; import org.eclipse.emf.ecp.view.spi.context.ViewModelContext; import org.eclipse.emf.ecp.view.spi.model.VControl; +import org.eclipse.emf.ecp.view.spi.swt.reporting.RenderingFailedReport; import org.eclipse.emf.ecp.view.template.model.VTViewTemplateProvider; import org.eclipse.emfforms.spi.common.report.ReportService; import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException; +import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedReport; import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding; import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider; +import org.eclipse.passage.lic.agreements.model.api.Agreement; import org.eclipse.passage.lic.api.LicensingException; import org.eclipse.passage.loc.internal.api.workspace.Agreements; import org.eclipse.passage.loc.internal.equinox.OperatorGearAware; @@ -35,6 +42,11 @@ import org.eclipse.swt.widgets.Control; @SuppressWarnings("restriction") +/** + * TODO: (1) validity: Workspace.Agreements must report existence for content + * file denoted by [file] attribute (2) rename: there should be a way to rename + * content file + */ public final class AgreementFileRenderer extends TextWithButtonRenderer { @Inject @@ -70,13 +82,14 @@ protected void locateAgreementContentFile() { try { reflect(reside(file.get())); } catch (Exception e) { -//TODO: expose error to user + getReportService().report(new RenderingFailedReport(e)); } } private String reside(File file) throws Exception { - Optional defined = definedName(); - String name = defined.orElse(file.getName()); + // rename in already defined name if any: + // String name = definedName().orElse(file.getName()); + String name = file.getName(); agreements().located(name).write(Files.readAllBytes(file.toPath())); return name; } @@ -85,8 +98,20 @@ private void reflect(String name) { if (definedName().orElse("").equals(name)) { //$NON-NLS-1$ return; } + reflectFileName(name); + reflectMimeType(name); + } + + private void reflectFileName(String name) { text.setText(name); - // TODO affect not only 'text' field, but also mime type + } + + private void reflectMimeType(String name) { + Optional format = new AgreementFormat.Supported().forFile(name); + if (format.isEmpty()) { + return; // cannot assist for not supported file types + } + agreement().ifPresent(agreement -> agreement.setMime(format.get().mime())); } private Optional definedName() { @@ -111,4 +136,24 @@ private Agreements agreements() throws LicensingException { return service.get(); } + private Optional agreement() { + try { + IObservableValue value = getModelValue(); + if (!IDecoratingObservable.class.isInstance(value)) { + return Optional.empty(); + } + IObservable decorated = ((IDecoratingObservable) value).getDecorated(); + if (!IObserving.class.isInstance(decorated)) { + return Optional.empty(); + } + Object source = ((IObserving) decorated).getObserved(); + if (!Agreement.class.isInstance(source)) { + return Optional.empty(); + } + return Optional.of((Agreement) source); + } catch (DatabindingFailedException e) { + getReportService().report(new DatabindingFailedReport(e)); + return Optional.empty(); + } + } } diff --git a/bundles/org.eclipse.passage.loc.agreements.emfforms/src/org/eclipse/passage/loc/agreements/emfforms/renderers/AgreementFormat.java b/bundles/org.eclipse.passage.loc.agreements.emfforms/src/org/eclipse/passage/loc/agreements/emfforms/renderers/AgreementFormat.java index 790a9e22e..35c66d0a6 100644 --- a/bundles/org.eclipse.passage.loc.agreements.emfforms/src/org/eclipse/passage/loc/agreements/emfforms/renderers/AgreementFormat.java +++ b/bundles/org.eclipse.passage.loc.agreements.emfforms/src/org/eclipse/passage/loc/agreements/emfforms/renderers/AgreementFormat.java @@ -14,29 +14,30 @@ import java.util.Arrays; import java.util.List; +import java.util.Optional; import java.util.function.Supplier; final class AgreementFormat { - private final String name; private final String extention; + private final String description; private final String mime; AgreementFormat(String name, String extention, String mime) { - this.name = name; - this.extention = extention; + this.extention = name; + this.description = extention; this.mime = mime; } - String name() { - return name; - } - String extention() { return extention; } - String extentionFilter() { + String description() { + return description; + } + + String name() { return '*' + extention; } @@ -45,12 +46,21 @@ String mime() { } static class Supported implements Supplier> { + // TODO: get rid of the code in constructor: use CashingFunction from + // ru.arsysop.lang + private final List supported = Arrays.asList(// + new AgreementFormat(".txt", "Text file (*.txt)", "text/plain") //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ + ); @Override public List get() { - return Arrays.asList(// - new AgreementFormat(".txt", "Text file (*.txt)", "text/plain") //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ - ); + return supported; + } + + public Optional forFile(String name) { + return supported.stream()// + .filter(format -> name.endsWith(format.extention))// + .findAny(); } } diff --git a/bundles/org.eclipse.passage.loc.agreements.emfforms/src/org/eclipse/passage/loc/agreements/emfforms/renderers/LocatedAgreementFile.java b/bundles/org.eclipse.passage.loc.agreements.emfforms/src/org/eclipse/passage/loc/agreements/emfforms/renderers/LocatedAgreementFile.java index 665ded2e8..e162f974f 100644 --- a/bundles/org.eclipse.passage.loc.agreements.emfforms/src/org/eclipse/passage/loc/agreements/emfforms/renderers/LocatedAgreementFile.java +++ b/bundles/org.eclipse.passage.loc.agreements.emfforms/src/org/eclipse/passage/loc/agreements/emfforms/renderers/LocatedAgreementFile.java @@ -40,8 +40,8 @@ public Optional get() { FileDialog dialog = new FileDialog(shell); List formats = new AgreementFormat.Supported().get(); dialog.setText("Point an agreement content file"); //$NON-NLS-1$ - dialog.setFilterExtensions(filters(formats, AgreementFormat::extentionFilter)); - dialog.setFilterExtensions(filters(formats, AgreementFormat::name)); + dialog.setFilterExtensions(filters(formats, AgreementFormat::extention)); + dialog.setFilterNames(filters(formats, AgreementFormat::description)); return file(Optional.ofNullable(dialog.open())); }