1 19 20 package org.openide.text; 21 22 import java.awt.Component ; 23 import java.awt.Container ; 24 import java.awt.Font ; 25 import java.awt.FontMetrics ; 26 import java.awt.Frame ; 27 import java.awt.Rectangle ; 28 import java.awt.datatransfer.Clipboard ; 29 import java.awt.datatransfer.DataFlavor ; 30 import java.awt.datatransfer.Transferable ; 31 import java.awt.dnd.DnDConstants ; 32 import java.awt.dnd.DropTarget ; 33 import java.awt.dnd.DropTargetDragEvent ; 34 import java.awt.dnd.DropTargetDropEvent ; 35 import java.awt.dnd.DropTargetEvent ; 36 import java.awt.dnd.DropTargetListener ; 37 import java.awt.event.InputEvent ; 38 import java.util.TooManyListenersException ; 39 import javax.swing.Icon ; 40 import javax.swing.JComponent ; 41 import javax.swing.JEditorPane ; 42 import javax.swing.SwingConstants ; 43 import javax.swing.SwingUtilities ; 44 import javax.swing.TransferHandler ; 45 import javax.swing.plaf.UIResource ; 46 import javax.swing.text.Caret ; 47 import javax.swing.text.Document ; 48 import javax.swing.text.JTextComponent ; 49 import org.openide.util.Lookup; 50 import org.openide.windows.ExternalDropHandler; 51 import org.openide.windows.TopComponent; 52 53 57 final class QuietEditorPane extends JEditorPane { 58 59 static DataFlavor constructActiveEditorDropFlavor() { 60 try { 61 return new DataFlavor ("text/active_editor_flavor;class=org.openide.text.ActiveEditorDrop", "Active Editor Flavor", QuietEditorPane.class.getClassLoader()); 64 } catch (ClassNotFoundException e) { 65 throw new AssertionError (e); 66 } 67 } 68 69 final static int FIRE = 0x1; 70 final static int PAINT = 0x2; 71 final static int ALL = FIRE | PAINT; 72 73 75 76 private int lastPosition = -1; 77 78 79 int working = FIRE; 81 82 private int fontHeight; 83 private int charWidth; 84 85 90 public QuietEditorPane() { 91 setFontHeightWidth(getFont()); 92 } 93 94 public void setFont(Font font) { 95 super.setFont(font); 96 setFontHeightWidth(getFont()); 97 } 98 99 100 private void setFontHeightWidth(Font font) { 101 FontMetrics metrics=getFontMetrics(font); 102 fontHeight=metrics.getHeight(); 103 charWidth=metrics.charWidth('m'); 104 } 105 106 111 public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { 112 switch (orientation) { 113 case SwingConstants.VERTICAL: 114 return fontHeight; 115 case SwingConstants.HORIZONTAL: 116 return charWidth; 117 default: 118 throw new IllegalArgumentException ("Invalid orientation: " +orientation); 119 } 120 } 121 122 public void setDocument(Document doc) { 123 super.setDocument(doc); 124 125 if (doc != null){ 129 TransferHandler thn = getTransferHandler(); 130 if( !(thn instanceof DelegatingTransferHandler) ) { 131 DelegatingTransferHandler dth = new DelegatingTransferHandler(thn); 132 setTransferHandler(dth); 133 } 134 135 DropTarget currDt = getDropTarget(); 136 if( !(currDt instanceof DelegatingDropTarget ) ) { 137 DropTarget dt = new DelegatingDropTarget( currDt ); 138 setDropTarget( dt ); 139 } 140 } 141 } 142 143 public void setWorking(int x) { 144 working = x; 145 } 146 147 public void firePropertyChange(String s, Object val1, Object val2) { 148 if ((working & FIRE) != 0) { 149 super.firePropertyChange(s, val1, val2); 150 } 151 } 152 153 157 public void setCaret(Caret caret) { 158 if (caret == null) { 159 Caret oldCaret = getCaret(); 160 161 if (oldCaret != null) { 162 lastPosition = oldCaret.getDot(); 163 } 164 } 165 166 super.setCaret(caret); 167 } 168 169 171 int getLastPosition() { 172 return lastPosition; 173 } 174 175 187 public void revalidate() { 188 if ((working & PAINT) != 0) { 189 super.revalidate(); 190 } 191 } 192 193 public void repaint() { 194 if ((working & PAINT) != 0) { 195 super.repaint(); 196 } 197 } 198 199 204 private class DelegatingTransferHandler extends TransferHandler { 205 206 TransferHandler delegator; 207 208 public DelegatingTransferHandler(TransferHandler delegator){ 209 this.delegator = delegator; 210 } 211 212 public void exportAsDrag(JComponent comp, InputEvent e, int action) { 213 delegator.exportAsDrag(comp, e, action); 214 } 215 216 public void exportToClipboard(JComponent comp, Clipboard clip, int action) { 217 delegator.exportToClipboard(comp, clip, action); 218 } 219 220 public boolean importData(JComponent comp, Transferable t) { 221 try { 222 if (t.isDataFlavorSupported(ActiveEditorDrop.FLAVOR)){ 223 Object obj = t.getTransferData(ActiveEditorDrop.FLAVOR); 224 if (obj instanceof ActiveEditorDrop && comp instanceof JTextComponent ){ 225 boolean success = false; 226 try { 227 success = ((ActiveEditorDrop)obj).handleTransfer((JTextComponent )comp); 228 } 229 finally { 230 requestFocus(comp); 231 } 232 return success; 233 } 234 } 235 } catch (Exception exc){ 236 exc.printStackTrace(); 237 } 238 return delegator.importData(comp, t); 239 } 240 241 private void requestFocus(JComponent comp) { 242 Container container = SwingUtilities.getAncestorOfClass(TopComponent.class, comp); 243 if (container != null) { 244 ((TopComponent)container).requestActive(); 245 } 246 else { 247 Component f = comp; 248 do { 249 f = f.getParent(); 250 if (f instanceof Frame ) { 251 break; 252 } 253 } while (f != null); 254 if (f != null) { 255 f.requestFocus(); 256 } 257 comp.requestFocus(); 258 } 259 } 260 261 public boolean canImport(JComponent comp, DataFlavor [] transferFlavors) { 262 for (int i=0; i<transferFlavors.length; i++){ 263 if (transferFlavors[i] == ActiveEditorDrop.FLAVOR){ 264 return true; 265 } 266 } 267 return delegator.canImport(comp, transferFlavors); 268 } 269 270 public int getSourceActions(JComponent c) { 271 return delegator.getSourceActions(c); 272 } 273 274 public Icon getVisualRepresentation(Transferable t) { 275 return delegator.getVisualRepresentation(t); 276 } 277 278 protected void exportDone(JComponent source, Transferable data, int action) { 279 try { 280 java.lang.reflect.Method method = delegator.getClass().getDeclaredMethod( 281 "exportDone", new Class [] {javax.swing.JComponent .class, Transferable .class, int.class}); 283 method.setAccessible(true); 284 method.invoke(delegator, new Object [] {source, data, new Integer (action)}); 285 } catch (NoSuchMethodException ex) { 286 ex.printStackTrace(); 287 } catch (IllegalAccessException ex) { 288 ex.printStackTrace(); 289 } catch (java.lang.reflect.InvocationTargetException ex) { 290 ex.printStackTrace(); 291 } 292 } 293 294 protected Transferable createTransferable(JComponent comp) { 295 try { 296 java.lang.reflect.Method method = delegator.getClass().getDeclaredMethod( 297 "createTransferable", new Class [] {javax.swing.JComponent .class}); 299 method.setAccessible(true); 300 return (Transferable )method.invoke(delegator, new Object [] {comp}); 301 } catch (NoSuchMethodException ex) { 302 ex.printStackTrace(); 303 } catch (IllegalAccessException ex) { 304 ex.printStackTrace(); 305 } catch (java.lang.reflect.InvocationTargetException ex) { 306 ex.printStackTrace(); 307 } 308 return null; 309 } 310 } 311 312 private class DelegatingDropTarget extends DropTarget implements UIResource { 313 private DropTarget orig; 314 private boolean isDragging = false; 315 316 public DelegatingDropTarget( DropTarget orig ) { 317 this.orig = orig; 318 } 319 public void addDropTargetListener(DropTargetListener dtl) throws TooManyListenersException { 320 orig.addDropTargetListener( dtl ); 321 } 322 323 public void removeDropTargetListener(DropTargetListener dtl) { 324 orig.removeDropTargetListener( dtl ); 325 } 326 327 public void dragEnter(DropTargetDragEvent dtde) { 328 ExternalDropHandler handler = (ExternalDropHandler)Lookup.getDefault().lookup( ExternalDropHandler.class ); 329 if( null != handler && handler.canDrop( dtde ) ) { 330 dtde.acceptDrag( DnDConstants.ACTION_COPY ); 331 isDragging = false; 332 } else { 333 orig.dragEnter( dtde ); 334 isDragging = true; 335 } 336 } 337 338 public void dragExit(DropTargetEvent dte) { 339 if( isDragging ) { 340 orig.dragExit( dte ); 341 } 342 isDragging = false; 343 } 344 345 public void dragOver(DropTargetDragEvent dtde) { 346 ExternalDropHandler handler = (ExternalDropHandler)Lookup.getDefault().lookup( ExternalDropHandler.class ); 347 if( null != handler && handler.canDrop( dtde ) ) { 348 dtde.acceptDrag( DnDConstants.ACTION_COPY ); 349 isDragging = false; 350 } else { 351 orig.dragOver( dtde ); 352 isDragging = true; 353 } 354 } 355 356 public void drop(DropTargetDropEvent e) { 357 ExternalDropHandler handler = (ExternalDropHandler)Lookup.getDefault().lookup( ExternalDropHandler.class ); 358 if( handler.canDrop( e ) ) { 359 e.acceptDrop( DnDConstants.ACTION_COPY ); 360 361 e.dropComplete( handler.handleDrop( e ) ); 362 } else { 363 orig.drop( e ); 364 } 365 isDragging = false; 366 } 367 368 public void dropActionChanged(DropTargetDragEvent dtde) { 369 if( isDragging ) 370 orig.dropActionChanged( dtde ); 371 } 372 } 373 } 374 | Popular Tags |