<commit_before>package org.zkoss.gridster;

import org.zkoss.json.JSONObject;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.UiException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.sys.ComponentCtrl;
import org.zkoss.zk.ui.sys.ContentRenderer;
import org.zkoss.zul.impl.XulElement;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * A <a href="http://gridster.net/">gridster.js</a> component.
 *
 * @author <NAME>
 */
public class Gridster extends XulElement {

    static {
        addClientEvent(Gridster.class, GridsterEvents.ON_CHANGE, ComponentCtrl.CE_DUPLICATE_IGNORE);
        addClientEvent(Gridster.class, GridsterEvents.ON_DRAG_START, ComponentCtrl.CE_IMPORTANT);
        addClientEvent(Gridster.class, GridsterEvents.ON_DRAG, ComponentCtrl.CE_IMPORTANT);
        addClientEvent(Gridster.class, GridsterEvents.ON_DRAG_STOP, ComponentCtrl.CE_IMPORTANT);
        addClientEvent(Gridster.class, GridsterEvents.ON_RESIZE_START, ComponentCtrl.CE_IMPORTANT);
        addClientEvent(Gridster.class, GridsterEvents.ON_RESIZE, ComponentCtrl.CE_IMPORTANT);
        addClientEvent(Gridster.class, GridsterEvents.ON_RESIZE_STOP, ComponentCtrl.CE_IMPORTANT);
    }

    private final int[] widgetMargins = new int[]{10, 10};
    private final int[] widgetBaseDimensions = new int[]{140, 140};

    private int extraRows = 0;
    private int extraCols = 0;

    private int minRows = 15;
    private int minCols = 1;

    private Integer maxCols = null;
    private Integer maxSizeX = null;
    private Integer maxSizeY = null;

    private boolean resizeEnabled = false;
    private String resizeAxes = "both";
    private final Integer[] resizeMaxSize = new Integer[]{null, null};
    private final Integer[] resizeMinSize = new Integer[]{null, null};

    public Gridster() {
        addEventListener(0, GridsterEvents.ON_CHANGE, new EventListener<Event>() {

            @Override
            public void onEvent(Event event) {
                Object[] data = (Object[]) event.getData();
                for (Object state : data) {
                    saveGridItemState((JSONObject) state);
                }
            }
        });
    }

    /**
     * Save the {@link GridItem} state returned from the client. Note that adjusting the state of one item often affects
     * many, as such it is the gridster's responsibility to notify all items.
     *
     * @param state the state of a grid item
     */
    private void saveGridItemState(JSONObject state) {
        String uuid = state.get("uuid").toString();
        String row = state.get("row").toString();
        String col = state.get("col").toString();
        String sizeX = state.get("size_x").toString();
        String sizeY = state.get("size_y").toString();
        GridItem item = getGridItem(uuid);
        item.setRow(Integer.valueOf(row));
        item.setCol(Integer.valueOf(col));
        item.setSizeX(Integer.valueOf(sizeX));
        item.setSizeY(Integer.valueOf(sizeY));
    }

    /**
     * Get the {@link GridItem} component in the grid with the specified {@code uuid}.
     *
     * @param uuid the UUID of the item (assigned by ZK when bound to the Desktop)
     * @return the grid item for the UUID
     * @throws IllegalStateException if no grid item was found with the id
     */
    private GridItem getGridItem(String uuid) {
        for (GridItem item : getGridItems()) {
            if (uuid.equals(item.getUuid())) {
                return item;
            }
        }
        throw new IllegalStateException("Cannot find GridItem '" + uuid + "'.");
    }

