Skip to content

Commit

Permalink
updated navigator example
Browse files Browse the repository at this point in the history
  • Loading branch information
pseay committed Feb 11, 2021
1 parent 5d8c90e commit ba02c79
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
Binary file modified out/production/HierarchyComponents/hswing/HFrame.class
Binary file not shown.
Binary file modified out/production/HierarchyComponents/hswing/Navigator.class
Binary file not shown.
15 changes: 11 additions & 4 deletions src/demo/NavigatorExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,24 @@ public static void main(String[] args) {
))
).getNavigator();

//this would throw a MoreThanOneComponentException
//navigator.getSingle("label");

//returns null because no components have the name 'label' and the class of a JButton
navigator.getSingleByClass("label", JButton.class);

//gets the one 'button1' with the class 'JButton' (automatically casts it from HButton -> JButton)
navigator.getSingleByClass("button1", JButton.class).addActionListener(e -> {
//gets all the JLabels
navigator.getListByClass(JLabel.class).forEach(jLabel -> {
//gets all the JLabels with name 'label'
navigator.getListByClass("label", JLabel.class).forEach(jLabel -> {
jLabel.setText(jLabel.getText().repeat(2));
});
navigator.getFrame().pack();
});

//getting a list based on just the class
//getting a list of JComponents based on just the class
navigator.getListByClass(JLabel.class).forEach(label -> {
label.setBackground(Color.blue);
label.setBorder(BorderFactory.createLineBorder(Color.blue, 5, true));
});

AtomicInteger aint = new AtomicInteger(10);
Expand Down
11 changes: 7 additions & 4 deletions src/hswing/Navigator.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ public JComponent getSingle(String name) throws MoreThanOneComponentException {

/**
* Gets a single T according to the name
* @param name the name of the {@link JComponent} to get
* @param t the class to automatically cast it to
* @param name the name of the T to get
* @param t the class to find and return
* @param <T> the class type to be returned
* @return the {@link JComponent} with the name. If the {@link JComponent} does not exist, it will return null
* @throws MoreThanOneComponentException if there are multiple {@link JComponent}s with the same name
* @return the T with the name. If the T does not exist, it will return null
* @throws MoreThanOneComponentException if there are multiple Ts with the same name
*/
public <T> T getSingleByClass(String name, Class<T> t) throws MoreThanOneComponentException {
List<JComponent> componentsWithName = components.get(name);
Expand All @@ -79,6 +79,9 @@ public <T> T getSingleByClass(String name, Class<T> t) throws MoreThanOneCompone
if (componentsWithName.size() == 1) {
return (T)componentsWithName.get(0);
}
if (componentsWithName.size() == 0) {
return null;
}
throw new MoreThanOneComponentException(name);
}

Expand Down

0 comments on commit ba02c79

Please sign in to comment.