View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.genericformat.tree;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (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 General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import javax.swing.JCheckBox;
28  import javax.swing.JLabel;
29  import javax.swing.JTree;
30  import javax.swing.UIDefaults;
31  import javax.swing.UIManager;
32  import javax.swing.tree.DefaultTreeCellRenderer;
33  import java.awt.Component;
34  import java.awt.Font;
35  
36  import static org.nuiton.i18n.I18n.t;
37  
38  /**
39   * Created on 3/31/15.
40   *
41   * @author Tony Chemit - chemit@codelutin.com
42   * @since 3.14.3
43   */
44  public class ImportDataSelectTreeCellRenderer extends DefaultTreeCellRenderer {
45  
46      private static final long serialVersionUID = 1L;
47  
48      private final JCheckBox normalCheckBox;
49  
50      private final JCheckBox partialCheckBox;
51  
52      private Font normalFont;
53  
54      private Font boldFont;
55  
56      public ImportDataSelectTreeCellRenderer() {
57  
58          normalCheckBox = new JCheckBox();
59          partialCheckBox = new JCheckBox();
60  
61          Object iconPainter = UIManager.getDefaults().get("CheckBox[Disabled+Selected].iconPainter");
62          UIDefaults defaults = new UIDefaults();
63          defaults.put("CheckBox[Disabled].iconPainter", iconPainter);
64          partialCheckBox.putClientProperty("Nimbus.Overrides", defaults);
65          partialCheckBox.putClientProperty("Nimbus.Overrides.InheritDefaults", false);
66          partialCheckBox.setEnabled(false);
67  
68          normalFont = UIManager.getFont("CheckBox.font");
69          boldFont = normalFont.deriveFont(Font.BOLD);
70      }
71  
72      @Override
73      public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
74  
75          JLabel label = (JLabel) super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
76  
77          JCheckBox checkBox = normalCheckBox;
78  
79          if (value != null && value instanceof DataSelectTreeNodeSupport) {
80  
81              DataSelectTreeNodeSupport node = (DataSelectTreeNodeSupport) value;
82  
83              if (node instanceof CruiseSelectTreeNode) {
84  
85                  checkBox = getCruiseComponent((CruiseSelectTreeNode) node);
86  
87              } else if (node instanceof OperationSelectTreeNode) {
88  
89                  checkBox = getOperationComponent((OperationSelectTreeNode) node);
90  
91              }
92  
93              boolean filled = node.isSelected();
94  
95              checkBox.setBackground(label.getBackground());
96              checkBox.setForeground(label.getForeground());
97  
98              Font font;
99              if (filled) {
100                 font = this.boldFont;
101             } else {
102                 font = normalFont;
103             }
104 
105             if (node.isExist()) {
106                 checkBox.setText(checkBox.getText() + " (*)");
107                 checkBox.setToolTipText(checkBox.getToolTipText() + " (donnée existante - identifiant " + node.getOptionalId() + ")");
108             }
109 
110             checkBox.setFont(font);
111             checkBox.setSelected(filled);
112 
113         }
114 
115         return checkBox;
116 
117     }
118 
119     protected JCheckBox getCruiseComponent(CruiseSelectTreeNode node) {
120 
121         JCheckBox checkBox = normalCheckBox;
122 
123         String text;
124         String tip;
125 
126         text = t("tutti.genericformat.import.cruiseNode", node.getLabel(), node.getNbChilds());
127 
128         if (node.isSelected()) {
129 
130             tip = t("tutti.genericformat.import.cruiseNode.fullSelected", text);
131 
132         } else if (node.isPartialSelected()) {
133 
134             checkBox = partialCheckBox;
135 
136             tip = t("tutti.genericformat.import.cruiseNode.partialSelected", text, node.getNbChildSelected());
137 
138         } else {
139 
140             tip = text;
141 
142         }
143 
144         checkBox.setText(text);
145         checkBox.setToolTipText(tip);
146 
147         return checkBox;
148 
149     }
150 
151     protected JCheckBox getOperationComponent(OperationSelectTreeNode node) {
152 
153         JCheckBox checkBox = normalCheckBox;
154 
155         String tip;
156 
157         String text = t("tutti.genericformat.import.operationNode", node.getLabel());
158 
159         if (node.isSelected()) {
160 
161             tip = t("tutti.genericformat.import.operationNode.selected", text, node.getId());
162 
163         } else {
164 
165             tip = text;
166 
167         }
168 
169         checkBox.setText(text);
170         checkBox.setToolTipText(tip);
171 
172         return checkBox;
173 
174     }
175 
176 }