1. /*
  2. * @(#)MetalDesktopIconUI.java 1.21 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.metal;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import javax.swing.*;
  11. import javax.swing.border.*;
  12. import javax.swing.plaf.*;
  13. import java.beans.*;
  14. import java.util.EventListener;
  15. import java.io.Serializable;
  16. import javax.swing.plaf.basic.BasicDesktopIconUI;
  17. /**
  18. * Metal desktop icon.
  19. *
  20. * @version 1.21 12/19/03
  21. * @author Steve Wilson
  22. */
  23. public class MetalDesktopIconUI extends BasicDesktopIconUI
  24. {
  25. JButton button;
  26. JLabel label;
  27. TitleListener titleListener;
  28. private int width;
  29. public static ComponentUI createUI(JComponent c) {
  30. return new MetalDesktopIconUI();
  31. }
  32. public MetalDesktopIconUI() {
  33. }
  34. protected void installDefaults() {
  35. super.installDefaults();
  36. LookAndFeel.installColorsAndFont(desktopIcon, "DesktopIcon.background", "DesktopIcon.foreground", "DesktopIcon.font");
  37. width = UIManager.getInt("DesktopIcon.width");
  38. }
  39. protected void installComponents() {
  40. frame = desktopIcon.getInternalFrame();
  41. Icon icon = frame.getFrameIcon();
  42. String title = frame.getTitle();
  43. button = new JButton (title, icon);
  44. button.addActionListener( new ActionListener() {
  45. public void actionPerformed(ActionEvent e) {
  46. deiconize(); }} );
  47. button.setFont(desktopIcon.getFont());
  48. button.setBackground(desktopIcon.getBackground());
  49. button.setForeground(desktopIcon.getForeground());
  50. int buttonH = button.getPreferredSize().height;
  51. Icon drag = new MetalBumps((buttonH3), buttonH,
  52. MetalLookAndFeel.getControlHighlight(),
  53. MetalLookAndFeel.getControlDarkShadow(),
  54. MetalLookAndFeel.getControl());
  55. label = new JLabel(drag);
  56. label.setBorder( new MatteBorder( 0, 2, 0, 1, desktopIcon.getBackground()) );
  57. desktopIcon.setLayout(new BorderLayout(2, 0));
  58. desktopIcon.add(button, BorderLayout.CENTER);
  59. desktopIcon.add(label, BorderLayout.WEST);
  60. }
  61. protected void uninstallComponents() {
  62. desktopIcon.setLayout(null);
  63. desktopIcon.remove(label);
  64. desktopIcon.remove(button);
  65. button = null;
  66. frame = null;
  67. }
  68. protected void installListeners() {
  69. super.installListeners();
  70. desktopIcon.getInternalFrame().addPropertyChangeListener(
  71. titleListener = new TitleListener());
  72. }
  73. protected void uninstallListeners() {
  74. desktopIcon.getInternalFrame().removePropertyChangeListener(
  75. titleListener);
  76. titleListener = null;
  77. super.uninstallListeners();
  78. }
  79. public Dimension getPreferredSize(JComponent c) {
  80. // Metal desktop icons can not be resized. Their dimensions should
  81. // always be the minimum size. See getMinimumSize(JComponent c).
  82. return getMinimumSize(c);
  83. }
  84. public Dimension getMinimumSize(JComponent c) {
  85. // For the metal desktop icon we will use the layout maanger to
  86. // determine the correct height of the component, but we want to keep
  87. // the width consistent according to the jlf spec.
  88. return new Dimension(width,
  89. desktopIcon.getLayout().minimumLayoutSize(desktopIcon).height);
  90. }
  91. public Dimension getMaximumSize(JComponent c) {
  92. // Metal desktop icons can not be resized. Their dimensions should
  93. // always be the minimum size. See getMinimumSize(JComponent c).
  94. return getMinimumSize(c);
  95. }
  96. class TitleListener implements PropertyChangeListener {
  97. public void propertyChange (PropertyChangeEvent e) {
  98. if (e.getPropertyName().equals("title")) {
  99. button.setText((String)e.getNewValue());
  100. }
  101. if (e.getPropertyName().equals("frameIcon")) {
  102. button.setIcon((Icon)e.getNewValue());
  103. }
  104. }
  105. }
  106. }