View Javadoc
1   package fr.ifremer.dali.ui.swing.util.table;
2   
3   /*
4    * #%L
5    * Dali :: 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.dali.dto.ErrorAware;
27  import fr.ifremer.quadrige3.ui.swing.ApplicationUIUtil;
28  import fr.ifremer.quadrige3.ui.swing.table.ColumnIdentifier;
29  import fr.ifremer.quadrige3.ui.swing.table.SwingTable;
30  import org.apache.commons.collections4.CollectionUtils;
31  import org.jdesktop.swingx.decorator.BorderHighlighter;
32  import org.jdesktop.swingx.decorator.ComponentAdapter;
33  import org.jdesktop.swingx.decorator.HighlightPredicate;
34  import org.nuiton.jaxx.application.swing.table.AbstractApplicationTableModel;
35  
36  import javax.swing.BorderFactory;
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 SwingTable table;
50      private final String title;
51      private List<String> message;
52      private static final Border innerBorder = BorderFactory.createEmptyBorder(0, 3, 0, 3);
53  
54      /**
55       * <p>getMessages.</p>
56       *
57       * @param bean         a {@link fr.ifremer.dali.dto.ErrorAware} object.
58       * @param propertyName a {@link java.lang.String} object.
59       * @param pmfmId       a {@link java.lang.Integer} object.
60       * @return a {@link java.util.List} object.
61       */
62      public abstract List<String> getMessages(ErrorAware bean, String propertyName, Integer pmfmId);
63  
64      /**
65       * <p>Constructor for ErrorHighlighter.</p>
66       *
67       * @param table  a {@link SwingTable} object.
68       * @param border a {@link javax.swing.border.Border} object.
69       * @param title  a {@link java.lang.String} object.
70       */
71      public ErrorHighlighter(SwingTable table, Border border, String title) {
72          super();
73  
74          this.table = table;
75          this.title = title;
76          setHighlightPredicate(newPredicate());
77          setBorder(BorderFactory.createCompoundBorder(border, innerBorder));
78          setCompound(false);
79  
80      }
81  
82      private HighlightPredicate newPredicate() {
83          return (renderer, adapter) -> {
84              AbstractApplicationTableModel model = (AbstractApplicationTableModel) table.getModel();
85              int modelRow = adapter.convertRowIndexToModel(adapter.row);
86              AbstractDaliRowUIModel row = (AbstractDaliRowUIModel) model.getEntry(modelRow);
87              if (row instanceof ErrorAware) {
88  
89                  // get column identifier
90                  ColumnIdentifier identifier = (ColumnIdentifier) adapter.getColumnIdentifierAt(adapter.convertColumnIndexToModel(adapter.column));
91                  if (identifier == null)
92                      return false;
93                  String propertyName = identifier.getPropertyName();
94                  Integer pmfmId = (identifier instanceof DaliPmfmColumnIdentifier) ? ((DaliPmfmColumnIdentifier) identifier).getPmfmId() : null;
95                  message = getMessages((ErrorAware) row, propertyName, pmfmId);
96                  return CollectionUtils.isNotEmpty(message);
97              } else {
98                  return false;
99              }
100         };
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     protected Component doHighlight(Component renderer, ComponentAdapter adapter) {
108         Component component = super.doHighlight(renderer, adapter);
109 
110         // message as tooltip
111         ((JComponent) component).setToolTipText(ApplicationUIUtil.getHtmlString(title, message));
112 
113         return component;
114     }
115 
116 }