| 1 | |
package org.jtheque.core.managers.view.impl.components; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
import org.jtheque.core.managers.Managers; |
| 20 | |
import org.jtheque.core.managers.view.able.IViewManager; |
| 21 | |
import org.jtheque.core.managers.view.able.components.StateBarComponent; |
| 22 | |
import org.jtheque.core.managers.view.listeners.StateBarEvent; |
| 23 | |
import org.jtheque.core.managers.view.listeners.StateBarListener; |
| 24 | |
import org.jtheque.utils.ui.GridBagUtils; |
| 25 | |
|
| 26 | |
import javax.swing.Box; |
| 27 | |
import javax.swing.JPanel; |
| 28 | |
import javax.swing.JSeparator; |
| 29 | |
import java.awt.Color; |
| 30 | |
import java.awt.Component; |
| 31 | |
import java.awt.Dimension; |
| 32 | |
import java.awt.GridBagLayout; |
| 33 | |
import java.util.ArrayList; |
| 34 | |
import java.util.Collections; |
| 35 | |
import java.util.Comparator; |
| 36 | |
import java.util.List; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public final class JThequeStateBar extends JPanel implements StateBarListener { |
| 44 | |
private int currentColumn; |
| 45 | |
|
| 46 | 0 | private final GridBagUtils gbc = new GridBagUtils(); |
| 47 | |
|
| 48 | |
private boolean leftFilled; |
| 49 | |
private boolean centerFilled; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
public JThequeStateBar() { |
| 55 | 0 | super(); |
| 56 | |
|
| 57 | 0 | setLayout(new GridBagLayout()); |
| 58 | 0 | setBackground(new Color(133, 144, 165)); |
| 59 | |
|
| 60 | 0 | build(); |
| 61 | 0 | } |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
private void build() { |
| 67 | 0 | List<StateBarComponent> components = new ArrayList<StateBarComponent>(Managers.getManager(IViewManager.class).getStateBarComponents()); |
| 68 | |
|
| 69 | 0 | if (components.isEmpty()) { |
| 70 | 0 | setVisible(false); |
| 71 | |
} else { |
| 72 | 0 | setVisible(true); |
| 73 | |
|
| 74 | 0 | Collections.sort(components, new LeftToRightComparator()); |
| 75 | |
|
| 76 | 0 | currentColumn = 0; |
| 77 | |
|
| 78 | 0 | addLeftComponents(components); |
| 79 | 0 | addCenterComponents(components); |
| 80 | 0 | addRightComponents(components); |
| 81 | |
} |
| 82 | |
|
| 83 | 0 | Managers.getManager(IViewManager.class).addStateBarListener(this); |
| 84 | 0 | } |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
private void addLeftComponents(Iterable<StateBarComponent> components) { |
| 92 | 0 | for (StateBarComponent component : components) { |
| 93 | 0 | if (component.isLeft()) { |
| 94 | 0 | addComponent(component.getComponent()); |
| 95 | 0 | leftFilled = true; |
| 96 | |
} |
| 97 | |
} |
| 98 | |
|
| 99 | 0 | addGlue(); |
| 100 | 0 | } |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
private void addCenterComponents(Iterable<StateBarComponent> components) { |
| 108 | 0 | addGlue(); |
| 109 | |
|
| 110 | 0 | if (leftFilled) { |
| 111 | 0 | addSeparator(); |
| 112 | |
} |
| 113 | |
|
| 114 | 0 | for (StateBarComponent component : components) { |
| 115 | 0 | if (component.isCenter()) { |
| 116 | 0 | addComponent(component.getComponent()); |
| 117 | 0 | centerFilled = true; |
| 118 | |
} |
| 119 | |
} |
| 120 | |
|
| 121 | 0 | addGlue(); |
| 122 | 0 | } |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
private void addRightComponents(Iterable<StateBarComponent> components) { |
| 130 | 0 | addGlue(); |
| 131 | |
|
| 132 | 0 | if (centerFilled) { |
| 133 | 0 | addSeparator(); |
| 134 | |
} |
| 135 | |
|
| 136 | 0 | for (StateBarComponent component : components) { |
| 137 | 0 | if (component.isRight()) { |
| 138 | 0 | addComponent(component.getComponent()); |
| 139 | |
} |
| 140 | |
} |
| 141 | 0 | } |
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
private void addGlue() { |
| 147 | 0 | add(Box.createHorizontalGlue(), gbc.gbcSet( |
| 148 | |
currentColumn++, 1, GridBagUtils.HORIZONTAL, GridBagUtils.BASELINE_LEADING, 1.0, 1.0)); |
| 149 | 0 | } |
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
private void addComponent(Component component) { |
| 157 | 0 | add(component, gbc.gbcSet(currentColumn++, 1)); |
| 158 | 0 | } |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
private void addSeparator() { |
| 164 | 0 | Component separator = new JSeparator(JSeparator.VERTICAL); |
| 165 | 0 | separator.setPreferredSize(new Dimension(1, 10)); |
| 166 | |
|
| 167 | 0 | add(separator, gbc.gbcSet(currentColumn++, 1)); |
| 168 | 0 | } |
| 169 | |
|
| 170 | |
@Override |
| 171 | |
public void componentAdded() { |
| 172 | 0 | build(); |
| 173 | |
|
| 174 | 0 | Managers.getManager(IViewManager.class).refresh(this); |
| 175 | 0 | } |
| 176 | |
|
| 177 | |
@Override |
| 178 | |
public void componentRemoved(StateBarEvent event) { |
| 179 | 0 | remove(event.getComponent().getComponent()); |
| 180 | |
|
| 181 | 0 | if (Managers.getManager(IViewManager.class).getStateBarComponents().isEmpty()) { |
| 182 | 0 | setVisible(false); |
| 183 | |
} |
| 184 | |
|
| 185 | 0 | Managers.getManager(IViewManager.class).refresh(this); |
| 186 | 0 | } |
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | 0 | private static final class LeftToRightComparator implements Comparator<StateBarComponent> { |
| 194 | |
@Override |
| 195 | |
public int compare(StateBarComponent component, StateBarComponent other) { |
| 196 | 0 | return component.getPosition().compareTo(other.getPosition()); |
| 197 | |
} |
| 198 | |
} |
| 199 | |
} |