Skip to content

Commit

Permalink
Issue #57
Browse files Browse the repository at this point in the history
  • Loading branch information
tviegut committed Jan 3, 2023
1 parent 47e5427 commit d225e1d
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 108 deletions.
2 changes: 1 addition & 1 deletion CIMToolPlugin/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
id="au.com.langdale.cimtoole.wizards.ImportCopyrightTemplates"
name="Import Copyright Templates">
<description>
A wizard for importing copyright templates to be used in relevant profiles for a given project.
Import and/or configure copyright templates to be used in the artifacts generated by a project.
</description>
</wizard>
<wizard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
*/
package au.com.langdale.cimtoole.wizards;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
Expand All @@ -20,7 +22,6 @@
import au.com.langdale.cimtoole.project.Cache;
import au.com.langdale.cimtoole.project.Info;
import au.com.langdale.cimtoole.project.Task;
import au.com.langdale.kena.Composition;
import au.com.langdale.kena.OntModel;
import au.com.langdale.util.Jobs;

Expand All @@ -32,9 +33,9 @@ public void init(IWorkbench workbench, IStructuredSelection selection) {
setWindowTitle("Import Copyright Templates");
setNeedsProgressMonitor(true);
main.setTitle(getWindowTitle());
main.setDescription("Import copyright templates to be utilized by the builders of the given project.");
main.setMultiLineCopyrightSources(new String[]{"*.copyright-multi-line", "*.txt"});
main.setSingleLineCopyrightSources(new String[]{"*.copyright-single-line", "*.txt"});
main.setDescription("Import and/or configure copyright templates to be used in the artifacts generated by a project.");
main.setMultiLineCopyrightSources(new String[]{"*.txt", "*.copyright-multi-line"});
main.setSingleLineCopyrightSources(new String[]{"*.txt", "*.copyright-single-line"});
main.setSelected(selection);
}

