View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.tab;
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 jaxx.runtime.SwingUtil;
27  import org.jdesktop.jxlayer.JXLayer;
28  import org.jdesktop.jxlayer.plaf.AbstractLayerUI;
29  import org.nuiton.jaxx.application.swing.tab.CustomTab;
30  
31  import javax.swing.ImageIcon;
32  import javax.swing.JTabbedPane;
33  import javax.swing.SwingUtilities;
34  import java.awt.Graphics2D;
35  import java.awt.RenderingHints;
36  import java.awt.event.MouseEvent;
37  import java.awt.image.BufferedImage;
38  
39  /**
40   * Layer on CustomTab that show the error icon when TabContentModel is not valid
41   * <p/>
42   * Created by Ludovic on 21/05/2015.
43   */
44  public class CustomTabLayerUI extends AbstractLayerUI<CustomTab> {
45  
46      private final BufferedImage image;
47  
48      /**
49       * <p>Constructor for CustomTabLayerUI.</p>
50       */
51      CustomTabLayerUI() {
52  
53          ImageIcon errorIcon = SwingUtil.createImageIcon("error.png");
54  
55          image = new BufferedImage(errorIcon.getIconWidth(), errorIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
56          Graphics2D g2 = (Graphics2D) image.getGraphics();
57          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
58          g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
59          g2.drawImage(errorIcon.getImage(), 0, 0, null);
60          g2.dispose();
61  
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      protected void paintLayer(Graphics2D g2, JXLayer<? extends CustomTab> layer) {
67          super.paintLayer(g2, layer);
68  
69          CustomTab tab = layer.getView();
70          if (!tab.getModel().isValid()) {
71              // paint the icon
72              g2.drawImage(image, layer.getWidth() - image.getWidth() + 1, -1, null);
73          }
74  
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      protected void processMouseEvent(MouseEvent e, JXLayer<? extends CustomTab> l) {
80  
81          // must redispatch event to the tabbed pane with correct mouse coordinate
82          if (l.getParent().getParent() instanceof JTabbedPane) {
83              JTabbedPane tabbedPane = (JTabbedPane) l.getParent().getParent();
84              tabbedPane.dispatchEvent(SwingUtilities.convertMouseEvent(l, e, tabbedPane));
85          }
86      }
87  }