1 19 20 21 package org.netbeans.core.windows.view; 22 23 24 25 import java.awt.*; 26 import java.awt.datatransfer.DataFlavor ; 27 import java.awt.dnd.*; 28 import java.util.Arrays ; 29 import java.util.logging.Level ; 30 import java.util.logging.Logger ; 31 import javax.swing.*; 32 import javax.swing.border.Border ; 33 import org.netbeans.core.windows.*; 34 import org.netbeans.core.windows.view.dnd.*; 35 import org.openide.util.*; 36 import org.openide.windows.*; 37 38 39 44 public class EditorView extends ViewElement { 45 46 private static final boolean IS_GTK = "GTK".equals(UIManager.getLookAndFeel().getID()); 48 private ViewElement editorArea; 49 50 private EditorAreaComponent editorAreaComponent; 51 52 private final WindowDnDManager windowDnDManager; 54 55 56 public EditorView(Controller controller, WindowDnDManager windowDnDManager, 57 double resizeWeight, ViewElement editorArea) { 58 super(controller, resizeWeight); 59 60 this.editorArea = editorArea; 61 this.windowDnDManager = windowDnDManager; 62 } 63 64 65 Rectangle getPureBounds() { 67 Component comp = getEditorAreaComponent(); 68 Rectangle bounds = comp.getBounds(); 69 Point location = new Point(0, 0); 70 javax.swing.SwingUtilities.convertPointToScreen(location, comp); 71 bounds.setLocation(location); 72 return bounds; 73 } 74 75 private EditorAreaComponent getEditorAreaComponent() { 76 if(editorAreaComponent == null) { 77 editorAreaComponent = new EditorAreaComponent(this, windowDnDManager); 78 } 79 80 if (EditorView.IS_GTK && !editorAreaComponent.isValid()) { 82 editorAreaComponent.repaint(); 83 } 84 return editorAreaComponent; 85 } 86 87 90 private void manageBorder (JPanel panel) { 91 if (editorArea != null) { 92 panel.setBorder(null); 93 } else { 94 if (Utilities.isMac()) { 95 panel.setBorder(BorderFactory.createEmptyBorder()); 97 } else { 98 panel.setBorder((Border )UIManager.get("Nb.ScrollPane.border")); 100 } 101 } 102 } 103 104 public ViewElement getEditorArea() { 105 return editorArea; 106 } 107 108 public void setEditorArea(ViewElement editorArea) { 109 this.editorArea = editorArea; 110 } 111 112 public Component getComponent() { 113 return getEditorAreaComponent(); 115 } 116 117 public boolean updateAWTHierarchy(Dimension availableSpace) { 118 boolean result = false; 120 EditorAreaComponent comp = getEditorAreaComponent(); 121 Dimension d = (Dimension) comp.getClientProperty ("lastAvailableSpace"); Dimension currDim = comp.getPreferredSize(); 123 if (!availableSpace.equals(d) || !availableSpace.equals(currDim)) { 124 comp.setPreferredSize(availableSpace); 126 comp.putClientProperty("lastAvailableSpace", availableSpace); result = true; 129 } 130 assureComponentInEditorArea(); 131 if (editorArea != null) { 132 result |= editorArea.updateAWTHierarchy(new Dimension(availableSpace.width - 5, availableSpace.height - 5)); 133 } 134 return result; 135 } 136 137 void assureComponentInEditorArea() { 138 EditorAreaComponent eac = getEditorAreaComponent(); 139 if(editorArea == null) { 140 eac.setAreaComponent(null); 141 } else { 142 eac.setAreaComponent(editorArea.getComponent()); 143 } 144 manageBorder(eac); 145 146 } 157 158 private static DataFlavor URI_LIST_DATA_FLAVOR; 159 static { 160 try { 161 URI_LIST_DATA_FLAVOR = new DataFlavor ("text/uri-list;class=java.lang.String"); 162 } catch( ClassNotFoundException cnfE ) { 163 cnfE.printStackTrace(); 164 } 165 } 166 167 private static class EditorAreaComponent extends JPanel 168 implements TopComponentDroppable { 169 170 private final EditorView editorView; 171 172 private final WindowDnDManager windowDnDManager; 174 175 private Component areaComponent; 176 177 178 public EditorAreaComponent(EditorView editorView, WindowDnDManager windowDnDManager) { 179 this.editorView = editorView; 180 this.windowDnDManager = windowDnDManager; 181 182 init(); 183 } 184 185 186 private void init() { 187 setLayout(new BorderLayout()); 188 String lfID = UIManager.getLookAndFeel().getID(); 190 194 String imageSource = Constants.SWITCH_IMAGE_SOURCE; if(imageSource != null) { 197 Image image = Utilities.loadImage(imageSource); 198 if(image != null) { 199 JLabel label = new JLabel(new ImageIcon(image)); 200 label.setMinimumSize(new Dimension(0, 0)); add(label, BorderLayout.CENTER); 202 } else { 203 Logger.getLogger(EditorView.class.getName()).log(Level.WARNING, null, 204 new java.lang.NullPointerException ("Image not found at " + 205 imageSource)); } 207 } 208 DropTarget dropTarget = new DropTarget( this, new DropTargetListener() { 210 public void dragEnter(DropTargetDragEvent dtde) { 211 } 212 public void dragExit(DropTargetEvent dte) { 213 } 214 public void dragOver(DropTargetDragEvent dtde) { 215 ExternalDropHandler handler = (ExternalDropHandler)Lookup.getDefault().lookup( ExternalDropHandler.class ); 216 if( null != handler && handler.canDrop( dtde ) ) { 218 dtde.acceptDrag( DnDConstants.ACTION_COPY ); 219 } else { 220 dtde.rejectDrag(); 221 } 222 } 223 public void drop(DropTargetDropEvent dtde) { 224 ExternalDropHandler handler = (ExternalDropHandler)Lookup.getDefault().lookup( ExternalDropHandler.class ); 225 if( handler.canDrop( dtde ) ) { 226 dtde.acceptDrop( DnDConstants.ACTION_COPY ); 228 dtde.dropComplete( handler.handleDrop( dtde ) ); 230 } else { 231 dtde.dropComplete( false ); 232 } 233 } 234 public void dropActionChanged(DropTargetDragEvent dtde) { 235 } 236 } ); 237 setDropTarget( dropTarget ); 238 } 239 240 public void setAreaComponent(Component areaComponent) { 241 if(this.areaComponent == areaComponent) { 242 if(areaComponent != null 245 && !Arrays.asList(getComponents()).contains(areaComponent)) { 246 add(areaComponent, BorderLayout.CENTER); 247 } 248 249 return; 250 } 251 252 if(this.areaComponent != null) { 253 remove(this.areaComponent); 254 } 255 256 this.areaComponent = areaComponent; 257 258 if(this.areaComponent != null) { 259 add(this.areaComponent, BorderLayout.CENTER); 260 } 261 } 262 263 public Shape getIndicationForLocation(Point location) { 264 ModeImpl mode = (ModeImpl)WindowManagerImpl.getInstance().findMode(windowDnDManager.getStartingTransfer()); 265 int kind = mode != null ? mode.getKind() : Constants.MODE_KIND_EDITOR; 266 267 if(kind == Constants.MODE_KIND_EDITOR) { 268 Rectangle rect = getBounds(); 269 rect.setLocation(0, 0); 270 return rect; 271 } else { 272 Rectangle rect = getBounds(); 273 rect.setLocation(0, 0); 274 275 String side = getSideForLocation(location); 276 277 double ratio = Constants.DROP_AROUND_RATIO; 278 if(Constants.TOP.equals(side)) { 279 return new Rectangle(0, 0, rect.width, (int)(rect.height * ratio)); 280 } else if(side == Constants.LEFT) { 281 return new Rectangle(0, 0, (int)(rect.width * ratio), rect.height); 282 } else if(side == Constants.RIGHT) { 283 return new Rectangle(rect.width - (int)(rect.width * ratio), 0, (int)(rect.width * ratio), rect.height); 284 } else if(side == Constants.BOTTOM) { 285 return new Rectangle(0, rect.height - (int)(rect.height * ratio), rect.width, (int)(rect.height * ratio)); 286 } else if(Constants.SWITCH_MODE_ADD_NO_RESTRICT 287 || WindowManagerImpl.getInstance().isTopComponentAllowedToMoveAnywhere(windowDnDManager.getStartingTransfer())) { 288 return rect; 289 } else { 290 return null; 291 } 292 } 293 }; 294 295 public Object getConstraintForLocation(Point location) { 296 ModeImpl mode = (ModeImpl)WindowManagerImpl.getInstance().findMode(windowDnDManager.getStartingTransfer()); 297 int kind = mode != null ? mode.getKind() : Constants.MODE_KIND_EDITOR; 298 299 if(kind == Constants.MODE_KIND_EDITOR) { 300 return null; 301 } else { 302 return getSideForLocation(location); 303 } 304 } 305 306 private String getSideForLocation(Point location) { 307 Rectangle bounds = getBounds(); 308 bounds.setLocation(0, 0); 309 310 int delta = Constants.DROP_AREA_SIZE; 312 313 Rectangle top = new Rectangle(0, 0, bounds.width, delta); 314 if(top.contains(location)) { 315 return Constants.TOP; 316 } 317 318 Rectangle left = new Rectangle(0, delta, delta, bounds.height - 2 * delta); 319 if(left.contains(location)) { 320 return Constants.LEFT; 321 } 322 323 Rectangle right = new Rectangle(bounds.width - delta, delta, delta, bounds.height - 2 * delta); 324 if(right.contains(location)) { 325 return Constants.RIGHT; 326 } 327 328 Rectangle bottom = new Rectangle(0, bounds.height - delta, bounds.width, delta); 329 if(bottom.contains(location)) { 330 return Constants.BOTTOM; 331 } 332 333 return null; 334 } 335 336 public Component getDropComponent() { 337 return this; 338 } 339 340 public ViewElement getDropViewElement() { 341 return editorView; 342 } 343 344 public boolean canDrop(TopComponent transfer, Point location) { 345 if(Constants.SWITCH_MODE_ADD_NO_RESTRICT 346 || WindowManagerImpl.getInstance().isTopComponentAllowedToMoveAnywhere(transfer)) { 347 return true; 348 } 349 350 ModeImpl mode = (ModeImpl)WindowManagerImpl.getInstance().findMode(transfer); 351 int kind = mode != null ? mode.getKind() : Constants.MODE_KIND_EDITOR; 352 353 if(kind == Constants.MODE_KIND_EDITOR) { 354 return true; 355 } else { 356 if(WindowManagerImpl.getInstance().getEditorAreaState() == Constants.EDITOR_AREA_JOINED 357 && getSideForLocation(location) != null) { 358 return true; 359 } else { 360 return false; 361 } 362 } 363 } 364 365 public boolean supportsKind(int kind, TopComponent tc) { 366 return true; 367 } 368 369 } 371 372 } 373 374 | Popular Tags |