Expand All @@ -47,10 +48,17 @@ public void addPages() {
public boolean performFinish() {
boolean result = false;
IProject project = main.getMultiLineCopyrightFile().getProject();
//
// Must first import copyrights before updating the OWL profiles...
IWorkspaceRunnable job = Task.importMultiLineCopyright(main.getMultiLineCopyrightFile(), main.getMultiLineCopyrightPathname());
job = Task.chain(job, Task.importSingleLineCopyright(main.getSingleLineCopyrightFile(), main.getSingleLineCopyrightPathname()));

String multilineCopyright = main.getMultiLineCopyrightTemplateTextForSelectedOption();
InputStream multilineInputStream = new ByteArrayInputStream(multilineCopyright.getBytes());
IFile multilineCopyrightTemplateFile = main.getMultiLineCopyrightFile();
IWorkspaceRunnable job = Task.importInputStreamToFile(multilineCopyrightTemplateFile, multilineInputStream);

String singleLineCopyright = main.getSingleLineCopyrightTemplateTextForSelectedOption();
InputStream singleLineInputStream = new ByteArrayInputStream(singleLineCopyright.getBytes());
IFile singleLineCopyrightTemplateFile = main.getSingleLineCopyrightFile();
job = Task.chain(job, Task.importInputStreamToFile(singleLineCopyrightTemplateFile, singleLineInputStream));

//
// Now proceed to update the OWL profile files with the new imported copyright...
IFolder profilesFolder = Info.getProfileFolder(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ public class ImportCopyrightTemplatesPage extends FurnishedWizardPage {

protected ProjectBinding projects = new ProjectBinding();
protected final boolean expectNewProject;
protected IProject currentSelectedProject;
protected IProject newProject;

public enum Option {
NO_TEMPLATE, DEFAULT_TEMPLATE, IMPORT_TEMPLATE
NONE_SELECTED, NO_TEMPLATE, DEFAULT_TEMPLATE, IMPORT_TEMPLATE
}

public String getMultiLineCopyrightTemplateTextForSelectedOption() {
Expand Down Expand Up @@ -118,6 +119,7 @@ public ImportCopyrightTemplatesPage(String pageName, boolean expectNewProject) {

public void setNewProject(IProject newProject) {
this.newProject = newProject;
this.currentSelectedProject = newProject;
}

public void setSelected(IStructuredSelection selection) {
Expand Down Expand Up @@ -245,34 +247,30 @@ protected Template define() {
Template template = null;
if (expectNewProject) {
template = Grid(
Group(Label(" Multiline file to import:")), //
Group(FileField("multi-line-source", "", multiLineCopyrightSources)),
Group(Label(" Single-line file to import:")), //
Group(FileField("single-line-source", "", singleLineCopyrightSources)),
Group(Grid(Group(RadioButton(Option.NO_TEMPLATE.name(), "Do not include copyrights"), RadioButton(Option.DEFAULT_TEMPLATE.name(), "Use default UCAIug copyrights"), RadioButton(Option.IMPORT_TEMPLATE.name(), "Select custom copyrights")))), //
Group(FileField("multi-line-source", "Multiline file to import: ", multiLineCopyrightSources)), //
Group(FileField("single-line-source", "Single-line file to import:", singleLineCopyrightSources)), //
Group(Label("* Valid extensions for custom copyright templates include: *.txt, *.copyright-multi-line and *.copyright-single-line")), //
Group(Label("")), //
Group(Label("multi-line-copyright-label", "Contents of Selected Copyright Template (multiline)")),
Group(DisplayArea("multi-line-copyright", 5, true)),
Group(Label("")), //
Group(RadioButton(Option.NO_TEMPLATE.name(), "Do not include copyrights in generated profiles and artifacts for this project")), //
Group(RadioButton(Option.DEFAULT_TEMPLATE.name(), "Use the default UCAIug Apache 2.0 copyright templates")), //
Group(RadioButton(Option.IMPORT_TEMPLATE.name(), "Select custom copyright template files to import for this project")), //
Group(Label("")), //
Group(Label("* Valid extensions for custom copyright templates include: *.copyright-multi-line, *.copyright-single-line and *.txt")), //
Group(Label("")), //
Group(Label("multi-line-copyright-label", "Contents of Selected Copyright Template (multiline)"), Label("single-line-copyright-label", "Contents of Selected Copyright Template (single-line)")),
Group(DisplayArea("multi-line-copyright", true), DisplayArea("single-line-copyright", true)));
Group(Label("single-line-copyright-label", "Contents of Selected Copyright Template (single-line)")),
Group(DisplayArea("single-line-copyright", 5, true)));
} else {
template = Grid(
Group(Label("Project")),
Group(CheckboxTableViewer("projects")),
Group(Grid(Group(RadioButton(Option.NO_TEMPLATE.name(), "Do not include copyrights"), RadioButton(Option.DEFAULT_TEMPLATE.name(), "Use default UCAIug copyrights"), RadioButton(Option.IMPORT_TEMPLATE.name(), "Select custom copyrights")))), //
Group(FileField("multi-line-source", "Multiline file to import: ", multiLineCopyrightSources)), //
Group(FileField("single-line-source", "Single-line file to import:", singleLineCopyrightSources)), //
Group(Label("* Valid extensions for custom copyright templates include: *.txt, *.copyright-multi-line and *.copyright-single-line")), //
Group(Label("")), //
Group(Label("multi-line-copyright-label", "Contents of Selected Copyright Template (multiline)")),
Group(DisplayArea("multi-line-copyright", 5, true)),
Group(Label("")), //
Group(Label("To have no copyrights applied to generated profiles and artifacts simply import copyright templates that are empty.")), //
Group(Label("Valid extensions for copyright templates include: *.copyright-multi-line, *.copyright-single-line and *.txt")), //
Group(Label("")), //
Group(Label(" Multiline file to import:")), //
Group(FileField("multi-line-source", "", multiLineCopyrightSources)),
Group(Label(" Single-line file to import:")), //
Group(FileField("single-line-source", "", singleLineCopyrightSources)),
Group(Label("")), //
Group(Label("multi-line-copyright-label", "Currently Assigned Copyright Template (multiline)"), Label("single-line-copyright-label", "Currently Assigned Copyright Template (single-line)")),
Group(DisplayArea("multi-line-copyright", true), DisplayArea("single-line-copyright", true)));
Group(Label("single-line-copyright-label", "Contents of Selected Copyright Template (single-line)")),
Group(DisplayArea("single-line-copyright", 5, true)));
}
return template;
}
Expand All @@ -286,47 +284,16 @@ protected void addBindings() {

@Override
public String validate() {
if (expectNewProject) {
if (getButton(Option.IMPORT_TEMPLATE.name()).getSelection()) {
multiLineCopyrightPathname = getText("multi-line-source").getText().trim();
if (multiLineCopyrightPathname.length() == 0)
return "A file containing a multiline copyright header template must be chosen for import.";

File multiLineCopyrightsource = new File(multiLineCopyrightPathname);
if (!multiLineCopyrightsource.canRead())
return "The chosen mulitline file cannot be read.";

singleLineCopyrightPathname = getText("single-line-source").getText().trim();
if (singleLineCopyrightPathname.length() == 0)
return "A file containing a single-line copyright header template must be chosen for import.";

File singleLineCopyrightsource = new File(singleLineCopyrightPathname);
if (!singleLineCopyrightsource.canRead())
return "The chosen single-line file cannot be read.";

IProject project = newProject;
if (project != null) {
String message = validateMultiLineCopyrightFileContents(multiLineCopyrightPathname);
if (message != null)
return message;

message = validateSingleLineCopyrightFileContents(singleLineCopyrightPathname);
if (message != null)
return message;

message = validateCopyrightFilesForConsistency(multiLineCopyrightPathname, singleLineCopyrightPathname);
if (message != null)
return message;
}
}
} else {
IProject project = (expectNewProject ? newProject : projects.getProject());

if (getButton(Option.IMPORT_TEMPLATE.name()).getSelection()) {
multiLineCopyrightPathname = getText("multi-line-source").getText().trim();
if (multiLineCopyrightPathname.length() == 0)
return "A file containing a multiline copyright header template must be chosen for import.";

File multiLineCopyrightsource = new File(multiLineCopyrightPathname);
if (!multiLineCopyrightsource.canRead())
return "The chosen mulitline file cannot be read.";
return "The chosen multiline file cannot be read.";

singleLineCopyrightPathname = getText("single-line-source").getText().trim();
if (singleLineCopyrightPathname.length() == 0)
Expand All @@ -336,7 +303,6 @@ public String validate() {
if (!singleLineCopyrightsource.canRead())
return "The chosen single-line file cannot be read.";

IProject project = projects.getProject();
if (project != null) {
String message = validateMultiLineCopyrightFileContents(multiLineCopyrightPathname);
if (message != null)
Expand All @@ -350,91 +316,133 @@ public String validate() {
if (message != null)
return message;
}
} else if (!getButton(Option.NO_TEMPLATE.name()).getSelection() && !getButton(Option.DEFAULT_TEMPLATE.name()).getSelection()) {
return "Choose a copyright configuration option for the project.";
}

return null;
}

@Override
public void refresh() {
if (expectNewProject) {
IProject project = (expectNewProject ? newProject : projects.getProject());
boolean projectChanged = (currentSelectedProject != project);

String currentMultiLineCopyright = getCurrentMultiLineCopyrightTemplate((!expectNewProject ? projects.getProject() : null));
String currentSingleLineCopyright = getCurrentSingleLineCopyrightTemplate((!expectNewProject ? projects.getProject() : null));
String defaultMultiLineCopyright = getDefaultMultiLineCopyrightTemplate();
String defaultSingleLineCopyright = getDefaultSingleLineCopyrightTemplate();

Option selection = Option.NONE_SELECTED;

if (expectNewProject || !projectChanged) {
if (getButton(Option.DEFAULT_TEMPLATE.name()).getSelection()) {
selection = Option.DEFAULT_TEMPLATE;
} else if (getButton(Option.IMPORT_TEMPLATE.name()).getSelection()) {
selection = Option.IMPORT_TEMPLATE;
} else if (getButton(Option.NO_TEMPLATE.name()).getSelection()) {
selection = Option.NO_TEMPLATE;
}
} else {
currentSelectedProject = project;
if (currentMultiLineCopyright != null && !"".equals(currentMultiLineCopyright)) {
if (defaultMultiLineCopyright.equals(currentMultiLineCopyright)) {
selection = Option.DEFAULT_TEMPLATE;
} else {
selection = Option.IMPORT_TEMPLATE;
}
} else {
selection = Option.NO_TEMPLATE;
}
getButton(Option.NO_TEMPLATE.name()).setSelection(selection == Option.NO_TEMPLATE);
getButton(Option.DEFAULT_TEMPLATE.name()).setSelection(selection == Option.DEFAULT_TEMPLATE);
getButton(Option.IMPORT_TEMPLATE.name()).setSelection(selection == Option.IMPORT_TEMPLATE);

}

initSelection(selection, currentMultiLineCopyright, defaultMultiLineCopyright, currentSingleLineCopyright, defaultSingleLineCopyright);
}

private void initSelection(Option selection, String currentMultiLineCopyright, String defaultMultiLineCopyright, String currentSingleLineCopyright, String defaultSingleLineCopyright) {
switch (selection) {
case DEFAULT_TEMPLATE:
if (getControl("multi-line-source").isEnabled()) {
getControl("multi-line-source").setEnabled(false);
getControl("multi-line-source-button").setEnabled(false);
}
setTextValue("multi-line-source", null);
setTextValue("multi-line-copyright", getDefaultMultiLineCopyrightTemplate());
setTextValue("multi-line-copyright-label", "Currently Assigned Copyright Template (multiline)");
setTextValue("multi-line-copyright", defaultMultiLineCopyright);
//
if (getControl("single-line-source").isEnabled()) {
getControl("single-line-source").setEnabled(false);
getControl("single-line-source-button").setEnabled(false);
}
setTextValue("single-line-source", null);
setTextValue("single-line-copyright", getDefaultSingleLineCopyrightTemplate());
} else if (getButton(Option.IMPORT_TEMPLATE.name()).getSelection()) {
setTextValue("single-line-copyright-label", "Currently Assigned Copyright Template (single-line)");
setTextValue("single-line-copyright", defaultSingleLineCopyright);
break;
case IMPORT_TEMPLATE:
if (!getControl("multi-line-source").isEnabled()) {
getControl("multi-line-source").setEnabled(true);
getControl("multi-line-source-button").setEnabled(true);
}
String pathname = getText("multi-line-source").getText().trim();
if ((pathname != null) && (pathname.length() != 0) && new File(pathname).canRead()) {
setTextValue("multi-line-copyright-label", "Contents of Selected Multiline Copyright Template");
setTextValue("multi-line-copyright", getFileContents(pathname));
} else {
setTextValue("multi-line-copyright", getCurrentMultiLineCopyrightTemplate(projects.getProject()));
setTextValue("multi-line-copyright-label", "Currently Assigned Copyright Template (multiline)");
setTextValue("multi-line-copyright", currentMultiLineCopyright);
}
//
if (!getControl("single-line-source").isEnabled()) {
getControl("single-line-source").setEnabled(true);
getControl("single-line-source-button").setEnabled(true);
}
pathname = getText("single-line-source").getText().trim();
if ((pathname != null) && (pathname.length() != 0) && new File(pathname).canRead()) {
setTextValue("single-line-copyright-label", "Contents of Selected Single-line Copyright Template");
setTextValue("single-line-copyright", getFileContents(pathname));
} else {
setTextValue("single-line-copyright", getCurrentSingleLineCopyrightTemplate(projects.getProject()));
setTextValue("single-line-copyright-label", "Currently Assigned Copyright Template (single-line)");
setTextValue("single-line-copyright", currentSingleLineCopyright);
}
} else if (getButton(Option.NO_TEMPLATE.name()).getSelection()) {
break;
case NO_TEMPLATE:
if (getControl("multi-line-source").isEnabled()) {
getControl("multi-line-source").setEnabled(false);
getControl("multi-line-source-button").setEnabled(false);
}
setTextValue("multi-line-source", null);
setTextValue("multi-line-copyright-label", "Currently Assigned Copyright Template (multiline)");
setTextValue("multi-line-copyright", null);
//
if (getControl("single-line-source").isEnabled()) {
getControl("single-line-source").setEnabled(false);
getControl("single-line-source-button").setEnabled(false);
}
setTextValue("single-line-source", null);
setTextValue("single-line-copyright-label", "Currently Assigned Copyright Template (single-line)");
setTextValue("single-line-copyright", null);
} else {
getButton(Option.NO_TEMPLATE.name()).setSelection(true);
break;
case NONE_SELECTED:
if (getControl("multi-line-source").isEnabled()) {
getControl("multi-line-source").setEnabled(false);
getControl("multi-line-source-button").setEnabled(false);
}
setTextValue("multi-line-source", null);
setTextValue("multi-line-copyright", null);
setTextValue("multi-line-copyright-label", "Currently Assigned Copyright Template (multiline)");
setTextValue("multi-line-copyright", defaultMultiLineCopyright);
//
getButton(Option.NO_TEMPLATE.name()).setSelection(true);
if (getControl("single-line-source").isEnabled()) {
getControl("single-line-source").setEnabled(false);
getControl("single-line-source-button").setEnabled(false);
}
setTextValue("single-line-source", null);
setTextValue("signle-line-copyright", null);
}
} else {
String pathname = getText("multi-line-source").getText().trim();
if ((pathname != null) && (pathname.length() != 0) && new File(pathname).canRead()) {
setTextValue("multi-line-copyright-label", "Contents of Selected Multiline Copyright Template");
setTextValue("multi-line-copyright", getFileContents(pathname));
} else {
setTextValue("multi-line-copyright-label", "Currently Assigned Copyright Template (multiline)");
setTextValue("multi-line-copyright", getCurrentMultiLineCopyrightTemplate(projects.getProject()));
}
//
pathname = getText("single-line-source").getText().trim();
if ((pathname != null) && (pathname.length() != 0) && new File(pathname).canRead()) {
setTextValue("single-line-copyright-label", "Contents of Selected Single-line Copyright Template");
setTextValue("single-line-copyright", getFileContents(pathname));
} else {
setTextValue("single-line-copyright-label", "Currently Assigned Copyright Template (single-line)");
setTextValue("single-line-copyright", getCurrentSingleLineCopyrightTemplate(projects.getProject()));
}
setTextValue("single-line-copyright", defaultSingleLineCopyright);
break;
}
}
};
Expand Down
Loading

0 comments on commit d225e1d

Please sign in to comment.