Improved data model for storing transparency alpha and other properties that are edited using a slider widget; as a result, the editing is more precise and does not randomly change to neighboring values

This commit is contained in:
Bane Stojkovic
2026-04-06 21:04:55 +02:00
parent 124d1ab577
commit 194c8e0606
98 changed files with 12813 additions and 13008 deletions
@@ -26,6 +26,7 @@ import java.awt.Component;
import java.awt.Font;
import javax.swing.JLabel;
import org.apache.log4j.Logger;
import org.diylc.common.Percentage;
import org.diylc.components.misc.LoadlineEntity;
import org.diylc.common.PropertyWrapper;
@@ -99,8 +100,8 @@ public class FieldEditorFactory {
DynamicValueEditor editor = new DynamicValueEditor(property);
return editor;
}
if (Byte.class.isAssignableFrom(property.getType()) || byte.class.isAssignableFrom(property.getType())) {
ByteEditor editor = new ByteEditor(property);
if (Percentage.class.isAssignableFrom(property.getType())) {
PercentageEditor editor = new PercentageEditor(property);
return editor;
}
if (Boolean.class.isAssignableFrom(property.getType()) || boolean.class.isAssignableFrom(property.getType())) {
@@ -1,117 +1,120 @@
/*
DIY Layout Creator (DIYLC).
Copyright (c) 2009-2025 held jointly by the individual authors.
This file is part of DIYLC.
DIYLC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DIYLC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.swing.gui.editor;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.diylc.swingframework.DoubleTextField;
import org.diylc.common.PropertyWrapper;
import org.diylc.core.annotations.PercentEditor;
import org.diylc.utils.Constants;
public class ByteEditor extends JPanel {
private static final long serialVersionUID = 1L;
private Color oldBg = getBackground();
private static final byte default100Percent = 127;
private byte _100PercentValue;
public ByteEditor(final PropertyWrapper property) {
super();
setLayout(new FlowLayout());
try {
PercentEditor percentEditor = property.getGetter().getAnnotation(PercentEditor.class);
if (percentEditor == null)
_100PercentValue = default100Percent;
else
_100PercentValue = percentEditor._100PercentValue();
} catch (Exception e) {
_100PercentValue = default100Percent;
}
final JSlider slider = new JSlider();
final DoubleTextField valueField = new DoubleTextField();
if (property.isReadOnly()) {
slider.setEnabled(false);
valueField.setEnabled(false);
}
final double percentFactor = (double) default100Percent / _100PercentValue;
slider.setMinimum(0);
slider.setMaximum(default100Percent * 100);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
property.setChanged(true);
setBackground(oldBg);
slider.setBackground(oldBg);
property.setValue(Integer.valueOf(slider.getValue() / 100).byteValue());
valueField.setText(Integer.toString((int) Math.round(percentFactor * (double) slider.getValue()
/ default100Percent)));
}
});
slider.setValue((Byte) property.getValue() * 100);
valueField
.setText(Integer.toString((int) Math.round(percentFactor * (double) slider.getValue() / default100Percent)));
valueField.setColumns(3);
valueField.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
try {
int newPosition = (int) (Double.parseDouble(valueField.getText()) * default100Percent / percentFactor);
System.out.println("keyReleased, pos: " + newPosition);
slider.setValue(newPosition);
} catch (Exception ex) {
}
}
});
if (!property.isUnique()) {
setBackground(Constants.MULTI_VALUE_COLOR);
slider.setBackground(Constants.MULTI_VALUE_COLOR);
}
add(slider);
add(valueField);
add(new JLabel("%"));
}
}
/*
DIY Layout Creator (DIYLC).
Copyright (c) 2009-2025 held jointly by the individual authors.
This file is part of DIYLC.
DIYLC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DIYLC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.swing.gui.editor;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.diylc.swingframework.DoubleTextField;
import org.diylc.common.Percentage;
import org.diylc.common.PropertyWrapper;
import org.diylc.core.annotations.PercentEditor;
import org.diylc.utils.Constants;
public class PercentageEditor extends JPanel {
private static final long serialVersionUID = 1L;
private Color oldBg = getBackground();
private int minValue = 0;
private int maxValue = 100;
public PercentageEditor(final PropertyWrapper property) {
super();
setLayout(new FlowLayout());
try {
PercentEditor percentEditor = property.getGetter().getAnnotation(PercentEditor.class);
if (percentEditor != null) {
minValue = percentEditor.minValue();
maxValue = percentEditor.maxValue();
}
} catch (Exception e) {
}
final JSlider slider = new JSlider();
final DoubleTextField valueField = new DoubleTextField();
if (property.isReadOnly()) {
slider.setEnabled(false);
valueField.setEnabled(false);
}
slider.setMinimum(minValue);
slider.setMaximum(maxValue);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
property.setChanged(true);
setBackground(oldBg);
slider.setBackground(oldBg);
property.setValue(new Percentage(slider.getValue()));
valueField.setText(Integer.toString(slider.getValue()));
}
});
Object val = property.getValue();
if (val != null) {
slider.setValue(((Percentage) val).getValue());
} else {
slider.setValue(minValue);
}
valueField.setText(Integer.toString(slider.getValue()));
valueField.setColumns(3);
valueField.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
try {
int newPosition = (int) Double.parseDouble(valueField.getText());
if (newPosition >= minValue && newPosition <= maxValue) {
slider.setValue(newPosition);
}
} catch (Exception ex) {
}
}
});
if (!property.isUnique()) {
setBackground(Constants.MULTI_VALUE_COLOR);
slider.setBackground(Constants.MULTI_VALUE_COLOR);
}
add(slider);
add(valueField);
add(new JLabel("%"));
}
}
@@ -35,6 +35,7 @@ import org.apache.poi.util.IOUtils;
import org.diylc.appframework.miscutils.IConfigurationManager;
import org.diylc.appframework.miscutils.InMemoryConfigurationManager;
import org.diylc.appframework.miscutils.Utils;
import org.diylc.common.*;
import org.diylc.components.connectivity.Dot;
import org.diylc.components.connectivity.Line;
import org.diylc.components.misc.Image;
@@ -47,12 +48,6 @@ import org.diylc.swingframework.DoubleTextField;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import org.diylc.common.EventType;
import org.diylc.common.HorizontalAlignment;
import org.diylc.common.IPlugIn;
import org.diylc.common.IPlugInPort;
import org.diylc.common.PropertyWrapper;
import org.diylc.common.VerticalAlignment;
import org.diylc.components.AbstractCurvedComponent.PointCount;
import org.diylc.core.IDIYComponent;
import org.diylc.core.IView;
@@ -687,7 +682,7 @@ public class LoadlineEditorFrame extends JFrame implements IView {
image.setControlPoint(new Point((int) spacing, (int) spacing), 0);
image.setControlPoint(new Point((int) (canvasDimensions.width - spacing),
(int) (canvasDimensions.height - spacing)), 1);
image.setAlpha((byte) 50); // make it transparent
image.setAlpha(new Percentage(50)); // make it transparent
fis.close();
// add to the bottom
project.getComponents().add(0, image);