mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-10 17:22:09 +10:00
Ability to type in hex value in color editor
This commit is contained in:
@@ -2,43 +2,64 @@ package org.diylc.swing.gui.editor;
|
|||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Cursor;
|
import java.awt.Cursor;
|
||||||
|
import java.awt.GridBagConstraints;
|
||||||
|
import java.awt.GridBagLayout;
|
||||||
|
import java.awt.Insets;
|
||||||
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseAdapter;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
|
|
||||||
import javax.swing.BorderFactory;
|
import javax.swing.BorderFactory;
|
||||||
import javax.swing.JColorChooser;
|
import javax.swing.JColorChooser;
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.JTextField;
|
||||||
import javax.swing.SwingConstants;
|
import javax.swing.SwingConstants;
|
||||||
|
import javax.swing.event.DocumentEvent;
|
||||||
|
import javax.swing.event.DocumentListener;
|
||||||
|
|
||||||
import org.diylc.common.PropertyWrapper;
|
import org.diylc.common.PropertyWrapper;
|
||||||
|
|
||||||
public class ColorEditor extends JLabel {
|
public class ColorEditor extends JPanel {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private static final String title = "Click to edit";
|
private static final String title = "Click to edit";
|
||||||
|
|
||||||
public ColorEditor(final PropertyWrapper property) {
|
private JLabel colorLabel;
|
||||||
super(property.isUnique() ? title : ("(multi value) " + title));
|
private JTextField colorField;
|
||||||
setOpaque(true);
|
private PropertyWrapper property;
|
||||||
setHorizontalAlignment(SwingConstants.CENTER);
|
|
||||||
setBorder(BorderFactory.createEtchedBorder());
|
|
||||||
setBackground((Color) property.getValue());
|
|
||||||
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
|
||||||
addMouseListener(new MouseAdapter() {
|
|
||||||
|
|
||||||
@Override
|
public ColorEditor(final PropertyWrapper property) {
|
||||||
public void mouseClicked(MouseEvent e) {
|
super();
|
||||||
Color newColor = JColorChooser.showDialog(ColorEditor.this, "Choose Color", getBackground());
|
this.property = property;
|
||||||
if (newColor != null) {
|
|
||||||
property.setChanged(true);
|
setLayout(new GridBagLayout());
|
||||||
property.setValue(newColor);
|
|
||||||
setBackground(newColor);
|
GridBagConstraints gbc = new GridBagConstraints();
|
||||||
}
|
gbc.gridx = 0;
|
||||||
}
|
gbc.gridy = 0;
|
||||||
});
|
gbc.weightx = 1;
|
||||||
|
gbc.weighty = 0;
|
||||||
|
gbc.fill = GridBagConstraints.HORIZONTAL;
|
||||||
|
|
||||||
|
add(getColorLabel(), gbc);
|
||||||
|
|
||||||
|
gbc.gridx++;
|
||||||
|
gbc.weightx = 0;
|
||||||
|
gbc.insets = new Insets(0, 2, 0, 0);
|
||||||
|
add(new JLabel("#"), gbc);
|
||||||
|
|
||||||
|
gbc.gridx++;
|
||||||
|
gbc.insets = new Insets(0, 0, 0, 0);
|
||||||
|
add(getColorField(), gbc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public JLabel getColorLabel() {
|
||||||
|
if (colorLabel == null) {
|
||||||
|
colorLabel = new JLabel(property.isUnique() ? title : ("(multi value) " + title)) {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setBackground(Color bg) {
|
public void setBackground(Color bg) {
|
||||||
if (bg.getRed() < 127 || bg.getBlue() < 127 || bg.getGreen() < 127) {
|
if (bg.getRed() < 127 || bg.getBlue() < 127 || bg.getGreen() < 127) {
|
||||||
@@ -48,4 +69,62 @@ public class ColorEditor extends JLabel {
|
|||||||
}
|
}
|
||||||
super.setBackground(bg);
|
super.setBackground(bg);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
colorLabel.setOpaque(true);
|
||||||
|
colorLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
|
colorLabel.setBorder(BorderFactory.createEtchedBorder());
|
||||||
|
colorLabel.setBackground((Color) property.getValue());
|
||||||
|
colorLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
||||||
|
colorLabel.addMouseListener(new MouseAdapter() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
Color newColor = JColorChooser.showDialog(ColorEditor.this, "Choose Color", getBackground());
|
||||||
|
if (newColor != null) {
|
||||||
|
property.setChanged(true);
|
||||||
|
property.setValue(newColor);
|
||||||
|
getColorLabel().setBackground(newColor);
|
||||||
|
getColorField().setText(Integer.toHexString(newColor.getRGB()).substring(2).toUpperCase());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return colorLabel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JTextField getColorField() {
|
||||||
|
if (colorField == null) {
|
||||||
|
Color color = (Color) property.getValue();
|
||||||
|
colorField =
|
||||||
|
new JTextField(property.isUnique() ? Integer.toHexString(color.getRGB()).substring(2).toUpperCase()
|
||||||
|
: ("(multi value) " + title));
|
||||||
|
colorField.setColumns(6);
|
||||||
|
colorField.getDocument().addDocumentListener(new DocumentListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeUpdate(DocumentEvent e) {
|
||||||
|
updateColor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void insertUpdate(DocumentEvent e) {
|
||||||
|
updateColor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void changedUpdate(DocumentEvent e) {
|
||||||
|
updateColor();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return colorField;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateColor() {
|
||||||
|
if (getColorField().getText().length() == 6) {
|
||||||
|
Color newColor = Color.decode("#" + getColorField().getText());
|
||||||
|
if (newColor != null)
|
||||||
|
getColorLabel().setBackground(newColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1566,6 +1566,10 @@
|
|||||||
<changeType>NEW_FEATURE</changeType>
|
<changeType>NEW_FEATURE</changeType>
|
||||||
<description>Show/hide the grid</description>
|
<description>Show/hide the grid</description>
|
||||||
</org.diylc.appframework.update.Change>
|
</org.diylc.appframework.update.Change>
|
||||||
|
<org.diylc.appframework.update.Change>
|
||||||
|
<changeType>IMPROVEMENT</changeType>
|
||||||
|
<description>Ability to type in hex value in color editor</description>
|
||||||
|
</org.diylc.appframework.update.Change>
|
||||||
<org.diylc.appframework.update.Change>
|
<org.diylc.appframework.update.Change>
|
||||||
<changeType>IMPROVEMENT</changeType>
|
<changeType>IMPROVEMENT</changeType>
|
||||||
<description>Ability to rotate tube sockets for arbitrary angle and to change color</description>
|
<description>Ability to rotate tube sockets for arbitrary angle and to change color</description>
|
||||||
|
|||||||
Reference in New Issue
Block a user