BrightSide Workbench Full Report + Source Code
OccurrencesVM.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.occurrence;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 import org.turro.string.Strings;
24 import org.turro.elephant.util.Messages;
25 import org.turro.reflection.Instances;
26 import org.zkoss.bind.BindUtils;
27 import org.zkoss.bind.annotation.BindingParam;
28 import org.zkoss.bind.annotation.Command;
29 import org.zkoss.bind.annotation.NotifyChange;
30 
35 public class OccurrencesVM {
36 
37  private String occurrence;
38  private String substitution;
39 
40  public OccurrencesVM() {
41  }
42 
43  public String getOccurrence() {
44  return occurrence;
45  }
46 
47  public void setOccurrence(String occurrence) {
48  this.occurrence = occurrence;
49  }
50 
51  public String getSubstitution() {
52  return substitution;
53  }
54 
55  public void setSubstitution(String substitution) {
56  this.substitution = substitution;
57  }
58 
59  @NotifyChange("model")
60  @Command("update")
61  public void update() {}
62 
63  @NotifyChange("model")
64  @Command("substitute")
65  public void substitute(@BindingParam("api") IOccurrenceAPI api) {
66  if(api != null && !Strings.isBlank(substitution)) {
67  api.changeOccurrences(substitution);
68  }
69  }
70 
71  @Command("remove")
72  public void remove(@BindingParam("api") IOccurrenceAPI api) {
73  if(api != null) {
74  Messages.confirmDeletion().add(occurrence).show(() -> {
75  api.removeOccurrences();
76  BindUtils.postNotifyChange(null, null, OccurrencesVM.this, "model");
77  });
78  }
79  }
80 
81  public List<IOccurrenceAPI> getModel() {
82  List<IOccurrenceAPI> list = new ArrayList<>();
83  if(!Strings.isBlank(occurrence)) {
84  for(IOccurrenceAPI oapi : Instances.cached().bySuper(AbstractOccurrenceAPI.class, IOccurrenceAPI.class)) {
85  if(oapi.hasOccurrences(occurrence)) {
86  list.add(oapi);
87  }
88  }
89  }
90  return list;
91  }
92 
93 }
static Messages confirmDeletion()
Definition: Messages.java:87
Messages add(String word)
Definition: Messages.java:50
void substitute(@BindingParam("api") IOccurrenceAPI api)
void setOccurrence(String occurrence)
void remove(@BindingParam("api") IOccurrenceAPI api)
void setSubstitution(String substitution)
List< IOccurrenceAPI > getModel()