View Javadoc
1   package fr.ifremer.dali.ui.swing;
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.config.DaliConfiguration;
27  import fr.ifremer.dali.ui.swing.action.StartAction;
28  import fr.ifremer.dali.ui.swing.content.DaliMainUI;
29  import fr.ifremer.quadrige3.core.config.QuadrigeCoreConfiguration;
30  import fr.ifremer.quadrige3.ui.swing.Application;
31  import fr.ifremer.quadrige3.ui.swing.ApplicationSplashScreen;
32  import fr.ifremer.quadrige3.ui.swing.ApplicationUIContext;
33  import fr.ifremer.quadrige3.ui.swing.plaf.FileChooserUI;
34  import fr.ifremer.quadrige3.ui.swing.plaf.WiderSynthComboBoxUI;
35  import jaxx.runtime.SwingUtil;
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import org.geotools.factory.GeoTools;
39  import org.jdesktop.swingx.util.PaintUtils;
40  
41  import javax.swing.SwingUtilities;
42  import javax.swing.UIManager;
43  import javax.swing.plaf.BorderUIResource;
44  import javax.swing.plaf.UIResource;
45  import java.awt.Color;
46  import java.awt.Dimension;
47  import java.awt.Toolkit;
48  import java.util.Arrays;
49  
50  import static org.nuiton.i18n.I18n.t;
51  
52  /**
53   * <p>DaliApplication class.</p>
54   *
55   * @author Lionel Touseau <lionel.touseau@e-is.pro>
56   */
57  public class DaliApplication extends Application {
58  
59      /* Logger */
60      private static final Log LOG = LogFactory.getLog(DaliApplication.class);
61  
62      private static DaliUIContext context;
63      private static DaliConfiguration config;
64      private static DaliMainUI mainUI;
65  
66      public static void main(String... args) {
67  
68          DaliApplication application = new DaliApplication();
69          application.start(args);
70      }
71  
72      @Override
73      protected boolean init(String... args) {
74  
75          if (LOG.isInfoEnabled()) {
76              LOG.info("Starting DALI with arguments: " + Arrays.toString(args));
77          }
78  
79          // Could override config file path (useful for dev)
80          String configFile = "dali.config";
81          if (System.getProperty(configFile) != null) {
82              configFile = System.getProperty(configFile);
83              configFile = configFile.replaceAll("\\\\", "/");
84          }
85  
86          // Create configuration
87          config = new DaliConfiguration(configFile, args);
88          DaliConfiguration.setInstance(config);
89  
90          // Init and customize splash screen
91          ApplicationSplashScreen.init(config);
92  
93          // Initialize GEOTOOLS library
94          GeoTools.init();
95  
96          // Create application context
97          context = DaliUIContext.newContext(config);
98          ApplicationSplashScreen.progress(20);
99  
100         initLookAndFeel(context);
101         ApplicationSplashScreen.progress(40);
102 
103         // prepare context (mainly init configs, i18n)
104         context.init("dali");
105         ApplicationSplashScreen.progress(60);
106 
107         return true;
108     }
109 
110     @Override
111     protected void show() {
112 
113         // Start this context
114         startUI(context);
115 
116     }
117 
118     @Override
119     public void restartUI() {
120 
121         initLookAndFeel(context);
122         startUI(context);
123     }
124 
125     @Override
126     protected QuadrigeCoreConfiguration getConfig() {
127         return config;
128     }
129 
130     @Override
131     protected ApplicationUIContext getContext() {
132         return context;
133     }
134 
135     private static void initLookAndFeel(DaliUIContext context) {
136         /*
137          * Override default color : http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html
138          */
139 
140         // background
141         UIManager.put("control", Color.WHITE);
142         UIManager.put("background", Color.WHITE);
143         UIManager.put("nimbusSelection", context.getConfiguration().getColorSelectedRow());
144         UIManager.put("nimbusDisabledText", Color.BLACK);
145 
146         // table
147         UIManager.put("Table[Disabled+Selected].textBackground", context.getConfiguration().getColorSelectedRow());
148         UIManager.put("Table[Enabled+Selected].textBackground", context.getConfiguration().getColorSelectedRow());
149         UIResource cellBorder = new BorderUIResource.CompoundBorderUIResource(
150             new BorderUIResource.LineBorderUIResource(context.getConfiguration().getColorSelectedCell()),
151             new BorderUIResource.EmptyBorderUIResource(1, 4, 1, 4));
152         UIManager.put("Table.focusCellHighlightBorder", cellBorder);
153 
154         // list
155         UIManager.put("List.focusCellHighlightBorder", cellBorder);
156 
157         // default selection
158         UIManager.put("nimbusSelectionBackground", context.getConfiguration().getColorSelectedRow());
159         UIManager.put("nimbusSelectedText", PaintUtils.computeForeground(context.getConfiguration().getColorSelectedRow()));
160 
161         // default label
162         UIManager.put("TitledBorder.titleColor", context.getConfiguration().getColorThematicLabel());
163         UIManager.put("thematicLabelColor", context.getConfiguration().getColorThematicLabel());
164 
165         // inactive components
166 //        UIManager.put("TextArea.disabledText", new ColorUIResource(Color.BLACK));
167 //        UIManager.put("TextArea[Disabled].textForeground", new ColorUIResource(Color.BLACK));
168 //        UIManager.put("TextArea[Disabled+NotInScrollPane].textForeground", new ColorUIResource(Color.BLACK));
169 //        UIManager.put("ComboBox:\"ComboBox.textField\"[Disabled].textForeground", Color.BLACK);
170 //        UIManager.put("TextField.inactiveForeground", Color.BLACK);
171 //        UIManager.put("FormattedTextField.inactiveForeground", Color.BLACK);
172 
173         // Prepare ui look&feel and load ui properties
174         try {
175             SwingUtil.initNimbusLoookAndFeel();
176 
177         } catch (Exception e) {
178             // could not find nimbus look-and-feel
179             if (LOG.isWarnEnabled()) {
180                 LOG.warn("Failed to init nimbus look and feel", e);
181             }
182         }
183 
184         // Add specific UI for ComboBox
185         UIManager.put("ComboBoxUI", WiderSynthComboBoxUI.class.getCanonicalName());
186 
187         // Override other L&F defaults
188         UIManager.getLookAndFeelDefaults().put("control", Color.WHITE);
189         UIManager.getLookAndFeelDefaults().put("background", Color.WHITE);
190 
191         // Set default button order (Mantis #43121)
192         UIManager.getDefaults().put("OptionPane.isYesLast", true);
193         UIManager.put("FileChooserUI", FileChooserUI.class.getCanonicalName());
194     }
195 
196     private static void startUI(DaliUIContext context) {
197 
198         // control screen resolution
199         int recommendedWidth = context.getConfiguration().getUIRecommendedWidth();
200         int recommendedHeight = context.getConfiguration().getUIRecommendedHeight();
201         if (recommendedWidth > 0 && recommendedHeight > 0) {
202             Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
203             if (screenSize.getWidth() < recommendedWidth || screenSize.getHeight() < recommendedHeight) {
204                 context.getErrorHelper().showWarningDialog(t("dali.screen.resolution.warning", recommendedWidth, recommendedHeight));
205             }
206         }
207 
208         ApplicationSplashScreen.progress(80);
209         mainUI = new DaliMainUI(context);
210         context.addMessageNotifier(mainUI.getHandler());
211         context.getSwingSession().add(mainUI, true);
212         ApplicationSplashScreen.progress(100);
213 
214         ApplicationSplashScreen.waitFor();
215         SwingUtilities.invokeLater(() -> {
216             mainUI.setVisible(true);
217             ApplicationSplashScreen.hide();
218         });
219 
220         StartAction uiAction = context.getActionFactory().createLogicAction(mainUI.getHandler(), StartAction.class);
221         context.getActionEngine().runAction(uiAction);
222     }
223 
224 }