Skip to content

Commit

Permalink
[#1191] "Import Licenses" wizard should support import from archive
Browse files Browse the repository at this point in the history
ImportLicenseDialog is extended with proper hooks
  • Loading branch information
eparovyshnaya committed Feb 19, 2024
1 parent 7d6be71 commit fd8eaf7
Show file tree
Hide file tree
Showing 13 changed files with 444 additions and 271 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021, 2022 ArSysOp
* Copyright (c) 2021, 2024 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -9,6 +9,7 @@
*
* Contributors:
* ArSysOp - initial API and implementation
* ArSysOp - further support
*******************************************************************************/
package org.eclipse.passage.lic.internal.base.conditions;

Expand Down Expand Up @@ -43,12 +44,12 @@ public final class LicenseConditions implements Supplier<ServiceInvocationResult

private final Path file;
private final Supplier<ServiceInvocationResult<LicenseReadingService>> owner;
private final Libraries libraries;
private final Optional<Libraries> libraries;

public LicenseConditions(//
Path file, //
Supplier<ServiceInvocationResult<LicenseReadingService>> provider, //
Libraries libraries) {
Optional<Libraries> libraries) {
this.file = file;
this.owner = provider;
this.libraries = libraries;
Expand All @@ -68,7 +69,8 @@ private ServiceInvocationResult<Collection<ConditionPack>> fromProduct() {
}

private ServiceInvocationResult<Collection<ConditionPack>> fromLibraries() {
Optional<ServiceInvocationResult<List<LicenseReadingService>>> request = libraries.licenseReadingServices();
Optional<ServiceInvocationResult<List<LicenseReadingService>>> request = libraries
.flatMap(Libraries::licenseReadingServices);
if (!request.isPresent()) {
return new BaseServiceInvocationResult<>(Collections.emptyList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.passage.lic.jface.EquinoxPassageUI;
import org.eclipse.swt.widgets.Shell;

@SuppressWarnings("restriction")
public final class InspectLicenseHandler {

@Execute
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2022 ArSysOp
* Copyright (c) 2022, 2024 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -9,13 +9,15 @@
*
* Contributors:
* ArSysOp - initial API and implementation
* ArSysOp - further support
*******************************************************************************/
package org.eclipse.passage.lic.internal.jface.dialogs.licensing;

import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

import org.eclipse.passage.lic.api.ServiceInvocationResult;
Expand All @@ -30,10 +32,10 @@
final class AllConditionsFromLicenses implements Supplier<ServiceInvocationResult<Collection<ConditionPack>>> {

private final List<Path> licenses;
private final Libraries libraries;
private final Optional<Libraries> libraries;
private final LicenseReadingServiceRequest product;

AllConditionsFromLicenses(List<Path> licenses, Libraries libraries) {
AllConditionsFromLicenses(List<Path> licenses, Optional<Libraries> libraries) {
this.licenses = licenses;
this.libraries = libraries;
this.product = new LicenseReadingServiceRequest();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*******************************************************************************
* Copyright (c) 2020, 2024 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
* ArSysOp - further support
*******************************************************************************/
package org.eclipse.passage.lic.internal.jface.dialogs.licensing;

import java.util.Collection;
import java.util.stream.Collectors;

import org.eclipse.passage.lic.api.agreements.AgreementToAccept;
import org.eclipse.passage.lic.api.diagnostic.Diagnostic;
import org.eclipse.passage.lic.api.restrictions.ExaminationCertificate;
import org.eclipse.passage.lic.base.diagnostic.DiagnosticExplained;
import org.eclipse.passage.lic.base.diagnostic.NoErrors;
import org.eclipse.passage.lic.base.diagnostic.RequirementStatus;
import org.eclipse.passage.lic.base.diagnostic.RequirementsCoverage;
import org.eclipse.passage.lic.base.restrictions.ExaminationExplained;
import org.eclipse.passage.lic.equinox.ProductContacts;
import org.eclipse.passage.lic.internal.jface.i18n.LicenseStatusDialogMessages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;

public abstract class BaseLicenseStatusDialog extends NotificationDialog {

protected final ExaminationCertificate certificate;
protected final Diagnostic diagnostic;
private GoodIntention intention = new GoodIntention.Nope(); // truly mutable ^:(
private StyledText notice;

protected BaseLicenseStatusDialog(Shell shell, ExaminationCertificate certificate, Diagnostic diagnostic) {
super(shell);
this.certificate = certificate;
this.diagnostic = diagnostic;
}

public final GoodIntention goodIntention() {
return intention;
}

@Override
protected final void configureShell(Shell shell) {
super.configureShell(shell);
shell.setText(LicenseStatusDialogMessages.LicenseStatusDialog_title);
shell.setImage(getDefaultImage());
shell.setSize(840, 600);
}

@Override
protected final void buildUI(Composite parent) {
viewer = new HereTable<RequirementStatus>(parent, RequirementStatus.class) //
.withColumn(LicenseStatusDialogMessages.LicenseStatusDialog_column_id, //
600, RequirementStatus::feature)
.withColumn(LicenseStatusDialogMessages.LicenseStatusDialog_column_status, //
200, RequirementStatus::status)
.viewer();
notice = new StyledText(parent, SWT.BORDER | SWT.READ_ONLY);
notice.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
}

@Override
protected final void inplaceData() {
viewer.setInput(new RequirementsCoverage(certificate).get());
notice.setText(new ProductContacts().get());
}

@Override
protected final void initButtons() {
int button = 1;
new ButtonConfig(button++, this::requestLicense, //
LicenseStatusDialogMessages.LicenseStatusDialog_intention_request, //
LicenseStatusDialogMessages.LicenseStatusDialog_intention_request_tooltip, "")//$NON-NLS-1$
.reside(buttons);
new ButtonConfig(button++, this::importLicense, //
LicenseStatusDialogMessages.LicenseStatusDialog_intention_import, //
LicenseStatusDialogMessages.LicenseStatusDialog_intention_import_tooltip, "") //$NON-NLS-1$
.reside(buttons);
new ButtonConfig(button++, copy(), //
LicenseStatusDialogMessages.LicenseStatusDialog_intention_copy,
LicenseStatusDialogMessages.LicenseStatusDialog_intention_copy_tooltip, "") //$NON-NLS-1$
.reside(buttons);
if (haveUnacceptedAgreements()) {
new ButtonConfig(button++, this::exposeAgreements, //
LicenseStatusDialogMessages.LicenseStatusDialog_intention_accept,
LicenseStatusDialogMessages.LicenseStatusDialog_intention_accept_tooltip, "") //$NON-NLS-1$
.reside(buttons);
}
if (!new NoErrors().test(diagnostic)) {
new ButtonConfig(button++, this::diagnose, //
LicenseStatusDialogMessages.LicenseStatusDialog_intention_diagnose,
LicenseStatusDialogMessages.LicenseStatusDialog_intention_diagnose_tooltip, "") //$NON-NLS-1$
.reside(buttons);
}
}

@Override
protected final void updateButtonsEnablement() {
// do nothing
}

@Override
protected final void initMessage() {
new CertificateSummary(certificate).accept(this);
}

protected abstract GoodIntention requestLicenseIntention();

protected abstract GoodIntention importLicenseIntention();

protected abstract GoodIntention diagnoseIntention();

protected abstract GoodIntention exposeLicenseAgreementsIntention(Collection<AgreementToAccept> collection);

private void requestLicense() {
intention = requestLicenseIntention();
super.okPressed();
}

private void importLicense() {
intention = importLicenseIntention();
super.okPressed();
}

private void diagnose() {
intention = diagnoseIntention();
super.okPressed();
}

private void exposeAgreements() {
intention = exposeLicenseAgreementsIntention(toExpose(certificate.agreements()));
super.okPressed();
}

private Collection<AgreementToAccept> toExpose(Collection<AgreementToAccept> agreements) {
return agreements.stream()//
.filter(this::toExpose)//
.collect(Collectors.toList());
}

private boolean toExpose(AgreementToAccept agreement) {
return !agreement.acceptance().accepted() && !agreement.acceptance().error().isPresent();
}

private boolean haveUnacceptedAgreements() {
return certificate.agreements().stream()//
.filter(this::toExpose)//
.findAny()//
.isPresent();
}

private Runnable copy() {
return new CopyToClipboard(this::getShell, //
new ExaminationExplained(certificate), //
new DiagnosticExplained(diagnostic));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*******************************************************************************
* Copyright (c) 2024 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.jface.dialogs.licensing;

import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

import org.eclipse.passage.lic.internal.jface.i18n.ImportLicenseDialogMessages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public final class FromLocalFileSystem implements LicenseFilesControl {

private Text path;

@Override
public void install(Composite parent, Consumer<List<Path>> onLicenses) {
Composite composite = row(parent, 3);
new Label(composite, SWT.NONE).setText(ImportLicenseDialogMessages.ImportLicenseDialog_path_label);
path = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
path.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
Button browse = new Button(composite, SWT.PUSH);
browse.setText(ImportLicenseDialogMessages.ImportLicenseDialog_browse);
browse.addListener(SWT.Selection, e -> browseAndLoad(onLicenses));
}

private void browseAndLoad(Consumer<List<Path>> onLicenses) {
onLicenses.accept(browse());
}

private Composite row(Composite parent, int columns) {
Composite row = new Composite(parent, SWT.NONE);
row.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
row.setLayout(new GridLayout(columns, false));
return row;
}

private List<Path> browse() {
DirectoryDialog dialog = new DirectoryDialog(path.getShell(), SWT.OPEN | SWT.SHEET);
dialog.setText(ImportLicenseDialogMessages.ImportLicenseDialog_browse_dialog_title);
String folder = dialog.open();
if (folder == null) {
return Collections.emptyList();
}
path.setText(folder);
List<Path> licenses = new AllLicensesFromFolder(folder).get();
path.setData(licenses);
return licenses;
}

}
Loading

0 comments on commit fd8eaf7

Please sign in to comment.