From 1cfe85eda1c77f3ea4421d59f326e4b535b011bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A1udio=20Gomes?= Date: Sat, 30 Mar 2024 12:32:50 -0300 Subject: [PATCH] wip combo image logo chooser --- .../pirilampo/core/compilers/Compiler.java | 26 ++++---- .../core/compilers/FolderToHTMLCompiler.java | 5 +- .../core/compilers/FolderToPDFCompiler.java | 3 +- .../core/enums/FileExtensionEnum.java | 25 ++++++++ .../core/compilers/CompilerTest.java | 15 +++-- .../gui/component/FileChooserComponent.java | 14 +++-- .../ProjectLogoChooserComponent.java | 62 ------------------- .../pirilampo/gui/form/MainForm.java | 30 ++++++--- .../clagomess/pirilampo/gui/ui/MainUI.java | 4 +- .../ProjectLogoChooserComponentTest.java | 28 --------- 10 files changed, 86 insertions(+), 126 deletions(-) create mode 100644 core/src/main/java/com/github/clagomess/pirilampo/core/enums/FileExtensionEnum.java delete mode 100644 gui/src/main/java/com/github/clagomess/pirilampo/gui/component/ProjectLogoChooserComponent.java delete mode 100644 gui/src/test/java/com/github/clagomess/pirilampo/gui/component/ProjectLogoChooserComponentTest.java diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/Compiler.java b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/Compiler.java index 532e81b..eec429f 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/Compiler.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/Compiler.java @@ -2,6 +2,7 @@ import com.github.clagomess.pirilampo.core.dto.FeatureMetadataDto; import com.github.clagomess.pirilampo.core.dto.ParametersDto; +import com.github.clagomess.pirilampo.core.enums.FileExtensionEnum; import com.github.clagomess.pirilampo.core.fi.UIProgressFI; import lombok.Getter; import lombok.Setter; @@ -13,7 +14,6 @@ import java.time.Instant; import java.util.*; import java.util.regex.Matcher; -import java.util.regex.Pattern; import static com.github.clagomess.pirilampo.core.enums.CompilationArtifactEnum.HTML; import static com.github.clagomess.pirilampo.core.enums.CompilationTypeEnum.FOLDER; @@ -26,29 +26,31 @@ public abstract class Compiler { @Setter protected float progressCount = 0; - - private final Pattern pFeatureExt = Pattern.compile("\\.feature$", Pattern.CASE_INSENSITIVE); - public String getFeatureExtension(File f){ - Matcher matcher = pFeatureExt.matcher(f.getName()); + public String getFileExtension(File f, FileExtensionEnum extension){ + Matcher matcher = extension.getPattern().matcher(f.getName()); return matcher.find() ? matcher.group(0) : null; } - protected Set listFolder(File curDir) throws Exception { + public Set listFolder(File curDir, FileExtensionEnum extension) throws Exception { Set buffer = new TreeSet<>(); - listFolder(buffer, curDir); + listFolder(buffer, curDir, extension); return buffer; } - private void listFolder(Set buffer, File curDir) throws Exception { + private void listFolder(Set buffer, File curDir, FileExtensionEnum extension) throws Exception { File[] filesList = curDir.listFiles(); - if(filesList == null) throw new Exception("*.features not found"); + if(filesList == null) throw new FileNotFoundException(); for (File f : filesList) { - if (f.isDirectory()) listFolder(buffer, f); + if (f.isDirectory()) listFolder(buffer, f, extension); + if(!f.isFile()) continue; + + String ext = getFileExtension(f, extension); + if(ext == null) continue; - if (f.isFile() && ".feature".equalsIgnoreCase(getFeatureExtension(f))) { + if (extension.getExtensions().contains(ext.toLowerCase())) { buffer.add(f); } } @@ -67,7 +69,7 @@ protected FeatureMetadataDto getFeatureMetadata(ParametersDto parameters, File f .trim(); FeatureMetadataDto result = new FeatureMetadataDto(); - result.setName(feature.getName().replace(getFeatureExtension(feature), "")); + result.setName(feature.getName().replace(getFileExtension(feature, FileExtensionEnum.FEATURE), "")); result.setId(htmlFeatureRoot + "_" + result.getName()); result.setIdHtml(result.getId() + ".html"); result.setIdFeature(result.getId() + ".feature"); diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompiler.java b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompiler.java index 81dac59..ba4eeaa 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompiler.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompiler.java @@ -5,6 +5,7 @@ import com.github.clagomess.pirilampo.core.dto.MenuDto; import com.github.clagomess.pirilampo.core.dto.ParametersDto; import com.github.clagomess.pirilampo.core.enums.DiffEnum; +import com.github.clagomess.pirilampo.core.enums.FileExtensionEnum; import com.github.clagomess.pirilampo.core.parsers.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; @@ -47,7 +48,7 @@ protected DiffEnum diffMaster(FeatureMetadataDto featureMetadataDto, File featur if(parameters.getProjectMasterSource() == null) return DiffEnum.NOT_COMPARED; if(masterFiles == null){ - masterFiles = listFolder(parameters.getProjectMasterSource()).stream() + masterFiles = listFolder(parameters.getProjectMasterSource(), FileExtensionEnum.FEATURE).stream() .map(item -> new FeatureMasterDto( getFilePathWithoutAbsolute(parameters.getProjectMasterSource(), item), item @@ -146,7 +147,7 @@ public void build() throws Exception { progressCount = 0; progress.setProgress(-1); - Set arquivos = listFolder(parameters.getProjectSource()); + Set arquivos = listFolder(parameters.getProjectSource(), FileExtensionEnum.FEATURE); if(arquivos.isEmpty()) return; File outArtifact = getOutArtifact(parameters); diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToPDFCompiler.java b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToPDFCompiler.java index cc8f611..92862d2 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToPDFCompiler.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToPDFCompiler.java @@ -1,6 +1,7 @@ package com.github.clagomess.pirilampo.core.compilers; import com.github.clagomess.pirilampo.core.dto.ParametersDto; +import com.github.clagomess.pirilampo.core.enums.FileExtensionEnum; import com.github.clagomess.pirilampo.core.parsers.GherkinDocumentParser; import com.github.clagomess.pirilampo.core.parsers.PdfParser; import lombok.extern.slf4j.Slf4j; @@ -32,7 +33,7 @@ public void build() throws Exception { progressCount = 0; progress.setProgress(-1); - Set arquivos = listFolder(parameters.getProjectSource()); + Set arquivos = listFolder(parameters.getProjectSource(), FileExtensionEnum.FEATURE); if(arquivos.isEmpty()) return; File outArtifact = getOutArtifact(parameters); diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/enums/FileExtensionEnum.java b/core/src/main/java/com/github/clagomess/pirilampo/core/enums/FileExtensionEnum.java new file mode 100644 index 0000000..ec085ff --- /dev/null +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/enums/FileExtensionEnum.java @@ -0,0 +1,25 @@ +package com.github.clagomess.pirilampo.core.enums; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.regex.Pattern; + +@Getter +@RequiredArgsConstructor +public enum FileExtensionEnum { + FEATURE( + Collections.singletonList(".feature"), + Pattern.compile("\\.feature$", Pattern.CASE_INSENSITIVE) + ), + IMAGE( + Arrays.asList(".jpg", ".jpeg", ".png"), + Pattern.compile("\\.(jpg|jpeg|png)$", Pattern.CASE_INSENSITIVE) + ); + + private final List extensions; + private final Pattern pattern; +} diff --git a/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/CompilerTest.java b/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/CompilerTest.java index 0759046..87b89c5 100644 --- a/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/CompilerTest.java +++ b/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/CompilerTest.java @@ -4,6 +4,7 @@ import com.github.clagomess.pirilampo.core.dto.ParametersDto; import com.github.clagomess.pirilampo.core.enums.CompilationArtifactEnum; import com.github.clagomess.pirilampo.core.enums.CompilationTypeEnum; +import com.github.clagomess.pirilampo.core.enums.FileExtensionEnum; import lombok.extern.slf4j.Slf4j; import lombok.val; import org.apache.commons.io.FileUtils; @@ -12,6 +13,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.EnumSource; import java.io.*; import java.util.Objects; @@ -22,16 +24,17 @@ public class CompilerTest extends Common { private final Compiler compiler = new Compiler(){}; - @Test - public void listFolder() throws Exception { - val result = compiler.listFolder(featureFolder); + @ParameterizedTest + @EnumSource(FileExtensionEnum.class) + public void listFolder(FileExtensionEnum extension) throws Exception { + val result = compiler.listFolder(featureFolder, extension); Assertions.assertThat(result.size()).isGreaterThanOrEqualTo(0); } @Test public void listFolder_NotDir() { - assertThrowsExactly(Exception.class, () -> { - compiler.listFolder(new File("")); + assertThrowsExactly(FileNotFoundException.class, () -> { + compiler.listFolder(new File(""), FileExtensionEnum.FEATURE); }); } @@ -151,7 +154,7 @@ public void getOutArtifact( "target/foo/AbC.feature.jpg,", }) public void getFeatureExtension(String source, String expected){ - assertEquals(expected, compiler.getFeatureExtension(new File(source))); + assertEquals(expected, compiler.getFileExtension(new File(source), FileExtensionEnum.FEATURE)); } @ParameterizedTest diff --git a/gui/src/main/java/com/github/clagomess/pirilampo/gui/component/FileChooserComponent.java b/gui/src/main/java/com/github/clagomess/pirilampo/gui/component/FileChooserComponent.java index 9019772..65ea654 100644 --- a/gui/src/main/java/com/github/clagomess/pirilampo/gui/component/FileChooserComponent.java +++ b/gui/src/main/java/com/github/clagomess/pirilampo/gui/component/FileChooserComponent.java @@ -10,19 +10,19 @@ import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import java.io.File; +import java.util.LinkedList; +import java.util.List; import java.util.prefs.Preferences; @Slf4j public class FileChooserComponent extends JPanel { + private final List onChangeList = new LinkedList<>(); final JTextField text = new JTextField(); private final JButton button; @Setter private FileChooserFI config = (fc) -> {}; - @Setter - private OnChangeFI onChange = file -> {}; - @Getter private File value = null; @@ -57,7 +57,7 @@ public void changedUpdate(DocumentEvent e) { public void update(){ value = StringUtils.isNotBlank(text.getText()) ? new File(text.getText()) : null; - onChange.change(value); + onChangeList.forEach(ch -> ch.change(value)); } }); @@ -84,9 +84,13 @@ public void update(){ add(button); } + public void addOnChange(OnChangeFI value){ + onChangeList.add(value); + } + public void reset(){ this.value = null; - onChange.change(null); + onChangeList.forEach(ch -> ch.change(null)); this.text.setText(null); } diff --git a/gui/src/main/java/com/github/clagomess/pirilampo/gui/component/ProjectLogoChooserComponent.java b/gui/src/main/java/com/github/clagomess/pirilampo/gui/component/ProjectLogoChooserComponent.java deleted file mode 100644 index a3a0a6f..0000000 --- a/gui/src/main/java/com/github/clagomess/pirilampo/gui/component/ProjectLogoChooserComponent.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.github.clagomess.pirilampo.gui.component; - -import lombok.Getter; -import lombok.Setter; -import lombok.extern.slf4j.Slf4j; -import net.miginfocom.swing.MigLayout; -import org.apache.commons.lang3.StringUtils; - -import javax.swing.*; -import java.io.File; - -@Slf4j -public class ProjectLogoChooserComponent extends JPanel { - final JTextField text = new JTextField(); - private final JButton button = new JButton("Choose"); - - @Setter - private ProjectSourceFI projectSource = () -> null; - - @Getter - private String value = null; - - public void setValue(String value){ - this.value = StringUtils.stripToNull(value); - text.setText(this.value); - } - - public ProjectLogoChooserComponent() { - setLayout(new MigLayout("insets 0 0 0 0", "[grow,fill]")); - - button.addActionListener(l -> choose()); - - add(text, "width 100%"); - add(button); - } - - protected void choose(){ - File source = projectSource.get(); - if(source == null || !source.isDirectory()){ - JOptionPane.showMessageDialog( - null, - " is required", - "ERROR", - JOptionPane.ERROR_MESSAGE - ); - return; - } - - log.info("impl"); //@TODO: impl: "jpg", "jpeg", "png" - } - - public void setEnabled(boolean enabled){ - setValue(null); - this.text.setEditable(enabled); - this.button.setEnabled(enabled); - } - - @FunctionalInterface - public interface ProjectSourceFI { - File get(); - } -} diff --git a/gui/src/main/java/com/github/clagomess/pirilampo/gui/form/MainForm.java b/gui/src/main/java/com/github/clagomess/pirilampo/gui/form/MainForm.java index e955b1a..d82bfdf 100644 --- a/gui/src/main/java/com/github/clagomess/pirilampo/gui/form/MainForm.java +++ b/gui/src/main/java/com/github/clagomess/pirilampo/gui/form/MainForm.java @@ -1,15 +1,13 @@ package com.github.clagomess.pirilampo.gui.form; +import com.github.clagomess.pirilampo.core.compilers.Compiler; import com.github.clagomess.pirilampo.core.dto.ParametersDto; -import com.github.clagomess.pirilampo.core.enums.CompilationArtifactEnum; -import com.github.clagomess.pirilampo.core.enums.CompilationTypeEnum; -import com.github.clagomess.pirilampo.core.enums.HtmlPanelToggleEnum; -import com.github.clagomess.pirilampo.core.enums.LayoutPdfEnum; +import com.github.clagomess.pirilampo.core.enums.*; import com.github.clagomess.pirilampo.gui.component.ColorChooserComponent; import com.github.clagomess.pirilampo.gui.component.FileChooserComponent; -import com.github.clagomess.pirilampo.gui.component.ProjectLogoChooserComponent; import com.github.clagomess.pirilampo.gui.component.RadioButtonGroupComponent; import com.github.clagomess.pirilampo.gui.util.AppenderUtil; +import lombok.extern.slf4j.Slf4j; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; @@ -24,8 +22,10 @@ import static com.github.clagomess.pirilampo.core.enums.LayoutPdfEnum.LANDSCAPE; import static com.github.clagomess.pirilampo.core.enums.LayoutPdfEnum.PORTRAIT; +@Slf4j public class MainForm { private final ParametersDto defaultDto = new ParametersDto(); + private final Compiler compiler = new Compiler(){}; // source public final RadioButtonGroupComponent rbCompilationType = new RadioButtonGroupComponent<>(Arrays.asList( @@ -60,8 +60,22 @@ public class MainForm { // project public final JTextField txtProjectName = new JTextField(defaultDto.getProjectName()); public final JTextField txtProjectVersion = new JTextField(defaultDto.getProjectVersion()); - public final ProjectLogoChooserComponent fcProjectLogo = new ProjectLogoChooserComponent() {{ - setProjectSource(fcProjectSource::getValue); + public final JComboBox fcProjectLogo = new JComboBox() {{ + // @TODO: fix problem with width + + fcProjectSource.addOnChange(value -> { + fcProjectLogo.removeAllItems(); + if(value == null) return; + + try { + compiler.listFolder(fcProjectSource.getValue(), FileExtensionEnum.IMAGE).stream() + .map(item -> compiler.getFilePathWithoutAbsolute(fcProjectSource.getValue(), item)) + .forEach(fcProjectLogo::addItem); + }catch (Throwable e){ + log.warn(e.getMessage()); + } + }); + rbCompilationType.addOnChange(value -> setEnabled(value != FEATURE)); }}; @@ -126,7 +140,7 @@ public ParametersDto toDto(){ ParametersDto dto = new ParametersDto(); dto.setProjectName(txtProjectName.getText()); dto.setProjectVersion(txtProjectVersion.getText()); - dto.setProjectLogo(fcProjectLogo.getValue()); + dto.setProjectLogo((String) fcProjectLogo.getSelectedItem()); dto.setLayoutPdf(rbLayoutPdfEnum.getSelectedValue()); dto.setHtmlPanelToggle(rbHtmlPanelToggle.getSelectedValue()); dto.setMenuColor(ccMenuColor.getValue()); diff --git a/gui/src/main/java/com/github/clagomess/pirilampo/gui/ui/MainUI.java b/gui/src/main/java/com/github/clagomess/pirilampo/gui/ui/MainUI.java index 7561877..592da35 100644 --- a/gui/src/main/java/com/github/clagomess/pirilampo/gui/ui/MainUI.java +++ b/gui/src/main/java/com/github/clagomess/pirilampo/gui/ui/MainUI.java @@ -80,7 +80,7 @@ public MainUI() { getRootPane().setDefaultButton(form.btnCompile); // add events - form.fcProjectSource.setOnChange(this::projectSourceOnChange); + form.fcProjectSource.addOnChange(this::projectSourceOnChange); pack(); setLocationRelativeTo(null); @@ -93,7 +93,7 @@ public void projectSourceOnChange(File file){ form.txtProjectName.setText(parameters.getProjectName()); form.txtProjectVersion.setText(parameters.getProjectVersion()); - form.fcProjectLogo.setValue(parameters.getProjectLogo()); + // form.fcProjectLogo.setValue(parameters.getProjectLogo()); //@TODO: check form.rbLayoutPdfEnum.setSelected(parameters.getLayoutPdf()); form.rbHtmlPanelToggle.setSelected(parameters.getHtmlPanelToggle()); form.ccMenuColor.setValue(parameters.getMenuColor()); diff --git a/gui/src/test/java/com/github/clagomess/pirilampo/gui/component/ProjectLogoChooserComponentTest.java b/gui/src/test/java/com/github/clagomess/pirilampo/gui/component/ProjectLogoChooserComponentTest.java deleted file mode 100644 index ceb099f..0000000 --- a/gui/src/test/java/com/github/clagomess/pirilampo/gui/component/ProjectLogoChooserComponentTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.github.clagomess.pirilampo.gui.component; - -import lombok.extern.slf4j.Slf4j; -import lombok.val; -import org.apache.commons.lang3.StringUtils; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -@Slf4j -public class ProjectLogoChooserComponentTest { - @ParameterizedTest - @CsvSource(value = { - ",false", - " ,false", - "foo/bar,true", - }, ignoreLeadingAndTrailingWhitespace = false) - public void setValue(String file, Boolean expected){ - log.info("{} - {}", file, expected); - - val plc = new ProjectLogoChooserComponent(); - plc.setValue(file); - - assertEquals(expected, plc.getValue() != null); - assertEquals(expected, StringUtils.stripToNull(plc.text.getText()) != null); - } -}