< prev index next >

src/share/classes/sun/tools/jconsole/BorderedComponent.java

Print this page
rev 1501 : 7017818: NLS: JConsoleResources.java cannot be handled by translation team
Reviewed-by: mchung, mfang


  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 
  37 import static javax.swing.SwingConstants.*;
  38 
  39 import static sun.tools.jconsole.JConsole.*;


  40 
  41 @SuppressWarnings("serial")
  42 public class BorderedComponent extends JPanel implements ActionListener {
  43     JButton moreOrLessButton;
  44     String valueLabelStr;
  45     JLabel label;
  46     JComponent comp;
  47     boolean collapsed = false;
  48 


  49     private Icon collapseIcon;
  50     private Icon expandIcon;
  51 
  52     private static Image getImage(String name) {
  53         Toolkit tk = Toolkit.getDefaultToolkit();
  54         name = "resources/" + name + ".png";
  55         return tk.getImage(BorderedComponent.class.getResource(name));
  56     }
  57 
  58     public BorderedComponent(String text) {
  59         this(text, null, false);
  60     }
  61 
  62     public BorderedComponent(String text, JComponent comp) {
  63         this(text, comp, false);
  64     }
  65 
  66     public BorderedComponent(String text, JComponent comp, boolean collapsible) {
  67         super(null);
  68 


  80                     }
  81                 };
  82                 borderLabel.add(textLabel);
  83                 border = new LabeledBorder(borderLabel);
  84                 textLabel.setForeground(border.getTitleColor());
  85 
  86                 if (IS_WIN) {
  87                     collapseIcon = new ImageIcon(getImage("collapse-winlf"));
  88                     expandIcon = new ImageIcon(getImage("expand-winlf"));
  89                 } else {
  90                     collapseIcon = new ArrowIcon(SOUTH, textLabel);
  91                     expandIcon = new ArrowIcon(EAST, textLabel);
  92                 }
  93 
  94                 moreOrLessButton = new JButton(collapseIcon);
  95                 moreOrLessButton.setContentAreaFilled(false);
  96                 moreOrLessButton.setBorderPainted(false);
  97                 moreOrLessButton.setMargin(new Insets(0, 0, 0, 0));
  98                 moreOrLessButton.addActionListener(this);
  99                 String toolTip =
 100                     Messages.BORDERED_COMPONENT_MORE_OR_LESS_BUTTON_TOOLTIP;
 101                 moreOrLessButton.setToolTipText(toolTip);
 102                 borderLabel.add(moreOrLessButton);
 103                 borderLabel.setSize(borderLabel.getPreferredSize());
 104                 add(borderLabel);
 105             } else {
 106                 border = new TitledBorder(text);
 107             }
 108             setBorder(new CompoundBorder(new FocusBorder(this), border));
 109         } else {
 110             setBorder(new FocusBorder(this));
 111         }
 112         if (comp != null) {
 113             add(comp);
 114         }
 115     }
 116 
 117     public void setComponent(JComponent comp) {
 118         if (this.comp != null) {
 119             remove(this.comp);
 120         }
 121         this.comp = comp;
 122         if (!collapsed) {
 123             LayoutManager lm = getLayout();
 124             if (lm instanceof BorderLayout) {
 125                 add(comp, BorderLayout.CENTER);
 126             } else {
 127                 add(comp);
 128             }
 129         }
 130         revalidate();
 131     }
 132 
 133     public void setValueLabel(String str) {
 134         this.valueLabelStr = str;
 135         if (label != null) {
 136             label.setText(Resources.format(Messages.CURRENT_VALUE,
 137                                            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;


< prev index next >