16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.tools.jconsole; 27 28 import java.awt.*; 29 import java.awt.event.*; 30 31 import javax.swing.*; 32 import javax.swing.border.*; 33 import javax.swing.plaf.*; 34 import javax.swing.plaf.basic.BasicGraphicsUtils; 35 36 import static javax.swing.SwingConstants.*; 37 38 import static sun.tools.jconsole.JConsole.*; 39 import static sun.tools.jconsole.Resources.*; 40 import static sun.tools.jconsole.Utilities.*; 41 42 @SuppressWarnings("serial") 43 public class BorderedComponent extends JPanel implements ActionListener { 44 JButton moreOrLessButton; 45 String valueLabelStr; 46 JLabel label; 47 JComponent comp; 48 boolean collapsed = false; 49 50 private JPopupMenu popupMenu; 51 52 private Icon collapseIcon; 53 private Icon expandIcon; 54 55 private static Image getImage(String name) { 56 Toolkit tk = Toolkit.getDefaultToolkit(); 57 name = "resources/" + name + ".png"; 58 return tk.getImage(BorderedComponent.class.getResource(name)); 59 } 60 61 public BorderedComponent(String text) { 62 this(text, null, false); 63 } 64 65 public BorderedComponent(String text, JComponent comp) { 66 this(text, comp, false); 67 } 68 69 public BorderedComponent(String text, JComponent comp, boolean collapsible) { 70 super(null); 71 83 } 84 }; 85 borderLabel.add(textLabel); 86 border = new LabeledBorder(borderLabel); 87 textLabel.setForeground(border.getTitleColor()); 88 89 if (IS_WIN) { 90 collapseIcon = new ImageIcon(getImage("collapse-winlf")); 91 expandIcon = new ImageIcon(getImage("expand-winlf")); 92 } else { 93 collapseIcon = new ArrowIcon(SOUTH, textLabel); 94 expandIcon = new ArrowIcon(EAST, textLabel); 95 } 96 97 moreOrLessButton = new JButton(collapseIcon); 98 moreOrLessButton.setContentAreaFilled(false); 99 moreOrLessButton.setBorderPainted(false); 100 moreOrLessButton.setMargin(new Insets(0, 0, 0, 0)); 101 moreOrLessButton.addActionListener(this); 102 String toolTip = 103 getText("BorderedComponent.moreOrLessButton.toolTip"); 104 moreOrLessButton.setToolTipText(toolTip); 105 borderLabel.add(moreOrLessButton); 106 borderLabel.setSize(borderLabel.getPreferredSize()); 107 add(borderLabel); 108 } else { 109 border = new TitledBorder(text); 110 } 111 setBorder(new CompoundBorder(new FocusBorder(this), border)); 112 } else { 113 setBorder(new FocusBorder(this)); 114 } 115 if (comp != null) { 116 add(comp); 117 } 118 } 119 120 public void setComponent(JComponent comp) { 121 if (this.comp != null) { 122 remove(this.comp); 123 } 124 this.comp = comp; 125 if (!collapsed) { 126 LayoutManager lm = getLayout(); 127 if (lm instanceof BorderLayout) { 128 add(comp, BorderLayout.CENTER); 129 } else { 130 add(comp); 131 } 132 } 133 revalidate(); 134 } 135 136 public void setValueLabel(String str) { 137 this.valueLabelStr = str; 138 if (label != null) { 139 label.setText(Resources.getText("Current value",valueLabelStr)); 140 } 141 } 142 143 public void actionPerformed(ActionEvent ev) { 144 if (collapsed) { 145 if (label != null) { 146 remove(label); 147 } 148 add(comp); 149 moreOrLessButton.setIcon(collapseIcon); 150 } else { 151 remove(comp); 152 if (valueLabelStr != null) { 153 if (label == null) { 154 label = new JLabel(Resources.getText("Current value", 155 valueLabelStr)); 156 } 157 add(label); 158 } 159 moreOrLessButton.setIcon(expandIcon); 160 } 161 collapsed = !collapsed; 162 163 JComponent container = (JComponent)getParent(); 164 if (container != null && 165 container.getLayout() instanceof VariableGridLayout) { 166 167 ((VariableGridLayout)container.getLayout()).setFillRow(this, !collapsed); 168 container.revalidate(); 169 } 170 } 171 172 public Dimension getMinimumSize() { 173 if (getLayout() != null) { 174 // A layout manager has been set, so delegate to it 422 423 // restore clip 424 g.setClip(saveClip); 425 426 } else { 427 border.paintBorder(c, g, grooveRect.x, grooveRect.y, 428 grooveRect.width, grooveRect.height); 429 } 430 431 label.setLocation(compLoc); 432 label.setSize(labelDim); 433 } 434 } 435 436 /** 437 * Reinitialize the insets parameter with this Border's current Insets. 438 * @param c the component for which this border insets value applies 439 * @param insets the object to be reinitialized 440 */ 441 public Insets getBorderInsets(Component c, Insets insets) { 442 int height = 16; 443 444 Border border = getBorder(); 445 if (border != null) { 446 if (border instanceof AbstractBorder) { 447 ((AbstractBorder)border).getBorderInsets(c, insets); 448 } else { 449 // Can't reuse border insets because the Border interface 450 // can't be enhanced. 451 Insets i = border.getBorderInsets(c); 452 insets.top = i.top; 453 insets.right = i.right; 454 insets.bottom = i.bottom; 455 insets.left = i.left; 456 } 457 } else { 458 insets.left = insets.top = insets.right = insets.bottom = 0; 459 } 460 461 insets.left += EDGE_SPACING + TEXT_SPACING; 462 insets.right += EDGE_SPACING + TEXT_SPACING; 463 insets.top += EDGE_SPACING + TEXT_SPACING; | 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.tools.jconsole; 27 28 import java.awt.*; 29 import java.awt.event.*; 30 31 import javax.swing.*; 32 import javax.swing.border.*; 33 import javax.swing.plaf.*; 34 import javax.swing.plaf.basic.BasicGraphicsUtils; 35 36 import sun.tools.jconsole.resources.Messages; 37 38 import static javax.swing.SwingConstants.*; 39 40 import static sun.tools.jconsole.JConsole.*; 41 42 @SuppressWarnings("serial") 43 public class BorderedComponent extends JPanel implements ActionListener { 44 JButton moreOrLessButton; 45 String valueLabelStr; 46 JLabel label; 47 JComponent comp; 48 boolean collapsed = false; 49 50 private Icon collapseIcon; 51 private Icon expandIcon; 52 53 private static Image getImage(String name) { 54 Toolkit tk = Toolkit.getDefaultToolkit(); 55 name = "resources/" + name + ".png"; 56 return tk.getImage(BorderedComponent.class.getResource(name)); 57 } 58 59 public BorderedComponent(String text) { 60 this(text, null, false); 61 } 62 63 public BorderedComponent(String text, JComponent comp) { 64 this(text, comp, false); 65 } 66 67 public BorderedComponent(String text, JComponent comp, boolean collapsible) { 68 super(null); 69 81 } 82 }; 83 borderLabel.add(textLabel); 84 border = new LabeledBorder(borderLabel); 85 textLabel.setForeground(border.getTitleColor()); 86 87 if (IS_WIN) { 88 collapseIcon = new ImageIcon(getImage("collapse-winlf")); 89 expandIcon = new ImageIcon(getImage("expand-winlf")); 90 } else { 91 collapseIcon = new ArrowIcon(SOUTH, textLabel); 92 expandIcon = new ArrowIcon(EAST, textLabel); 93 } 94 95 moreOrLessButton = new JButton(collapseIcon); 96 moreOrLessButton.setContentAreaFilled(false); 97 moreOrLessButton.setBorderPainted(false); 98 moreOrLessButton.setMargin(new Insets(0, 0, 0, 0)); 99 moreOrLessButton.addActionListener(this); 100 String toolTip = 101 Messages.BORDERED_COMPONENT_MORE_OR_LESS_BUTTON_TOOLTIP; 102 moreOrLessButton.setToolTipText(toolTip); 103 borderLabel.add(moreOrLessButton); 104 borderLabel.setSize(borderLabel.getPreferredSize()); 105 add(borderLabel); 106 } else { 107 border = new TitledBorder(text); 108 } 109 setBorder(new CompoundBorder(new FocusBorder(this), border)); 110 } else { 111 setBorder(new FocusBorder(this)); 112 } 113 if (comp != null) { 114 add(comp); 115 } 116 } 117 118 public void setComponent(JComponent comp) { 119 if (this.comp != null) { 120 remove(this.comp); 121 } 122 this.comp = comp; 123 if (!collapsed) { 124 LayoutManager lm = getLayout(); 125 if (lm instanceof BorderLayout) { 126 add(comp, BorderLayout.CENTER); 127 } else { 128 add(comp); 129 } 130 } 131 revalidate(); 132 } 133 134 public void setValueLabel(String str) { 135 this.valueLabelStr = str; 136 if (label != null) { 137 label.setText(Resources.format(Messages.CURRENT_VALUE, valueLabelStr)); 138 } 139 } 140 141 public void actionPerformed(ActionEvent ev) { 142 if (collapsed) { 143 if (label != null) { 144 remove(label); 145 } 146 add(comp); 147 moreOrLessButton.setIcon(collapseIcon); 148 } else { 149 remove(comp); 150 if (valueLabelStr != null) { 151 if (label == null) { 152 label = new JLabel(Resources.format(Messages.CURRENT_VALUE, 153 valueLabelStr)); 154 } 155 add(label); 156 } 157 moreOrLessButton.setIcon(expandIcon); 158 } 159 collapsed = !collapsed; 160 161 JComponent container = (JComponent)getParent(); 162 if (container != null && 163 container.getLayout() instanceof VariableGridLayout) { 164 165 ((VariableGridLayout)container.getLayout()).setFillRow(this, !collapsed); 166 container.revalidate(); 167 } 168 } 169 170 public Dimension getMinimumSize() { 171 if (getLayout() != null) { 172 // A layout manager has been set, so delegate to it 420 421 // restore clip 422 g.setClip(saveClip); 423 424 } else { 425 border.paintBorder(c, g, grooveRect.x, grooveRect.y, 426 grooveRect.width, grooveRect.height); 427 } 428 429 label.setLocation(compLoc); 430 label.setSize(labelDim); 431 } 432 } 433 434 /** 435 * Reinitialize the insets parameter with this Border's current Insets. 436 * @param c the component for which this border insets value applies 437 * @param insets the object to be reinitialized 438 */ 439 public Insets getBorderInsets(Component c, Insets insets) { 440 Border border = getBorder(); 441 if (border != null) { 442 if (border instanceof AbstractBorder) { 443 ((AbstractBorder)border).getBorderInsets(c, insets); 444 } else { 445 // Can't reuse border insets because the Border interface 446 // can't be enhanced. 447 Insets i = border.getBorderInsets(c); 448 insets.top = i.top; 449 insets.right = i.right; 450 insets.bottom = i.bottom; 451 insets.left = i.left; 452 } 453 } else { 454 insets.left = insets.top = insets.right = insets.bottom = 0; 455 } 456 457 insets.left += EDGE_SPACING + TEXT_SPACING; 458 insets.right += EDGE_SPACING + TEXT_SPACING; 459 insets.top += EDGE_SPACING + TEXT_SPACING; |