I have an application, that contains a list of department names- when a user clicks one of those a JPanel showing its details should appear. I load a Department object in my model devoted class, but is it a bad practice to pass that entire object into the custom DepartmentDetailsPanel constructor and extract data to show from it there (such as name, List of User members etc...)?
public class DepartmentDetailsPanel extends JPanel {
private ClassLoader cl = this.getClass().getClassLoader();
private JPanel iconAndName = new JPanel();
private JLabel icon;
private JLabel name;
private JPanel body = new JPanel();
private JPanel basicInfo = new JPanel();
private JLabel headLabel = new JLabel(Constants.head_name_lable);
private JLabel managerLabel = new JLabel(Constants.manager_name_label);
private JLabel typeLabel = new JLabel(Constants.type_label);
private JLabel head;
private JLabel manager;
private JLabel type;
private JPanel membersPanel = new JPanel();
private JLabel usersLabel = new JLabel(Constants.members_label);
private JList members;
private JPanel memberButtons = new JPanel();
private JButton removeButton = new JButton(Constants.remove);
private JButton addButton = new JButton(Constants.add);
public DepartmentDetailsPanel(Department d) {
setLayout(new BorderLayout());
setBorder(new EmptyBorder(10, 10, 10, 10) );
iconAndName.setLayout(new FlowLayout(FlowLayout.LEFT));
int dType = d.getType();
if (dType == 1)
icon = new JLabel(new ImageIcon(cl.getResource("leader.png")));
if (dType == 2)
icon = new JLabel(new ImageIcon(cl.getResource("creative.png")));
if (dType == 3)
icon = new JLabel(new ImageIcon(cl.getResource("finance.png")));
name = new JLabel(d.getName());
iconAndName.add(icon);
iconAndName.add(name);
add(iconAndName, BorderLayout.NORTH);
basicInfo.setLayout(new GridLayout(3, 2));
basicInfo.add(typeLabel);
type = new JLabel(Constants.department_types[dType-1]);
basicInfo.add(type);
basicInfo.add(headLabel);
head = new JLabel(d.getHeadName());
basicInfo.add(head);
basicInfo.add(managerLabel);
manager = new JLabel(d.getManagerName());
basicInfo.add(manager);
body.setLayout(new BoxLayout(body, BoxLayout.Y_AXIS));
body.add(basicInfo);
membersPanel.setLayout(new BorderLayout());
membersPanel.add(usersLabel, BorderLayout.NORTH);
JComponent memberList = makeList(extractMembers(d), members);
membersPanel.add(memberList, BorderLayout.CENTER);
memberButtons.setLayout(new BoxLayout(memberButtons, BoxLayout.Y_AXIS));
memberButtons.add(addButton);
memberButtons.add(removeButton);
membersPanel.add(memberButtons, BorderLayout.EAST);
body.add(membersPanel);
add(body, BorderLayout.CENTER);
}
private JComponent makeList(String[] data, JList list) {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 1));
panel.setBackground(Constants.lightGrey);
if (data == null){
JLabel message = new JLabel(Constants.nothing_to_view);
panel.add(message);
} else {
list = new JList(data);
list.setVisibleRowCount(10);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayoutOrientation(JList.VERTICAL);
list.setBackground(Constants.lightGrey);
panel.add(new JScrollPane(list));
}
return panel;
}
private String[] extractMembers(Department d){
String[] members = null;
List<User> list = d.getMembers();
if (list != null || list.size()==0){
members = new String[list.size()];
int i = 0;
for (User u: list){
members[i] = u.getName() +" "+ u.getLastname();
i++;
}
}
return members;
}
}