BrightSide Workbench Full Report + Source Code
InputDialog.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.zkoss.dialog;
19 
20 import java.util.Set;
21 import org.turro.command.Command;
22 import org.turro.command.Context;
23 import org.turro.elephant.zkoss.Modal;
24 import org.turro.elephant.zkoss.ModalWindow;
25 import org.zkoss.zk.ui.Page;
26 import org.zkoss.zk.ui.event.Event;
27 import org.zkoss.zk.ui.event.EventListener;
28 import org.zkoss.zk.ui.event.Events;
29 import org.zkoss.zk.ui.ext.AfterCompose;
30 import org.zkoss.zul.*;
31 
36 public class InputDialog extends ModalWindow {
37 
38  private InputField[] fields;
39  private Set<DialogButton> buttons;
40  private Panel panel;
41  private Vbox container;
42  private DialogButton pressedButton;
43 
44  public InputDialog(String title, InputField[] fields, Set<DialogButton> buttons) {
45  super(title, "normal", false);
46  this.fields = fields;
47  this.buttons = buttons;
48  panel = new Panel();
49  appendChild(panel);
50  Panelchildren pc = new Panelchildren();
51  pc.setStyle("padding: 5px;");
52  panel.appendChild(pc);
53  container = new Vbox();
54  container.setSclass("inputDialogContainer");
55  pc.appendChild(container);
56  createFields();
57  container.appendChild(new Separator());
58  createButtons();
59  }
60 
61  public InputField[] getFields() {
62  return fields;
63  }
64 
66  return pressedButton;
67  }
68 
69  private void createFields() {
70  for(InputField field : fields) {
71  container.appendChild(field);
72  if(field.getEditor() instanceof AfterCompose) {
73  ((AfterCompose) field.getEditor()).afterCompose();
74  }
75  }
76  }
77 
78  private void createButtons() {
79  Hbox butCont = new Hbox();
80  butCont.setSclass("buttonDialogContainer");
81  butCont.setHflex("true");
82  butCont.setPack("end");
83  butCont.setStyle("padding:10px");
84  butCont.setSpacing("15px");
85  container.appendChild(butCont);
86  boolean panelDone = false;
87  for(final DialogButton button : buttons) {
88  Button b = button.getButton();
89  EventListener el = new EventListener() {
90  @Override
91  public void onEvent(Event event) throws Exception {
92  pressedButton = button;
93  Events.postEvent(new Event(Events.ON_CLOSE, InputDialog.this));
94  }
95  };
96  b.addEventListener(Events.ON_CLICK, el);
97  if(!panelDone) {
98  panel.addEventListener(Events.ON_OK, el);
99  panelDone = true;
100  }
101  butCont.appendChild(b);
102  }
103  }
104 
105  public static void getInput(Page page, String title, String label, Object value,
106  String format, int scale, final Command onOk) {
107  getInput(page, title, label, value, format, scale, onOk, null);
108  }
109 
110  public static void getInput(Page page, String title, String label, Object value,
111  String format, int scale, final Command onOk, final Command onClose) {
112 // try {
113  InputField field = new InputField(label, value, format, scale);
114  final InputDialog id = new InputDialog(title, new InputField[] { field }, DialogButton.getOkCancelButtons());
115  id.setPage(page);
116  Modal.doModal(id, new Command() {
117  @Override
118  public Object execute(Context context) {
119  if(onClose != null) onClose.execute(context);
120  if(id.getPressedButton().equals(DialogButton.BUTTON_OK)) {
121  context.put("value", id.getFields()[0].getValue());
122  if(onOk != null) onOk.execute(context);
123  }
124  return null;
125  }
126  });
127 // id.show(page);
128 // if(id.getPressedButton().equals(DialogButton.BUTTON_OK)) {
129 // for(InputField f : id.getFields()) {
130 // return f.getValue();
131 // }
132 // }
133 // } catch (InterruptedException ex) {
134 // Logger.getLogger(InputDialog.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
135 // }
136 // return null;
137  }
138 
139  public static void getInput(Page page, String title, InputField[] fields, final Command onOk) {
140  getInput(page, title, fields, onOk, null);
141  }
142 
143  public static void getInput(Page page, String title, InputField[] fields, final Command onOk, final Command onClose) {
144 // try {
145  final InputDialog id = new InputDialog(title, fields, DialogButton.getOkCancelButtons());
146  id.setPage(page);
147  Modal.doModal(id, new Command() {
148  @Override
149  public Object execute(Context context) {
150  if(onClose != null) onClose.execute(context);
151  if(id.getPressedButton().equals(DialogButton.BUTTON_OK)) {
152  context.put("fields", id.getFields());
153  if(onOk != null) onOk.execute(context);
154  }
155  return null;
156  }
157  });
158 // id.show(page);
159 // if(id.getPressedButton().equals(DialogButton.BUTTON_OK)) {
160 // return id.getFields();
161 // }
162 // } catch (InterruptedException ex) {
163 // Logger.getLogger(InputDialog.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
164 // }
165 // return new InputField[0];
166  }
167 
168 }
static int doModal(String file)
Definition: Modal.java:38
static void getInput(Page page, String title, InputField[] fields, final Command onOk)
static void getInput(Page page, String title, InputField[] fields, final Command onOk, final Command onClose)
static void getInput(Page page, String title, String label, Object value, String format, int scale, final Command onOk)
InputDialog(String title, InputField[] fields, Set< DialogButton > buttons)
static void getInput(Page page, String title, String label, Object value, String format, int scale, final Command onOk, final Command onClose)
static Set< DialogButton > getOkCancelButtons()