PageRenderTime 43ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/ui/beans/ExpandablePanel.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 161 lines | 101 code | 27 blank | 33 comment | 2 complexity | 43be60e5e05e57615a92789563363a5c MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. /*
  6. * ExpandablePanel.java
  7. *
  8. * Created on Aug 17, 2010, 4:10:12 PM
  9. */
  10. package mpv5.ui.beans;
  11. import java.awt.Color;
  12. import java.awt.Dimension;
  13. import java.awt.Font;
  14. import java.awt.Graphics;
  15. import java.awt.Graphics2D;
  16. import java.awt.GridBagConstraints;
  17. import java.awt.GridBagLayout;
  18. import java.awt.Insets;
  19. import java.awt.RenderingHints;
  20. import java.awt.event.MouseEvent;
  21. import java.awt.event.MouseListener;
  22. import java.awt.font.FontRenderContext;
  23. import java.awt.font.LineMetrics;
  24. import java.awt.image.BufferedImage;
  25. import javax.swing.JLabel;
  26. import javax.swing.JPanel;
  27. /**
  28. *
  29. * @author andreas.weber
  30. */
  31. public class ExpandablePanel extends javax.swing.JPanel {
  32. public ExpandablePanel() {
  33. this("+", new JPanel());
  34. }
  35. public JPanel getContentPane() {
  36. return contentPanel_;
  37. }
  38. /** This method is called from within the constructor to
  39. * initialize the form.
  40. * WARNING: Do NOT modify this code. The content of this method is
  41. * always regenerated by the Form Editor.
  42. */
  43. @SuppressWarnings("unchecked")
  44. // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  45. private void initComponents() {
  46. setName("Form"); // NOI18N
  47. setLayout(new java.awt.BorderLayout());
  48. }// </editor-fold>//GEN-END:initComponents
  49. // Variables declaration - do not modify//GEN-BEGIN:variables
  50. // End of variables declaration//GEN-END:variables
  51. private boolean selected;
  52. private JPanel contentPanel_;
  53. private HeaderPanel headerPanel_;
  54. private class HeaderPanel extends JPanel implements MouseListener {
  55. private String text_;
  56. private Font font;
  57. private BufferedImage open, closed;
  58. final int OFFSET = 30, PAD = 5;
  59. public HeaderPanel(String text) {
  60. addMouseListener(this);
  61. text_ = text;
  62. font = new Font("sans-serif", Font.PLAIN, 12);
  63. // setRequestFocusEnabled(true);
  64. setPreferredSize(new Dimension(200, 20));
  65. int w = getWidth();
  66. int h = getHeight();
  67. /*try {
  68. open = ImageIO.read(new File("images/arrow_down_mini.png"));
  69. closed = ImageIO.read(new File("images/arrow_right_mini.png"));
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. }*/
  73. }
  74. @Override
  75. protected void paintComponent(Graphics g) {
  76. super.paintComponent(g);
  77. Graphics2D g2 = (Graphics2D) g;
  78. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  79. RenderingHints.VALUE_ANTIALIAS_ON);
  80. int h = getHeight();
  81. /*if (selected)
  82. g2.drawImage(open, PAD, 0, h, h, this);
  83. else
  84. g2.drawImage(closed, PAD, 0, h, h, this);
  85. */ // Uncomment once you have your own images
  86. g2.setFont(font);
  87. FontRenderContext frc = g2.getFontRenderContext();
  88. LineMetrics lm = font.getLineMetrics(text_, frc);
  89. float height = lm.getAscent() + lm.getDescent();
  90. float x = OFFSET;
  91. float y = (h + height) / 2 - lm.getDescent();
  92. g2.drawString(text_, x, y);
  93. }
  94. public void mouseClicked(MouseEvent e) {
  95. toggleSelection();
  96. }
  97. public void mouseEntered(MouseEvent e) {
  98. }
  99. public void mouseExited(MouseEvent e) {
  100. }
  101. public void mousePressed(MouseEvent e) {
  102. }
  103. public void mouseReleased(MouseEvent e) {
  104. }
  105. }
  106. public ExpandablePanel(String text, JPanel content) {
  107. super(new GridBagLayout());
  108. GridBagConstraints gbc = new GridBagConstraints();
  109. gbc.insets = new Insets(1, 3, 0, 3);
  110. gbc.weightx = 1.0;
  111. gbc.fill = GridBagConstraints.HORIZONTAL;
  112. gbc.gridwidth = GridBagConstraints.REMAINDER;
  113. selected = false;
  114. headerPanel_ = new HeaderPanel(text);
  115. setBackground(new Color(200, 200, 220));
  116. contentPanel_ = content;
  117. add(headerPanel_, gbc);
  118. add(contentPanel_, gbc);
  119. contentPanel_.setVisible(false);
  120. JLabel padding = new JLabel();
  121. gbc.weighty = 1.0;
  122. add(padding, gbc);
  123. }
  124. public void toggleSelection() {
  125. selected = !selected;
  126. if (contentPanel_.isShowing()) {
  127. contentPanel_.setVisible(false);
  128. } else {
  129. contentPanel_.setVisible(true);
  130. }
  131. validate();
  132. headerPanel_.repaint();
  133. }
  134. }