raimund@2977: package de.intevation.flys.client.client.ui.chart; raimund@2977: raimund@2977: import java.util.Date; raimund@2977: import java.util.HashMap; raimund@2977: import java.util.Map; raimund@2977: import java.util.Set; raimund@2977: raimund@2977: import com.google.gwt.core.client.GWT; raimund@2977: raimund@2977: import com.smartgwt.client.widgets.Canvas; raimund@2977: import com.smartgwt.client.widgets.Img; raimund@2977: import com.smartgwt.client.widgets.events.ResizedEvent; raimund@2977: import com.smartgwt.client.widgets.events.ResizedHandler; raimund@2977: import com.smartgwt.client.widgets.layout.HLayout; raimund@2977: import com.smartgwt.client.widgets.layout.VLayout; raimund@2977: raimund@2977: import de.intevation.flys.client.client.Config; raimund@2977: import de.intevation.flys.client.client.ui.CollectionView; raimund@2977: import de.intevation.flys.client.client.ui.OutputTab; raimund@2977: raimund@2977: import de.intevation.flys.client.shared.model.Collection; raimund@2977: import de.intevation.flys.client.shared.model.OutputMode; raimund@2977: raimund@2977: raimund@2977: /** raimund@2977: * An overview output tab. This tab displays an overview of all charts raimund@2977: * integrated in the current calculation mode. raimund@2977: * raimund@2977: * @author Raimund Renkert raimund@2977: */ raimund@2977: public class OverviewOutputTab raimund@2977: extends OutputTab raimund@2977: implements ResizedHandler raimund@2977: { raimund@2977: /** The collection view.*/ raimund@2977: protected CollectionView view; raimund@2977: raimund@2977: /** The root layout. */ raimund@2977: protected HLayout root; raimund@2977: raimund@2977: /** The columns */ raimund@2977: protected VLayout[] columns; raimund@2977: raimund@2977: /** The chart container.*/ raimund@2977: protected Canvas[][] charts; raimund@2977: raimund@2977: /** All relevant output modes. */ raimund@2977: protected Map relModes; raimund@2977: raimund@2977: raimund@2977: public OverviewOutputTab( raimund@2977: String title, raimund@2977: Collection collection, raimund@2977: OutputMode mode, raimund@2977: CollectionView collectionView) raimund@2977: { raimund@2977: super(title, collection, collectionView, mode); raimund@2977: raimund@2977: relModes = new HashMap(); raimund@2977: raimund@2977: Map modes = collection.getOutputModes(); raimund@2977: Set keys = modes.keySet(); raimund@2977: raimund@2977: // Get all relevant outputs. raimund@2977: for (String key: keys) { raimund@2977: OutputMode m = modes.get(key); raimund@2977: if (m.getType().equals("chart")) { raimund@2977: relModes.put(key, m); raimund@2977: } raimund@2977: } raimund@2977: raimund@2977: root = new HLayout(); raimund@2977: setPane(root); raimund@2977: root.addResizedHandler(this); raimund@2977: raimund@2977: // Get the column and row count and initialize the grid. raimund@2977: int width = getColumnCount(relModes.size()); raimund@2977: int height = getRowCount(relModes.size()); raimund@2977: charts = new Canvas[width][height]; raimund@2977: columns = new VLayout[width]; raimund@2977: raimund@2977: for (int i = 0; i < width; i++) { raimund@2977: columns[i] = new VLayout(); raimund@2977: root.addMember(columns[i]); raimund@2977: raimund@2977: for (int j = 0; j < height; j++) { raimund@2977: charts[i][j] = new Canvas(); raimund@2977: raimund@2977: // This is for 3, 6 or 9 charts only! raimund@2977: // TODO: Calculate the height. raimund@2982: charts[i][j].setHeight("50%"); raimund@2977: raimund@2977: String type = raimund@2977: ((OutputMode)relModes.values() raimund@2977: .toArray()[j + i * height]).getName(); raimund@2977: columns[i].addMember(charts[i][j]); raimund@2977: charts[i][j].addChild( raimund@2977: new Img(getImgUrl( raimund@2977: charts[i][j].getWidth(), raimund@2977: charts[i][j].getHeight(), type))); raimund@2977: raimund@2977: } raimund@2977: } raimund@2977: } raimund@2977: raimund@2977: raimund@2977: /** raimund@2977: * Resize handler. raimund@2977: * raimund@2977: * @param event The resize event. raimund@2977: */ raimund@2977: @Override raimund@2977: public void onResized(ResizedEvent event) { raimund@2977: for (int i = 0; i < charts.length; i++) { raimund@2982: // This is for 3, 6 or 9 charts only! raimund@2982: // TODO: Calculate the width. raimund@2982: columns[i].setWidth(root.getWidth()/3); raimund@2982: raimund@2977: for (int j = 0; j < charts[i].length; j++) { raimund@2977: String type = raimund@2977: ((OutputMode)relModes.values() raimund@2977: .toArray()[j + i * charts[i].length]).getName(); raimund@2977: Canvas[] children = charts[i][j].getChildren(); raimund@2977: for (int k = 0; k < children.length; k++) { raimund@2977: charts[i][j].removeChild(children[k]); raimund@2977: } raimund@2977: charts[i][j].addChild(new Img( raimund@2977: getImgUrl( raimund@2977: charts[i][j].getWidth(), raimund@2977: charts[i][j].getHeight(), raimund@2977: type), raimund@2977: charts[i][j].getWidth(), raimund@2977: charts[i][j].getHeight() raimund@2977: )); raimund@2977: } raimund@2977: } raimund@2977: } raimund@2977: raimund@2977: raimund@2977: /** raimund@2977: * Returns the column count for the grid. raimund@2977: * raimund@2977: * @param count all fields raimund@2977: * @return the column count raimund@2977: */ raimund@2982: protected int getRowCount(int count) { raimund@2977: if (count <= 3) { raimund@2977: return 1; raimund@2977: } raimund@2977: else if (count > 3 && count < 9) { raimund@2977: return 2; raimund@2977: } raimund@2977: else { raimund@2977: return 3; raimund@2977: } raimund@2977: } raimund@2977: raimund@2977: raimund@2977: /** raimund@2977: * Returns the row count for the grid. raimund@2977: * raimund@2977: * @param count all fields raimund@2977: * @return the row count raimund@2977: */ raimund@2982: protected int getColumnCount(int count) { raimund@2977: if(count <= 3) { raimund@2977: return count; raimund@2977: } raimund@2977: else if(count > 3 && count < 9) { raimund@2982: return ((count + (count % 2))/getRowCount(count)); raimund@2977: } raimund@2977: else { raimund@2982: return (count + (3 - (count % 3))/getRowCount(count)); raimund@2977: } raimund@2977: } raimund@2977: raimund@2977: raimund@2977: /** raimund@2977: * Builds the URL that points to the chart image. raimund@2977: * raimund@2977: * @param width The width of the requested chart. raimund@2977: * @param height The height of the requested chart. raimund@2977: * @param xr Optional x range (used for zooming). raimund@2977: * @param yr Optional y range (used for zooming). raimund@2977: * raimund@2977: * @return the URL to the chart image. raimund@2977: */ raimund@2977: protected String getImgUrl(int width, int height, String type) { raimund@2977: Config config = Config.getInstance(); raimund@2977: raimund@2977: String imgUrl = GWT.getModuleBaseURL(); raimund@2977: imgUrl += "chart"; raimund@2977: imgUrl += "?uuid=" + collection.identifier(); raimund@2977: imgUrl += "&type=" + type; raimund@2977: imgUrl += "&locale=" + config.getLocale(); raimund@2977: imgUrl += "×tamp=" + new Date().getTime(); raimund@2977: imgUrl += "&width=" + Integer.toString(width); raimund@2977: imgUrl += "&height=" + Integer.toString(height); raimund@2977: raimund@2977: return imgUrl; raimund@2977: } raimund@2977: } raimund@2977: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :