Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.ui.swing.framemanager.buttonbar;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.FrameContainerComparator;
  25. import com.dmdirc.Main;
  26. import com.dmdirc.config.IdentityManager;
  27. import com.dmdirc.interfaces.IconChangeListener;
  28. import com.dmdirc.interfaces.NotificationListener;
  29. import com.dmdirc.interfaces.SelectionListener;
  30. import com.dmdirc.ui.interfaces.FrameManager;
  31. import com.dmdirc.ui.interfaces.FramemanagerPosition;
  32. import com.dmdirc.ui.interfaces.Window;
  33. import com.dmdirc.ui.swing.UIUtilities;
  34. import com.dmdirc.util.MapList;
  35. import java.awt.BorderLayout;
  36. import java.awt.Color;
  37. import java.awt.Dimension;
  38. import java.awt.GridBagConstraints;
  39. import java.awt.GridBagLayout;
  40. import java.awt.Insets;
  41. import java.awt.event.ActionEvent;
  42. import java.awt.event.ActionListener;
  43. import java.awt.event.ComponentEvent;
  44. import java.awt.event.ComponentListener;
  45. import java.io.Serializable;
  46. import java.util.Collections;
  47. import java.util.HashMap;
  48. import java.util.List;
  49. import java.util.Map;
  50. import javax.swing.BorderFactory;
  51. import javax.swing.Icon;
  52. import javax.swing.JComponent;
  53. import javax.swing.JPanel;
  54. import javax.swing.JToggleButton;
  55. import javax.swing.SwingConstants;
  56. /**
  57. * The button bar manager is a grid of buttons that presents a manager similar
  58. * to that used by mIRC.
  59. *
  60. * @author chris
  61. */
  62. public final class ButtonBar implements FrameManager, ActionListener,
  63. ComponentListener, Serializable, NotificationListener,
  64. SelectionListener, IconChangeListener {
  65. /**
  66. * A version number for this class. It should be changed whenever the class
  67. * structure is changed (or anything else that would prevent serialized
  68. * objects being unserialized with the new class).
  69. */
  70. private static final long serialVersionUID = 3;
  71. /** A map of parent containers to their respective windows. */
  72. private final MapList<FrameContainer, FrameContainer> windows;
  73. /** A map of containers to the buttons we're using for them. */
  74. private final Map<FrameContainer, JToggleButton> buttons;
  75. /** The position of this frame manager. */
  76. private final FramemanagerPosition position;
  77. /** The parent for the manager. */
  78. private JComponent parent;
  79. /** The panel used for our buttons. */
  80. private JPanel panel;
  81. /** The currently selected window. */
  82. private transient FrameContainer selected;
  83. /** The number of buttons per row or column. */
  84. private int cells = 1;
  85. /** The number of buttons to render per {cell,row}. */
  86. private int maxButtons = Integer.MAX_VALUE;
  87. /** The width of buttons. */
  88. private int buttonWidth;
  89. /** Creates a new instance of DummyFrameManager. */
  90. public ButtonBar() {
  91. windows = new MapList<FrameContainer, FrameContainer>();
  92. buttons = new HashMap<FrameContainer, JToggleButton>();
  93. position = FramemanagerPosition.getPosition(
  94. IdentityManager.getGlobalConfig().getOption("ui", "framemanagerPosition"));
  95. panel = new JPanel(new GridBagLayout());
  96. }
  97. /** {@inheritDoc} */
  98. public void setParent(final JComponent parent) {
  99. this.parent = parent;
  100. parent.setLayout(new BorderLayout());
  101. parent.setBorder(BorderFactory.createEmptyBorder(
  102. UIUtilities.SMALL_BORDER, UIUtilities.SMALL_BORDER,
  103. UIUtilities.SMALL_BORDER, UIUtilities.SMALL_BORDER));
  104. parent.add(panel, BorderLayout.NORTH);
  105. buttonWidth = position.isHorizontal() ? 150 : (parent.getWidth() - UIUtilities.SMALL_BORDER * 3) / cells;
  106. if (position.isHorizontal()) {
  107. maxButtons = parent.getWidth() / (buttonWidth + UIUtilities.SMALL_BORDER * 2);
  108. }
  109. parent.addComponentListener(this);
  110. }
  111. /**
  112. * Removes all buttons from the bar and readds them.
  113. */
  114. private void relayout() {
  115. panel.removeAll();
  116. final GridBagConstraints constraints = new GridBagConstraints();
  117. constraints.gridx = 0;
  118. constraints.gridy = 0;
  119. constraints.insets.set(UIUtilities.SMALL_BORDER,
  120. UIUtilities.SMALL_BORDER, 0, 0);
  121. for (Map.Entry<FrameContainer, List<FrameContainer>> entry : windows.entrySet()) {
  122. buttons.get(entry.getKey()).setPreferredSize(new Dimension(buttonWidth, 25));
  123. buttons.get(entry.getKey()).setMinimumSize(new Dimension(buttonWidth, 25));
  124. panel.add(buttons.get(entry.getKey()), constraints);
  125. increment(constraints);
  126. Collections.sort(entry.getValue(), new FrameContainerComparator());
  127. for (FrameContainer child : entry.getValue()) {
  128. buttons.get(child).setPreferredSize(new Dimension(buttonWidth, 25));
  129. buttons.get(child).setMinimumSize(new Dimension(buttonWidth, 25));
  130. panel.add(buttons.get(child), constraints);
  131. increment(constraints);
  132. }
  133. }
  134. panel.validate();
  135. }
  136. /**
  137. * Increments the x and y offsets (where appropriate) of the gridbag
  138. * constraints.
  139. *
  140. * @param constraints The constraints to modify
  141. */
  142. public void increment(final GridBagConstraints constraints) {
  143. if (position.isHorizontal()) {
  144. constraints.gridx++;
  145. if (constraints.gridx > maxButtons) {
  146. constraints.gridy++;
  147. constraints.gridx = 0;
  148. }
  149. } else {
  150. constraints.gridy++;
  151. if (constraints.gridy > maxButtons) {
  152. constraints.gridx++;
  153. constraints.gridy = 0;
  154. }
  155. }
  156. }
  157. /**
  158. * Adds a button to the button array with the details from the specified
  159. * container.
  160. *
  161. * @param source The Container to get title/icon info from
  162. */
  163. private void addButton(final FrameContainer source) {
  164. final JToggleButton button = new JToggleButton(source.toString(), source.getIcon());
  165. button.addActionListener(this);
  166. button.setHorizontalAlignment(SwingConstants.LEFT);
  167. button.setMargin(new Insets(0, 0, 0, 0));
  168. buttons.put(source, button);
  169. }
  170. /** {@inheritDoc} */
  171. public boolean canPositionVertically() {
  172. return true;
  173. }
  174. /** {@inheritDoc} */
  175. public boolean canPositionHorizontally() {
  176. return true;
  177. }
  178. /** {@inheritDoc} */
  179. public void setSelected(final FrameContainer source) {
  180. if (selected != null && buttons.containsKey(selected)) {
  181. buttons.get(selected).setSelected(false);
  182. }
  183. selected = source;
  184. if (buttons.containsKey(source)) {
  185. buttons.get(source).setSelected(true);
  186. }
  187. }
  188. /** {@inheritDoc} */
  189. public void showNotification(final FrameContainer source, final Color colour) {
  190. if (buttons.containsKey(source)) {
  191. buttons.get(source).setForeground(colour);
  192. }
  193. }
  194. /** {@inheritDoc} */
  195. public void clearNotification(final FrameContainer source) {
  196. showNotification(source, source.getNotification());
  197. }
  198. /** {@inheritDoc} */
  199. public void addWindow(final FrameContainer window) {
  200. windows.add(window);
  201. addButton(window);
  202. relayout();
  203. window.addNotificationListener(this);
  204. window.addSelectionListener(this);
  205. window.addIconChangeListener(this);
  206. }
  207. /** {@inheritDoc} */
  208. public void delWindow(final FrameContainer window) {
  209. windows.remove(window);
  210. relayout();
  211. window.removeNotificationListener(this);
  212. window.removeIconChangeListener(this);
  213. window.removeSelectionListener(this);
  214. }
  215. /** {@inheritDoc} */
  216. public void addWindow(final FrameContainer parent, final FrameContainer window) {
  217. windows.add(parent, window);
  218. addButton(window);
  219. relayout();
  220. window.addNotificationListener(this);
  221. window.addSelectionListener(this);
  222. window.addIconChangeListener(this);
  223. }
  224. /** {@inheritDoc} */
  225. public void delWindow(final FrameContainer parent, final FrameContainer window) {
  226. windows.remove(parent, window);
  227. relayout();
  228. window.removeNotificationListener(this);
  229. window.removeIconChangeListener(this);
  230. window.removeSelectionListener(this);
  231. }
  232. /** {@inheritDoc} */
  233. public void iconUpdated(final FrameContainer window) {
  234. buttons.get(window).setIcon(window.getIcon());
  235. }
  236. /**
  237. * Called when the user clicks on one of the buttons.
  238. *
  239. * @param e The action event associated with this action
  240. */
  241. public void actionPerformed(final ActionEvent e) {
  242. final Window active = Main.getUI().getActiveWindow();
  243. for (Map.Entry<FrameContainer, JToggleButton> entry : buttons.entrySet()) {
  244. if (entry.getValue().equals(e.getSource())) {
  245. if (entry.getKey().getFrame().equals(active)) {
  246. entry.getValue().setSelected(true);
  247. }
  248. entry.getKey().activateFrame();
  249. }
  250. }
  251. }
  252. /**
  253. * Called when the parent component is resized.
  254. *
  255. * @param e A ComponentEvent corresponding to this event.
  256. */
  257. public void componentResized(final ComponentEvent e) {
  258. buttonWidth = position.isHorizontal() ? 150 : (parent.getWidth() - UIUtilities.SMALL_BORDER * 3) / cells;
  259. if (position.isHorizontal()) {
  260. maxButtons = parent.getWidth() / (buttonWidth + UIUtilities.SMALL_BORDER * 2);
  261. }
  262. relayout();
  263. }
  264. /**
  265. * Called when the parent component is moved.
  266. *
  267. * @param e A ComponentEvent corresponding to this event.
  268. */
  269. public void componentMoved(final ComponentEvent e) {
  270. // Do nothing
  271. }
  272. /**
  273. * Called when the parent component is made visible.
  274. *
  275. * @param e A ComponentEvent corresponding to this event.
  276. */
  277. public void componentShown(final ComponentEvent e) {
  278. // Do nothing
  279. }
  280. /**
  281. * Called when the parent component is made invisible.
  282. *
  283. * @param e A ComponentEvent corresponding to this event.
  284. */
  285. public void componentHidden(final ComponentEvent e) {
  286. // Do nothing
  287. }
  288. /** {@inheritDoc} */
  289. @Override
  290. public void notificationSet(final Window window, final Color colour) {
  291. if (buttons.containsKey(window.getContainer())) {
  292. buttons.get(window.getContainer()).setForeground(colour);
  293. }
  294. }
  295. /** {@inheritDoc} */
  296. @Override
  297. public void notificationCleared(final Window window) {
  298. notificationSet(window, window.getContainer().getNotification());
  299. }
  300. /** {@inheritDoc} */
  301. @Override
  302. public void selectionChanged(final Window window) {
  303. if (selected != null && buttons.containsKey(selected)) {
  304. buttons.get(selected).setSelected(false);
  305. }
  306. selected = window.getContainer();
  307. if (buttons.containsKey(window.getContainer())) {
  308. buttons.get(window.getContainer()).setSelected(true);
  309. }
  310. }
  311. /** {@inheritDoc} */
  312. @Override
  313. public void iconChanged(final Window window, final Icon icon) {
  314. buttons.get(window.getContainer()).setIcon(icon);
  315. }
  316. }