BrightSide Workbench Full Report + Source Code
GroupItComposer.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 org.turro.command.Command;
22 import org.turro.command.Context;
23 import org.turro.contacts.FieldIt;
24 import org.turro.contacts.GroupIt;
25 import org.turro.contacts.db.ContactsPU;
26 import org.turro.elephant.util.Messages;
27 import org.turro.jpa.Dao;
28 import org.turro.zul.fieldit.FieldItGrid;
29 import org.zkoss.lang.Strings;
30 import org.zkoss.zk.ui.Component;
31 import org.zkoss.zk.ui.select.SelectorComposer;
32 import org.zkoss.zk.ui.select.annotation.Listen;
33 import org.zkoss.zk.ui.select.annotation.Wire;
34 import org.zkoss.zul.Checkbox;
35 import org.zkoss.zul.Toolbarbutton;
36 
41 public class GroupItComposer extends SelectorComposer<Component> {
42 
43  @Wire("#categoryBox")
44  private CategoryCombobox categoryBox;
45 
46  @Wire("#createGroup")
47  private Toolbarbutton createGroup;
48 
49  @Wire("#editGroup")
50  private Toolbarbutton editGroup;
51 
52  @Wire("#deleteGroup")
53  private Toolbarbutton deleteGroup;
54 
55  @Wire("#groupTree")
56  private GroupItTree groupTree;
57 
58  @Wire("#entities")
59  private GroupEntityGrid entities;
60 
61  @Wire("#exact")
62  private Checkbox exact;
63 
64  @Wire("#fields")
65  private FieldItGrid fields;
66 
67  @Wire("#saveFields")
68  private Toolbarbutton saveFields;
69 
70  @Listen("onChange = #categoryBox")
71  public void onChangeCategory() {
72  groupTree.setCategory(categoryBox.getText());
73  createGroup.setDisabled(Strings.isBlank(groupTree.getCategory()));
74  editGroup.setDisabled(true);
75  deleteGroup.setDisabled(editGroup.isDisabled());
76  fields.setEntityPath(null);
77  entities.setGroupIt(null);
78  entities.setCategory(null);
79  entities.setExact(exact.isChecked());
80  entities.reload();
81  }
82 
83  @Listen("onSelect = #groupTree; onCheck = #exact")
84  public void onSelectGroup() {
85  editGroup.setDisabled(groupTree.getSelectedGroup() == null);
86  deleteGroup.setDisabled(editGroup.isDisabled());
87  saveFields.setDisabled(true);
89  entities.setGroupIt(groupTree.getSelectedGroup());
90  entities.setCategory(categoryBox.getText());
91  entities.setExact(exact.isChecked());
92  entities.reload();
93  }
94 
95  @Listen("onClick = #createGroup")
96  public void onCreateGroup() {
97  GroupItForm.newGroup(groupTree.getCategory(), groupTree.getSelectedGroup(), new Command() {
98  @Override
99  public Object execute(Context context) {
100  GroupIt g = (GroupIt) context.get("group");
101  if(g != null) {
102  GroupItItem gi = (GroupItItem) groupTree.getSelectedItem();
103  if(gi == null) {
104  groupTree.setCategory(categoryBox.getText());
105  } else {
106  gi.reloadContents();
107  }
108  editGroup.setDisabled(true);
109  deleteGroup.setDisabled(editGroup.isDisabled());
110  saveFields.setDisabled(true);
111  }
112  return null;
113  }
114  });
115  }
116 
117  @Listen("onClick = #editGroup")
118  public void onEditGroup() {
119  GroupItForm.editGroup(groupTree.getSelectedGroup(), new Command() {
120  @Override
121  public Object execute(Context context) {
122  GroupIt g = (GroupIt) context.get("group");
123  if(g != null) {
124  groupTree.changeSelectedGroup(g);
125  }
126  return null;
127  }
128  });
129  }
130 
131  @Listen("onClick = #deleteGroup")
132  public void onDeleteGroup() {
133  Messages.confirmDeletion().add(groupTree.getName()).show(() -> {
134  if(GroupItForm.deleteGroup(groupTree.getSelectedGroup())) {
135  groupTree.getSelectedItem().detach();
136  }
137  });
138  }
139 
140  @Listen("onChange = #fields")
141  public void onChangeFields() {
142  saveFields.setDisabled(false);
143  }
144 
145  @Listen("onClick = #saveFields")
146  public void onSaveFields() {
147  Dao dao = new ContactsPU();
148  for(FieldIt f : fields.getValues()) {
149  dao.saveObject(f);
150  }
152  }
153 
154  @Override
155  public void doFinally() throws Exception {
156  super.doFinally();
157  createGroup.setDisabled(true);
158  editGroup.setDisabled(true);
159  deleteGroup.setDisabled(editGroup.isDisabled());
160  saveFields.setDisabled(true);
161  saveFields.setAutodisable("+self");
162  }
163 
164 }
static String getObjectPath(Object object)
Definition: ContactsPU.java:68
static Messages confirmDeletion()
Definition: Messages.java:87
Messages add(String word)
Definition: Messages.java:50
void setEntityPath(String entityPath)
static void newGroup(String category, GroupIt parent, final Command command)
static boolean deleteGroup(GroupIt group)
static void editGroup(GroupIt group, final Command command)
void setCategory(String category)
void changeSelectedGroup(GroupIt group)