|
22 | 22 |
|
23 | 23 |
|
24 | 24 | import com.vaadin.flow.component.Component; |
25 | | -import com.vaadin.flow.component.HasComponents; |
| 25 | +import com.vaadin.flow.component.HasOrderedComponents; |
26 | 26 | import com.vaadin.flow.component.Tag; |
27 | 27 | import com.vaadin.flow.component.dependency.HtmlImport; |
28 | 28 | import com.vaadin.flow.component.dependency.JsModule; |
29 | 29 | import com.vaadin.flow.component.dependency.NpmPackage; |
30 | 30 | import com.vaadin.flow.component.html.Div; |
31 | 31 | import com.vaadin.flow.component.html.Image; |
| 32 | +import com.vaadin.flow.dom.Element; |
32 | 33 |
|
33 | 34 | /** |
34 | 35 | * Component that renders the app toolbar |
|
44 | 45 | @JsModule("@polymer/app-layout/app-toolbar/app-toolbar.js") |
45 | 46 | @JsModule("@polymer/iron-icons/iron-icons.js") |
46 | 47 | @Tag("app-toolbar") |
47 | | -public class AppToolbar extends Component implements HasComponents { |
| 48 | +public class AppToolbar extends Component { |
48 | 49 |
|
49 | 50 | private ToolbarIconButton menu; |
50 | | - private Component ctitle; |
51 | 51 | private Div divTitle; |
| 52 | + private int index; |
| 53 | + |
| 54 | + private HasOrderedComponents<AppToolbar> hasOrderedComponents = new HasOrderedComponents<AppToolbar>() { |
| 55 | + @Override |
| 56 | + public Element getElement() { |
| 57 | + return AppToolbar.this.getElement(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public int getComponentCount() { |
| 62 | + return (int) getChildren().count(); |
| 63 | + }; |
| 64 | + |
| 65 | + @Override |
| 66 | + public Component getComponentAt(int index) { |
| 67 | + if (index < 0) { |
| 68 | + throw new IllegalArgumentException( |
| 69 | + "The 'index' argument should be greater than or equal to 0. It was: " |
| 70 | + + index); |
| 71 | + } |
| 72 | + return getChildren().sequential().skip(index).findFirst() |
| 73 | + .orElseThrow(() -> new IllegalArgumentException( |
| 74 | + "The 'index' argument should not be greater than or equals to the number of children components. It was: " |
| 75 | + + index)); |
| 76 | + }; |
| 77 | + |
| 78 | + }; |
52 | 79 |
|
53 | 80 | public AppToolbar(String title, AppDrawer drawer) { |
54 | 81 | this(null,title, drawer); |
55 | 82 | } |
56 | 83 |
|
57 | 84 | public AppToolbar(Image logo, String title, AppDrawer drawer) { |
58 | | - menu = new ToolbarIconButton().setIcon("menu"); |
| 85 | + menu = new ToolbarIconButton().setIcon("menu"); |
| 86 | + add(menu); |
| 87 | + |
59 | 88 | drawer.getId().ifPresent(id -> menu.getElement().setAttribute("onclick", id + ".toggle()")); |
60 | 89 | if (logo!=null) { |
61 | | - ctitle = logo; |
| 90 | + add(logo); |
62 | 91 | } |
| 92 | + |
63 | 93 | divTitle = new Div(); |
64 | | - divTitle.getElement().setAttribute("main-title", true); |
| 94 | + divTitle.getElement().setAttribute("main-title", true); |
65 | 95 | setTitle(title); |
66 | | - |
67 | | - clearToolbarIconButtons(); |
| 96 | + add(divTitle); |
| 97 | + |
| 98 | + index = hasOrderedComponents.getComponentCount(); |
| 99 | + } |
| 100 | + |
| 101 | + private void add(Component... components) { |
| 102 | + hasOrderedComponents.add(components); |
| 103 | + } |
| 104 | + |
| 105 | + private void addComponentAtIndex(int index, Component component) { |
| 106 | + hasOrderedComponents.addComponentAtIndex(index, component); |
68 | 107 | } |
69 | 108 |
|
70 | 109 | public void setTitle(String title) { |
71 | 110 | divTitle.setText(title); |
72 | 111 | } |
73 | 112 |
|
74 | 113 | public void clearToolbarIconButtons() { |
75 | | - this.removeAll(); |
76 | | - this.add(menu); |
77 | | - if (ctitle!=null) this.add(ctitle); |
78 | | - if (divTitle!=null) this.add(divTitle); |
| 114 | + while (hasOrderedComponents.getComponentCount()>index) { |
| 115 | + hasOrderedComponents.remove(hasOrderedComponents.getComponentAt(index)); |
| 116 | + } |
79 | 117 | } |
80 | | - |
81 | | - public void addToolbarIconButtons(Component... components) { |
82 | | - this.add(components); |
83 | | - } |
84 | | - |
| 118 | + |
85 | 119 | public void setMenuIconVisible(boolean visible) { |
86 | 120 | menu.setVisible(visible); |
87 | 121 | } |
88 | 122 |
|
| 123 | + public void addToolbarIconButtons(Component... components) { |
| 124 | + this.add(components); |
| 125 | + } |
| 126 | + |
| 127 | + public void addToolbarIconButtonAsFirst(Component component) { |
| 128 | + this.addComponentAtIndex(index, component); |
| 129 | + } |
89 | 130 |
|
90 | 131 | } |
0 commit comments