Package mpmetr.ui

Source Code of mpmetr.ui.SectorPanel

package mpmetr.ui;

import mpmetr.core.SectorEnum;
import static mpmetr.core.SectorEnum.SE_0130;
import static mpmetr.core.SectorEnum.SE_0430;
import static mpmetr.core.SectorEnum.SE_0730;
import static mpmetr.core.SectorEnum.SE_1030;
import mpmetr.core.SectorModel;
import mpmetr.core.SectorState;
import static mpmetr.core.SectorState.ON;

import javax.swing.JLabel;
import javax.swing.JPanel;
import static javax.swing.SwingConstants.BOTTOM;
import static javax.swing.SwingConstants.CENTER;
import static javax.swing.SwingConstants.LEFT;
import static javax.swing.SwingConstants.RIGHT;
import static javax.swing.SwingConstants.TOP;
import java.awt.GridLayout;

class SectorPanel extends JPanel {
    private static final char OFF_CHAR = '\u2729';
    private final SectorModel model;
    private final JLabel label;

    public SectorPanel(SectorEnum sector) {
        SectorModel model = new SectorModel(sector);
        setLayout(new GridLayout(1, 1));
        this.model = model;
        label = new JLabel("" + OFF_CHAR);
        if (sector == SE_0130) align(BOTTOM, LEFT);
        else if (sector == SE_0430) align(TOP, LEFT);
        else if (sector == SE_0730) align(TOP, RIGHT);
        else if (sector == SE_1030) align(BOTTOM, RIGHT);
        else align(CENTER, CENTER);
        add(label);
    }

    private void align(int vert, int horiz) {
        UiUtil.alignText(label, horiz, vert);
    }

    public SectorModel getModel() {
        return model;
    }

    public JLabel getLabel() {
        return label;
    }

    public void checkState() {
        String desired = calcDesiredStr(model.getState());
        assignLabel(desired);
    }

    private void assignLabel(String desired) {
        // Not sure why I'm doing this... does it make it more efficient?
        String text = label.getText();
        if (!text.equals(desired)) label.setText(desired);
    }

    private String calcDesiredStr(SectorState sectorState) {
        return "" + calcDesired(sectorState);
    }

    private char calcDesired(SectorState state) {
        // NOTE: Only Dingbats symbols is guaranteed on Lucida Sans.
        if (state == ON) return '\u272a';
        return OFF_CHAR;
    }
}
TOP

Related Classes of mpmetr.ui.SectorPanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.