package be.iminds.ilabt.jfed.experimenter_gui.util.ui;

import javafx.beans.binding.StringBinding;
import javafx.beans.value.ObservableStringValue;

/**
 * User: twalcari
 * Date: 5/12/2015
 * Time: 10:18
 */
public class MacWithDotsBinding extends StringBinding {
    private final ObservableStringValue macAddress;

    public MacWithDotsBinding(ObservableStringValue macAddress) {
        this.macAddress = macAddress;

        super.bind(macAddress);
    }


    @Override
    protected String computeValue() {
        String mac = macAddress.get();
        if (mac != null && !mac.contains(":")) {

            StringBuilder macWithDots = new StringBuilder();

            int pos = 0;

            while (pos < mac.length()) {
                if (pos != 0)
                    macWithDots.append(":");

                int endPos = pos + 2;

                if (endPos > mac.length()) //make processing robust
                    endPos = mac.length();

                macWithDots.append(mac.substring(pos, endPos));

                pos += 2;
            }


            return macWithDots.toString();
        } else {
            return mac;
        }
    }
}
