1 /* 2 * Copyright (c) 1997, 2005, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 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 com.sun.java.swing.plaf.motif; 27 28 import sun.swing.SwingUtilities2; 29 import java.awt.*; 30 import java.awt.event.*; 31 import javax.swing.*; 32 import javax.swing.border.*; 33 import javax.swing.plaf.*; 34 import javax.swing.plaf.basic.*; 35 import java.beans.*; 36 import java.util.EventListener; 37 import java.io.Serializable; 38 39 import sun.awt.AWTAccessor; 40 import sun.awt.AWTAccessor.MouseEventAccessor; 41 42 /** 43 * Motif rendition of the component. 44 * 45 * @author Thomas Ball 46 * @author Rich Schiavi 47 */ 48 public class MotifDesktopIconUI extends BasicDesktopIconUI 49 { 50 protected DesktopIconActionListener desktopIconActionListener; 51 protected DesktopIconMouseListener desktopIconMouseListener; 52 53 protected Icon defaultIcon; 54 protected IconButton iconButton; 55 protected IconLabel iconLabel; 56 57 // This is only used for its system menu, but we need a reference to it so 58 // we can remove its listeners. 59 private MotifInternalFrameTitlePane sysMenuTitlePane; 60 61 JPopupMenu systemMenu; 62 EventListener mml; 63 64 final static int LABEL_HEIGHT = 18; 65 final static int LABEL_DIVIDER = 4; // padding between icon and label 66 67 final static Font defaultTitleFont = 68 new Font(Font.SANS_SERIF, Font.PLAIN, 12); 69 70 public static ComponentUI createUI(JComponent c) { 71 return new MotifDesktopIconUI(); 72 } 73 74 public MotifDesktopIconUI() { 75 } 76 77 protected void installDefaults(){ 78 super.installDefaults(); 79 setDefaultIcon(UIManager.getIcon("DesktopIcon.icon")); 80 iconButton = createIconButton(defaultIcon); 81 // An underhanded way of creating a system popup menu. 82 sysMenuTitlePane = new MotifInternalFrameTitlePane(frame); 83 systemMenu = sysMenuTitlePane.getSystemMenu(); 84 85 MotifBorders.FrameBorder border = new MotifBorders.FrameBorder(desktopIcon); 86 desktopIcon.setLayout(new BorderLayout()); 87 iconButton.setBorder(border); 88 desktopIcon.add(iconButton, BorderLayout.CENTER); 89 iconLabel = createIconLabel(frame); 90 iconLabel.setBorder(border); 91 desktopIcon.add(iconLabel, BorderLayout.SOUTH); 92 desktopIcon.setSize(desktopIcon.getPreferredSize()); 93 desktopIcon.validate(); 94 JLayeredPane.putLayer(desktopIcon, JLayeredPane.getLayer(frame)); 95 } 96 97 protected void installComponents(){ 98 } 99 100 protected void uninstallComponents(){ 101 } 102 103 protected void installListeners(){ 104 super.installListeners(); 105 desktopIconActionListener = createDesktopIconActionListener(); 106 desktopIconMouseListener = createDesktopIconMouseListener(); 107 iconButton.addActionListener(desktopIconActionListener); 108 iconButton.addMouseListener(desktopIconMouseListener); 109 iconLabel.addMouseListener(desktopIconMouseListener); 110 } 111 112 JInternalFrame.JDesktopIcon getDesktopIcon(){ 113 return desktopIcon; 114 } 115 116 void setDesktopIcon(JInternalFrame.JDesktopIcon d){ 117 desktopIcon = d; 118 } 119 120 JInternalFrame getFrame(){ 121 return frame; 122 } 123 124 void setFrame(JInternalFrame f){ 125 frame = f ; 126 } 127 128 protected void showSystemMenu(){ 129 systemMenu.show(iconButton, 0, getDesktopIcon().getHeight()); 130 } 131 132 protected void hideSystemMenu(){ 133 systemMenu.setVisible(false); 134 } 135 136 protected IconLabel createIconLabel(JInternalFrame frame){ 137 return new IconLabel(frame); 138 } 139 140 protected IconButton createIconButton(Icon i){ 141 return new IconButton(i); 142 } 143 144 protected DesktopIconActionListener createDesktopIconActionListener(){ 145 return new DesktopIconActionListener(); 146 } 147 148 protected DesktopIconMouseListener createDesktopIconMouseListener(){ 149 return new DesktopIconMouseListener(); 150 } 151 152 protected void uninstallDefaults(){ 153 super.uninstallDefaults(); 154 desktopIcon.setLayout(null); 155 desktopIcon.remove(iconButton); 156 desktopIcon.remove(iconLabel); 157 } 158 159 protected void uninstallListeners(){ 160 super.uninstallListeners(); 161 iconButton.removeActionListener(desktopIconActionListener); 162 iconButton.removeMouseListener(desktopIconMouseListener); 163 sysMenuTitlePane.uninstallListeners(); 164 } 165 166 public Dimension getMinimumSize(JComponent c) { 167 JInternalFrame iframe = desktopIcon.getInternalFrame(); 168 169 int w = defaultIcon.getIconWidth(); 170 int h = defaultIcon.getIconHeight() + LABEL_HEIGHT + LABEL_DIVIDER; 171 172 Border border = iframe.getBorder(); 173 if(border != null) { 174 w += border.getBorderInsets(iframe).left + 175 border.getBorderInsets(iframe).right; 176 h += border.getBorderInsets(iframe).bottom + 177 border.getBorderInsets(iframe).top; 178 } 179 180 return new Dimension(w, h); 181 } 182 183 public Dimension getPreferredSize(JComponent c) { 184 return getMinimumSize(c); 185 } 186 187 public Dimension getMaximumSize(JComponent c){ 188 return getMinimumSize(c); 189 } 190 191 /** 192 * Returns the default desktop icon. 193 */ 194 public Icon getDefaultIcon() { 195 return defaultIcon; 196 } 197 198 /** 199 * Sets the icon used as the default desktop icon. 200 */ 201 public void setDefaultIcon(Icon newIcon) { 202 defaultIcon = newIcon; 203 } 204 205 protected class IconLabel extends JPanel { 206 JInternalFrame frame; 207 208 IconLabel(JInternalFrame f) { 209 super(); 210 this.frame = f; 211 setFont(defaultTitleFont); 212 213 // Forward mouse events to titlebar for moves. 214 addMouseMotionListener(new MouseMotionListener() { 215 public void mouseDragged(MouseEvent e) { 216 forwardEventToParent(e); 217 } 218 public void mouseMoved(MouseEvent e) { 219 forwardEventToParent(e); 220 } 221 }); 222 addMouseListener(new MouseListener() { 223 public void mouseClicked(MouseEvent e) { 224 forwardEventToParent(e); 225 } 226 public void mousePressed(MouseEvent e) { 227 forwardEventToParent(e); 228 } 229 public void mouseReleased(MouseEvent e) { 230 forwardEventToParent(e); 231 } 232 public void mouseEntered(MouseEvent e) { 233 forwardEventToParent(e); 234 } 235 public void mouseExited(MouseEvent e) { 236 forwardEventToParent(e); 237 } 238 }); 239 } 240 241 void forwardEventToParent(MouseEvent e) { 242 MouseEvent newEvent = new MouseEvent( 243 getParent(), e.getID(), e.getWhen(), e.getModifiers(), 244 e.getX(), e.getY(), e.getXOnScreen(), 245 e.getYOnScreen(), e.getClickCount(), 246 e.isPopupTrigger(), MouseEvent.NOBUTTON); 247 MouseEventAccessor meAccessor = AWTAccessor.getMouseEventAccessor(); 248 meAccessor.setCausedByTouchEvent(newEvent, 249 meAccessor.isCausedByTouchEvent(e)); 250 getParent().dispatchEvent(newEvent); 251 } 252 253 public boolean isFocusTraversable() { 254 return false; 255 } 256 257 public Dimension getMinimumSize() { 258 return new Dimension(defaultIcon.getIconWidth() + 1, 259 LABEL_HEIGHT + LABEL_DIVIDER); 260 } 261 262 public Dimension getPreferredSize() { 263 String title = frame.getTitle(); 264 FontMetrics fm = frame.getFontMetrics(defaultTitleFont); 265 int w = 4; 266 if (title != null) { 267 w += SwingUtilities2.stringWidth(frame, fm, title); 268 } 269 return new Dimension(w, LABEL_HEIGHT + LABEL_DIVIDER); 270 } 271 272 public void paint(Graphics g) { 273 super.paint(g); 274 275 // touch-up frame 276 int maxX = getWidth() - 1; 277 Color shadow = 278 UIManager.getColor("inactiveCaptionBorder").darker().darker(); 279 g.setColor(shadow); 280 g.setClip(0, 0, getWidth(), getHeight()); 281 g.drawLine(maxX - 1, 1, maxX - 1, 1); 282 g.drawLine(maxX, 0, maxX, 0); 283 284 // fill background 285 g.setColor(UIManager.getColor("inactiveCaption")); 286 g.fillRect(2, 1, maxX - 3, LABEL_HEIGHT + 1); 287 288 // draw text -- clipping to truncate text like CDE/Motif 289 g.setClip(2, 1, maxX - 4, LABEL_HEIGHT); 290 int y = LABEL_HEIGHT - SwingUtilities2.getFontMetrics(frame, g). 291 getDescent(); 292 g.setColor(UIManager.getColor("inactiveCaptionText")); 293 String title = frame.getTitle(); 294 if (title != null) { 295 SwingUtilities2.drawString(frame, g, title, 4, y); 296 } 297 } 298 } 299 300 protected class IconButton extends JButton { 301 Icon icon; 302 303 IconButton(Icon icon) { 304 super(icon); 305 this.icon = icon; 306 // Forward mouse events to titlebar for moves. 307 addMouseMotionListener(new MouseMotionListener() { 308 public void mouseDragged(MouseEvent e) { 309 forwardEventToParent(e); 310 } 311 public void mouseMoved(MouseEvent e) { 312 forwardEventToParent(e); 313 } 314 }); 315 addMouseListener(new MouseListener() { 316 public void mouseClicked(MouseEvent e) { 317 forwardEventToParent(e); 318 } 319 public void mousePressed(MouseEvent e) { 320 forwardEventToParent(e); 321 } 322 public void mouseReleased(MouseEvent e) { 323 if (!systemMenu.isShowing()) { 324 forwardEventToParent(e); 325 } 326 } 327 public void mouseEntered(MouseEvent e) { 328 forwardEventToParent(e); 329 } 330 public void mouseExited(MouseEvent e) { 331 forwardEventToParent(e); 332 } 333 }); 334 } 335 336 void forwardEventToParent(MouseEvent e) { 337 MouseEvent newEvent = new MouseEvent( 338 getParent(), e.getID(), e.getWhen(), e.getModifiers(), 339 e.getX(), e.getY(), e.getXOnScreen(), e.getYOnScreen(), 340 e.getClickCount(), e.isPopupTrigger(), MouseEvent.NOBUTTON ); 341 MouseEventAccessor meAccessor = AWTAccessor.getMouseEventAccessor(); 342 meAccessor.setCausedByTouchEvent(newEvent, 343 meAccessor.isCausedByTouchEvent(e)); 344 getParent().dispatchEvent(newEvent); 345 } 346 347 public boolean isFocusTraversable() { 348 return false; 349 } 350 } 351 352 353 protected class DesktopIconActionListener implements ActionListener { 354 public void actionPerformed(ActionEvent e){ 355 systemMenu.show(iconButton, 0, getDesktopIcon().getHeight()); 356 } 357 } 358 359 protected class DesktopIconMouseListener extends MouseAdapter { 360 // if we drag or move we should deengage the popup 361 public void mousePressed(MouseEvent e){ 362 if (e.getClickCount() > 1) { 363 try { 364 getFrame().setIcon(false); 365 } catch (PropertyVetoException e2){ } 366 systemMenu.setVisible(false); 367 /* the mouse release will not get reported correctly, 368 because the icon will no longer be in the hierarchy; 369 maybe that should be fixed, but until it is, we need 370 to do the required cleanup here. */ 371 getFrame().getDesktopPane().getDesktopManager().endDraggingFrame((JComponent)e.getSource()); 372 } 373 } 374 } 375 }