BrightSide Workbench Full Report + Source Code
DefinitionsVM.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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.documentation.model;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25 import org.turro.action.Contacts;
26 import org.turro.string.Strings;
27 import org.turro.documentation.DocumentDefinition;
28 import org.turro.documentation.DocumentDefinitions;
29 import org.turro.documentation.DocumentType;
30 import org.turro.elephant.util.Messages;
31 import org.turro.file.Document;
32 import org.turro.file.FileWrapper;
33 import org.turro.file.Folder;
34 import org.turro.i18n.I_;
35 import org.turro.plugin.contacts.IContact;
36 import org.turro.upload.Medias;
37 import org.turro.zkoss.dialog.DialogField;
38 import org.turro.zkoss.dialog.Dialogs;
39 import org.zkoss.bind.BindUtils;
40 import org.zkoss.bind.annotation.BindingParam;
41 import org.zkoss.bind.annotation.Command;
42 import org.zkoss.util.media.Media;
43 import org.zkoss.zul.Textbox;
44 
49 public class DefinitionsVM {
50 
51  private IContact contact;
52  private DocumentDefinition selected;
53  private Map<String, List<Document>> documentMap;
54 
55  public IContact getContact() {
56  return contact;
57  }
58 
59  public void setContact(IContact contact) {
60  this.contact = contact;
61  }
62 
64  return selected;
65  }
66 
67  public void setSelected(DocumentDefinition selected) {
68  this.selected = selected;
69  this.documentMap = selected.getDocumentsMap();
70  BindUtils.postNotifyChange(null, null, DefinitionsVM.this, "documentKeys");
71  }
72 
73  public List<String> getDocumentKeys() {
74  return selected != null ? new ArrayList<>(documentMap.keySet()) : null;
75  }
76 
77  public List<Document> getDocuments(String key) {
78  return documentMap.get(key);
79  }
80 
81  public String getKeyName(String key) {
82  if("*".equals(key)) return I_.get("All");
83  IContact folder = Contacts.getContactById(key);
84  if(Contacts.isValid(folder)) return folder.getFullName();
85  return "!";
86  }
87 
88  @Command("upload")
89  public void upload(@BindingParam("medias") Media medias[]) {
90  Folder folder = selected.getFolder();
91  if(contact != null) {
92  folder = folder.child(contact.getId());
93  }
94  for(Media media : medias) {
95  Medias.toFolder(media, folder);
96  }
97  setSelected(selected);
98  }
99 
100  @Command("download")
101  public void download(@BindingParam("document") Document document) {
102  new FileWrapper(document.document()).download();
103  }
104 
105  @Command("deleteDoc")
106  public void deleteDoc(@BindingParam("document") Document document) {
107  Messages.confirmDeletion().add(document.name()).show(() -> {
108  document.delete();
109  document.folder().removeEmpties();
110  setSelected(selected);
111  });
112  }
113 
114  @Command("delete")
115  public void delete() {
116  if(selected.canDelete()) {
117  Messages.confirmDeletion().add(selected.getName()).show(() -> {
118  selected.getFolder().clean();
119  DocumentDefinitions.instance().remove(selected);
120  selected = null;
121  BindUtils.postNotifyChange(null, null, DefinitionsVM.this, "model");
122  });
123  }
124  }
125 
126  @Command("create")
127  public void create() {
128  Dialogs.title(I_.get("Create"))
129  .width("400px")
130  .height("500px")
131  .addField(DialogField.field("Documentation"))
132  .addField(DialogField.field("Type").onEditor(() -> {
133  return new DefinitionTypeListbox();
134  }))
135  .onOk((dialogs) -> {
136  String docdef = dialogs.<Textbox>getEditor("Documentation").getValue();
137  if(!Strings.isBlank(docdef) && !DocumentDefinitions.instance().names().contains(docdef)) {
138  DocumentType dt = dialogs.<DefinitionTypeListbox>getEditor("Type").getObjectValue();
139  DocumentDefinition dd = new DocumentDefinition(docdef, dt);
140  DocumentDefinitions.instance().add(dd);
141  BindUtils.postNotifyChange(null, null, DefinitionsVM.this, "model");
142  }
143  })
144  .emptyCancel()
145  .show();
146  }
147 
148  @Command("save")
149  public void save() {
151  }
152 
153  public Set<DocumentDefinition> getModel() {
154  return DocumentDefinitions.instance();
155  }
156 
157 }
static boolean isValid(IContact contact)
Definition: Contacts.java:52
static IContact getContactById(String id)
Definition: Contacts.java:72
void setSelected(DocumentDefinition selected)
void deleteDoc(@BindingParam("document") Document document)
List< Document > getDocuments(String key)
void upload(@BindingParam("medias") Media medias[])
void download(@BindingParam("document") Document document)
static Messages confirmDeletion()
Definition: Messages.java:87
Messages add(String word)
Definition: Messages.java:50
static String get(String msg)
Definition: I_.java:41
static void toFolder(Media media, Folder folder)
Definition: Medias.java:50
static DialogField field(String label)
Dialogs width(String width)
Definition: Dialogs.java:70
Dialogs height(String height)
Definition: Dialogs.java:75
Dialogs onOk(Consumer< Dialogs > onOk)
Definition: Dialogs.java:85
static Dialogs title(String title)
Definition: Dialogs.java:161
Dialogs addField(DialogField field)
Definition: Dialogs.java:110