1 7 8 package com.sun.java.swing.plaf.motif; 9 10 import javax.swing.*; 11 import java.awt.Rectangle ; 12 import java.awt.Dimension ; 13 import java.awt.Insets ; 14 import java.awt.Color ; 15 import java.awt.Graphics ; 16 import java.awt.Component ; 17 import java.awt.Point ; 18 import javax.swing.plaf.*; 19 import java.io.Serializable ; 20 21 33 public class MotifDesktopPaneUI extends javax.swing.plaf.basic.BasicDesktopPaneUI 34 { 35 36 public static ComponentUI createUI(JComponent d) { 38 return new MotifDesktopPaneUI(); 39 } 40 41 public MotifDesktopPaneUI() { 42 } 43 44 protected void installDesktopManager() { 45 desktopManager = desktop.getDesktopManager(); 46 if(desktopManager == null) { 47 desktopManager = new MotifDesktopManager(); 48 desktop.setDesktopManager(desktopManager); 49 ((MotifDesktopManager)desktopManager).adjustIcons(desktop); 50 } 51 } 52 53 public Insets getInsets(JComponent c) {return new Insets (0,0,0,0);} 54 55 private class DragPane extends JComponent { 59 public void paint(Graphics g) { 60 g.setColor(Color.darkGray); 61 g.drawRect(0, 0, getWidth()-1, getHeight()-1); 62 } 63 }; 64 65 private class MotifDesktopManager extends DefaultDesktopManager implements Serializable , UIResource { 69 JComponent dragPane; 70 boolean usingDragPane = false; 71 private transient JLayeredPane layeredPaneForDragPane; 72 int iconWidth, iconHeight; 73 74 public void setBoundsForFrame(JComponent f, int newX, int newY, 76 int newWidth, int newHeight) { 77 if(!usingDragPane) { 78 boolean didResize; 79 didResize = (f.getWidth() != newWidth || f.getHeight() != newHeight); 80 Rectangle r = f.getBounds(); 81 f.setBounds(newX, newY, newWidth, newHeight); 82 SwingUtilities.computeUnion(newX, newY, newWidth, newHeight, r); 83 f.getParent().repaint(r.x, r.y, r.width, r.height); 84 if(didResize) { 85 f.validate(); 86 } 87 } else { 88 Rectangle r = dragPane.getBounds(); 89 dragPane.setBounds(newX, newY, newWidth, newHeight); 90 SwingUtilities.computeUnion(newX, newY, newWidth, newHeight, r); 91 dragPane.getParent().repaint(r.x, r.y, r.width, r.height); 92 } 93 } 94 95 public void beginDraggingFrame(JComponent f) { 96 usingDragPane = false; 97 if(f.getParent() instanceof JLayeredPane) { 98 if(dragPane == null) 99 dragPane = new DragPane(); 100 layeredPaneForDragPane = (JLayeredPane)f.getParent(); 101 layeredPaneForDragPane.setLayer(dragPane, Integer.MAX_VALUE); 102 dragPane.setBounds(f.getX(), f.getY(), f.getWidth(), f.getHeight()); 103 layeredPaneForDragPane.add(dragPane); 104 usingDragPane = true; 105 } 106 } 107 108 public void dragFrame(JComponent f, int newX, int newY) { 109 setBoundsForFrame(f, newX, newY, f.getWidth(), f.getHeight()); 110 } 111 112 public void endDraggingFrame(JComponent f) { 113 if(usingDragPane) { 114 layeredPaneForDragPane.remove(dragPane); 115 usingDragPane = false; 116 if (f instanceof JInternalFrame) { 117 setBoundsForFrame(f, dragPane.getX(), dragPane.getY(), 118 dragPane.getWidth(), dragPane.getHeight()); 119 } else if (f instanceof JInternalFrame.JDesktopIcon) { 120 adjustBoundsForIcon((JInternalFrame.JDesktopIcon)f, 121 dragPane.getX(), dragPane.getY()); 122 } 123 } 124 } 125 126 public void beginResizingFrame(JComponent f, int direction) { 127 usingDragPane = false; 128 if(f.getParent() instanceof JLayeredPane) { 129 if(dragPane == null) 130 dragPane = new DragPane(); 131 JLayeredPane p = (JLayeredPane)f.getParent(); 132 p.setLayer(dragPane, Integer.MAX_VALUE); 133 dragPane.setBounds(f.getX(), f.getY(), 134 f.getWidth(), f.getHeight()); 135 p.add(dragPane); 136 usingDragPane = true; 137 } 138 } 139 140 public void resizeFrame(JComponent f, int newX, int newY, 141 int newWidth, int newHeight) { 142 setBoundsForFrame(f, newX, newY, newWidth, newHeight); 143 } 144 145 public void endResizingFrame(JComponent f) { 146 if(usingDragPane) { 147 JLayeredPane p = (JLayeredPane)f.getParent(); 148 p.remove(dragPane); 149 usingDragPane = false; 150 setBoundsForFrame(f, dragPane.getX(), dragPane.getY(), 151 dragPane.getWidth(), dragPane.getHeight()); 152 } 153 } 154 155 public void iconifyFrame(JInternalFrame f) { 156 JInternalFrame.JDesktopIcon icon = f.getDesktopIcon(); 157 Point p = icon.getLocation(); 158 adjustBoundsForIcon(icon, p.x, p.y); 159 super.iconifyFrame(f); 160 } 161 162 166 protected void adjustIcons(JDesktopPane desktop) { 167 JInternalFrame.JDesktopIcon icon = new JInternalFrame.JDesktopIcon( 169 new JInternalFrame()); 170 Dimension iconSize = icon.getPreferredSize(); 171 iconWidth = iconSize.width; 172 iconHeight = iconSize.height; 173 174 JInternalFrame[] frames = desktop.getAllFrames(); 175 for (int i=0; i<frames.length; i++) { 176 icon = frames[i].getDesktopIcon(); 177 Point ip = icon.getLocation(); 178 adjustBoundsForIcon(icon, ip.x, ip.y); 179 } 180 } 181 182 186 protected void adjustBoundsForIcon(JInternalFrame.JDesktopIcon icon, 187 int x, int y) { 188 JDesktopPane c = icon.getDesktopPane(); 189 190 int maxy = c.getHeight(); 191 int w = iconWidth; 192 int h = iconHeight; 193 c.repaint(x, y, w, h); 194 x = x < 0 ? 0 : x; 195 y = y < 0 ? 0 : y; 196 197 200 y = y >= maxy ? (maxy - 1) : y; 201 202 203 int lx = (x / w) * w; 204 int ygap = maxy % h; 205 int ly = ((y-ygap) / h) * h + ygap; 206 207 208 int dx = x - lx; 209 int dy = y - ly; 210 211 212 x = dx < w/2 ? lx: lx + w; 213 y = dy < h/2 ? ly: ((ly + h) < maxy ? ly + h: ly); 214 215 while (getIconAt(c, icon, x, y) != null) { 216 x += w; 217 } 218 219 220 if (x > c.getWidth()) { 221 return; 222 } 223 if (icon.getParent() != null) { 224 setBoundsForFrame(icon, x, y, w, h); 225 } else { 226 icon.setLocation(x, y); 227 } 228 } 229 230 protected JInternalFrame.JDesktopIcon getIconAt(JDesktopPane desktop, 231 JInternalFrame.JDesktopIcon icon, int x, int y) { 232 233 JInternalFrame.JDesktopIcon currentIcon = null; 234 Component [] components = desktop.getComponents(); 235 236 for (int i=0; i<components.length; i++) { 237 Component comp = components[i]; 238 if (comp instanceof JInternalFrame.JDesktopIcon && 239 comp != icon) { 240 241 Point p = comp.getLocation(); 242 if (p.x == x && p.y == y) { 243 return (JInternalFrame.JDesktopIcon)comp; 244 } 245 } 246 } 247 return null; 248 } 249 }; } 251 | Popular Tags |