View Javadoc

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