1 package org.sape.carbon.services.console; 2 3 4 import java.awt.Component ; 5 import java.awt.Dimension ; 6 import java.awt.Insets ; 7 import java.awt.Point ; 8 import java.beans.PropertyVetoException ; 9 10 import javax.swing.DefaultDesktopManager ; 11 import javax.swing.JComponent ; 12 import javax.swing.JDesktopPane ; 13 import javax.swing.JInternalFrame ; 14 import javax.swing.JScrollPane ; 15 import javax.swing.JViewport ; 16 17 18 29 public class MDIDesktopPane extends JDesktopPane { 30 private static int FRAME_OFFSET=20; 31 private MDIDesktopManager manager; 32 33 public MDIDesktopPane() { 34 manager=new MDIDesktopManager(this); 35 setDesktopManager(manager); 36 setDragMode(JDesktopPane.OUTLINE_DRAG_MODE); 37 } 38 39 public void setBounds(int x, int y, int w, int h) { 40 super.setBounds(x,y,w,h); 41 checkDesktopSize(); 42 } 43 44 public Component add(JInternalFrame frame) { 45 JInternalFrame [] array = getAllFrames(); 46 Point p; 47 int w; 48 int h; 49 50 Component retval=super.add(frame); 51 checkDesktopSize(); 52 if (array.length > 0) { 53 p = array[0].getLocation(); 54 p.x = p.x + FRAME_OFFSET; 55 p.y = p.y + FRAME_OFFSET; 56 } 57 else { 58 p = new Point (0, 0); 59 } 60 frame.setLocation(p.x, p.y); 61 if (frame.isResizable()) { 62 w = getWidth() - (getWidth()/3); 63 h = getHeight() - (getHeight()/3); 64 if (w < frame.getMinimumSize().getWidth()) w = (int)frame.getMinimumSize().getWidth(); 65 if (h < frame.getMinimumSize().getHeight()) h = (int)frame.getMinimumSize().getHeight(); 66 frame.setSize(w, h); 67 } 68 moveToFront(frame); 69 frame.setVisible(true); 70 try { 71 frame.setSelected(true); 72 } catch (PropertyVetoException e) { 73 frame.toBack(); 74 } 75 return retval; 76 } 77 78 public void remove(Component c) { 79 super.remove(c); 80 checkDesktopSize(); 81 } 82 83 86 public void cascadeFrames() { 87 int x = 0; 88 int y = 0; 89 JInternalFrame allFrames[] = getAllFrames(); 90 91 manager.setNormalSize(); 92 int frameHeight = (getBounds().height - 5) - allFrames.length * FRAME_OFFSET; 93 int frameWidth = (getBounds().width - 5) - allFrames.length * FRAME_OFFSET; 94 for (int i = allFrames.length - 1; i >= 0; i--) { 95 allFrames[i].setSize(frameWidth,frameHeight); 96 allFrames[i].setLocation(x,y); 97 x = x + FRAME_OFFSET; 98 y = y + FRAME_OFFSET; 99 } 100 } 101 102 105 public void tileFrames() { 106 java.awt.Component allFrames[] = getAllFrames(); 107 manager.setNormalSize(); 108 int frameHeight = getBounds().height/allFrames.length; 109 int y = 0; 110 for (int i = 0; i < allFrames.length; i++) { 111 allFrames[i].setSize(getBounds().width,frameHeight); 112 allFrames[i].setLocation(0,y); 113 y = y + frameHeight; 114 } 115 } 116 117 121 public void setAllSize(Dimension d){ 122 setMinimumSize(d); 123 setMaximumSize(d); 124 setPreferredSize(d); 125 } 126 127 131 public void setAllSize(int width, int height){ 132 setAllSize(new Dimension (width,height)); 133 } 134 135 private void checkDesktopSize() { 136 if (getParent()!=null&&isVisible()) manager.resizeDesktop(); 137 } 138 } 139 140 144 class MDIDesktopManager extends DefaultDesktopManager { 145 private MDIDesktopPane desktop; 146 147 public MDIDesktopManager(MDIDesktopPane desktop) { 148 this.desktop = desktop; 149 } 150 151 public void endResizingFrame(JComponent f) { 152 super.endResizingFrame(f); 153 resizeDesktop(); 154 } 155 156 public void endDraggingFrame(JComponent f) { 157 super.endDraggingFrame(f); 158 resizeDesktop(); 159 } 160 161 public void setNormalSize() { 162 JScrollPane scrollPane=getScrollPane(); 163 int x = 0; 164 int y = 0; 165 Insets scrollInsets = getScrollPaneInsets(); 166 167 if (scrollPane != null) { 168 Dimension d = scrollPane.getVisibleRect().getSize(); 169 if (scrollPane.getBorder() != null) { 170 d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right, 171 d.getHeight() - scrollInsets.top - scrollInsets.bottom); 172 } 173 174 d.setSize(d.getWidth() - 20, d.getHeight() - 20); 175 desktop.setAllSize(x,y); 176 scrollPane.invalidate(); 177 scrollPane.validate(); 178 } 179 } 180 181 private Insets getScrollPaneInsets() { 182 JScrollPane scrollPane=getScrollPane(); 183 if (scrollPane==null) return new Insets (0,0,0,0); 184 else return getScrollPane().getBorder().getBorderInsets(scrollPane); 185 } 186 187 private JScrollPane getScrollPane() { 188 if (desktop.getParent() instanceof JViewport ) { 189 JViewport viewPort = (JViewport )desktop.getParent(); 190 if (viewPort.getParent() instanceof JScrollPane ) 191 return (JScrollPane )viewPort.getParent(); 192 } 193 return null; 194 } 195 196 protected void resizeDesktop() { 197 int x = 0; 198 int y = 0; 199 JScrollPane scrollPane = getScrollPane(); 200 Insets scrollInsets = getScrollPaneInsets(); 201 202 if (scrollPane != null) { 203 JInternalFrame allFrames[] = desktop.getAllFrames(); 204 for (int i = 0; i < allFrames.length; i++) { 205 if (allFrames[i].getX()+allFrames[i].getWidth()>x) { 206 x = allFrames[i].getX() + allFrames[i].getWidth(); 207 } 208 if (allFrames[i].getY()+allFrames[i].getHeight()>y) { 209 y = allFrames[i].getY() + allFrames[i].getHeight(); 210 } 211 } 212 Dimension d=scrollPane.getVisibleRect().getSize(); 213 if (scrollPane.getBorder() != null) { 214 d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right, 215 d.getHeight() - scrollInsets.top - scrollInsets.bottom); 216 } 217 218 if (x <= d.getWidth()) x = ((int)d.getWidth()) - 20; 219 if (y <= d.getHeight()) y = ((int)d.getHeight()) - 20; 220 desktop.setAllSize(x,y); 221 scrollPane.invalidate(); 222 scrollPane.validate(); 223 } 224 } 225 } | Popular Tags |