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.action.feature.Scalable;
43  import info.magnolia.ui.mediaeditor.data.MediaState;
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.CropField;
48  import info.magnolia.ui.mediaeditor.provider.MediaEditorActionDefinition;
49  
50  import java.util.ArrayList;
51  import java.util.List;
52  
53  import javax.inject.Named;
54  
55  import org.apache.commons.lang3.StringUtils;
56  
57  import com.vaadin.ui.Component;
58  import com.vaadin.ui.Label;
59  
60  /**
61   * Installs UI components necessary for conducting the image crop operations.
62   */
63  public class CropImageAction extends MediaEditorUIAction {
64  
65      private CropFieldeld/image/CropField.html#CropField">CropField cropField = new CropField();
66  
67      private final SimpleTranslator i18n;
68  
69      public CropImageAction(MediaEditorActionDefinition definition,
70                             MediaEditorView view,
71                             @Named(MediaEditorEventBus.NAME) EventBus eventBus,
72                             ValueContext<MediaState> valueContext,
73                             SimpleTranslator i18n) {
74          super(definition, view, valueContext, eventBus);
75  
76          this.i18n = i18n;
77      }
78  
79      @Override
80      public void execute() throws ActionExecutionException {
81          super.execute();
82          if (view.getContentView().asVaadinComponent() instanceof Scalable) {
83              ((Scalable) view.getContentView().asVaadinComponent()).scaleToFit();
84          }
85      }
86  
87      @Override
88      protected List<ActionContext> getActionContextList() {
89          List<ActionContext> result = new ArrayList<>();
90          result.add(new ActionContext(new InternalMediaEditorActionDefinition("crop", i18n.translate("ui-mediaeditor.action.crop.label"), true), new ActionListener() {
91              @Override
92              public void onActionFired(String actionName, Object... actionContextParams) {
93                  MediaState mediaState = valueContext.getSingleOrThrow();
94                  mediaState.performMediaModification(StringUtils.lowerCase(getDefinition().getLabel()));
95                  cropField.execute();
96                  mediaState.setState(cropField.getValue());
97                  eventBus.fireEvent(new MediaEditorInternalEvent(EventType.APPLY));
98              }
99          }));
100 
101         result.add(new ActionContext(new InternalMediaEditorActionDefinition("cancel", i18n.translate("ui-mediaeditor.internalAction.cancel.label"), true), new ActionListener() {
102             @Override
103             public void onActionFired(String actionName, Object... actionContextParams) {
104                 eventBus.fireEvent(new MediaEditorInternalEvent(EventType.CANCEL_ALL));
105             }
106         }));
107 
108         return result;
109     }
110 
111     @Override
112     protected Component getStatusControls() {
113         final Label l = new Label();
114         l.setWidth("400px");
115         cropField.setStatusComponent(l);
116         return l;
117     }
118 
119     @Override
120     protected MediaField createMediaField() {
121         return cropField;
122     }
123 }