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

org.beangle.struts2.view.component.Grid Maven / Gradle / Ivy

There is a newer version: 3.11.3
Show newest version
/*
 * Beangle, Agile Development Scaffold and Toolkits.
 *
 * Copyright © 2005, The Beangle Software.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */
package org.beangle.struts2.view.component;

import java.io.Writer;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.struts2.util.MakeIterator;
import org.beangle.commons.collection.CollectUtils;
import org.beangle.commons.collection.page.Page;
import org.beangle.commons.lang.Numbers;
import org.beangle.commons.lang.Objects;
import org.beangle.commons.lang.Strings;
import org.beangle.struts2.view.template.Theme;

import com.opensymphony.xwork2.util.ValueStack;

import freemarker.template.utility.StringUtil;

/**
 * Data table
 *
 * @author chaostone
 */
public class Grid extends ClosingUIBean {
  private List cols = CollectUtils.newArrayList();
  private Set colTitles = CollectUtils.newHashSet();
  private Object items;
  private String var;
  private String caption;
  // gridbar
  private String bar;
  private String sortable = "true";
  private String filterable = "false";
  private Map filters = CollectUtils.newHashMap();

  /** 重新载入的时间间隔(以秒为单位) */
  private String refresh;

  private String overflow;

  /** 没有数据时显示的文本 */
  private String emptyMsg;

  public Grid(ValueStack stack) {
    super(stack);
  }

  public boolean getHasbar() {
    return (null != bar || items instanceof Page);
  }

  public boolean isPageable() {
    return items instanceof Page;
  }

  public boolean isNotFullPage() {
    if ((items instanceof Page)) return ((Page) items).size() < ((Page) items).getPageSize();
    else return ((Collection) items).isEmpty();
  }

  public String defaultSort(String property) {
    return Strings.concat(var, ".", property);
  }

  public boolean isSortable(Col cln) {
    Object sortby = cln.getParameters().get("sort");
    if (null != sortby) return true;
    return ("true".equals(sortable) && !Objects.equals(cln.getSortable(), "false") && null != cln
        .getProperty());
  }

  public boolean isFilterable(Col cln) {
    return ("true".equals(filterable) && !Objects.equals(cln.getFilterable(), "false") && null != cln
        .getProperty());
  }

  public String getCaption() {
    return caption;
  }

  public void setCaption(String caption) {
    this.caption = caption;
  }

  protected void addCol(Col column) {
    String title = column.getTitle();
    if (null == title) title = column.getProperty();
    if (null == column.getWidth() && column instanceof Boxcol) column.setWidth("25px");
    if (!colTitles.contains(title)) {
      colTitles.add(title);
      cols.add(column);
    }
  }

  public boolean start(Writer writer) {
    generateIdIfEmpty();
    return true;
  }

  public List getCols() {
    return cols;
  }

  public String getVar() {
    return var;
  }

  public void setVar(String var) {
    this.var = var;
  }

  public String getRefresh() {
    return refresh;
  }

  public void setRefresh(String refresh) {
    int refreshNum = Numbers.toInt(refresh);
    if (refreshNum > 0) this.refresh = String.valueOf(refreshNum);
  }

  public void setItems(Object datas) {
    if (datas instanceof String) {
      this.items = findValue((String) datas);
    } else {
      this.items = datas;
    }
  }

  public Object getItems() {
    return items;
  }

  public String getSortable() {
    return sortable;
  }

  public void setSortable(String sortable) {
    this.sortable = sortable;
  }

  public String getOverflow() {
    return overflow;
  }

  public void setOverflow(String overflow) {
    this.overflow = overflow;
  }

  public String getFilterable() {
    return filterable;
  }

  public void setFilterable(String filterable) {
    this.filterable = filterable;
  }

  public String getBar() {
    return bar;
  }

  public String getEmptyMsg() {
    return emptyMsg;
  }

  public void setEmptyMsg(String emptyMsg) {
    this.emptyMsg = emptyMsg;
  }

  public Map getFilters() {
    return filters;
  }

  public void setFilters(Map filters) {
    this.filters = filters;
  }

  public static class Filter extends ClosingUIBean {
    String property;

    public Filter(ValueStack stack) {
      super(stack);
    }

    @Override
    public boolean doEnd(Writer writer, String body) {
      Grid grid = (Grid) findAncestor(Grid.class);
      if (null != property && null != grid) {
        grid.getFilters().put(property, body);
      }
      return false;
    }

    public String getProperty() {
      return property;
    }

