BrightSide Workbench Full Report + Source Code
SelectionDialog.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.ModalWindow;
24 import org.zkoss.zk.ui.Component;
25 import org.zkoss.zk.ui.HtmlBasedComponent;
26 import org.zkoss.zk.ui.Page;
27 import org.zkoss.zk.ui.event.Event;
28 import org.zkoss.zk.ui.event.EventListener;
29 import org.zkoss.zk.ui.event.Events;
30 import org.zkoss.zk.ui.ext.AfterCompose;
31 import org.zkoss.zul.*;
32 
37 public class SelectionDialog extends ModalWindow implements AfterCompose {
38 
39  private Set<DialogButton> buttons;
40  private Panel panel;
41  private Vlayout container;
42  private DialogButton pressedButton;
43  private Component component;
44 
45  public SelectionDialog(String title, Component component, Set<DialogButton> buttons) {
46  super(title, "normal", false);
47  this.component = component;
48  this.buttons = buttons;
49  panel = new Panel();
50  panel.setVflex("true");
51  panel.setHflex("true");
52  appendChild(panel);
53  Panelchildren pc = new Panelchildren();
54  panel.appendChild(pc);
55  container = new Vlayout();
56  container.setHflex("true");
57  container.setVflex("true");
58  container.setSclass("_inputDialogContainer");
59  pc.appendChild(container);
60  container.appendChild(component);
61  if(component instanceof AfterCompose) {
62  ((AfterCompose) component).afterCompose();
63  }
64  if(buttons != null) {
65  createButtons();
66  setSizable(true);
67  setMaximizable(true);
68  } else {
69  setClosable(true);
70  setSizable(true);
71  setMaximizable(true);
72  //setWidth(((HtmlBasedComponent) component).getWidth());
73  //setHeight(((HtmlBasedComponent) component).getHeight());
74  //((HtmlBasedComponent) component).setVflex("true");
75  //((HtmlBasedComponent) component).setHflex("true");
76  }
77  if(((HtmlBasedComponent) this.component).getHeight()== null) {
78  ((HtmlBasedComponent) this.component).setVflex("true");
79  }
80  if(((HtmlBasedComponent) this.component).getWidth() == null) {
81  ((HtmlBasedComponent) this.component).setHflex("true");
82  }
83  }
84 
85  public Component getComponent() {
86  return component;
87  }
88 
90  return pressedButton;
91  }
92 
93  @Override
94  public void afterCompose() {
95  ((HtmlBasedComponent) component).setVflex("true");
96  ((HtmlBasedComponent) component).setHflex("true");
97  }
98 
99  private void createButtons() {
100  Hbox butCont = new Hbox();
101  butCont.setSclass("_buttonDialogContainer");
102  butCont.setHflex("true");
103  butCont.setPack("end");
104  butCont.setStyle("padding:10px");
105  butCont.setSpacing("15px");
106  container.appendChild(butCont);
107  boolean panelDone = false;
108  for(final DialogButton button : buttons) {
109  Button b = button.getButton();
110  EventListener el = new EventListener() {
111  @Override
112  public void onEvent(Event event) throws Exception {
113  pressedButton = button;
114  Events.postEvent(new Event(Events.ON_CLOSE, SelectionDialog.this));
115  }
116  };
117  b.addEventListener(Events.ON_CLICK, el);
118  if(!panelDone) {
119  panel.addEventListener(Events.ON_OK, el);
120  panelDone = true;
121  }
122  butCont.appendChild(b);
123  }
124  }
125 
126  public static void getComponent(Page page, String title, Component component, String width, String height, final Command command) {
127  final SelectionDialog id = new SelectionDialog(title, component, DialogButton.getOkCancelButtons());
128  if(width != null) id.setWidth(width);
129  if(height != null) id.setHeight(height);
130  id.show(page, new Command() {
131  @Override
132  public Object execute(Context context) {
133  if(id.getPressedButton().equals(DialogButton.BUTTON_OK)) {
134  context.put("component", id.getComponent());
135  if(command != null) command.execute(context);
136  }
137  return null;
138  }
139  });
140  }
141 
142  public static void showComponent(Page page, String title, Component component, String width, String height, final Command command) {
143  final SelectionDialog id = new SelectionDialog(title, component, null);
144  if(width != null) id.setWidth(width);
145  if(height != null) id.setHeight(height);
146  id.show(page, new Command() {
147  @Override
148  public Object execute(Context context) {
149  context.put("component", id.getComponent());
150  if(command != null) command.execute(context);
151  return null;
152  }
153  });
154  }
155 
156  public static void popupComponent(Page page, String title, Component component, String width, String height) {
157  SelectionDialog id = new SelectionDialog(title, component, DialogButton.getCloseButtons());
158  id.setMode("popup");
159  if(width != null) id.setWidth(width);
160  if(height != null) id.setHeight(height);
161  id.show(page, null);
162  }
163 
164 }
SelectionDialog(String title, Component component, Set< DialogButton > buttons)
static void showComponent(Page page, String title, Component component, String width, String height, final Command command)
static void popupComponent(Page page, String title, Component component, String width, String height)
static void getComponent(Page page, String title, Component component, String width, String height, final Command command)
static Set< DialogButton > getCloseButtons()
static Set< DialogButton > getOkCancelButtons()