Ability to type in hex value in color editor

This commit is contained in:
bancika
2017-01-25 13:02:04 +01:00
parent aa1a327ac1
commit c2aa863c2a
2 changed files with 109 additions and 26 deletions
@@ -2,50 +2,129 @@ 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);
} }
@Override public JLabel getColorLabel() {
public void setBackground(Color bg) { if (colorLabel == null) {
if (bg.getRed() < 127 || bg.getBlue() < 127 || bg.getGreen() < 127) { colorLabel = new JLabel(property.isUnique() ? title : ("(multi value) " + title)) {
setForeground(Color.white);
} else { private static final long serialVersionUID = 1L;
setForeground(Color.black);
@Override
public void setBackground(Color bg) {
if (bg.getRed() < 127 || bg.getBlue() < 127 || bg.getGreen() < 127) {
setForeground(Color.white);
} else {
setForeground(Color.black);
}
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);
} }
super.setBackground(bg);
} }
} }
+4
View File
@@ -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>