View Javadoc
1   package fr.ifremer.reefdb.ui.swing.util.table;
2   
3   /*
4    * #%L
5    * Reef DB :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (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 Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import fr.ifremer.quadrige3.ui.swing.table.ColumnIdentifier;
27  import fr.ifremer.quadrige3.ui.swing.table.PmfmColumnIdentifier;
28  import fr.ifremer.reefdb.dto.ErrorAware;
29  import fr.ifremer.reefdb.ui.swing.util.ReefDbUIs;
30  import org.apache.commons.collections4.CollectionUtils;
31  import org.jdesktop.swingx.JXTable;
32  import org.jdesktop.swingx.decorator.BorderHighlighter;
33  import org.jdesktop.swingx.decorator.ComponentAdapter;
34  import org.jdesktop.swingx.decorator.HighlightPredicate;
35  import org.nuiton.jaxx.application.swing.table.AbstractApplicationTableModel;
36  
37  import javax.swing.JComponent;
38  import javax.swing.border.Border;
39  import java.awt.Component;
40  import java.util.List;
41  
42  /**
43   * Highlighter using an ErrorAwareDTO row to show a border around a particular cell and messages as tooltip
44   * <p/>
45   * Created by Ludovic on 29/05/2015.
46   */
47  public abstract class ErrorHighlighter extends BorderHighlighter {
48  
49      private final JXTable table;
50      private final String title;
51      private List<String> message;
52  
53      /**
54       * <p>getMessages.</p>
55       *
56       * @param bean a {@link fr.ifremer.reefdb.dto.ErrorAware} object.
57       * @param propertyName a {@link java.lang.String} object.
58       * @param pmfmId a {@link java.lang.Integer} object.
59       * @return a {@link java.util.List} object.
60       */
61      public abstract List<String> getMessages(ErrorAware bean, String propertyName, Integer pmfmId);
62  
63      /**
64       * <p>Constructor for ErrorHighlighter.</p>
65       *
66       * @param table a {@link org.jdesktop.swingx.JXTable} object.
67       * @param border a {@link javax.swing.border.Border} object.
68       * @param title a {@link java.lang.String} object.
69       */
70      public ErrorHighlighter(JXTable table, Border border, String title) {
71          super();
72  
73          this.table = table;
74          this.title = title;
75          setHighlightPredicate(newPredicate());
76          setBorder(border);
77          setCompound(false);
78  
79      }
80  
81      private HighlightPredicate newPredicate() {
82          return (renderer, adapter) -> {
83              AbstractApplicationTableModel model = (AbstractApplicationTableModel) table.getModel();
84              int modelRow = adapter.convertRowIndexToModel(adapter.row);
85              AbstractReefDbRowUIModel row = (AbstractReefDbRowUIModel) model.getEntry(modelRow);
86              if (row instanceof ErrorAware) {
87  
88                  // get column identifier
89                  ColumnIdentifier identifier = (ColumnIdentifier) adapter.getColumnIdentifierAt(adapter.convertColumnIndexToModel(adapter.column));
90                  String propertyName = identifier.getPropertyName();
91                  Integer pmfmId = null;
92                  if (identifier instanceof PmfmColumnIdentifier) {
93                      pmfmId = ((PmfmColumnIdentifier) identifier).getPmfmId();
94                  }
95  
96                  message = getMessages((ErrorAware) row, propertyName, pmfmId);
97                  return CollectionUtils.isNotEmpty(message);
98              } else {
99                  return false;
100             }
101         };
102     }
103 
104     /** {@inheritDoc} */
105     @Override
106     protected Component doHighlight(Component renderer, ComponentAdapter adapter) {
107         Component component = super.doHighlight(renderer, adapter);
108 
109         // message as tooltip
110         ((JComponent) component).setToolTipText(ReefDbUIs.getHtmlString(title, message));
111 
112         return component;
113     }
114 
115 }