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