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/29/15.
40   *
41   * @author Tony Chemit - chemit@codelutin.com
42   * @since 3.14.3
43   */
44  public class ExportDataSelectTreeCellRenderer 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 ExportDataSelectTreeCellRenderer() {
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          String text = null;
78          String tip = null;
79          boolean filled = false;
80  
81          JCheckBox checkBox = normalCheckBox;
82  
83          if (value != null && value instanceof DataSelectTreeNodeSupport) {
84  
85              DataSelectTreeNodeSupport node = (DataSelectTreeNodeSupport) value;
86  
87              filled = node.isSelected();
88  
89              if (node instanceof CruiseSelectTreeNode) {
90  
91                  CruiseSelectTreeNode cruiseSelectTreeNode = (CruiseSelectTreeNode) node;
92  
93                  text = t("tutti.genericformat.export.cruiseNode", cruiseSelectTreeNode.getLabel(), cruiseSelectTreeNode.getNbChilds());
94  
95                  if (filled) {
96  
97                      tip = t("tutti.genericformat.export.cruiseNode.fullSelected", text, cruiseSelectTreeNode.getId());
98  
99                  } else if (cruiseSelectTreeNode.isPartialSelected()) {
100 
101                     checkBox = partialCheckBox;
102 
103                     tip = t("tutti.genericformat.export.cruiseNode.partialSelected", text, cruiseSelectTreeNode.getId(), cruiseSelectTreeNode.getNbChildSelected());
104 
105                 }else {
106 
107                     tip = text;
108 
109                 }
110 
111             } else if (node instanceof OperationSelectTreeNode) {
112 
113                 text = t("tutti.genericformat.export.operationNode", node.getLabel());
114 
115                 if (filled) {
116 
117                     tip = t("tutti.genericformat.export.operationNode.selected", text, node.getId());
118 
119                 } else {
120 
121                     tip = text;
122 
123                 }
124             }
125         }
126 
127         Font font;
128         if (filled) {
129             font = this.boldFont;
130         } else {
131             font = normalFont;
132         }
133 
134         checkBox.setFont(font);
135         checkBox.setSelected(filled);
136         checkBox.setBackground(label.getBackground());
137         checkBox.setForeground(label.getForeground());
138         checkBox.setText(text);
139         checkBox.setToolTipText(tip);
140 
141         return checkBox;
142 
143     }
144 
145 }