View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.action;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: UI Swing Common
6    * %%
7    * Copyright (C) 2017 - 2019 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  
25  import fr.ifremer.quadrige3.core.ProgressionCoreModel;
26  import org.nuiton.jaxx.application.swing.action.ApplicationActionUIHandler;
27  
28  import java.awt.Dimension;
29  
30  /**
31   * @author peck7 on 28/02/2019.
32   */
33  public class ActionUIHandler extends ApplicationActionUIHandler {
34  
35      public ActionUIHandler() {
36          super();
37  
38          // override the progression listener
39          progressionListener = evt -> {
40  
41              String propertyName = evt.getPropertyName();
42              if (ProgressionCoreModel.PROPERTY_MESSAGE.equals(propertyName)) {
43  
44                  // change message
45                  String newMessage = (String) evt.getNewValue();
46                  ui.getTaskActionLabel().setText("<html><body>" + newMessage + "</body></html>");
47                  // don't call ui.pack() unconditionally, it can lead to an exception in UI thread
48                  adaptSize();
49              } else if (ProgressionCoreModel.PROPERTY_TOTAL.equals(propertyName)) {
50  
51                  // change total progressbar max
52                  ui.getTaskProgressBar().setMaximum((Integer) evt.getNewValue());
53  
54              } else if (ProgressionCoreModel.PROPERTY_CURRENT.equals(propertyName)) {
55  
56                  // change value of progress bar
57                  ui.getTaskProgressBar().setValue((Integer) evt.getNewValue());
58              }
59          };
60      }
61  
62      /**
63       * Adapt the dialog size only if preferred size is bigger than actual size
64       */
65      private void adaptSize() {
66  
67          Dimension prefSize = ui.getPreferredSize();
68          Dimension size = ui.getSize();
69  
70          if (prefSize.getWidth() > size.getWidth() || prefSize.getHeight() > size.getHeight()) {
71              ui.pack();
72          }
73  
74      }
75  }