View Javadoc
1   package fr.ifremer.dali.ui.swing.util.plaf;
2   
3   /*-
4    * #%L
5    * Dali :: UI
6    * %%
7    * Copyright (C) 2017 - 2018 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (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 Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  import jaxx.runtime.validator.swing.ui.AbstractBeanValidatorUI;
25  import org.jdesktop.jxlayer.JXLayer;
26  import org.nuiton.validator.NuitonValidatorScope;
27  
28  import javax.swing.BorderFactory;
29  import javax.swing.JComponent;
30  import javax.swing.border.Border;
31  import java.awt.Color;
32  import java.awt.Graphics2D;
33  import java.util.Collection;
34  
35  /**
36   * @author peck7 on 17/01/2018.
37   */
38  public class BorderValidationUI extends AbstractBeanValidatorUI {
39  
40  //    private static final String VALIDATION_BREAK = "<br/><br/>-----<br/>";
41  //    private Set<String> messages = new HashSet<>();
42  
43      public BorderValidationUI(String field) {
44          super(field);
45      }
46  
47      public BorderValidationUI(Collection<String> fields) {
48          super(fields);
49      }
50  
51      @Override
52      protected void paintLayer(Graphics2D g2, JXLayer<? extends JComponent> l) {
53          super.paintLayer(g2, l);
54          NuitonValidatorScope scope = getScope();
55          if (scope == null) return;
56  
57          JComponent view = l.getView();
58          Color color = null;
59          // Override scope color from nuiton
60          switch (scope) {
61              case FATAL:
62                  color = Color.MAGENTA;
63                  break;
64              case ERROR:
65                  color = Color.RED;
66                  break;
67              case WARNING:
68                  color = Color.ORANGE;
69                  break;
70              case INFO:
71                  color = Color.GREEN;
72                  break;
73          }
74  
75          // Paint dashed border
76          Border border = BorderFactory.createDashedBorder(color, 2f, 4, 4, false);
77          border.paintBorder(view, g2, 0, 0, view.getWidth(), view.getHeight());
78  
79          // Add tooltip text on view
80          // FIXME LP 17/01/18 : tooltip retiré car
81          // 1 - produit des JavaHeapSpace erreur ç cause des nombreux appels de cette methode
82          // 2 - le tooltip apparait uniquement sur la bordure des composant car le composant editeur a déjà sont propre tooltip
83  //        String toolTipText = view.getToolTipText();
84  //        if (!messages.isEmpty()) {
85  //            if (toolTipText != null) {
86  //                if (toolTipText.contains(VALIDATION_BREAK)) {
87  //                    toolTipText = removeValidationBreak(toolTipText);
88  //                }
89  //                toolTipText += toolTipText + VALIDATION_BREAK + getMessages();
90  //            } else {
91  //                toolTipText = VALIDATION_BREAK + getMessages();
92  //            }
93  //
94  //        } else {
95  //            if (toolTipText != null && toolTipText.contains(VALIDATION_BREAK)) {
96  //                toolTipText = removeValidationBreak(toolTipText);
97  //            }
98  //        }
99  //        view.setToolTipText(ApplicationUIUtil.getHtmlString(toolTipText));
100     }
101 
102 //    private String removeValidationBreak(String text) {
103 //        return text.substring(0, text.indexOf(VALIDATION_BREAK));
104 //    }
105 //
106 //    private String getMessages() {
107 //        StringJoiner joiner = new StringJoiner("<br/>");
108 //        messages.forEach(message -> joiner.add(t(message)));
109 //        return joiner.toString();
110 //    }
111 //
112 //    @Override
113 //    protected NuitonValidatorScope getHighestScope(SimpleBeanValidatorEvent event) {
114 //
115 //        // intercept messages
116 //        if (event.getMessagesToAdd() != null) {
117 //            messages.addAll(Arrays.asList(event.getMessagesToAdd()));
118 //        }
119 //        if (event.getMessagesToDelete() != null) {
120 //            messages.removeAll(Arrays.asList(event.getMessagesToDelete()));
121 //        }
122 //
123 //        return super.getHighestScope(event);
124 //    }
125 }