001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Vadim L. Bogdanov
020: * @version $Revision$
021: */package javax.swing.plaf.metal;
022:
023: import java.awt.BorderLayout;
024: import java.awt.Component;
025: import java.awt.Dimension;
026: import java.awt.Graphics;
027: import java.awt.event.ActionEvent;
028: import java.beans.PropertyChangeEvent;
029: import java.beans.PropertyChangeListener;
030:
031: import javax.swing.AbstractAction;
032: import javax.swing.BorderFactory;
033: import javax.swing.Icon;
034: import javax.swing.JButton;
035: import javax.swing.JComponent;
036: import javax.swing.JInternalFrame;
037: import javax.swing.JLabel;
038: import javax.swing.LookAndFeel;
039: import javax.swing.SwingUtilities;
040: import javax.swing.UIManager;
041:
042: import javax.swing.plaf.ComponentUI;
043: import javax.swing.plaf.basic.BasicDesktopIconUI;
044:
045: public class MetalDesktopIconUI extends BasicDesktopIconUI {
046:
047: public static ComponentUI createUI(final JComponent c) {
048: return new MetalDesktopIconUI();
049: }
050:
051: private class DeiconizeAction extends AbstractAction {
052: public void actionPerformed(final ActionEvent e) {
053: deiconize();
054: }
055: }
056:
057: private class InternalFramePropertyChangeListener implements
058: PropertyChangeListener {
059:
060: public void propertyChange(final PropertyChangeEvent e) {
061: if (JInternalFrame.TITLE_PROPERTY.equals(e
062: .getPropertyName())
063: || JInternalFrame.FRAME_ICON_PROPERTY.equals(e
064: .getPropertyName())) {
065: loadInternalFrameProperties();
066: }
067: }
068: }
069:
070: /*
071: * This is a small icon to the left of the button.
072: */
073: private class DecorationIcon implements Icon {
074: public int getIconHeight() {
075: return iconPane.getHeight();
076: }
077:
078: public int getIconWidth() {
079: return 8;
080: }
081:
082: public void paintIcon(final Component c, final Graphics g,
083: final int x, final int y) {
084: MetalBumps.paintBumps(g, SwingUtilities.calculateInnerArea(
085: decoration, null), MetalLookAndFeel
086: .getControlDarkShadow(), MetalLookAndFeel
087: .getControlHighlight());
088: }
089: }
090:
091: private int desktopIconWidth;
092:
093: private JLabel decoration;
094:
095: private InternalFramePropertyChangeListener internalFramePropertyChangeListener;
096:
097: /**
098: */
099: public MetalDesktopIconUI() {
100: }
101:
102: /**
103: */
104: public Dimension getMaximumSize(final JComponent c) {
105: Dimension size = super .getMaximumSize(c);
106: size.width = desktopIconWidth;
107: return size;
108: }
109:
110: /**
111: */
112: public Dimension getMinimumSize(final JComponent c) {
113: Dimension size = super .getMinimumSize(c);
114: size.width = desktopIconWidth;
115: return size;
116: }
117:
118: /**
119: */
120: public Dimension getPreferredSize(final JComponent c) {
121: Dimension size = super .getPreferredSize(c);
122: size.width = desktopIconWidth;
123: return size;
124: }
125:
126: /**
127: */
128: protected void installComponents() {
129: iconPane = new JButton(new DeiconizeAction());
130:
131: loadInternalFrameProperties();
132: desktopIcon.add(iconPane, BorderLayout.CENTER);
133:
134: decoration = new JLabel(new DecorationIcon());
135: decoration.setBorder(BorderFactory
136: .createEmptyBorder(1, 1, 1, 2));
137: desktopIcon.add(decoration, BorderLayout.WEST);
138: }
139:
140: /**
141: */
142: protected void uninstallComponents() {
143: desktopIcon.remove(decoration);
144: desktopIcon.remove(iconPane);
145: }
146:
147: /**
148: */
149: protected void installDefaults() {
150: super .installDefaults();
151:
152: LookAndFeel.installColorsAndFont(desktopIcon,
153: "DesktopIcon.background", "DesktopIcon.foreground",
154: "DesktopIcon.font");
155: desktopIconWidth = UIManager.getInt("DesktopIcon.width");
156: LookAndFeel
157: .installProperty(desktopIcon, "opaque", Boolean.TRUE);
158: }
159:
160: /**
161: */
162: protected void installListeners() {
163: super .installListeners();
164:
165: if (internalFramePropertyChangeListener == null) {
166: internalFramePropertyChangeListener = new InternalFramePropertyChangeListener();
167: }
168: desktopIcon.getInternalFrame().addPropertyChangeListener(
169: internalFramePropertyChangeListener);
170: }
171:
172: /**
173: */
174: protected void uninstallListeners() {
175: super .uninstallListeners();
176: desktopIcon.getInternalFrame().removePropertyChangeListener(
177: internalFramePropertyChangeListener);
178: }
179:
180: private void loadInternalFrameProperties() {
181: ((JButton) iconPane).setIcon(desktopIcon.getInternalFrame()
182: .getFrameIcon());
183: ((JButton) iconPane).setText(desktopIcon.getInternalFrame()
184: .getTitle());
185: }
186: }
|