1 /*
2 * Copyright 2002-2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package javax.swing.plaf.synth;
27
28 import java.awt;
29 import java.awt.event;
30 import javax.swing;
31 import javax.swing.event;
32 import javax.swing.border;
33 import javax.swing.plaf;
34 import javax.swing.plaf.basic.BasicDesktopIconUI;
35 import java.beans;
36 import java.io.Serializable;
37 import sun.swing.plaf.synth.SynthUI;
38
39
40 /**
41 * Synth L&F for a minimized window on a desktop.
42 *
43 * @author Joshua Outwater
44 */
45 class SynthDesktopIconUI extends BasicDesktopIconUI implements SynthUI,
46 ActionListener, PropertyChangeListener {
47 private SynthStyle style;
48
49 public static ComponentUI createUI(JComponent c) {
50 return new SynthDesktopIconUI();
51 }
52
53 protected void installComponents() {
54 if (UIManager.getBoolean("InternalFrame.useTaskBar")) {
55 iconPane = new JToggleButton(frame.getTitle(), frame.getFrameIcon()) {
56 public String getToolTipText() {
57 return getText();
58 }
59
60 public JPopupMenu getComponentPopupMenu() {
61 return frame.getComponentPopupMenu();
62 }
63 };
64 ToolTipManager.sharedInstance().registerComponent(iconPane);
65 iconPane.setFont(desktopIcon.getFont());
66 iconPane.setBackground(desktopIcon.getBackground());
67 iconPane.setForeground(desktopIcon.getForeground());
68 } else {
69 iconPane = new SynthInternalFrameTitlePane(frame);
70 iconPane.setName("InternalFrame.northPane");
71 }
72 desktopIcon.setLayout(new BorderLayout());
73 desktopIcon.add(iconPane, BorderLayout.CENTER);
74 }
75
76 protected void installListeners() {
77 super.installListeners();
78 desktopIcon.addPropertyChangeListener(this);
79
80 if (iconPane instanceof JToggleButton) {
81 frame.addPropertyChangeListener(this);
82 ((JToggleButton)iconPane).addActionListener(this);
83 }
84 }
85
86 protected void uninstallListeners() {
87 if (iconPane instanceof JToggleButton) {
88 frame.removePropertyChangeListener(this);
89 }
90 desktopIcon.removePropertyChangeListener(this);
91 super.uninstallListeners();
92 }
93
94 protected void installDefaults() {
95 updateStyle(desktopIcon);
96 }
97
98 private void updateStyle(JComponent c) {
99 SynthContext context = getContext(c, ENABLED);
100 style = SynthLookAndFeel.updateStyle(context, this);
101 context.dispose();
102 }
103
104 protected void uninstallDefaults() {
105 SynthContext context = getContext(desktopIcon, ENABLED);
106 style.uninstallDefaults(context);
107 context.dispose();
108 style = null;
109 }
110
111 public SynthContext getContext(JComponent c) {
112 return getContext(c, getComponentState(c));
113 }
114
115 private SynthContext getContext(JComponent c, int state) {
116 Region region = getRegion(c);
117 return SynthContext.getContext(SynthContext.class, c, region,
118 style, state);
119 }
120
121 private int getComponentState(JComponent c) {
122 return SynthLookAndFeel.getComponentState(c);
123 }
124
125 Region getRegion(JComponent c) {
126 return SynthLookAndFeel.getRegion(c);
127 }
128
129 public void update(Graphics g, JComponent c) {
130 SynthContext context = getContext(c);
131
132 SynthLookAndFeel.update(context, g);
133 context.getPainter().paintDesktopIconBackground(context, g, 0, 0,
134 c.getWidth(), c.getHeight());
135 paint(context, g);
136 context.dispose();
137 }
138
139 public void paint(Graphics g, JComponent c) {
140 SynthContext context = getContext(c);
141
142 paint(context, g);
143 context.dispose();
144 }
145
146 protected void paint(SynthContext context, Graphics g) {
147 }
148
149 public void paintBorder(SynthContext context, Graphics g, int x,
150 int y, int w, int h) {
151 context.getPainter().paintDesktopIconBorder(context, g, x, y, w, h);
152 }
153
154 public void actionPerformed(ActionEvent evt) {
155 if (evt.getSource() instanceof JToggleButton) {
156 // Either iconify the frame or deiconify and activate it.
157 JToggleButton button = (JToggleButton)evt.getSource();
158 try {
159 boolean selected = button.isSelected();
160 if (!selected && !frame.isIconifiable()) {
161 button.setSelected(true);
162 } else {
163 frame.setIcon(!selected);
164 if (selected) {
165 frame.setSelected(true);
166 }
167 }
168 } catch (PropertyVetoException e2) {
169 }
170 }
171 }
172
173 public void propertyChange(PropertyChangeEvent evt) {
174 if (evt.getSource() instanceof JInternalFrame.JDesktopIcon) {
175 if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
176 updateStyle((JInternalFrame.JDesktopIcon)evt.getSource());
177 }
178 } else if (evt.getSource() instanceof JInternalFrame) {
179 JInternalFrame frame = (JInternalFrame)evt.getSource();
180 if (iconPane instanceof JToggleButton) {
181 JToggleButton button = (JToggleButton)iconPane;
182 String prop = evt.getPropertyName();
183 if (prop == "title") {
184 button.setText((String)evt.getNewValue());
185 } else if (prop == "frameIcon") {
186 button.setIcon((Icon)evt.getNewValue());
187 } else if (prop == JInternalFrame.IS_ICON_PROPERTY ||
188 prop == JInternalFrame.IS_SELECTED_PROPERTY) {
189 button.setSelected(!frame.isIcon() && frame.isSelected());
190 }
191 }
192 }
193 }
194 }