Skip to content

Commit

Permalink
Merge pull request #528 from dlsc-software-consulting-gmbh/features-a…
Browse files Browse the repository at this point in the history
…dd-px2em-converter

Add Px2EmConverter; Hide fields in ShowcaseOverview box that are not used
  • Loading branch information
dlemmermann authored Oct 23, 2023
2 parents c1a2ed6 + 665078d commit 9f7afab
Show file tree
Hide file tree
Showing 10 changed files with 548 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ public DoubleTextField() {
if (text == null || text.isEmpty() || SPECIAL_CASES.contains(text)) {
return 0.0;
}
return Double.parseDouble(text);
try {
return Double.parseDouble(text);
} catch (NumberFormatException e) {
return 0.0;
}
}, textProperty()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.scene.layout.VBox;
import org.apache.commons.lang3.StringUtils;

import java.time.format.DateTimeFormatter;

Expand All @@ -31,32 +32,17 @@ public ShowcaseOverviewBox(RealWorldApp app) {

@Override
protected Node createTopNode() {
Label locationHeader = new Label("LOCATION");
locationHeader.getStyleClass().add("field-title");
FieldGroup locationGroup = new FieldGroup("LOCATION", new String[]{"field-value"});
locationLabel = locationGroup.getLabel();

locationLabel = new Label();
locationLabel.getStyleClass().add("field-value");
locationLabel.setWrapText(true);
FieldGroup domainGroup = new FieldGroup("DOMAIN", new String[]{"field-value"});
domainLabel = domainGroup.getLabel();

Label domainHeader = new Label("DOMAIN");
domainHeader.getStyleClass().add("field-title");
FieldGroup createdByGroup = new FieldGroup("CREATED BY", new String[]{"field-value", "created-by-label"});
createdByLabel = createdByGroup.getLabel();

domainLabel = new Label();
domainLabel.getStyleClass().add("field-value");
domainLabel.setWrapText(true);

Label createdByHeader = new Label("CREATED BY");
createdByHeader.getStyleClass().add("field-title");

createdByLabel = new Label();
createdByLabel.getStyleClass().addAll("field-value", "created-by-label");
createdByLabel.setWrapText(true);

Label createdOnHeader = new Label("CREATED ON");
createdOnHeader.getStyleClass().add("field-title");

createdOnLabel = new Label();
createdOnLabel.getStyleClass().add("field-value");
FieldGroup createdOnGroup = new FieldGroup("CREATED ON", new String[]{"field-value"});
createdOnLabel = createdOnGroup.getLabel();

setBaseURL(DataRepository2.getInstance().getRepositoryDirectoryURL() + "realworld/" + getModel().getId());
fillData();
Expand All @@ -67,32 +53,40 @@ protected Node createTopNode() {
for (int i = 0; i < 4; i++) {
ColumnConstraints columnConstraints = new ColumnConstraints();
columnConstraints.setHalignment(HPos.LEFT);
columnConstraints.setHgrow(Priority.ALWAYS);
if (i == 0) {
columnConstraints.hgrowProperty().bind(locationLabel.visibleProperty().map(b -> b ? Priority.ALWAYS : Priority.NEVER));
}else if (i == 1) {
columnConstraints.hgrowProperty().bind(domainLabel.visibleProperty().map(b -> b ? Priority.ALWAYS : Priority.NEVER));
}else if (i == 2) {
columnConstraints.hgrowProperty().bind(createdByLabel.visibleProperty().map(b -> b ? Priority.ALWAYS : Priority.NEVER));
} else {
columnConstraints.hgrowProperty().bind(createdOnLabel.visibleProperty().map(b -> b ? Priority.ALWAYS : Priority.NEVER));
}
gridPane.getColumnConstraints().add(columnConstraints);
}
for (int i = 0; i < 2; i++) {
RowConstraints rowConstraints = new RowConstraints();
rowConstraints.setValignment(VPos.TOP);
gridPane.getRowConstraints().add(rowConstraints);
}
gridPane.add(locationHeader, 0, 0);
gridPane.add(locationGroup.getHeader(), 0, 0);
gridPane.add(locationLabel, 0, 1);
gridPane.add(domainHeader, 1, 0);
gridPane.add(domainGroup.getHeader(), 1, 0);
gridPane.add(domainLabel, 1, 1);
gridPane.add(createdByHeader, 2, 0);
gridPane.add(createdByGroup.getHeader(), 2, 0);
gridPane.add(createdByLabel, 2, 1);
gridPane.add(createdOnHeader, 3, 0);
gridPane.add(createdOnGroup.getHeader(), 3, 0);
gridPane.add(createdOnLabel, 3, 1);
return gridPane;
} else {
VBox topBox = new VBox(
locationHeader,
locationGroup.getHeader(),
locationLabel,
domainHeader,
domainGroup.getHeader(),
domainLabel,
createdByHeader,
createdByGroup.getHeader(),
createdByLabel,
createdOnHeader,
createdOnGroup.getHeader(),
createdOnLabel
);
createdOnLabel.getStyleClass().add("last");
Expand All @@ -119,4 +113,30 @@ private void fillData() {
setMarkdown("");
}
}

private static class FieldGroup {
private final Label header;
private final Label label;

public FieldGroup(String headerText, String[] labelStyles) {
header = new Label(headerText);
header.getStyleClass().add("field-title");
header.managedProperty().bind(header.visibleProperty());

label = new Label();
label.getStyleClass().addAll(labelStyles);
label.setWrapText(true);
label.managedProperty().bind(label.visibleProperty());
label.visibleProperty().bind(label.textProperty().map(s -> !StringUtils.isEmpty(s) && !StringUtils.equalsIgnoreCase(s, "Unknown")));
header.visibleProperty().bind(label.visibleProperty());
}

public Label getHeader() {
return header;
}

public Label getLabel() {
return label;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
import com.dlsc.jfxcentral.data.model.Utility;
import com.dlsc.jfxcentral2.components.CustomMarkdownView;
import com.dlsc.jfxcentral2.utilities.cssplayground.CssPlaygroundView;
import com.dlsc.jfxcentral2.utilities.effectdesigner.EffectDesignerView;
import com.dlsc.jfxcentral2.utilities.paintpicker.GradientDesignerView;
import com.dlsc.jfxcentral2.utilities.pathextractor.SVGPathExtractorView;
import com.dlsc.jfxcentral2.utilities.effectdesigner.EffectDesignerView;
import com.dlsc.jfxcentral2.utilities.pxemconverter.Px2EmView;
import com.dlsc.jfxcentral2.utils.ModelObjectTool;
import javafx.scene.Node;
import javafx.scene.control.Label;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.kordamp.ikonli.carbonicons.CarbonIcons;
import org.kordamp.ikonli.elusive.Elusive;
import org.kordamp.ikonli.fileicons.FileIcons;
import org.kordamp.ikonli.javafx.FontIcon;
Expand Down Expand Up @@ -72,6 +74,13 @@ private Node createToolPane(Utility model) {
EffectDesignerView effectDesignerView = new EffectDesignerView();
effectDesignerView.sizeProperty().bind(sizeProperty());
return effectDesignerView;
} else if (StringUtils.equalsIgnoreCase(model.getId(), "pxtoemconverter")) {
setTitle("Pixel-to-EM Converter");
setIcon(CarbonIcons.CD_CREATE_EXCHANGE);
getStyleClass().add("px-2-em-overview-box");
Px2EmView view = new Px2EmView();
view.sizeProperty().bind(sizeProperty());
return view;
}

return createComingSoonPane();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.dlsc.jfxcentral2.utilities.pxemconverter;

import com.dlsc.jfxcentral2.utils.NumberUtil;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Px2EmConverter {
private Px2EmConverter() {
}

/**
* Converts px units to em units for specified CSS properties in a CSS file.
*
* @param sourceFile the source CSS file
* @param targetFile the target CSS file
* @param propertiesToConvert a list of CSS properties to be converted
* @param baseFontSize the base font size for em unit calculation
* @param addComment whether to add original value as comment
* @throws IOException if an I/O error occurs
*/
public static void convert(File sourceFile, File targetFile, List<String> propertiesToConvert, double baseFontSize, boolean addComment) throws IOException {
String content = Files.readString(sourceFile.toPath());
Pattern commentPattern = Pattern.compile("/\\*.*?\\*/");
Matcher commentMatcher = commentPattern.matcher(content);

Map<Integer, String> commentsMap = new HashMap<>();
while (commentMatcher.find()) {
commentsMap.put(commentMatcher.start(), commentMatcher.group());
}

for (String property : propertiesToConvert) {
String regex = property + "\\s*:\\s*([0-9]+(?:\\.[0-9]+)?(?:em|px)?(?:\\s*[0-9]+(?:\\.[0-9]+)?(?:em|px)?)*)\\s*;?";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);

StringBuilder sb = new StringBuilder();
while (matcher.find()) {
int start = matcher.start();
int end = matcher.end();
boolean inComment = false;
for (Map.Entry<Integer, String> entry : commentsMap.entrySet()) {
int commentStart = entry.getKey();
int commentEnd = commentStart + entry.getValue().length();
if (start >= commentStart && end <= commentEnd) {
inComment = true;
break;
}
}

if (!inComment) {
String values = matcher.group(1);
if (!values.contains("em")) {
String emValues = convertToEm(values, baseFontSize);
if (addComment) {
matcher.appendReplacement(sb, property + ": " + emValues + ";" + "/* " + values + " */");
} else {
matcher.appendReplacement(sb, property + ": " + emValues + ";");
}
} else {
matcher.appendReplacement(sb, property + ": " + values + ";");
}
}
}
matcher.appendTail(sb);
content = sb.toString();
}

Files.writeString(Objects.requireNonNullElse(targetFile, sourceFile).toPath(), content);
}

public static String convertToEm(String values, double baseFontSize) {
if (values.trim().endsWith("em")) {
return values.trim();
}

String[] valueArray = values.split("\\s+");
StringBuilder emBuilder = new StringBuilder();
for (String value : valueArray) {
if (!value.isEmpty()) {
double px = Double.parseDouble(value.replaceAll("px", ""));
double em = px / baseFontSize;
emBuilder.append(NumberUtil.trimTrailingZeros(em, 5)).append("em").append(" ");
}
}
return emBuilder.toString().trim();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.dlsc.jfxcentral2.utilities.pxemconverter;

import java.util.List;

/**
* convertibleProps: properties that can be converted from px to em;
* ignoredProps: properties that should be ignored when converting from px to em.
*/
public record Px2EmRules(List<String> convertibleProps, List<String> ignoredProps) {
}
Loading

0 comments on commit 9f7afab

Please sign in to comment.