Java tutorial
/* * Copyright (c) 2011 Zhihua (Dennis) Jiang * * 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 com.gwtm.ui.client.widgets; import java.beans.Beans; import com.google.gwt.core.client.Scheduler; import com.google.gwt.core.client.Scheduler.ScheduledCommand; import com.google.gwt.event.logical.shared.ResizeEvent; import com.google.gwt.event.logical.shared.ResizeHandler; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.SimplePanel; import com.google.gwt.user.client.ui.Widget; import com.gwtm.ui.client.core.Utils; import com.gwtm.ui.client.core.widgets.PanelBase; import com.gwtm.ui.client.themes.ThemeManager; public class FlowGridPanel2 extends PanelBase implements ResizeHandler { public enum Columns { One(1), Two(2), Three(3), Four(4), Five(5), Six(6), Seven(7), Eight(8); private int col; private Columns(int col) { this.col = col; } public int getColNumber() { return this.col; } }; private Columns portraitColumns = Columns.Four; private Columns landscapeColumns = Columns.Four; private int calculatedWidth; public FlowGridPanel2() { Utils.Widgets.setPrimaryCssClass(this, ThemeManager.getTheme().flowgridpanel()); if (Beans.isDesignTime()) { calculateCellWidth(FlowGridPanel2.this); updateUi(FlowGridPanel2.this); } Scheduler.get().scheduleDeferred(new ScheduledCommand() { @Override public void execute() { calculateCellWidth(FlowGridPanel2.this); updateUi(FlowGridPanel2.this); } }); } @Override public void onLoad() { Window.addResizeHandler(this); } @Override public void add(Widget w) { super.add(new SimplePanel(w)); } private static void calculateCellWidth(FlowGridPanel2 panel) { //int width = RootPanel.get().getOffsetWidth() - panel.getElement().getAbsoluteLeft() - panel.getElement().getAbsoluteRight(); int width = panel.getElement().getOffsetWidth();// panel.getElement().getClientWidth(); if (width == 0) width = RootPanel.get().getOffsetWidth() - panel.getElement().getAbsoluteLeft() - panel.getElement().getAbsoluteRight(); int numCols = Utils.DeviceInfo.isLandscape() ? panel.getLandscapeColumns().getColNumber() : panel.getPortraitColumns().getColNumber(); panel.calculatedWidth = width / numCols; } private static void updateUi(FlowGridPanel2 panel) { int width = panel.getElement().getClientWidth(); if (width == 0) width = RootPanel.get().getOffsetWidth() - panel.getElement().getAbsoluteLeft() - panel.getElement().getAbsoluteRight(); int numCols = Utils.DeviceInfo.isLandscape() ? panel.getLandscapeColumns().getColNumber() : panel.getPortraitColumns().getColNumber(); panel.calculatedWidth = width / numCols; panel.calculatedWidth = panel.calculatedWidth; // reduce 30px and give all to the padding on the parent if (width < 0) return; for (int i = 0; i < panel.getWidgetCount(); i++) { Widget w = panel.getWidget(i); w.setWidth(panel.calculatedWidth + "px"); w.setHeight(panel.calculatedWidth + "px"); } } public Columns getPortraitColumns() { return portraitColumns; } public void setPortraitColumns(Columns portraitColumns) { this.portraitColumns = portraitColumns; updateUi(this); } public Columns getLandscapeColumns() { return landscapeColumns; } public void setLandscapeColumns(Columns landscapeColumns) { this.landscapeColumns = landscapeColumns; updateUi(this); } // private void updateUi2() { // if (getWidgetCount() == 0) return; // // int colls = 1; // if (Utils.DeviceInfo.isPortrait()) // colls = getPortraitColumns().getColNumber(); // if (Utils.DeviceInfo.isLandscape()) // colls = getLandscapeColumns().getColNumber(); // // int colWidthValue = (int)((100 / (double)colls)-7); // String colWidth = colWidthValue+"%"; // // // calculate the number of rows // int rows = (int) Math.ceil((double)getWidgetCount() / (double)colls); // //int rows = (int)(getWidgetCount() / colls); // if (rows == 0) rows = 1; //// if (getWidgetCount() > (colls*rows)) //// rows++; // // int capMax = colls*rows; // // int remainingCells = capMax - getWidgetCount(); // // Utils.Console("dg: " + colls + " / " + rows + " ( max:"+ capMax+") - (count: " + getWidgetCount() + ") (remaining: " + remainingCells + ")- mode: " + Utils.DeviceInfo.isPortrait()); //// super.clear(); // // } @Override public void onResize(ResizeEvent event) { calculateCellWidth(FlowGridPanel2.this); updateUi(FlowGridPanel2.this); } }