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 info.magnolia.ui.api.view.View;
37  import info.magnolia.ui.vaadin.editorlike.DialogActionListener;
38  
39  import com.vaadin.event.ShortcutAction.KeyCode;
40  import com.vaadin.ui.Component;
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      public ConfirmationDialog(final String message, boolean cancelIsDefault) {
53          setMessage(message);
54          init(cancelIsDefault);
55      }
56  
57      public ConfirmationDialog(final View contents, boolean cancelIsDefault) {
58          message = "";
59          setContent(contents.asVaadinComponent());
60          init(cancelIsDefault);
61      }
62  
63      public void init(boolean cancelIsDefault) {
64          // Add a class to the default button
65          if (cancelIsDefault) {
66              this.setDefaultAction(CANCEL_ACTION_NAME);
67          } else {
68              this.setDefaultAction(CONFIRM_ACTION_NAME);
69          }
70  
71          addAction(CONFIRM_ACTION_NAME, "OK", new DialogActionListener() {
72              @Override
73              public void onActionExecuted(String actionName) {
74                  fireEvent(new ConfirmationEvent(ConfirmationDialog.this, true));
75              }
76          });
77          // we need to explicitly add an ENTER shortcut bound to the confirm action, because addAction(..) do it only for commit and cancel actions.
78          addShortcut(CONFIRM_ACTION_NAME, KeyCode.ENTER);
79  
80          addAction(CANCEL_ACTION_NAME, "Cancel", new DialogActionListener() {
81              @Override
82              public void onActionExecuted(String actionName) {
83                  fireEvent(new ConfirmationEvent(ConfirmationDialog.this, false));
84              }
85          });
86      }
87  
88  
89      public void setConfirmActionLabel(final String label) {
90          addAction(CONFIRM_ACTION_NAME, label);
91      }
92  
93      public void setRejectActionLabel(final String label) {
94          addAction(CANCEL_ACTION_NAME, label);
95      }
96  
97      public void setMessage(String message) {
98          this.message = message;
99          if (getContent() != null && getContent() instanceof Label) {
100             ((Label) getContent()).setValue(message);
101         }
102     }
103 
104     public String getMessage() {
105         return message;
106     }
107 
108     @Override
109     public void setContent(Component content) {
110         super.setContent(content);
111     }
112 
113     @Override
114     protected Component createDefaultContent() {
115         return new Label();
116     }
117 
118     public void addConfirmationHandler(ConfirmationEvent.Handler handler) {
119         addListener("confirmation_event", ConfirmationEvent.class, handler, ConfirmationEvent.ON_CONFIRMATION);
120     }
121 
122     public void removeConfirmationHandler(ConfirmationEvent.Handler handler) {
123         removeListener("confirmation_event", ConfirmationEvent.class, handler);
124     }
125 
126     /**
127      * ConfirmationEvent.
128      */
129     public static class ConfirmationEvent extends Component.Event {
130 
131         /**
132          * Handler.
133          */
134         public interface Handler {
135             void onConfirmation(ConfirmationEvent event);
136         }
137 
138         public static final java.lang.reflect.Method ON_CONFIRMATION;
139 
140         static {
141             try {
142                 ON_CONFIRMATION = ConfirmationEvent.Handler.class.getDeclaredMethod("onConfirmation", new Class[]{ConfirmationEvent.class});
143             } catch (final java.lang.NoSuchMethodException e) {
144                 throw new java.lang.RuntimeException(e);
145             }
146         }
147 
148         private final boolean isConfirmed;
149 
150         public ConfirmationEvent(Component source, boolean isConfirmed) {
151             super(source);
152             this.isConfirmed = isConfirmed;
153         }
154 
155         public boolean isConfirmed() {
156             return isConfirmed;
157         }
158     }
159 }