1 package fr.ifremer.tutti.ui.swing.content.operation.catches;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import fr.ifremer.tutti.ui.swing.util.AbstractTuttiBeanUIModel;
28 import org.nuiton.jaxx.application.ApplicationTechnicalException;
29 import org.nuiton.jaxx.application.swing.tab.CustomTab;
30 import org.nuiton.jaxx.application.swing.tab.TabContainerHandler;
31 import org.nuiton.jaxx.application.swing.tab.TabContentModel;
32 import org.nuiton.util.beans.BeanMonitor;
33 import org.nuiton.util.beans.BeanUtil;
34
35 import javax.swing.UIManager;
36 import java.awt.Font;
37 import java.util.Set;
38
39 import static org.nuiton.i18n.I18n.t;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class CatchCustomTab extends CustomTab {
56
57 private static final long serialVersionUID = 1L;
58
59 private final Set<String> modifyPropertyNames;
60
61 public static CatchCustomTab newCustomTab(TabContentModel model, TabContainerHandler handler, Set<String> modifyPropertyNames) {
62
63 CatchCustomTab customTab = new CatchCustomTab(model, handler, modifyPropertyNames);
64 customTab.init();
65 return customTab;
66
67 }
68
69 protected transient BeanMonitor monitor;
70
71 protected final Font defaultFont;
72
73 protected void init() {
74
75
76
77 monitor = new BeanMonitor(modifyPropertyNames.toArray(new String[modifyPropertyNames.size()]));
78 monitor.setBean(model);
79
80
81 try {
82 BeanUtil.addPropertyChangeListener(evt -> onPropertyChanged(evt.getPropertyName(), evt.getNewValue()), this.model);
83 } catch (Exception e) {
84 throw new ApplicationTechnicalException("Could not init listener", e);
85 }
86
87 }
88
89 void onPropertyChanged(String propertyName, Object value) {
90
91 if (propertyName.equals(AbstractTuttiBeanUIModel.PROPERTY_MODIFY)) {
92
93 Boolean newValue = (Boolean) value;
94 if (newValue != null && !newValue) {
95
96
97 monitor.clearModified();
98 updateTitle2(monitor.wasModified(), model.isEmpty());
99
100 }
101
102 } else {
103
104 if (modifyPropertyNames.contains(propertyName)) {
105
106 updateTitle2(monitor.wasModified(), model.isEmpty());
107
108 }
109
110 }
111
112 }
113
114 CatchCustomTab(TabContentModel model, TabContainerHandler handler, Set<String> modifyPropertyNames) {
115 super(model, handler);
116 this.modifyPropertyNames = modifyPropertyNames;
117 this.defaultFont = UIManager.getDefaults().getFont("Label.font");
118 }
119
120 @Override
121 protected void updateTitle() {
122
123 }
124
125 protected void updateTitle2(boolean modelModify, boolean modelEmpty) {
126
127 String titleValue = t(model.getTitle());
128
129 int style;
130 if (modelModify) {
131 style = Font.BOLD;
132 titleValue += "*";
133
134 } else if (modelEmpty) {
135 style = Font.ITALIC;
136
137 } else {
138 style = Font.PLAIN;
139 }
140
141 title.setText(titleValue);
142 title.setFont(defaultFont.deriveFont(style));
143
144 }
145
146 }