001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     troger
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.pictures.tiles.gwt.client.view;
021
022import org.nuxeo.ecm.platform.pictures.tiles.gwt.client.controller.TilingController;
023import org.nuxeo.ecm.platform.pictures.tiles.gwt.client.model.TilingInfo;
024import org.nuxeo.ecm.platform.pictures.tiles.gwt.client.model.TilingInfoCallback;
025import org.nuxeo.ecm.platform.pictures.tiles.gwt.client.model.TilingModel;
026
027import com.google.gwt.i18n.client.Dictionary;
028import com.google.gwt.user.client.DOM;
029import com.google.gwt.user.client.Event;
030import com.google.gwt.user.client.Window;
031import com.google.gwt.user.client.ui.AbsolutePanel;
032import com.google.gwt.user.client.ui.ClickListener;
033import com.google.gwt.user.client.ui.Composite;
034import com.google.gwt.user.client.ui.Image;
035import com.google.gwt.user.client.ui.MouseListenerAdapter;
036import com.google.gwt.user.client.ui.RootPanel;
037import com.google.gwt.user.client.ui.Widget;
038
039/**
040 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
041 */
042public class TilingMainPanel extends Composite {
043
044    private static class ThumbnailButton extends Image {
045
046        private static int THUMBNAIL_BUTTON_SIZE = 17;
047
048        private boolean showThumbnail = true;
049
050        public ThumbnailButton(final TilingPreviewControllerPanel previewControllerPanel) {
051            super(HIDE_THUMBNAIL_IMAGE);
052
053            addMouseListener(new MouseListenerAdapter() {
054                @Override
055                public void onMouseDown(Widget sender, int x, int y) {
056                    Event currentEvent = Event.getCurrentEvent();
057                    DOM.eventPreventDefault(currentEvent);
058                    currentEvent.cancelBubble(true);
059                }
060            });
061
062            final ThumbnailButton thumbnailButton = this;
063            addClickListener(new ClickListener() {
064                public void onClick(Widget arg0) {
065                    showThumbnail = !showThumbnail;
066                    previewControllerPanel.setVisible(showThumbnail);
067                    thumbnailButton.setUrl(showThumbnail ? HIDE_THUMBNAIL_IMAGE : SHOW_THUMBNAIL_IMAGE);
068                }
069            });
070        }
071
072        @Override
073        public int getWidth() {
074            return THUMBNAIL_BUTTON_SIZE;
075        }
076
077        @Override
078        public int getHeight() {
079            return THUMBNAIL_BUTTON_SIZE;
080        }
081
082    }
083
084    private static final int TILE_WIDTH = 256;
085
086    private static final int TILE_HEIGHT = 256;
087
088    private static final int PREVIEW_PANEL_BORDER_SIZE = 3;
089
090    private static String HIDE_THUMBNAIL_IMAGE;
091
092    private static String SHOW_THUMBNAIL_IMAGE;
093
094    private String repoId;
095
096    private String docId;
097
098    private String contextPath;
099
100    public TilingMainPanel() {
101        final Dictionary dictionary = Dictionary.getDictionary("serverSetting");
102
103        repoId = dictionary.get("repoId");
104        docId = dictionary.get("docId");
105        contextPath = dictionary.get("contextPath");
106
107        HIDE_THUMBNAIL_IMAGE = contextPath + "/img/hide_thumbnail.png";
108        SHOW_THUMBNAIL_IMAGE = contextPath + "/img/show_thumbnail.png";
109
110        loadControllerPanelTilingInfo();
111    }
112
113    private void loadControllerPanelTilingInfo() {
114        final TilingInfo tilingInfo = new TilingInfo(repoId, docId, contextPath, 64, 64, 3);
115        tilingInfo.updateTilingInfo(new TilingInfoCallback() {
116            public void tilingInfoUpdated() {
117                // Continue the loading
118                loadModelTilingInfo(tilingInfo);
119            }
120        });
121    }
122
123    private void loadModelTilingInfo(final TilingInfo tilingInfo) {
124        // Compute the maximum number of tiles we can display
125        int maxTilesW = ((Window.getClientWidth() - PREVIEW_PANEL_BORDER_SIZE * 2) / TILE_WIDTH) + 1;
126        int maxTilesH = ((Window.getClientHeight() - PREVIEW_PANEL_BORDER_SIZE * 2) / TILE_HEIGHT) + 1;
127
128        int maxTiles = maxTilesW > maxTilesH ? maxTilesW : maxTilesH;
129        maxTiles += 1;
130        final TilingInfo currentTilingInfo = new TilingInfo(repoId, docId, contextPath, TILE_WIDTH, TILE_HEIGHT,
131                maxTiles);
132        currentTilingInfo.updateTilingInfo(new TilingInfoCallback() {
133            public void tilingInfoUpdated() {
134                finishLoading(tilingInfo, currentTilingInfo);
135            }
136        });
137    }
138
139    private void finishLoading(TilingInfo sourceTilingInfo, TilingInfo currentTilingInfo) {
140        // Size of the view area
141        final int width = Window.getClientWidth();
142        final int height = Window.getClientHeight();
143        TilingModel model = new TilingModel(currentTilingInfo, width, height, currentTilingInfo.getZoom());
144
145        // Create a controller
146        TilingController controller = new TilingController(sourceTilingInfo, model);
147
148        // the panels
149        AbsolutePanel rootPanel = new AbsolutePanel();
150        TilingPreviewPanel previewPanel = new TilingPreviewPanel(controller, model);
151        rootPanel.add(previewPanel);
152
153        final TilingPreviewControllerPanel controllerPanel = new TilingPreviewControllerPanel(sourceTilingInfo, model);
154        controllerPanel.addStyleName("thumbnail-panel");
155        final int controllerPanelWidth = (int) Math.round(sourceTilingInfo.getOriginalImageWidth()
156                * sourceTilingInfo.getZoom());
157        final int controllerPanelHeight = (int) Math.round(sourceTilingInfo.getOriginalImageHeight()
158                * sourceTilingInfo.getZoom());
159        rootPanel.add(controllerPanel, width - controllerPanelWidth - PREVIEW_PANEL_BORDER_SIZE, height
160                - controllerPanelHeight - PREVIEW_PANEL_BORDER_SIZE);
161
162        // the button to show / hide the thumbnail
163        ThumbnailButton thumbnailButton = new ThumbnailButton(controllerPanel);
164        rootPanel.add(thumbnailButton, width - thumbnailButton.getWidth(), height - thumbnailButton.getHeight());
165
166        initWidget(rootPanel);
167
168        // fix bug IE hiding everything when resizing
169        rootPanel.getElement().getStyle().setProperty("position", "absolute");
170
171        RootPanel.get("display").add(this);
172        // fire event
173        model.notifyListeners();
174    }
175
176}