java-org.hwo/src/org/hwo/io/servicelink/BitFieldEditor.java

121 lines
2.7 KiB
Java

package org.hwo.io.servicelink;
import java.awt.Component;
import java.util.Arrays;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import org.hwo.io.servicelink.ServiceLink.ServiceNode;
import org.hwo.io.servicelink.ServiceLink.ServiceNode.ServiceNodeRegister;
import java.awt.FlowLayout;
import javax.swing.JScrollPane;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.BoxLayout;
public class BitFieldEditor extends JPanel implements RegisterEditorControl {
private ServiceNodeRegister serviceNodeRegister;
private JCheckBox[] checkboxes;
private JScrollPane scrollPane;
private JPanel pBits;
public BitFieldEditor() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0};
gridBagLayout.rowHeights = new int[]{0, 0};
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 0;
add(scrollPane, gbc_scrollPane);
pBits = new JPanel();
scrollPane.setViewportView(pBits);
pBits.setLayout(new BoxLayout(pBits, BoxLayout.Y_AXIS));
checkboxes = new JCheckBox[0];
}
private void initialize()
{
for (JCheckBox cb:checkboxes)
{
remove(cb);
}
if (serviceNodeRegister != null)
{
checkboxes = new JCheckBox[32];
for (int i=0;i<32;i++)
{
if (serviceNodeRegister.getBitField().getLabels()[i]!=null)
{
checkboxes[i] = new JCheckBox();
checkboxes[i].setText(serviceNodeRegister.getBitField().getLabels()[i]);
pBits.add(checkboxes[i]);
};
}
synchronize();
}
invalidate();
}
public void synchronize()
{
for (int i=0;i<32;i++)
{
if (checkboxes[i]!=null)
checkboxes[i].setSelected(
(serviceNodeRegister.getIntValue() & (1<<i))!=0
);
}
}
@Override
public void setRegister(ServiceNodeRegister serviceNodeRegister) {
setServiceNodeRegister(serviceNodeRegister);
}
@Override
public void updateRegister() {
Integer v = serviceNodeRegister.getIntValue();
for (int i=0;i<32;i++)
{
if (checkboxes[i]!=null)
{
if (checkboxes[i].isSelected())
v |= 1<<i;
else
v &= ~(1<<i);
}
}
serviceNodeRegister.setIntValue(v);
}
@Override
public Component getComponent() {
return this;
}
public ServiceNodeRegister getServiceNodeRegister() {
return serviceNodeRegister;
}
public void setServiceNodeRegister(ServiceNodeRegister serviceNodeRegister) {
this.serviceNodeRegister = serviceNodeRegister;
initialize();
}
}