All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bytezone.dm3270.attributes.Attribute Maven / Gradle / Ivy

Go to download

This is a trimmed down version of https://github.com/dmolony/dm3270 to be used as TN3270 client library

There is a newer version: 0.9.1
Show newest version
package com.bytezone.dm3270.attributes;

import com.bytezone.dm3270.display.ScreenContext;

import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class Attribute {

  public static final byte XA_RESET = 0x00;
  public static final byte XA_HIGHLIGHTING = 0x41;
  public static final byte XA_FGCOLOR = 0x42;
  public static final byte XA_CHARSET = 0x43;
  public static final byte XA_BGCOLOR = 0x45;
  public static final byte XA_TRANSPARENCY = 0x46;
  public static final byte XA_START_FIELD = (byte) 0xC0;

  private static final Logger LOG = LoggerFactory.getLogger(Attribute.class);

  private static final byte XA_VALIDATION = (byte) 0xC1;
  private static final byte XA_OUTLINING = (byte) 0xC2;

  protected final byte attributeValue;

  private final AttributeType attributeType;
  private final byte attributeCode;

  public enum AttributeType {
    START_FIELD, HIGHLIGHT, FOREGROUND_COLOR, BACKGROUND_COLOR, RESET
  }

  public Attribute(AttributeType attributeType, byte attributeCode, byte attributeValue) {
    this.attributeType = attributeType;
    this.attributeCode = attributeCode;
    this.attributeValue = attributeValue;
  }

  public boolean matches(byte... types) {
    for (byte type : types) {
      if (attributeCode == type) {
        return true;
      }
    }
    return false;
  }

  public int pack(byte[] buffer, int offset) {
    buffer[offset++] = attributeCode;
    buffer[offset++] = attributeValue;
    return offset;
  }

  public abstract ScreenContext process(ScreenContext defaultContext, ScreenContext currentContext);

  public byte getAttributeValue() {
    return attributeValue;
  }

  public AttributeType getAttributeType() {
    return attributeType;
  }

  public static Optional getAttribute(byte attributeCode, byte attributeValue) {
    switch (attributeCode) {
      case 0:
        return Optional.of(new ResetAttribute(attributeValue));
      case XA_START_FIELD:
        return Optional.of(new StartFieldAttribute(attributeValue));
      case XA_HIGHLIGHTING:
        return Optional.of(new ExtendedHighlight(attributeValue));
      case XA_BGCOLOR:
        return Optional.of(new BackgroundColor(attributeValue));
      case XA_FGCOLOR:
        return Optional.of(new ForegroundColor(attributeValue));
      case XA_CHARSET:
        LOG.warn("Charset not written");
        return Optional.empty();
      case XA_VALIDATION:
        LOG.warn("Validation not written");
        return Optional.empty();
      case XA_OUTLINING:
        LOG.warn("Outlining not written");
        return Optional.empty();
      case XA_TRANSPARENCY:
        LOG.warn("Transparency not written");
        return Optional.empty();
      default:
        LOG.warn("Unknown attribute: {}", String.format("%02X", attributeCode));
        return Optional.empty();
    }
  }

  protected String name() {
    return getTypeName(attributeCode);
  }

  public static String getTypeName(byte type) {
    switch (type) {
      case XA_RESET:
        return "Reset";
      case XA_HIGHLIGHTING:
        return "Highlight";
      case XA_FGCOLOR:
        return "Foreground";
      case XA_BGCOLOR:
        return "Background";
      case XA_TRANSPARENCY:
        return "Transparency";
      case XA_START_FIELD:
        return "Start Field";
      case XA_VALIDATION:
        return "Validation";
      case XA_OUTLINING:
        return "Outlining";
      case XA_CHARSET:
        return "Charset";
      default:
        return "Unknown";
    }
  }

  @Override
  public String toString() {
    return String.format("%-12s : %02X", name(), attributeValue);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy