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.dialog.actionarea.ActionListener;
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.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.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.v7.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 CropField cropField = new CropField();
66  
67      private final SimpleTranslator i18n;
68  
69      public CropImageAction(MediaEditorActionDefinition definition, MediaEditorView view, @Named(MediaEditorEventBus.NAME) EventBus eventBus, EditHistoryTrackingProperty dataSource, SimpleTranslator i18n) {
70          super(definition, view, dataSource, eventBus);
71  
72          this.i18n = i18n;
73      }
74  
75      @Override
76      public void execute() throws ActionExecutionException {
77          super.execute();
78          view.getDialog().asVaadinComponent().addStyleName("active-footer");
79          if (view.getDialog().getContentView().asVaadinComponent() instanceof Scalable) {
80              Scalable scalable = (Scalable) view.getDialog().getContentView().asVaadinComponent();
81              scalable.scaleToFit();
82          }
83      }
84  
85      @Override
86      protected List<ActionContext> getActionContextList() {
87          List<ActionContext> result = new ArrayList<ActionContext>();
88          result.add(new ActionContext(new InternalMediaEditorActionDefinition("crop", i18n.translate("ui-mediaeditor.action.crop.label"), true), new ActionListener() {
89              @Override
90              public void onActionFired(String actionName, Object... actionContextParams) {
91                  dataSource.startAction(StringUtils.lowerCase(getDefinition().getLabel()));
92                  cropField.execute();
93                  eventBus.fireEvent(new MediaEditorInternalEvent(EventType.APPLY));
94              }
95          }));
96  
97          result.add(new ActionContext(new InternalMediaEditorActionDefinition("cancel", i18n.translate("ui-mediaeditor.internalAction.cancel.label"), true), new ActionListener() {
98              @Override
99              public void onActionFired(String actionName, Object... actionContextParams) {
100                 eventBus.fireEvent(new MediaEditorInternalEvent(EventType.CANCEL_ALL));
101             }
102         }));
103 
104         return result;
105     }
106 
107     @Override
108     protected Component getStatusControls() {
109         final Label l = new Label();
110         l.setWidth("400px");
111         cropField.setStatusComponent(l);
112         return l;
113     }
114 
115     @Override
116     protected MediaField createMediaField() {
117         return cropField;
118     }
119 }