View Javadoc
1   package fr.ifremer.dali.ui.swing.util;
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.dto.DaliBeans;
28  import fr.ifremer.dali.ui.swing.DaliUIContext;
29  import fr.ifremer.quadrige3.core.dao.technical.decorator.Decorator;
30  import fr.ifremer.quadrige3.ui.core.dto.QuadrigeBean;
31  import fr.ifremer.quadrige3.ui.swing.AbstractUIHandler;
32  import fr.ifremer.quadrige3.ui.swing.ApplicationUIUtil;
33  import org.apache.commons.lang3.StringUtils;
34  import org.jdesktop.beans.AbstractBean;
35  
36  import javax.swing.ImageIcon;
37  import javax.swing.JPanel;
38  import javax.swing.JTextField;
39  import java.awt.Color;
40  import java.awt.Font;
41  import java.io.Serializable;
42  import java.net.URL;
43  
44  import static org.nuiton.i18n.I18n.t;
45  
46  /**
47   * Contract of any UI handler.
48   *
49   * @param <M>
50   * @param <UI>
51   * @author Lionel Touseau <lionel.touseau@e-is.pro>
52   * @since 1.0
53   */
54  public abstract class AbstractDaliUIHandler<M extends AbstractBean, UI extends DaliUI<M, ?>> extends AbstractUIHandler<M, UI> {
55  
56      // ------------------------------------------------------------------------//
57      // -- Public methods --//
58      // ------------------------------------------------------------------------//
59  
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public DaliUIContext getContext() {
65          return (DaliUIContext) super.getContext();
66      }
67  
68      /**
69       * get the screen title
70       *
71       * @return the screen title
72       */
73      public String getTitle() {
74          return t("dali.screen." + getUI().getClass().getSimpleName() + ".title");
75      }
76  
77      /**
78       * <p>getConfig.</p>
79       *
80       * @return a {@link DaliConfiguration} object.
81       */
82      public DaliConfiguration getConfig() {
83          return getContext().getConfiguration();
84      }
85  
86      // ------------------------------------------------------------------------//
87      // -- Init methods --//
88      // ------------------------------------------------------------------------//
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      protected void initUIComponent(Object component) {
95          super.initUIComponent(component);
96  
97          if (component instanceof JPanel) {
98              initPanel((JPanel) component);
99  
100         }
101     }
102 
103     private void initPanel(JPanel panel) {
104 
105         // set background color
106         String panelType = (String) panel.getClientProperty("panelType");
107         if (StringUtils.isNotBlank(panelType)) {
108 
109             Color backgroundColor = null;
110             switch (panelType) {
111                 case DaliUI.CONTEXT_PANEL_TYPE:
112                     backgroundColor = getConfig().getColorContextPanelBackground();
113                     break;
114                 case DaliUI.SELECTION_PANEL_TYPE:
115                     backgroundColor = getConfig().getColorSelectionPanelBackground();
116                     break;
117                 case DaliUI.EDITION_PANEL_TYPE:
118                     backgroundColor = getConfig().getColorEditionPanelBackground();
119                     break;
120             }
121 
122             if (backgroundColor != null) {
123                 ApplicationUIUtil.setComponentTreeBackground(panel, backgroundColor);
124             }
125         }
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     protected void initTextField(JTextField jTextField) {
133         super.initTextField(jTextField);
134 
135         Boolean computed = (Boolean) jTextField.getClientProperty("computed");
136         if (computed != null && computed) {
137             Font font = jTextField.getFont().deriveFont(Font.ITALIC);
138             jTextField.setFont(font);
139             jTextField.setEditable(false);
140             jTextField.setEnabled(false);
141             jTextField.setDisabledTextColor(getConfig().getColorComputedWeights());
142         }
143 
144     }
145 
146     @Override
147     public String decorate(Serializable object, String context) {
148         String result = "";
149         if (object != null) {
150             Decorator decorator = getDecorator(object.getClass(), context);
151             if (decorator != null)
152                 // decorate with this decorator
153                 result = getDecorator(object.getClass(), context).toString(object);
154             else if (object instanceof QuadrigeBean)
155                 // try decorate a QuadrigeBean
156                 result = DaliBeans.toString((QuadrigeBean) object);
157         }
158         return result;
159     }
160 
161     public ImageIcon getResourceImage(String imageName, String imageExtension) {
162 
163         // try with current locale
164         ImageIcon image = getResourceImage(imageName, imageExtension, getConfig().getI18nLocale().toString());
165         if (image == null) {
166             // fallback to default locale
167             image = getResourceImage(imageName, imageExtension, getConfig().getFallbackLocale());
168         }
169         return image;
170     }
171 
172     private ImageIcon getResourceImage(String imageName, String imageExtension, String locale) {
173 
174         String resource = String.format("/images/%s_%s.%s", imageName, locale, imageExtension);
175         URL resourceURL = getClass().getResource(resource);
176         return resourceURL != null ? new ImageIcon(resourceURL) : null;
177     }
178 }