View Javadoc
1   package fr.ifremer.tutti.ui.swing.util.caracteristics;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.collect.Maps;
26  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
27  import fr.ifremer.tutti.persistence.entities.referential.Gear;
28  import fr.ifremer.tutti.service.csv.TuttiCsvUtil;
29  import org.apache.commons.collections4.MapUtils;
30  import org.nuiton.csv.ValueFormatter;
31  import org.nuiton.decorator.Decorator;
32  
33  import javax.swing.JComponent;
34  import javax.swing.JList;
35  import javax.swing.ListCellRenderer;
36  import java.awt.Component;
37  import java.io.Serializable;
38  import java.util.Map;
39  
40  import static org.nuiton.i18n.I18n.t;
41  
42  /**
43   * Created on 10/2/13.
44   *
45   * @author Tony Chemit - chemit@codelutin.com
46   * @since 2.7
47   */
48  public class GearCaracteristicListCellRenderer implements ListCellRenderer {
49  
50      private final ListCellRenderer delegate;
51  
52      private final Decorator<Caracteristic> decorator;
53  
54      private final Decorator<Gear> gearDecorator;
55  
56      protected final Map<Gear, String> cache;
57  
58      protected final boolean useCache;
59  
60      public GearCaracteristicListCellRenderer(ListCellRenderer delegate,
61                                               Decorator<Caracteristic> decorator,
62                                               Decorator<Gear> gearDecorator, boolean useCache) {
63          this.gearDecorator = gearDecorator;
64          this.useCache = useCache;
65          this.delegate = delegate;
66          this.decorator = decorator;
67          this.cache = Maps.newHashMap();
68      }
69  
70      public void clear() {
71          cache.clear();
72      }
73  
74      @Override
75      public Component getListCellRendererComponent(JList list,
76                                                    Object item,
77                                                    int index,
78                                                    boolean isSelected,
79                                                    boolean cellHasFocus) {
80  
81          String toolTipText = getToolTipText((Gear) item);
82  
83          Component result = delegate.getListCellRendererComponent(list, item, index, isSelected, cellHasFocus);
84          ((JComponent) result).setToolTipText(toolTipText);
85          return result;
86      }
87  
88      public String getToolTipText(Gear gear) {
89          String toolTipText;
90  
91          if (gear == null) {
92              toolTipText = "";
93          } else {
94  
95              if (useCache) {
96                  if (cache.containsKey(gear)) {
97  
98                      toolTipText = cache.get(gear);
99                  } else {
100                     toolTipText = buildTip(gear);
101                     cache.put(gear, toolTipText);
102                 }
103             } else {
104                 toolTipText = buildTip(gear);
105             }
106         }
107         return toolTipText;
108     }
109 
110     protected String buildTip(Gear gear) {
111         String toolTipText;
112 
113         ValueFormatter<Serializable> caracteristicValueFormatter = TuttiCsvUtil.CARACTERISTIC_VALUE_FORMATTER;
114         StringBuilder sb = new StringBuilder("<html><body>");
115 
116         String gearStr = gearDecorator.toString(gear);
117         sb.append("<h3>").append(t("tutti.gear.withCaracteristics", gearStr)).append("</h3></hr>");
118 
119         if (MapUtils.isNotEmpty(gear.getCaracteristics())) {
120 
121             // got some caracteristics
122 
123             sb.append("<ul>");
124             for (Map.Entry<Caracteristic, Serializable> entry : gear.getCaracteristics().entrySet()) {
125                 Caracteristic key = entry.getKey();
126                 String keyStr = decorator.toString(key);
127                 Serializable value = entry.getValue();
128                 String valueStr = caracteristicValueFormatter.format(value);
129                 sb.append("<li>").append(keyStr).append(" :");
130                 sb.append(valueStr).append("</strong></li>");
131             }
132             sb.append("</ul>");
133         } else {
134 
135             sb.append("<i>").append(t("tutti.gear.noCaracteristics")).append("</i>");
136         }
137         sb.append("</body></html>");
138 
139         toolTipText = sb.toString();
140         return toolTipText;
141     }
142 }