Skip to content

Commit

Permalink
wip combo image logo chooser
Browse files Browse the repository at this point in the history
  • Loading branch information
clagomess committed Mar 30, 2024
1 parent e36c992 commit 1cfe85e
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<File> listFolder(File curDir) throws Exception {
public Set<File> listFolder(File curDir, FileExtensionEnum extension) throws Exception {
Set<File> buffer = new TreeSet<>();

listFolder(buffer, curDir);
listFolder(buffer, curDir, extension);

return buffer;
}

private void listFolder(Set<File> buffer, File curDir) throws Exception {
private void listFolder(Set<File> 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);
}
}
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -146,7 +147,7 @@ public void build() throws Exception {
progressCount = 0;
progress.setProgress(-1);

Set<File> arquivos = listFolder(parameters.getProjectSource());
Set<File> arquivos = listFolder(parameters.getProjectSource(), FileExtensionEnum.FEATURE);
if(arquivos.isEmpty()) return;

File outArtifact = getOutArtifact(parameters);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -32,7 +33,7 @@ public void build() throws Exception {
progressCount = 0;
progress.setProgress(-1);

Set<File> arquivos = listFolder(parameters.getProjectSource());
Set<File> arquivos = listFolder(parameters.getProjectSource(), FileExtensionEnum.FEATURE);
if(arquivos.isEmpty()) return;

File outArtifact = getOutArtifact(parameters);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> extensions;
private final Pattern pattern;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
});
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OnChangeFI> 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;

Expand Down Expand Up @@ -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));
}
});

Expand All @@ -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);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<CompilationTypeEnum> rbCompilationType = new RadioButtonGroupComponent<>(Arrays.asList(
Expand Down Expand Up @@ -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<String> fcProjectLogo = new JComboBox<String>() {{
// @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));
}};

Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
Expand Down
Loading

0 comments on commit 1cfe85e

Please sign in to comment.