View Javadoc

1   /**
2    * This file Copyright (c) 2010-2014 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  
80          cancelButton = new Button(cancelLabel, new ClickListener() {
81              @Override
82              public void buttonClick(ClickEvent event) {
83                  cancel();
84              }
85          });
86  
87          footer.addComponent(cancelButton);
88          footer.addComponent(confirmButton);
89  
90          cancelButton.addStyleName("btn-dialog");
91          cancelButton.addStyleName("cancel");
92          confirmButton.addStyleName("btn-dialog");
93          confirmButton.addStyleName("commit");
94  
95          footer.setWidth(100, Unit.PERCENTAGE);
96          setFooterToolbar(footer);
97  
98          // Add a class to the default button
99          if (cancelIsDefault) {
100             cancelButton.focus();
101         } else {
102             confirmButton.focus();
103         }
104     }
105 
106     public void confirm(){
107         fireEvent(new ConfirmationEvent(ConfirmationDialog.this, true));
108     }
109 
110     public void cancel(){
111         fireEvent(new ConfirmationEvent(ConfirmationDialog.this, false));
112     }
113 
114     public void setMessage(String message) {
115         this.message = message;
116         if (getContent() != null && getContent() instanceof Label) {
117             ((Label) getContent()).setValue(message);
118         }
119     }
120 
121     public String getMessage() {
122         return message;
123     }
124 
125     @Override
126     public void setContent(Component content) {
127         super.setContent(content);
128     }
129 
130     @Override
131     protected Component createDefaultContent() {
132         return new Label();
133     }
134 
135     public void addConfirmationHandler(ConfirmationEvent.Handler handler) {
136         addListener("confirmation_event", ConfirmationEvent.class, handler, ConfirmationEvent.ON_CONFIRMATION);
137     }
138 
139     public void removeConfirmationHandler(ConfirmationEvent.Handler handler) {
140         removeListener("confirmation_event", ConfirmationEvent.class, handler);
141     }
142 
143     /**
144      * ConfirmationEvent.
145      */
146     public static class ConfirmationEvent extends Component.Event {
147 
148         /**
149          * Handler.
150          */
151         public interface Handler {
152             void onConfirmation(ConfirmationEvent event);
153         }
154 
155         public static final java.lang.reflect.Method ON_CONFIRMATION;
156 
157         static {
158             try {
159                 ON_CONFIRMATION = ConfirmationEvent.Handler.class.getDeclaredMethod("onConfirmation", new Class[]{ConfirmationEvent.class});
160             } catch (final java.lang.NoSuchMethodException e) {
161                 throw new java.lang.RuntimeException(e);
162             }
163         }
164 
165         private final boolean isConfirmed;
166 
167         public ConfirmationEvent(Component source, boolean isConfirmed) {
168             super(source);
169             this.isConfirmed = isConfirmed;
170         }
171 
172         public boolean isConfirmed() {
173             return isConfirmed;
174         }
175     }
176 }