public class SpecificTreeCellRenderer implements TreeCellRenderer {
ImageIcon deviceIcon;
ImageIcon deviceActiveIcon;
ImageIcon deviceInactiveIcon;
ImageIcon deviceTypeEth;
ImageIcon deviceTypeUsb;
DefaultTreeCellRenderer defaultRenderer = new DefaultTreeCellRenderer();
public SpecificTreeCellRenderer() {
deviceTypeEth = new ImageIcon(getClass().getResource("/images/networkWired.png"));
deviceTypeUsb = new ImageIcon(getClass().getResource("/images/usbWired.png"));
deviceIcon = new ImageIcon(getClass().getResource("/images/device.png"));
deviceActiveIcon = new ImageIcon(getClass().getResource("/images/active.png"));
deviceInactiveIcon = new ImageIcon(getClass().getResource("/images/inactive.png"));
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object obj, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
Component returnValue = null;
if((obj != null) && (obj instanceof FrontendMutableTreeNode)) {
Object userObject = ((FrontendMutableTreeNode) obj).getNodeObject();
FrontendMutableTreeNode treeNode = (FrontendMutableTreeNode) obj;
if (userObject instanceof Frontend) {
Frontend frontend = (Frontend) userObject;
if(frontend.getDeviceConnectionType().equals(ComGlobal.deviceDriverETHTcp)) {
JPanel panelFrontend = createComponent(deviceIcon, frontend.getFrontendName(), deviceTypeEth, tree.getBackground(), selected);
panelFrontend.setToolTipText("Frontend => Type: " + frontend.getDeviceConnectionType() + " Address: " + frontend.getFrontendAddress());
returnValue = panelFrontend;
} else if(frontend.getDeviceConnectionType().equals(ComGlobal.deviceDriverUSB)) {
JPanel panelFrontend = createComponent(deviceIcon, frontend.getFrontendName(), deviceTypeUsb, tree.getBackground(), selected);
panelFrontend.setToolTipText("Frontend => Type: " + frontend.getDeviceConnectionType() + " Address: " + frontend.getFrontendAddress());
returnValue = panelFrontend;
}
} else if (userObject instanceof Device) {
Device device = (Device) userObject;
returnValue = createComponent(deviceIcon, device.getDeviceType(), null, tree.getBackground(), selected);
} else if (userObject instanceof Channel) {
Channel channel = (Channel) userObject;
if(channel.isActive()) {
System.err.println("CHANNEL ACTIVE!");
JPanel panelChannel = createComponent(deviceIcon, channel.getChannelName(), deviceActiveIcon, tree.getBackground(), selected);
panelChannel.setToolTipText("Channel is active");
returnValue = panelChannel;
} else {
JPanel panelChannel = createComponent(deviceIcon, channel.getChannelName(), deviceInactiveIcon, tree.getBackground(), selected);
panelChannel.setToolTipText("Channel is offline");
returnValue = panelChannel;
}
}
}
if (returnValue == null) {
returnValue = defaultRenderer.getTreeCellRendererComponent(tree,
obj, selected, expanded, leaf, row, hasFocus);
}
return returnValue;
}
public JPanel createComponent(ImageIcon icon, String device, ImageIcon statusIcon, Color backgroundColor, boolean selected) {
JPanel panelFrontend = new JPanel();
JLabel lbName = new JLabel(device);
JLabel lbDeviceIcon = new JLabel(deviceIcon);
JLabel lbStatusOfDeviceIcon = new JLabel(statusIcon);
lbName.setPreferredSize(new Dimension(60,22));
lbName.setMinimumSize(new Dimension(60,22));
lbName.setToolTipText("Test");
lbDeviceIcon.setPreferredSize(new Dimension(22,22));
lbDeviceIcon.setMinimumSize(new Dimension(22,22));
lbStatusOfDeviceIcon.setPreferredSize(new Dimension(22,22));
lbStatusOfDeviceIcon.setMinimumSize(new Dimension(22,22));
lbName.setOpaque(true);
lbDeviceIcon.setOpaque(true);
lbStatusOfDeviceIcon.setOpaque(true);
lbName.setBackground(backgroundColor);
lbDeviceIcon.setBackground(backgroundColor);
lbStatusOfDeviceIcon.setBackground(backgroundColor);
panelFrontend.setBackground(backgroundColor);
if(selected) {
panelFrontend.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY.brighter().brighter()));
}
panelFrontend.setLayout(new GridBagLayout());
panelFrontend.add(lbDeviceIcon, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 2, 0, 2), 0, 0));
panelFrontend.add(lbName, new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 7, 0, 10), 0, 0));
panelFrontend.add(lbStatusOfDeviceIcon, new GridBagConstraints(2, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 0, 0, 2), 0, 0));
return panelFrontend;
}
}