View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation.catches;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
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   * Une amélioration de CustomTab pour ne pas utiliser la propriété modify d'un modèle mais un sous ensemble de ses
43   * propriétés.
44   *
45   * De plus on utilise un monitor donc quand plus rien n'est modifié, on peut aussi le savoir.
46   *
47   * TODO Il faudrait utiliser cet objet sur tous les onglets une fois un gros travail de nettoyage des listeners effectué
48   * TODO car là c'est un peu chaotique et le monitor devient rapidemment inconsitent.
49   *
50   * Created on 3/12/15.
51   *
52   * @author Tony Chemit - chemit@codelutin.com
53   * @since 3.13.3
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          // init first monitor
76          // so monitor will be notify before later listener
77          monitor = new BeanMonitor(modifyPropertyNames.toArray(new String[modifyPropertyNames.size()]));
78          monitor.setBean(model);
79  
80          // listen to the model
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                  // model no more modified, reset the monitor
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         // bad method (fire before monitor is notified + fired from constructor, bad, bad, bad!)
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 }