org.hwo.pulscounter/src/org/hwo/pulscounter/ui/SimpleScriptAddressEditor.java

154 lines
3.7 KiB
Java
Raw Normal View History

2016-10-26 19:21:57 +02:00
package org.hwo.pulscounter.ui;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JComboBox;
import java.awt.GridBagConstraints;
import java.awt.Insets;
2016-11-17 16:37:45 +01:00
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
2016-10-26 19:21:57 +02:00
import javax.swing.JSpinner;
2016-11-17 16:37:45 +01:00
import javax.swing.SpinnerNumberModel;
import org.hwo.pulscounter.simplescript.SimpleScriptAddress;
import org.hwo.ui.JComboBoxEx;
2016-10-26 19:21:57 +02:00
public class SimpleScriptAddressEditor extends JPanel {
AddressRange[] addressRanges = new AddressRange[]{
new AddressRange("Zähler", 0, 32),
new AddressRange("Analogspannung", 32, 8),
new AddressRange("Merker", 40, 24),
new AddressRange("PullUp Status", 64, 32),
new AddressRange("Ausgang", 96, 32),
new AddressRange("Inverter", 128, 32),
2016-11-17 16:37:45 +01:00
new AddressRange("Eingang", 160, 32),
new AddressRange("Variable", 192, 64)
2016-10-26 19:21:57 +02:00
};
2016-11-17 16:37:45 +01:00
private JComboBoxEx cbRange;
private JSpinner spValue;
private SpinnerNumberModel numberModel = new SpinnerNumberModel(0, 0, 0, 1);
private int
address;
2016-10-26 19:21:57 +02:00
/**
* Create the panel.
*/
public SimpleScriptAddressEditor() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0};
gridBagLayout.columnWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
2016-11-17 16:37:45 +01:00
cbRange = new JComboBoxEx();
GridBagConstraints gbc_cbRange = new GridBagConstraints();
gbc_cbRange.insets = new Insets(0, 0, 0, 5);
gbc_cbRange.fill = GridBagConstraints.BOTH;
gbc_cbRange.gridx = 0;
gbc_cbRange.gridy = 0;
cbRange.setItems(addressRanges);
cbRange.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
syncNumberFormat();
}
});
add(cbRange, gbc_cbRange);
2016-10-26 19:21:57 +02:00
2016-11-17 16:37:45 +01:00
spValue = new JSpinner();
GridBagConstraints gbc_spValue = new GridBagConstraints();
gbc_spValue.fill = GridBagConstraints.BOTH;
gbc_spValue.gridx = 1;
gbc_spValue.gridy = 0;
spValue.setModel(numberModel);
add(spValue, gbc_spValue);
2016-10-26 19:21:57 +02:00
2016-11-17 16:37:45 +01:00
cbRange.setSelectedIndex(0);
}
private AddressRange getSelectedRange(){
return (AddressRange)cbRange.getSelectedItem();
}
private int getSelectedDisplayValue(){
return (Integer)spValue.getValue();
2016-10-26 19:21:57 +02:00
}
2016-11-17 16:37:45 +01:00
private void syncNumberFormat(){
AddressRange range = getSelectedRange();
numberModel.setMaximum(range.getSize()-1);
if (range.getSize() <= getSelectedDisplayValue()){
numberModel.setValue(range.getSize()-1);
}
}
public int getAddress() {
this.address = getSelectedRange().scriptValue(((Integer)numberModel.getValue()).intValue());
return address;
}
public void setAddress(int address) {
this.address = address;
cbRange.setEnabled(true);
cbRange.setSelectedItem(findRange(address));
syncNumberFormat();
numberModel.setValue(getSelectedRange().displayValue(address));
}
private AddressRange findRange(int address){
for (AddressRange range: addressRanges){
if (range.contains(address))
return range;
}
return null;
}
2016-10-26 19:21:57 +02:00
private class AddressRange{
private String name;
private int base;
private int size;
public AddressRange(String name,int base,int size){
this.name = name;
this.size = size;
this.base = base;
}
public int getBase() {
return base;
}
public int getSize() {
return size;
}
public String getName() {
return name;
}
2016-11-17 16:37:45 +01:00
public boolean contains(int address){
return (base <= address) && (address < (base+size));
}
public int displayValue(int address){
return address - base;
}
public int scriptValue(int displayAddress){
return displayAddress + base;
}
@Override
public String toString() {
return name;
}
2016-10-26 19:21:57 +02:00
}
}