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

br.com.objectos.xls.pojo.plugin.SheetMethodStyle Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2015 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.xls.pojo.plugin;

import java.util.EnumSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import br.com.objectos.code.AnnotationInfo;
import br.com.objectos.collections.ImmutableMap;
import br.com.objectos.collections.ImmutableSet;
import br.com.objectos.pojo.plugin.Property;
import br.com.objectos.xls.Alignment;
import br.com.objectos.xls.pojo.Style;

import com.squareup.javapoet.MethodSpec;

/**
 * @author [email protected] (Marcio Endo)
 */
enum SheetMethodStyle {

  ALIGNMENT_CENTER("centered") {
    @Override
    public void writeHeader(MethodSpec.Builder write) {
      write.addCode(stylesMethodCall());
    }
  },

  ALIGNMENT_LEFT("leftAligned"){
    @Override
    public void writeHeader(MethodSpec.Builder write) {
      write.addCode(stylesMethodCall());
    }
  },

  ALIGNMENT_RIGHT("rightAligned") {
    @Override
    public void writeHeader(MethodSpec.Builder write) {
      write.addCode(stylesMethodCall());
    }
  },

  BOLD("bold"),

  ITALIC("italic"),

  STRIKE("strike"),

  UNDERLINE("underline");

  private static final Map ALIGMENT_MAP = ImmutableMap
      . builder()
      .put(Alignment.CENTER, ALIGNMENT_CENTER)
      .put(Alignment.LEFT, ALIGNMENT_LEFT)
      .put(Alignment.RIGHT, ALIGNMENT_RIGHT)
      .build();

  private static final Set BOOLEAN_SET = EnumSet.range(BOLD, UNDERLINE);

  private final String methodName;
  private final String stylesMethodCall;

  private SheetMethodStyle(String methodName) {
    this.methodName = methodName;
    stylesMethodCall = "." + methodName + "()";
  }

  public static Set of(Property property) {
    ImmutableSet.Builder set = ImmutableSet. builder();

    Optional maybeStyle = property.annotationInfo(Style.class);
    if (maybeStyle.isPresent()) {
      AnnotationInfo style = maybeStyle.get();

      Alignment alignment = style.enumValue(Alignment.class, "alignment", Alignment.LEFT);
      set.add(ALIGMENT_MAP.get(alignment));

      for (SheetMethodStyle sheetMethodStyle : BOOLEAN_SET) {
        sheetMethodStyle.booleanStyle(style, set);
      }
    }

    return set.build();
  }

  public void toXls(MethodSpec.Builder toXls) {
    toXls.addCode(stylesMethodCall());
  }

  public void writeHeader(MethodSpec.Builder write) {
  }

  final String stylesMethodCall() {
    return stylesMethodCall;
  }

  private void booleanStyle(AnnotationInfo style, ImmutableSet.Builder set) {
    if (style.booleanValue(methodName, false)) {
      set.add(this);
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy