1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.support.components;
14
15 import java.awt.BorderLayout;
16 import java.awt.Dimension;
17 import java.awt.Font;
18 import java.awt.event.ActionEvent;
19
20 import javax.swing.AbstractAction;
21 import javax.swing.Action;
22 import javax.swing.ImageIcon;
23 import javax.swing.JButton;
24 import javax.swing.JComponent;
25 import javax.swing.JLabel;
26 import javax.swing.JPanel;
27
28 import com.eviware.soapui.support.UISupport;
29
30 public class JCollapsiblePanel extends JPanel
31 {
32 private static ImageIcon minusIcon = UISupport.createImageIcon( "/button1.gif" );
33 private static ImageIcon plusIcon = UISupport.createImageIcon( "/button2.gif" );
34
35 private JPanel contentPanel;
36 private JXToolBar toolbar;
37 private ToggleAction toggleAction;
38
39 public JCollapsiblePanel( JPanel contentPanel, String title )
40 {
41 super( new BorderLayout() );
42 this.contentPanel = contentPanel;
43
44 add( contentPanel, BorderLayout.CENTER );
45 add( startToolbar( title ), BorderLayout.NORTH );
46 }
47
48 public JCollapsiblePanel( String title )
49 {
50 this( new JPanel(), title );
51 }
52
53 protected JXToolBar startToolbar( String title )
54 {
55 toolbar = UISupport.createToolbar();
56 toolbar.setBorder( null );
57 toolbar.setPreferredSize( new Dimension( 22, 22 ) );
58
59 toggleAction = new ToggleAction();
60 JButton toggleButton = new JButton( toggleAction );
61 toggleButton.setBorder( null );
62 toggleButton.setPreferredSize( new Dimension( 15, 15) );
63
64 toolbar.addSpace( 3 );
65 toolbar.addFixed( toggleButton);
66 toolbar.addSpace( 3 );
67
68 if( title != null )
69 {
70 JLabel titleLabel = new JLabel( title );
71 titleLabel.setFont( titleLabel.getFont().deriveFont( Font.BOLD ) );
72 toolbar.addFixed(titleLabel);
73 toolbar.addSpace( 3 );
74 }
75
76 return toolbar;
77 }
78
79 public boolean isExpanded()
80 {
81 return toggleAction.getValue( Action.SMALL_ICON ) == minusIcon;
82 }
83
84 public void setExpanded( boolean expanded )
85 {
86 if( !expanded )
87 {
88 toggleAction.setShow();
89 }
90 else
91 {
92 toggleAction.setHide();
93 }
94
95 contentPanel.setVisible( expanded );
96 refresh();
97 }
98
99 private void refresh()
100 {
101 contentPanel.revalidate();
102 if( contentPanel.getParent() instanceof JComponent )
103 ((JComponent)contentPanel.getParent()).revalidate();
104 }
105
106 public void setContentPanel( JPanel panel )
107 {
108 remove( contentPanel );
109 add( panel, BorderLayout.CENTER );
110 contentPanel = panel;
111
112 refresh();
113 }
114
115 private class ToggleAction extends AbstractAction
116 {
117 public ToggleAction()
118 {
119 setHide();
120 }
121
122 public void setHide()
123 {
124 putValue( Action.SMALL_ICON, minusIcon );
125 putValue( Action.SHORT_DESCRIPTION, "Hides the content of this block" );
126 }
127
128 public void setShow()
129 {
130 putValue( Action.SMALL_ICON, plusIcon );
131 putValue( Action.SHORT_DESCRIPTION, "Shows the content of this block" );
132 }
133
134 public void actionPerformed( ActionEvent e )
135 {
136 setExpanded( !isExpanded() );
137 }
138 }
139
140 public JXToolBar getToolbar()
141 {
142 return toolbar;
143 }
144 }