    /**
     * Get all {@link GridItem} components in the grid.
     *
     * @return the grid items
     * @throws IllegalStateException if one or more child is not a grid item
     */
    public List<GridItem> getGridItems() {
        List<GridItem> items = new ArrayList<GridItem>();
        for (Component child : getChildren()) {
            if (child instanceof GridItem) {
                items.add((GridItem) child);
            } else {
                throw new IllegalStateException("Invalid child for Gridster, expected GridItem but found "
                        + child.getClass().getSimpleName());
            }
        }
        return items;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean insertBefore(Component newChild, Component refChild) {
        if (newChild instanceof GridItem) {
            return super.insertBefore(newChild, refChild);
        } else {
            throw new IllegalArgumentException("Invalid child for Gridster, expected GridItem but got "
                    + newChild.getClass().getSimpleName());
        }
    }

    public int[] getWidgetMargins() {
        return widgetMargins;
    }

    public int getWidgetMarginHorizontal() {
        return widgetMargins[0];
    }

    public int getWidgetMarginVertical() {
        return widgetMargins[1];
    }

    public int[] getWidgetBaseDimensions() {<commit_msg>getGridItems now returns items naturally sorted<commit_after>package org.zkoss.gridster;

import org.zkoss.json.JSONObject;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.UiException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.sys.ComponentCtrl;
import org.zkoss.zk.ui.sys.ContentRenderer;
import org.zkoss.zul.impl.XulElement;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * A <a href="http://gridster.net/">gridster.js</a> component.
 *
 * @author <NAME>
 */
public class Gridster extends XulElement {

    static {
        addClientEvent(Gridster.class, GridsterEvents.ON_CHANGE, ComponentCtrl.CE_DUPLICATE_IGNORE);
        addClientEvent(Gridster.class, GridsterEvents.ON_DRAG_START, ComponentCtrl.CE_IMPORTANT);
        addClientEvent(Gridster.class, GridsterEvents.ON_DRAG, ComponentCtrl.CE_IMPORTANT);
        addClientEvent(Gridster.class, GridsterEvents.ON_DRAG_STOP, ComponentCtrl.CE_IMPORTANT);
        addClientEvent(Gridster.class, GridsterEvents.ON_RESIZE_START, ComponentCtrl.CE_IMPORTANT);
        addClientEvent(Gridster.class, GridsterEvents.ON_RESIZE, ComponentCtrl.CE_IMPORTANT);
        addClientEvent(Gridster.class, GridsterEvents.ON_RESIZE_STOP, ComponentCtrl.CE_IMPORTANT);
    }

    private final int[] widgetMargins = new int[]{10, 10};
    private final int[] widgetBaseDimensions = new int[]{140, 140};

    private int extraRows = 0;
    private int extraCols = 0;

    private int minRows = 15;
    private int minCols = 1;

    private Integer maxCols = null;
    private Integer maxSizeX = null;
    private Integer maxSizeY = null;

    private boolean resizeEnabled = false;
    private String resizeAxes = "both";
    private final Integer[] resizeMaxSize = new Integer[]{null, null};
    private final Integer[] resizeMinSize = new Integer[]{null, null};

    public Gridster() {
        addEventListener(0, GridsterEvents.ON_CHANGE, new EventListener<Event>() {

            @Override
            public void onEvent(Event event) {
                Object[] data = (Object[]) event.getData();
                for (Object state : data) {
                    saveGridItemState((JSONObject) state);
                }
            }
        });
    }

    /**
     * Save the {@link GridItem} state returned from the client. Note that adjusting the state of one item often affects
     * many, as such it is the gridster's responsibility to notify all items.
     *
     * @param state the state of a grid item
     */
    private void saveGridItemState(JSONObject state) {
        String uuid = state.get("uuid").toString();
        String row = state.get("row").toString();
        String col = state.get("col").toString();
        String sizeX = state.get("size_x").toString();
        String sizeY = state.get("size_y").toString();
        GridItem item = getGridItem(uuid);
        item.setRow(Integer.valueOf(row));
        item.setCol(Integer.valueOf(col));
        item.setSizeX(Integer.valueOf(sizeX));
        item.setSizeY(Integer.valueOf(sizeY));
    }

    /**
     * Get the {@link GridItem} component in the grid with the specified {@code uuid}.
     *
     * @param uuid the UUID of the item (assigned by ZK when bound to the Desktop)
     * @return the grid item for the UUID
     * @throws IllegalStateException if no grid item was found with the id
     */
    private GridItem getGridItem(String uuid) {
        for (GridItem item : getGridItems()) {
            if (uuid.equals(item.getUuid())) {
                return item;
            }
        }
        throw new IllegalStateException("Cannot find GridItem '" + uuid + "'.");
    }

    /**
     * Get all {@link GridItem} components in the grid.
     *
     * @return the grid items
     * @throws IllegalStateException if one or more child is not a grid item
     */
    public List<GridItem> getGridItems() {
        List<GridItem> items = new ArrayList<GridItem>();
        for (Component child : getChildren()) {
            if (child instanceof GridItem) {
                items.add((GridItem) child);
            } else {
                throw new IllegalStateException("Invalid child for Gridster, expected GridItem but found "
                        + child.getClass().getSimpleName());
            }
        }
        Collections.sort(items, new Comparator<GridItem>() {
            @Override
            public int compare(GridItem a, GridItem b) {
                int order = Integer.compare(a.getRow(), b.getRow());
                if (order == 0) {
                    order = Integer.compare(a.getCol(), b.getCol());
                }
                return order;
            }
        });
        return items;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean insertBefore(Component newChild, Component refChild) {
        if (newChild instanceof GridItem) {
            return super.insertBefore(newChild, refChild);
        } else {
            throw new IllegalArgumentException("Invalid child for Gridster, expected GridItem but got "
                    + newChild.getClass().getSimpleName());
        }
    }

    public int[] getWidgetMargins() {
        return widgetMargins;
    }

    public int getWidgetMarginHorizontal() {
        return widgetMargins[0];
    }

    public int getWidgetMarginVertical() {
        return widgetMargins[1];
    }

    public int[] getWidgetBaseDimensions() {
{'fraction_non_alphanumeric': 0.10477541371158393, 'fraction_numerical': 0.0034988179669030732, 'mean_word_length': 2.587516960651289, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5892211', 'n_tokens_mistral': 3001, 'n_tokens_neox': 2923, 'n_words': 773}