Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 575165 - [Passage][Operator] License Agreement management: gui #869

Merged
merged 1 commit into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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<String> 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;
}
Expand All @@ -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<AgreementFormat> 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<String> definedName() {
Expand All @@ -111,4 +136,24 @@ private Agreements agreements() throws LicensingException {
return service.get();
}

private Optional<Agreement> 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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -45,12 +46,21 @@ String mime() {
}

static class Supported implements Supplier<List<AgreementFormat>> {
// TODO: get rid of the code in constructor: use CashingFunction from
// ru.arsysop.lang
private final List<AgreementFormat> supported = Arrays.asList(//
new AgreementFormat(".txt", "Text file (*.txt)", "text/plain") //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
);

@Override
public List<AgreementFormat> 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<AgreementFormat> forFile(String name) {
return supported.stream()//
.filter(format -> name.endsWith(format.extention))//
.findAny();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public Optional<File> get() {
FileDialog dialog = new FileDialog(shell);
List<AgreementFormat> 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()));
}

Expand Down