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.actionbar.ActionbarView;
39  import info.magnolia.ui.api.action.ActionExecutionException;
40  import info.magnolia.ui.dialog.actionarea.ActionListener;
41  import info.magnolia.ui.mediaeditor.MediaEditorEventBus;
42  import info.magnolia.ui.mediaeditor.MediaEditorView;
43  import info.magnolia.ui.mediaeditor.data.EditHistoryTrackingProperty;
44  import info.magnolia.ui.mediaeditor.event.MediaEditorInternalEvent;
45  import info.magnolia.ui.mediaeditor.event.MediaEditorInternalEvent.EventType;
46  import info.magnolia.ui.mediaeditor.field.MediaField;
47  import info.magnolia.ui.mediaeditor.field.image.ViewImageField;
48  import info.magnolia.ui.mediaeditor.provider.MediaEditorActionDefinition;
49  import info.magnolia.ui.vaadin.gwt.client.actionbar.shared.ActionbarItem;
50  
51  import java.util.ArrayList;
52  import java.util.List;
53  
54  import javax.inject.Named;
55  
56  import com.vaadin.ui.Component;
57  import com.vaadin.v7.ui.Label;
58  
59  /**
60   * Simply displays an image with dialog actions to
61   * submit or revert previous modifications. Also updates the undo/redo actions.
62   */
63  public class ViewImageAction extends MediaEditorUIAction {
64  
65      private ViewImageField viewField = new ViewImageField();
66  
67      private ImageSizeLabel imageSizeLabel;
68  
69      private final SimpleTranslator i18n;
70  
71      public ViewImageAction(MediaEditorActionDefinition definition, MediaEditorView view, @Named(MediaEditorEventBus.NAME) EventBus eventBus,
72              EditHistoryTrackingProperty dataSource, SimpleTranslator i18n) {
73          super(definition, view, dataSource, eventBus);
74          this.i18n = i18n;
75          imageSizeLabel = new ImageSizeLabel(i18n);
76          viewField.addImageResizeListener(imageSizeLabel);
77      }
78  
79      @Override
80      protected List<ActionContext> getActionContextList() {
81          List<ActionContext> result = new ArrayList<ActionContext>();
82          result.add(new ActionContext(new InternalMediaEditorActionDefinition("save", i18n.translate("ui-mediaeditor.internalAction.save.label"), false), new ActionListener() {
83              @Override
84              public void onActionFired(String actionName, Object... actionContextParams) {
85                  eventBus.fireEvent(new MediaEditorInternalEvent(EventType.SUBMIT));
86              }
87          }));
88  
89          result.add(new ActionContext(new InternalMediaEditorActionDefinition("cancel", i18n.translate("ui-mediaeditor.internalAction.cancel.label"), true), new ActionListener() {
90              @Override
91              public void onActionFired(String actionName, Object... actionContextParams) {
92                  eventBus.fireEvent(new MediaEditorInternalEvent(EventType.CANCEL_ALL));
93              }
94          }));
95          return result;
96      }
97  
98      @Override
99      public void execute() throws ActionExecutionException {
100         ActionbarView actionbar = view.getActionbar();
101         actionbar.removeAction("undo");
102         actionbar.removeAction("redo");
103 
104         String undoLabel = i18n.translate("ui-mediaeditor.action.undo.label")+" " + (dataSource.getLastDoneActionName() != null ? dataSource.getLastDoneActionName() : "");
105         String redoLabel = i18n.translate("ui-mediaeditor.action.redo.label")+" " + (dataSource.getLastUnDoneActionName() != null ? dataSource.getLastUnDoneActionName() : "");
106 
107         ActionbarItem undo = new ActionbarItem("undo", undoLabel, "icon-undo", "track");
108         ActionbarItem redo = new ActionbarItem("redo", redoLabel, "icon-redo", "track");
109 
110         actionbar.addAction(undo, "operations");
111         actionbar.addAction(redo, "operations");
112 
113         actionbar.setActionEnabled("undo", dataSource.getLastDoneActionName() != null);
114         actionbar.setActionEnabled("redo", dataSource.getLastUnDoneActionName() != null);
115         view.getDialog().asVaadinComponent().removeStyleName("active-footer");
116         super.execute();
117 
118     }
119 
120     @Override
121     protected Component getStatusControls() {
122         return imageSizeLabel;
123     }
124 
125     @Override
126     protected MediaField createMediaField() {
127         return viewField;
128     }
129 
130     /**
131      * ImageSizeLabel.
132      */
133     public static class ImageSizeLabel extends Label implements ViewImageField.ImageSizeChangeListener {
134 
135         private final SimpleTranslator i18n;
136 
137         public ImageSizeLabel(SimpleTranslator i18n){
138             this.i18n = i18n;
139         }
140         @Override
141         public void onSizeChanged(ViewImageField.ImageResizeEvent e) {
142             setValue(String.format(i18n.translate("ui-mediaeditor.view.actualSize.display"), e.getWidth(), e.getHeight()));
143         }
144     }
145 }