1 22 23 package org.gjt.sp.jedit.gui; 24 25 import java.awt.CardLayout ; 27 import java.awt.Cursor ; 28 import java.awt.Dimension ; 29 import java.awt.Insets ; 30 import java.awt.Point ; 31 import java.awt.event.MouseAdapter ; 32 import java.awt.event.MouseEvent ; 33 import java.awt.event.MouseMotionListener ; 34 35 import javax.swing.JPanel ; 36 import javax.swing.border.Border ; 37 39 42 class DockablePanel extends JPanel 43 { 44 private PanelWindowContainer panel; 45 private DockableWindowManager wm; 46 47 DockablePanel(PanelWindowContainer panel) 49 { 50 super(new CardLayout ()); 51 52 this.panel = panel; 53 this.wm = panel.getDockableWindowManager(); 54 55 ResizeMouseHandler resizeMouseHandler = new ResizeMouseHandler(); 56 addMouseListener(resizeMouseHandler); 57 addMouseMotionListener(resizeMouseHandler); 58 } 60 PanelWindowContainer getWindowContainer() 62 { 63 return panel; 64 } 66 void showDockable(String name) 68 { 69 ((CardLayout )getLayout()).show(this,name); 70 } 72 public Dimension getMinimumSize() 74 { 75 return new Dimension (0,0); 76 } 78 public Dimension getPreferredSize() 80 { 81 final String position = panel.getPosition(); 82 final int dimension = panel.getDimension(); 83 84 if(panel.getCurrent() == null) 85 return new Dimension (0,0); 86 else 87 { 88 if(position.equals(DockableWindowManager.TOP) 89 || position.equals(DockableWindowManager.BOTTOM)) 90 { 91 if(dimension <= 0) 92 { 93 int height = super.getPreferredSize().height; 94 panel.setDimension(height); 95 } 96 return new Dimension (0, 97 dimension + PanelWindowContainer 98 .SPLITTER_WIDTH); 99 } 100 else 101 { 102 if(dimension <= 0) 103 { 104 int width = super.getPreferredSize().width; 105 panel.setDimension(width); 106 } 107 return new Dimension (dimension + 108 PanelWindowContainer.SPLITTER_WIDTH, 109 0); 110 } 111 } 112 } 114 public void setBounds(int x, int y, int width, int height) 116 { 117 final String position = panel.getPosition(); 118 final int dimension = panel.getDimension(); 119 120 if(position.equals(DockableWindowManager.TOP) || 121 position.equals(DockableWindowManager.BOTTOM)) 122 { 123 if(dimension != 0 && height <= PanelWindowContainer.SPLITTER_WIDTH) 124 panel.show(null); 125 else 126 panel.setDimension(height); 127 } 128 else 129 { 130 if(dimension != 0 && width <= PanelWindowContainer.SPLITTER_WIDTH) 131 panel.show(null); 132 else 133 panel.setDimension(width); 134 } 135 136 super.setBounds(x,y,width,height); 137 } 139 class ResizeMouseHandler extends MouseAdapter implements MouseMotionListener 141 { 142 143 boolean canDrag; 144 Point dragStart; 145 146 public void mousePressed(MouseEvent evt) 148 { 149 if(canDrag) 150 { 151 wm.setResizePos(panel.getDimension(),panel); 152 dragStart = evt.getPoint(); 153 } 154 } 156 public void mouseReleased(MouseEvent evt) 158 { 159 if(canDrag) 160 { 161 panel.setDimension(wm.resizePos 162 + PanelWindowContainer 163 .SPLITTER_WIDTH); 164 wm.finishResizing(); 165 dragStart = null; 166 wm.revalidate(); 167 } 168 } 170 public void mouseMoved(MouseEvent evt) 172 { 173 Border border = getBorder(); 174 if(border == null) 175 { 176 return; 178 } 179 180 String position = panel.getPosition(); 181 182 Insets insets = border.getBorderInsets(DockablePanel.this); 183 canDrag = false; 184 if(position.equals(DockableWindowManager.TOP)) 186 { 187 if(evt.getY() >= getHeight() - insets.bottom) 188 canDrag = true; 189 } else if(position.equals(DockableWindowManager.LEFT)) 192 { 193 if(evt.getX() >= getWidth() - insets.right) 194 canDrag = true; 195 } else if(position.equals(DockableWindowManager.BOTTOM)) 198 { 199 if(evt.getY() <= insets.top) 200 canDrag = true; 201 } else if(position.equals(DockableWindowManager.RIGHT)) 204 { 205 if(evt.getX() <= insets.left) 206 canDrag = true; 207 } 209 if(canDrag) 210 { 211 wm.setCursor(Cursor.getPredefinedCursor( 212 getAppropriateCursor())); 213 } 214 else 215 { 216 wm.setCursor(Cursor.getPredefinedCursor( 217 Cursor.DEFAULT_CURSOR)); 218 } 219 } 221 public void mouseDragged(MouseEvent evt) 223 { 224 if(!canDrag) 225 return; 226 227 if(dragStart == null) return; 229 230 int dimension = panel.getDimension(); 231 232 String position = panel.getPosition(); 233 234 if(position.equals(DockableWindowManager.TOP)) 236 { 237 wm.setResizePos( 238 evt.getY() - dragStart.y 239 + dimension, 240 panel); 241 } else if(position.equals(DockableWindowManager.LEFT)) 244 { 245 wm.setResizePos(evt.getX() - dragStart.x 246 + dimension, 247 panel); 248 } else if(position.equals(DockableWindowManager.BOTTOM)) 251 { 252 wm.setResizePos(dimension - evt.getY() 253 + dragStart.y, 254 panel); 255 } else if(position.equals(DockableWindowManager.RIGHT)) 258 { 259 wm.setResizePos(dimension - evt.getX() 260 + dragStart.x, 261 panel); 262 } } 265 public void mouseExited(MouseEvent evt) 267 { 268 if (dragStart == null) 269 { 270 wm.setCursor(Cursor.getPredefinedCursor( 271 Cursor.DEFAULT_CURSOR)); 272 } 273 } 275 private int getAppropriateCursor() 277 { 278 String position = panel.getPosition(); 279 280 if(position.equals(DockableWindowManager.TOP)) 281 return Cursor.N_RESIZE_CURSOR; 282 else if(position.equals(DockableWindowManager.LEFT)) 283 return Cursor.W_RESIZE_CURSOR; 284 else if(position.equals(DockableWindowManager.BOTTOM)) 285 return Cursor.S_RESIZE_CURSOR; 286 else if(position.equals(DockableWindowManager.RIGHT)) 287 return Cursor.E_RESIZE_CURSOR; 288 else 289 throw new InternalError (); 290 } } } 293 | Popular Tags |