View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.plaf;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 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  
27  import org.jdesktop.jxlayer.JXLayer;
28  
29  import javax.swing.JComponent;
30  import java.awt.*;
31  import java.awt.event.ActionEvent;
32  
33  /**
34   * A JXLayer ui implementation that permits to block a component
35   * and draw a wait indicator
36   *
37   * @author Ludovic - ludovic.pecquot@e-is.pro
38   */
39  public class WaitBlockingLayerUI extends ComponentBlockingLayerUI {
40  
41      private int angle;
42  
43      private static final int MAX_DIMENSION = 150;
44  
45      /**
46       * <p>Constructor for WaitBlockingLayerUI.</p>
47       */
48      public WaitBlockingLayerUI() {
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      protected void paintLayer(Graphics2D g2, JXLayer<? extends JComponent> l) {
54          super.paintLayer(g2, l);
55          if (!running || !started) {
56              return;
57          }
58  
59          // Paint the wait indicator.
60          int s = Math.min(Math.min(l.getWidth(), MAX_DIMENSION), Math.min(l.getHeight(), MAX_DIMENSION)) / 5;
61          int cx = l.getWidth() / 2;
62          int cy = l.getHeight() / 2;
63          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
64          g2.setStroke(new BasicStroke(s / 4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
65          g2.setPaint(Color.white);
66          g2.rotate(Math.PI * angle / 180, cx, cy);
67          for (int i = 0; i < 12; i++) {
68              float scale = (11.0f - (float) i) / 11.0f;
69              g2.drawLine(cx + s, cy, cx + s * 2, cy);
70              g2.rotate(-Math.PI / 6, cx, cy);
71              g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, scale * getFade()));
72          }
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public void actionPerformed(ActionEvent e) {
78          super.actionPerformed(e);
79          if (running) {
80              angle += 3;
81              if (angle >= 360) angle = 0;
82          }
83      }
84  
85  }
86