/plugins/Console/tags/Console_4_2_4/console/gui/PanelStack.java

# · Java · 66 lines · 45 code · 12 blank · 9 comment · 5 complexity · 74591ed5dcdf82aba7d5f8a755111207 MD5 · raw file

  1. package console.gui;
  2. import java.awt.BorderLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.HashMap;
  6. import javax.swing.JPanel;
  7. /**
  8. * Arranges JPanels in a stacked layout, and gives a convenient
  9. * interface for adding/switching by name.
  10. * @author ezust
  11. *
  12. */
  13. public class PanelStack extends JPanel implements ActionListener
  14. {
  15. HashMap<String, JPanel> panels;
  16. JPanel current;
  17. public PanelStack() {
  18. panels = new HashMap<String, JPanel>();
  19. setLayout(new BorderLayout());
  20. current = null;
  21. }
  22. /**
  23. * @deprecated - use @ref add(String, JPanel)
  24. */
  25. public void add(Object o) {
  26. throw new RuntimeException ("Don't call this.");
  27. }
  28. public void add(String name, JPanel panel) {
  29. panels.put(name, panel);
  30. }
  31. public boolean raise(String name) {
  32. JPanel p = panels.get(name);
  33. if (p != null) {
  34. if (p==current) return true;
  35. if (current != null) {
  36. remove(current);
  37. }
  38. add(p, BorderLayout.CENTER);
  39. revalidate();
  40. repaint();
  41. current = p;
  42. return true;
  43. }
  44. return false;
  45. }
  46. public JPanel get(String name) {
  47. return panels.get(name);
  48. }
  49. public void actionPerformed(ActionEvent e)
  50. {
  51. String name = e.getActionCommand();
  52. raise(name);
  53. }
  54. }