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