View Javadoc
1   /**
2    * This file Copyright (c) 2013-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.mediaeditor.action;
35  
36  import info.magnolia.event.EventBus;
37  import info.magnolia.i18nsystem.SimpleTranslator;
38  import info.magnolia.ui.api.action.ActionExecutionException;
39  import info.magnolia.ui.ValueContext;
40  import info.magnolia.ui.mediaeditor.MediaEditorEventBus;
41  import info.magnolia.ui.mediaeditor.MediaEditorView;
42  import info.magnolia.ui.mediaeditor.data.MediaState;
43  import info.magnolia.ui.mediaeditor.event.MediaEditorInternalEvent;
44  import info.magnolia.ui.mediaeditor.event.MediaEditorInternalEvent.EventType;
45  import info.magnolia.ui.mediaeditor.field.MediaField;
46  import info.magnolia.ui.mediaeditor.field.image.ViewImageField;
47  import info.magnolia.ui.mediaeditor.provider.MediaEditorActionDefinition;
48  
49  import java.util.ArrayList;
50  import java.util.List;
51  
52  import javax.inject.Named;
53  
54  import com.google.common.collect.Sets;
55  import com.vaadin.ui.Component;
56  import com.vaadin.ui.Label;
57  
58  /**
59   * Simply displays an image with dialog actions to
60   * submit or revertState previous modifications. Also updates the undoLastState/redoLastState actions.
61   */
62  public class ViewImageAction extends MediaEditorUIAction {
63  
64      private ViewImageFieldmage/ViewImageField.html#ViewImageField">ViewImageField viewField = new ViewImageField();
65  
66      private ImageSizeLabel imageSizeLabel;
67  
68      private final SimpleTranslator i18n;
69  
70      public ViewImageAction(MediaEditorActionDefinition definition,
71                             MediaEditorView view, @Named(MediaEditorEventBus.NAME) EventBus eventBus,
72                             ValueContext<MediaState> valueContext,
73                             SimpleTranslator i18n) {
74          super(definition, view, valueContext, eventBus);
75          this.i18n = i18n;
76          imageSizeLabel = new ImageSizeLabel(i18n);
77          viewField.addImageResizeListener(imageSizeLabel);
78      }
79  
80      @Override
81      protected List<ActionContext> getActionContextList() {
82          List<ActionContext> result = new ArrayList<ActionContext>();
83          result.add(new ActionContext(new InternalMediaEditorActionDefinition("save", i18n.translate("ui-mediaeditor.internalAction.save.label"), false), (actionName, actionContextParams) -> eventBus.fireEvent(new MediaEditorInternalEvent(EventType.SUBMIT))));
84          result.add(new ActionContext(new InternalMediaEditorActionDefinition("cancel", i18n.translate("ui-mediaeditor.internalAction.cancel.label"), true), (actionName, actionContextParams) -> eventBus.fireEvent(new MediaEditorInternalEvent(EventType.CANCEL_ALL))));
85          return result;
86      }
87  
88      @Override
89      public void execute() throws ActionExecutionException {
90          super.execute();
91          valueContext.current().update((editHistoryTrackingProperties) -> {
92              Sets.newHashSet(valueContext.getSingleOrThrow());
93          });
94      }
95  
96      @Override
97      protected Component getStatusControls() {
98          return imageSizeLabel;
99      }
100 
101     @Override
102     protected MediaField createMediaField() {
103         return viewField;
104     }
105 
106     /**
107      * ImageSizeLabel.
108      */
109     public static class ImageSizeLabel extends Label implements ViewImageField.ImageSizeChangeListener {
110 
111         private final SimpleTranslator i18n;
112 
113         public ImageSizeLabel(SimpleTranslator i18n) {
114             this.i18n = i18n;
115         }
116 
117         @Override
118         public void onSizeChanged(ViewImageField.ImageResizeEvent e) {
119             setValue(String.format(i18n.translate("ui-mediaeditor.view.actualSize.display"), e.getWidth(), e.getHeight()));
120         }
121     }
122 }