BrightSide Workbench Full Report + Source Code
GroupItForm.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 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 
19 package org.turro.zul.groupit;
20 
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.turro.command.Command;
24 import org.turro.command.Context;
25 import org.turro.contacts.GroupIt;
26 import org.turro.contacts.db.ContactsPU;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.elephant.util.Images;
29 import org.turro.i18n.I_;
30 import org.turro.zkoss.dialog.SelectionDialog;
31 import org.turro.zkoss.layout.GridLayout;
32 import org.turro.zul.frame.Framework;
33 import org.zkoss.zk.ui.ext.AfterCompose;
34 import org.zkoss.zul.Checkbox;
35 import org.zkoss.zul.Image;
36 import org.zkoss.zul.Space;
37 import org.zkoss.zul.Textbox;
38 
43 public class GroupItForm extends GridLayout implements AfterCompose {
44 
45  private GroupIt group;
46 
47  public static void newGroup(String category, GroupIt parent, final Command command) {
48  final GroupItForm gf = new GroupItForm();
49  GroupIt group = new GroupIt();
50  group.setCategory(category);
51  group.setParent(parent);
52  group.setPublishable(true);
53  gf.setGroup(group);
55  I_.get("Group"),
56  gf, "500px", "300px", new Command() {
57  @Override
58  public Object execute(Context context) {
59  if(gf != null) {
60  GroupIt group = gf.getGroup();
61  if(group.isValid()) {
62  try {
63  group = new ContactsPU().saveObject(group);
64  context.put("group", GroupIt.normalizePaths(group));
65  if(command != null) command.execute(context);
66  } catch (Exception ex) {
67  Logger.getLogger(GroupItForm.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
68  }
69  }
70  }
71  return null;
72  }
73  });
74  }
75 
76  public static void editGroup(GroupIt group, final Command command) {
77  final GroupItForm gf = new GroupItForm();
78  gf.setGroup(group);
80  I_.get("Group"),
81  gf, "500px", "300px", new Command() {
82  @Override
83  public Object execute(Context context) {
84  if(gf != null) {
85  GroupIt group = gf.getGroup();
86  if(group.isValid()) {
87  try {
88  group = new ContactsPU().saveObject(group);
90  context.put("group", GroupIt.normalizePaths(group));
91  if(command != null) command.execute(context);
92  } catch (Exception ex) {
93  Logger.getLogger(GroupItForm.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
94  }
95  }
96  }
97  return null;
98  }
99  });
100  }
101 
102  public static boolean deleteGroup(GroupIt group) {
103  GroupIt.delete(group.getPathName());
104  return true;
105  }
106 
107  public void setGroup(GroupIt group) {
108  this.group = group;
109  }
110 
111  public GroupIt getGroup() {
112  assignFields();
113  return group;
114  }
115 
116  @Override
117  public void afterCompose() {
118  getChildren().clear();
119  setHflex("true");
120  setColumns("min,1,min");
121  addFields();
122  }
123 
124  private Textbox name;
125  private Checkbox publishable;
126 
127  private void addFields() {
128  addRow();
129  addCaption(I_.get("Name"));
130  name = new Textbox(group.getName());
131  name.setHflex("true");
132  addComponent(name);
133  addRequired(true);
134 
135  addRow();
136  addCaption(I_.get("Publishable"));
137  publishable = new Checkbox();
138  publishable.setChecked(group.isPublishable());
139  addComponent(publishable);
140  addRequired(false);
141 }
142 
143  private void addRequired(boolean required) {
144  if(required) {
145  addComponent(new Image(Images.getImage("required")));
146  } else {
147  addComponent(new Space());
148  }
149  }
150 
151  private void assignFields() {
152  group.setName(name.getValue());
153  group.setPublishable(publishable.isChecked());
154  }
155 
156 }
static GroupIt normalizePaths(GroupIt group)
Definition: GroupIt.java:206
void setPublishable(boolean publishable)
Definition: GroupIt.java:104
void setParent(GroupIt parent)
Definition: GroupIt.java:96
void setName(String name)
Definition: GroupIt.java:80
void setCategory(String category)
Definition: GroupIt.java:72
static void delete(String path)
Definition: GroupIt.java:234
static String get(String msg)
Definition: I_.java:41
GridLayout addComponent(HtmlBasedComponent comp)
GridLayout addCaption(String label)
static Framework getCurrent()
Definition: Framework.java:203
static void newGroup(String category, GroupIt parent, final Command command)
static boolean deleteGroup(GroupIt group)
static void editGroup(GroupIt group, final Command command)