    public void setProperty(String property) {
      this.property = property;
    }

  }

  public static class Bar extends ClosingUIBean {
    private Grid grid;

    public Bar(ValueStack stack) {
      super(stack);
      grid = (Grid) findAncestor(Grid.class);
    }

    @Override
    public boolean doEnd(Writer writer, String body) {
      grid.bar = body;
      return false;
    }

    public Grid getTable() {
      return grid;
    }
  }

  public static class Row extends IterableUIBean {
    private Grid table;
    private String var_index;
    private Iterator iterator;
    private int index = -1;
    protected Object curObj;
    private Boolean innerTr;

    public Row(ValueStack stack) {
      super(stack);
      table = (Grid) findAncestor(Grid.class);
      Object iteratorTarget = table.items;
      iterator = MakeIterator.convert(iteratorTarget);
      if (!iterator.hasNext()) {
        iterator = Collections.singleton(null).iterator();
      }
      this.var_index = table.var + "_index";
    }

    public boolean isHasTr() {
      if (null != innerTr) return innerTr;
      int i = 0;
      innerTr = Boolean.FALSE;
      // max try count is 10
      while (i < body.length() && i < 10) {
        if (body.charAt(i) == '<' && Strings.substring(body, i, i + 3).equals("");
          if (Strings.isNotEmpty(body)) {
            writer.append(body);
          } else if (null != property) {
            Object val = getValue();
            if (null != val) writer.append(StringUtil.XMLEncNA(val.toString()));
          }
          writer.append("");
        } catch (Exception e) {
          e.printStackTrace();
        }
        return false;
      } else {
        return super.doEnd(writer, body);
      }
    }

    public String getProperty() {
      return property;
    }

    public void setProperty(String property) {
      this.property = property;
    }

    /**
     * find value of row.obj's property
     */
    public Object getValue() {
      return getValue(row.curObj, property);
    }

    public void setTitle(String title) {
      this.title = title;
    }

    public String getPropertyPath() {
      return Strings.concat(row.table.var, ".", property);
    }

    /**
     * 支持按照属性提取国际化英文名
     */
    public String getTitle() {
      if (null == title) {
        title = Strings.concat(row.table.var, ".", property);
      }
      return getText(title);
    }

    public String getWidth() {
      return width;
    }

    public void setWidth(String width) {
      this.width = width;
    }

    public String getSortable() {
      return sortable;
    }

    public void setSortable(String sortable) {
      this.sortable = sortable;
    }

    public String getFilterable() {
      return filterable;
    }

    public void setFilterable(String filterable) {
      this.filterable = filterable;
    }

    public Object getCurObj() {
      return row.curObj;
    }

  }

  public static class Treecol extends Col {

    public Treecol(ValueStack stack) {
      super(stack);
    }

    @Override
    public boolean doEnd(Writer writer, String body) {
      this.body = body;
      try {
        mergeTemplate(writer);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
      return false;
    }

  }

  public static class Boxcol extends Col {

    public Boxcol(ValueStack stack) {
      super(stack);
    }

    String type = "checkbox";

    // checkbox or radiobox name
    String boxname = null;

    /** display or none */
    boolean display = true;

    boolean checked;

    @Override
    public boolean start(Writer writer) {
      if (null == property) this.property = "id";

      row = (Row) findAncestor(Row.class);
      if (null == boxname) boxname = row.table.var + "." + property;

      if (row.index == 0) row.table.addCol(this);
      return null != row.curObj;
    }

    @Override
    public boolean doEnd(Writer writer, String body) {
      if (getTheme().equals(Theme.DefaultTheme)) {
        try {
          writer.append("");
          if (display) {
            writer.append("");
          }
          if (Strings.isNotEmpty(body)) writer.append(body);
          writer.append("");
        } catch (Exception e) {
          e.printStackTrace();
        }
        return false;
      } else {
        return super.doEnd(writer, body);
      }
    }

    public String getType() {
      return type;
    }

    @Override
    public String getTitle() {
      return Strings.concat(row.table.var, "_", property);
    }

    public String getBoxname() {
      return boxname;
    }

    public void setBoxname(String boxname) {
      this.boxname = boxname;
    }

    public void setType(String type) {
      this.type = type;
    }

    public boolean isChecked() {
      return checked;
    }

    public void setChecked(boolean checked) {
      this.checked = checked;
    }

    public boolean isDisplay() {
      return display;
    }

    public void setDisplay(boolean display) {
      this.display = display;
    }
  }
}