View Javadoc
1   /**
2    * This file Copyright (c) 2010-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.vaadin.dialog;
35  
36  import com.vaadin.ui.Button;
37  import com.vaadin.ui.Button.ClickEvent;
38  import com.vaadin.ui.Button.ClickListener;
39  import com.vaadin.ui.Component;
40  import com.vaadin.ui.CssLayout;
41  import com.vaadin.ui.Label;
42  
43  /**
44   * ConfirmationDialog.
45   */
46  public class ConfirmationDialog extends LightDialog {
47  
48      public static final String CONFIRM_ACTION_NAME = "confirm";
49  
50      private String message;
51  
52      private Button confirmButton;
53  
54      private Button cancelButton;
55  
56      public ConfirmationDialog(final String message, String confirmLabel, String cancelLabel, boolean cancelIsDefault) {
57          super();
58          setMessage(message);
59          init(confirmLabel, cancelLabel, cancelIsDefault);
60      }
61  
62      public ConfirmationDialog(final Component contents, String confirmLabel, String cancelLabel, boolean cancelIsDefault) {
63          super();
64          message = "";
65          setContent(contents);
66          init(confirmLabel, cancelLabel, cancelIsDefault);
67      }
68  
69      public void init(String confirmLabel, String cancelLabel, boolean cancelIsDefault) {
70          CssLayout footer = new CssLayout();
71          footer.addStyleName("v-align-right");
72  
73          confirmButton = new Button(confirmLabel, new ClickListener() {
74              @Override
75              public void buttonClick(ClickEvent event) {
76                  confirm();
77              }
78          });
79          confirmButton.setDisableOnClick(true);
80  
81          cancelButton = new Button(cancelLabel, new ClickListener() {
82              @Override
83              public void buttonClick(ClickEvent event) {
84                  cancel();
85              }
86          });
87          cancelButton.setDisableOnClick(true);
88  
89          footer.addComponent(cancelButton);
90          footer.addComponent(confirmButton);
91  
92          cancelButton.addStyleName("btn-dialog");
93          cancelButton.addStyleName("cancel");
94          confirmButton.addStyleName("btn-dialog");
95          confirmButton.addStyleName("commit");
96  
97          footer.setWidth(100, Unit.PERCENTAGE);
98          setFooterToolbar(footer);
99  
100         // Add a class to the default button
101         if (cancelIsDefault) {
102             cancelButton.focus();
103         } else {
104             confirmButton.focus();
105         }
106     }
107 
108     public void confirm(){
109         fireEvent(new ConfirmationEvent(ConfirmationDialog.this, true));
110     }
111 
112     public void cancel(){
113         fireEvent(new ConfirmationEvent(ConfirmationDialog.this, false));
114     }
115 
116     public void setMessage(String message) {
117         this.message = message;
118         if (getContent() != null && getContent() instanceof Label) {
119             ((Label) getContent()).setValue(message);
120         }
121     }
122 
123     public String getMessage() {
124         return message;
125     }
126 
127     @Override
128     public void setContent(Component content) {
129         super.setContent(content);
130     }
131 
132     @Override
133     protected Component createDefaultContent() {
134         return new Label();
135     }
136 
137     public void addConfirmationHandler(ConfirmationEvent.Handler handler) {
138         addListener("confirmation_event", ConfirmationEvent.class, handler, ConfirmationEvent.ON_CONFIRMATION);
139     }
140 
141     public void removeConfirmationHandler(ConfirmationEvent.Handler handler) {
142         removeListener("confirmation_event", ConfirmationEvent.class, handler);
143     }
144 
145     /**
146      * ConfirmationEvent.
147      */
148     public static class ConfirmationEvent extends Component.Event {
149 
150         /**
151          * Handler.
152          */
153         public interface Handler {
154             void onConfirmation(ConfirmationEvent event);
155         }
156 
157         public static final java.lang.reflect.Method ON_CONFIRMATION;
158 
159         static {
160             try {
161                 ON_CONFIRMATION = ConfirmationEvent.Handler.class.getDeclaredMethod("onConfirmation", new Class[]{ConfirmationEvent.class});
162             } catch (final java.lang.NoSuchMethodException e) {
163                 throw new java.lang.RuntimeException(e);
164             }
165         }
166 
167         private final boolean isConfirmed;
168 
169         public ConfirmationEvent(Component source, boolean isConfirmed) {
170             super(source);
171             this.isConfirmed = isConfirmed;
172         }
173 
174         public boolean isConfirmed() {
175             return isConfirmed;
176         }
177     }
178 }