/[debian]/libjgraph-java/trunk/migrate.xml
ViewVC logotype

Contents of /libjgraph-java/trunk/migrate.xml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 443 - (show annotations)
Sat Nov 25 17:52:34 2006 UTC (14 years, 4 months ago) by gregoa
File MIME type: text/xml
File size: 31701 byte(s)
[svn-inject] Forking libjgraph-java source to Trunk
1 <!--
2
3 @(#)migrate.xml 1.0 03-JUL-04
4
5 Copyright (c) 2001-2004 Gaudenz Alder
6
7 -->
8
9 <!-- ===================== Project Properties =========================== -->
10
11 <project name="jgraph-migrate" default="all" basedir=".">
12
13 <!-- ===================== Build Environment =========================== -->
14
15 <property name="build.home" value="${basedir}/build"/>
16
17 <!-- ==================== All ==================== -->
18
19 <target name="all" depends="checkversion, migrate"
20 description="Clean up and produce all distributions"/>
21
22 <!-- ==================== Checkversion ==================== -->
23
24 <target name="checkversion" description="Check Java version">
25 <!-- Check Java Version: doMigrate if Java 1.3 -->
26 <condition property="doMigrate" value="Yes">
27 <equals arg1="${java.specification.version}" arg2="1.3" />
28 </condition>
29 </target>
30
31 <!-- ==================== Migrate ==================== -->
32
33 <target name="migrate" depends="adapter" description="Migrate the build tree" if="doMigrate">
34 <echo message="Migrating to Java 1.3"/>
35
36 <replace dir="${build.home}/src">
37 <include name="**/*.java"/>
38
39 <!-- Java 1.3: Replace superclass in com.jgraph.JGraph -->
40 <replacefilter
41 token="extends JComponent // JAVA13"
42 value="extends org.jgraph.plaf.basic.TransferHandler.JAdapterComponent // JAVA13"/>
43
44 <!-- Java 1.3: Replace import in com.jgraph.plaf.basic.BasicGraphUI -->
45 <replacefilter
46 token="javax.swing.TransferHandler"
47 value="org.jgraph.plaf.basic.TransferHandler"/>
48
49 <!-- Java 1.3: Replace typecast in com.jgraph.plaf.basic.BasciGraphDropTargetListener -->
50 <replacefilter
51 token="(JComponent) // JAVA13"
52 value="(org.jgraph.plaf.basic.TransferHandler.JDNDAdapter) // JAVA13"/>
53
54 <!-- Java 1.3: Replace typedef in com.jgraph.plaf.basic.BasciGraphDropTargetListener -->
55 </replace>
56
57 <replace dir="${build.home}/example">
58 <include name="**/*.java"/>
59
60 <!-- Java 1.3: Replace TransferHandler class in GraphEd -->
61 <replacefilter
62 token="javax.swing.TransferHandler // JAVA13"
63 value="org.jgraph.plaf.basic.TransferHandler // JAVA13"/>
64
65 </replace>
66 </target>
67
68 <!-- ==================== adapter ==================== -->
69
70 <target name="adapter" description="Create TransferHandler.java" if="doMigrate">
71 <echo message="Creating adapter for Java 1.3"/>
72 <echo file="${build.home}/src/org/jgraph/plaf/basic/TransferHandler.java"><![CDATA[
73 /*
74 * @(#)TransferHandler 1.0 29/2/02
75 *
76 * Copyright (c) 2001-2004 Gaudenz Alder
77 *
78 */
79 package org.jgraph.plaf.basic;
80
81 import java.awt.datatransfer.Clipboard;
82 import java.awt.datatransfer.DataFlavor;
83 import java.awt.datatransfer.Transferable;
84 import java.awt.datatransfer.UnsupportedFlavorException;
85 import java.awt.dnd.DnDConstants;
86 import java.awt.dnd.DragGestureEvent;
87 import java.awt.dnd.DragGestureListener;
88 import java.awt.dnd.DragGestureRecognizer;
89 import java.awt.dnd.DragSource;
90 import java.awt.dnd.DragSourceContext;
91 import java.awt.dnd.DragSourceDragEvent;
92 import java.awt.dnd.DragSourceDropEvent;
93 import java.awt.dnd.DragSourceEvent;
94 import java.awt.dnd.DragSourceListener;
95 import java.awt.dnd.DropTarget;
96 import java.awt.dnd.DropTargetDragEvent;
97 import java.awt.dnd.DropTargetDropEvent;
98 import java.awt.dnd.DropTargetEvent;
99 import java.awt.dnd.DropTargetListener;
100 import java.awt.event.ActionEvent;
101 import java.awt.event.InputEvent;
102 import java.awt.event.MouseEvent;
103 import java.beans.BeanInfo;
104 import java.beans.IntrospectionException;
105 import java.beans.Introspector;
106 import java.beans.PropertyDescriptor;
107 import java.io.IOException;
108 import java.io.Serializable;
109 import java.lang.reflect.Method;
110 import java.util.TooManyListenersException;
111
112 import javax.swing.AbstractAction;
113 import javax.swing.Action;
114 import javax.swing.Icon;
115 import javax.swing.JComponent;
116 import javax.swing.event.EventListenerList;
117 import javax.swing.plaf.UIResource;
118
119 /**
120 * This compatibility class is required by JGraph for Java 1.3
121 */
122
123 public class TransferHandler implements Serializable {
124
125 // Adapter
126 public static interface JDNDAdapter {
127
128 TransferHandler getTransferHandler();
129
130 }
131
132 public static class JAdapterComponent
133 extends JComponent
134 implements JDNDAdapter {
135
136
137 protected TransferHandler transfer = null;
138
139 public void setTransferHandler(TransferHandler newHandler) {
140 TransferHandler oldHandler = transfer;
141 transfer = newHandler;
142
143 DropTarget dropHandler = getDropTarget();
144 if ((dropHandler == null) || (dropHandler instanceof UIResource)) {
145 if (newHandler == null) {
146 setDropTarget(null);
147 } else {
148 setDropTarget(new TransferHandler.SwingDropTarget(this));
149 }
150 }
151 firePropertyChange("transferHandler", oldHandler, transfer);
152 }
153
154 public TransferHandler getTransferHandler() {
155 return transfer;
156 }
157
158 }
159
160 /**
161 * An <code>int</code> representing no transfer action.
162 */
163 public static final int NONE = DnDConstants.ACTION_NONE;
164
165 /**
166 * An <code>int</code> representing a &quot;copy&quot; transfer action.
167 * This value is used when data is copied to a clipboard
168 * or copied elsewhere in a drag and drop operation.
169 */
170 public static final int COPY = DnDConstants.ACTION_COPY;
171
172 /**
173 * An <code>int</code> representing a &quot;move&quot; transfer action.
174 * This value is used when data is moved to a clipboard (i.e. a cut)
175 * or moved elsewhere in a drag and drop operation.
176 */
177 public static final int MOVE = DnDConstants.ACTION_MOVE;
178
179 /**
180 * An <code>int</code> representing a source action capability of either
181 * &quot;copy&quot; or &quot;move&quot;.
182 */
183 public static final int COPY_OR_MOVE = DnDConstants.ACTION_COPY_OR_MOVE;
184
185 /**
186 * Returns an <code>Action</code> that behaves like a 'cut' operation.
187 * That is, this will invoke <code>exportToClipboard</code> with
188 * a <code>MOVE</code> argument on the <code>TransferHandler</code>
189 * associated with the <code>JComponent</code> that is the source of
190 * the <code>ActionEvent</code>.
191 *
192 * @return cut Action
193 */
194 public static Action getCutAction() {
195 return cutAction;
196 }
197
198 /**
199 * Returns an <code>Action</code> that behaves like a 'copy' operation.
200 * That is, this will invoke <code>exportToClipboard</code> with
201 * a <code>COPY</code> argument on the <code>TransferHandler</code>
202 * associated with the <code>JComponent</code> that is the source of
203 * the <code>ActionEvent</code>.
204 *
205 * @return cut Action
206 */
207 public static Action getCopyAction() {
208 return copyAction;
209 }
210
211 /**
212 * Returns an <code>Action</code> that behaves like a 'paste' operation.
213 * That is, this will invoke <code>importData</code> on the
214 * <code>TransferHandler</code>
215 * associated with the <code>JComponent</code> that is the source of
216 * the <code>ActionEvent</code>.
217 *
218 * @return cut Action
219 */
220 public static Action getPasteAction() {
221 return pasteAction;
222 }
223
224 /**
225 * Constructs a transfer handler that can transfer a Java Bean property
226 * from one component to another via the clipboard or a drag and drop
227 * operation.
228 *
229 * @param property the name of the property to transfer; this can
230 * be <code>null</code> if there is no property associated with the transfer
231 * handler (a subclass that performs some other kind of transfer, for example)
232 */
233 public TransferHandler(String property) {
234 propertyName = property;
235 }
236
237 /**
238 * Convenience constructor for subclasses.
239 */
240 protected TransferHandler() {
241 this(null);
242 }
243
244 /**
245 * Causes the Swing drag support to be initiated. This is called by
246 * the various UI implementations in the <code>javax.swing.plaf.basic</code>
247 * package if the dragEnabled property is set on the component.
248 * This can be called by custom UI
249 * implementations to use the Swing drag support. This method can also be called
250 * by a Swing extension written as a subclass of <code>JComponent</code>
251 * to take advantage of the Swing drag support.
252 * <p>
253 * The transfer <em>will not necessarily</em> have been completed at the
254 * return of this call (i.e. the call does not block waiting for the drop).
255 * The transfer will take place through the Swing implementation of the
256 * <code>java.awt.dnd</code> mechanism, requiring no further effort
257 * from the developer. The <code>exportDone</code> method will be called
258 * when the transfer has completed.
259 *
260 * @param comp the component holding the data to be transferred; this
261 * argument is provided to enable sharing of <code>TransferHandler</code>s by
262 * multiple components
263 * @param e the event that triggered the transfer
264 * @param action the transfer action initially requested; this should
265 * be a value of either <code>COPY</code> or <code>MOVE</code>;
266 * the value may be changed during the course of the drag operation
267 */
268 public void exportAsDrag(JComponent comp, InputEvent e, int action) {
269 int srcActions = getSourceActions(comp);
270 int dragAction = srcActions & action;
271 if (!(e instanceof MouseEvent)) {
272 // only mouse events supported for drag operations
273 dragAction = NONE;
274 }
275 if (dragAction != NONE) { // && !GraphicsEnvironment.isHeadless()) {
276 if (recognizer == null) {
277 recognizer = new SwingDragGestureRecognizer(new DragHandler());
278 }
279 recognizer.gestured(comp, (MouseEvent) e, srcActions, dragAction);
280 } else {
281 exportDone(comp, null, NONE);
282 }
283 }
284
285 /**
286 * Causes a transfer from the given component to the
287 * given clipboard. This method is called by the default cut and
288 * copy actions registered in a component's action map.
289 * <p>
290 * The transfer <em>will</em> have been completed at the
291 * return of this call. The transfer will take place using the
292 * <code>java.awt.datatransfer</code> mechanism,
293 * requiring no further effort from the developer.
294 * The <code>exportDone</code> method will be called when the
295 * transfer has completed.
296 *
297 * @param comp the component holding the data to be transferred; this
298 * argument is provided to enable sharing of <code>TransferHandler</code>s by
299 * multiple components
300 * @param clip the clipboard to transfer the data into
301 * @param action the transfer action requested; this should
302 * be a value of either <code>COPY</code> or <code>MOVE</code>;
303 * the operation performed is the intersection of the transfer
304 * capabilities given by getSourceActions and the requested action;
305 * the intersection may result in an action of <code>NONE</code>
306 * if the requested action isn't supported
307 */
308 public void exportToClipboard(
309 JComponent comp,
310 Clipboard clip,
311 int action) {
312 boolean exportSuccess = false;
313 Transferable t = null;
314
315 int clipboardAction = getSourceActions(comp) & action;
316 if (clipboardAction != NONE) {
317 t = createTransferable(comp);
318 if (t != null) {
319 clip.setContents(t, null);
320 exportSuccess = true;
321 }
322 }
323
324 if (exportSuccess) {
325 exportDone(comp, t, clipboardAction);
326 } else {
327 exportDone(comp, null, NONE);
328 }
329 }
330
331 /**
332 * Causes a transfer to a component from a clipboard or a
333 * DND drop operation. The <code>Transferable</code> represents
334 * the data to be imported into the component.
335 *
336 * @param comp the component to receive the transfer; this
337 * argument is provided to enable sharing of <code>TransferHandler</code>s
338 * by multiple components
339 * @param t the data to import
340 * @return true if the data was inserted into the component, false otherwise
341 */
342 public boolean importData(JComponent comp, Transferable t) {
343 PropertyDescriptor prop = getPropertyDescriptor(comp);
344 if (prop != null) {
345 Method writer = prop.getWriteMethod();
346 if (writer == null) {
347 // read-only property. ignore
348 return false;
349 }
350 Class[] params = writer.getParameterTypes();
351 if (params.length != 1) {
352 // zero or more than one argument, ignore
353 return false;
354 }
355 DataFlavor flavor =
356 getPropertyDataFlavor(params[0], t.getTransferDataFlavors());
357 if (flavor != null) {
358 try {
359 Object value = t.getTransferData(flavor);
360 Object[] args = { value };
361 writer.invoke(comp, args);
362 return true;
363 } catch (Exception ex) {
364 System.err.println("Invocation failed");
365 // invocation code
366 }
367 }
368 }
369 return false;
370 }
371
372 /**
373 * Indicates whether a component would accept an import of the given
374 * set of data flavors prior to actually attempting to import it.
375 *
376 * @param comp the component to receive the transfer; this
377 * argument is provided to enable sharing of <code>TransferHandlers</code>
378 * by multiple components
379 * @param transferFlavors the data formats available
380 * @return true if the data can be inserted into the component, false otherwise
381 */
382 public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
383 PropertyDescriptor prop = getPropertyDescriptor(comp);
384 if (prop != null) {
385 Method writer = prop.getWriteMethod();
386 if (writer == null) {
387 // read-only property. ignore
388 return false;
389 }
390 Class[] params = writer.getParameterTypes();
391 if (params.length != 1) {
392 // zero or more than one argument, ignore
393 return false;
394 }
395 DataFlavor flavor =
396 getPropertyDataFlavor(params[0], transferFlavors);
397 if (flavor != null) {
398 return true;
399 }
400 }
401 return false;
402 }
403
404 /**
405 * Returns the type of transfer actions supported by the source.
406 * Some models are not mutable, so a transfer operation of <code>COPY</code>
407 * only should be advertised in that case.
408 *
409 * @param c the component holding the data to be transferred; this
410 * argument is provided to enable sharing of <code>TransferHandler</code>s
411 * by multiple components.
412 * @return <code>COPY</code> if the transfer property can be found,
413 * otherwise returns <code>NONE</code>; a return value of
414 * of <code>NONE</code> disables any transfers out of the component
415 */
416 public int getSourceActions(JComponent c) {
417 PropertyDescriptor prop = getPropertyDescriptor(c);
418 if (prop != null) {
419 return COPY;
420 }
421 return NONE;
422 }
423
424 /**
425 * Returns an object that establishes the look of a transfer. This is
426 * useful for both providing feedback while performing a drag operation and for
427 * representing the transfer in a clipboard implementation that has a visual
428 * appearance. The implementation of the <code>Icon</code> interface should
429 * not alter the graphics clip or alpha level.
430 * The icon implementation need not be rectangular or paint all of the
431 * bounding rectangle and logic that calls the icons paint method should
432 * not assume the all bits are painted. <code>null</code> is a valid return value
433 * for this method and indicates there is no visual representation provided.
434 * In that case, the calling logic is free to represent the
435 * transferable however it wants.
436 * <p>
437 * The default Swing logic will not do an alpha blended drag animation if
438 * the return is <code>null</code>.
439 *
440 * @param t the data to be transferred; this value is expected to have been
441 * created by the <code>createTransferable</code> method
442 * @return <code>null</code>, indicating
443 * there is no default visual representation
444 */
445 public Icon getVisualRepresentation(Transferable t) {
446 return null;
447 }
448
449 /**
450 * Creates a <code>Transferable</code> to use as the source for
451 * a data transfer. Returns the representation of the data to
452 * be transferred, or <code>null</code> if the component's
453 * property is <code>null</code>
454 *
455 * @param c the component holding the data to be transferred; this
456 * argument is provided to enable sharing of <code>TransferHandler</code>s
457 * by multiple components
458 * @return the representation of the data to be transferred, or
459 * <code>null</code> if the property associated with <code>c</code>
460 * is <code>null</code>
461 *
462 */
463 protected Transferable createTransferable(JComponent c) {
464 PropertyDescriptor property = getPropertyDescriptor(c);
465 if (property != null) {
466 return new PropertyTransferable(property, c);
467 }
468 return null;
469 }
470
471 /**
472 * Invoked after data has been exported. This method should remove
473 * the data that was transferred if the action was <code>MOVE</code>.
474 * <p>
475 * This method is implemented to do nothing since <code>MOVE</code>
476 * is not a supported action of this implementation
477 * (<code>getSourceActions</code> does not include <code>MOVE</code>).
478 *
479 * @param source the component that was the source of the data
480 * @param data The data that was transferred or possibly null
481 * if the action is <code>NONE</code>.
482 * @param action the actual action that was performed
483 */
484 protected void exportDone(
485 JComponent source,
486 Transferable data,
487 int action) {
488 }
489
490 /**
491 * Fetches the property descriptor for the property assigned to this transfer
492 * handler on the given component (transfer handler may be shared). This
493 * returns <code>null</code> if the property descriptor can't be found
494 * or there is an error attempting to fetch the property descriptor.
495 */
496 private PropertyDescriptor getPropertyDescriptor(JComponent comp) {
497 if (propertyName == null) {
498 return null;
499 }
500 Class k = comp.getClass();
501 BeanInfo bi;
502 try {
503 bi = Introspector.getBeanInfo(k);
504 } catch (IntrospectionException ex) {
505 return null;
506 }
507 PropertyDescriptor props[] = bi.getPropertyDescriptors();
508 for (int i = 0; i < props.length; i++) {
509 if (propertyName.equals(props[i].getName())) {
510 Method reader = props[i].getReadMethod();
511
512 if (reader != null) {
513 Class[] params = reader.getParameterTypes();
514
515 if (params == null || params.length == 0) {
516 // found the desired descriptor
517 return props[i];
518 }
519 }
520 }
521 }
522 return null;
523 }
524
525 /**
526 * Fetches the data flavor from the array of possible flavors that
527 * has data of the type represented by property type. Null is
528 * returned if there is no match.
529 */
530 private DataFlavor getPropertyDataFlavor(Class k, DataFlavor[] flavors) {
531 for (int i = 0; i < flavors.length; i++) {
532 DataFlavor flavor = flavors[i];
533 if ("application".equals(flavor.getPrimaryType())
534 && "x-java-jvm-local-objectref".equals(flavor.getSubType())
535 && k.isAssignableFrom(flavor.getRepresentationClass())) {
536
537 return flavor;
538 }
539 }
540 return null;
541 }
542
543 private String propertyName;
544 private static SwingDragGestureRecognizer recognizer = null;
545 private static DropTargetListener dropLinkage = null;
546
547 private static DropTargetListener getDropTargetListener() {
548 if (dropLinkage == null) {
549 dropLinkage = new DropHandler();
550 }
551 return dropLinkage;
552 }
553
554 static class PropertyTransferable implements Transferable {
555
556 PropertyTransferable(PropertyDescriptor p, JComponent c) {
557 property = p;
558 component = c;
559 }
560
561 // --- Transferable methods ----------------------------------------------
562
563 /**
564 * Returns an array of <code>DataFlavor</code> objects indicating the flavors the data
565 * can be provided in. The array should be ordered according to preference
566 * for providing the data (from most richly descriptive to least descriptive).
567 * @return an array of data flavors in which this data can be transferred
568 */
569 public DataFlavor[] getTransferDataFlavors() {
570 DataFlavor[] flavors = new DataFlavor[1];
571 Class propertyType = property.getPropertyType();
572 String mimeType =
573 DataFlavor.javaJVMLocalObjectMimeType
574 + ";class="
575 + propertyType.getName();
576 try {
577 flavors[0] = new DataFlavor(mimeType);
578 } catch (ClassNotFoundException cnfe) {
579 flavors = new DataFlavor[0];
580 }
581 return flavors;
582 }
583
584 /**
585 * Returns whether the specified data flavor is supported for
586 * this object.
587 * @param flavor the requested flavor for the data
588 * @return true if this <code>DataFlavor</code> is supported,
589 * otherwise false
590 */
591 public boolean isDataFlavorSupported(DataFlavor flavor) {
592 Class propertyType = property.getPropertyType();
593 if ("application".equals(flavor.getPrimaryType())
594 && "x-java-jvm-local-objectref".equals(flavor.getSubType())
595 && flavor.getRepresentationClass().isAssignableFrom(
596 propertyType)) {
597
598 return true;
599 }
600 return false;
601 }
602
603 /**
604 * Returns an object which represents the data to be transferred. The class
605 * of the object returned is defined by the representation class of the flavor.
606 *
607 * @param flavor the requested flavor for the data
608 * @see DataFlavor#getRepresentationClass
609 * @exception IOException if the data is no longer available
610 * in the requested flavor.
611 * @exception UnsupportedFlavorException if the requested data flavor is
612 * not supported.
613 */
614 public Object getTransferData(DataFlavor flavor)
615 throws UnsupportedFlavorException, IOException {
616 if (!isDataFlavorSupported(flavor)) {
617 throw new UnsupportedFlavorException(flavor);
618 }
619 Method reader = property.getReadMethod();
620 Object value = null;
621 try {
622 value = reader.invoke(component, null);
623 } catch (Exception ex) {
624 throw new IOException(
625 "Property read failed: " + property.getName());
626 }
627 return value;
628 }
629
630 JComponent component;
631 PropertyDescriptor property;
632 }
633
634 /**
635 * This is the default drop target for drag and drop operations if
636 * one isn't provided by the developer. <code>DropTarget</code>
637 * only supports one <code>DropTargetListener</code> and doesn't
638 * function properly if it isn't set.
639 * This class sets the one listener as the linkage of drop handling
640 * to the <code>TransferHandler</code>, and adds support for
641 * additional listeners which some of the <code>ComponentUI</code>
642 * implementations install to manipulate a drop insertion location.
643 */
644 public static class SwingDropTarget
645 extends DropTarget
646 implements UIResource {
647
648 public SwingDropTarget(JComponent c) {
649 super();
650 setComponent(c);
651 try {
652 super.addDropTargetListener(getDropTargetListener());
653 } catch (TooManyListenersException tmle) {
654 }
655 }
656
657 public void addDropTargetListener(DropTargetListener dtl)
658 throws TooManyListenersException {
659 // Since the super class only supports one DropTargetListener,
660 // and we add one from the constructor, we always add to the
661 // extended list.
662 if (listenerList == null) {
663 listenerList = new EventListenerList();
664 }
665 listenerList.add(DropTargetListener.class, dtl);
666 }
667
668 public void removeDropTargetListener(DropTargetListener dtl) {
669 if (listenerList != null) {
670 listenerList.remove(DropTargetListener.class, dtl);
671 }
672 }
673
674 // --- DropTargetListener methods (multicast) --------------------------
675
676 public void dragEnter(DropTargetDragEvent e) {
677 super.dragEnter(e);
678 if (listenerList != null) {
679 Object[] listeners = listenerList.getListenerList();
680 for (int i = listeners.length - 2; i >= 0; i -= 2) {
681 if (listeners[i] == DropTargetListener.class) {
682 ((DropTargetListener) listeners[i + 1]).dragEnter(e);
683 }
684 }
685 }
686 }
687
688 public void dragOver(DropTargetDragEvent e) {
689 super.dragOver(e);
690 if (listenerList != null) {
691 Object[] listeners = listenerList.getListenerList();
692 for (int i = listeners.length - 2; i >= 0; i -= 2) {
693 if (listeners[i] == DropTargetListener.class) {
694 ((DropTargetListener) listeners[i + 1]).dragOver(e);
695 }
696 }
697 }
698 }
699
700 public void dragExit(DropTargetEvent e) {
701 super.dragExit(e);
702 if (listenerList != null) {
703 Object[] listeners = listenerList.getListenerList();
704 for (int i = listeners.length - 2; i >= 0; i -= 2) {
705 if (listeners[i] == DropTargetListener.class) {
706 ((DropTargetListener) listeners[i + 1]).dragExit(e);
707 }
708 }
709 }
710 }
711
712 public void drop(DropTargetDropEvent e) {
713 super.drop(e);
714 if (listenerList != null) {
715 Object[] listeners = listenerList.getListenerList();
716 for (int i = listeners.length - 2; i >= 0; i -= 2) {
717 if (listeners[i] == DropTargetListener.class) {
718 ((DropTargetListener) listeners[i + 1]).drop(e);
719 }
720 }
721 }
722 }
723
724 public void dropActionChanged(DropTargetDragEvent e) {
725 super.dropActionChanged(e);
726 if (listenerList != null) {
727 Object[] listeners = listenerList.getListenerList();
728 for (int i = listeners.length - 2; i >= 0; i -= 2) {
729 if (listeners[i] == DropTargetListener.class) {
730 (
731 (DropTargetListener) listeners[i
732 + 1]).dropActionChanged(
733 e);
734 }
735 }
736 }
737 }
738
739 private EventListenerList listenerList;
740 }
741
742 private static class DropHandler
743 implements DropTargetListener, Serializable {
744
745 private boolean canImport;
746
747 private boolean actionSupported(int action) {
748 return (action & COPY_OR_MOVE) != NONE;
749 }
750
751 // --- DropTargetListener methods -----------------------------------
752
753 public void dragEnter(DropTargetDragEvent e) {
754 DataFlavor[] flavors = e.getCurrentDataFlavors();
755
756 JDNDAdapter c =
757 (JDNDAdapter) e.getDropTargetContext().getComponent();
758 TransferHandler importer = c.getTransferHandler();
759
760 if (importer != null
761 && importer.canImport((JComponent) c, flavors)) {
762 canImport = true;
763 } else {
764 canImport = false;
765 }
766
767 int dropAction = e.getDropAction();
768
769 if (canImport && actionSupported(dropAction)) {
770 e.acceptDrag(dropAction);
771 } else {
772 e.rejectDrag();
773 }
774 }
775
776 public void dragOver(DropTargetDragEvent e) {
777 int dropAction = e.getDropAction();
778
779 if (canImport && actionSupported(dropAction)) {
780 e.acceptDrag(dropAction);
781 } else {
782 e.rejectDrag();
783 }
784 }
785
786 public void dragExit(DropTargetEvent e) {
787 }
788
789 public void drop(DropTargetDropEvent e) {
790 int dropAction = e.getDropAction();
791
792 JDNDAdapter c =
793 (JDNDAdapter) e.getDropTargetContext().getComponent();
794 TransferHandler importer = c.getTransferHandler();
795
796 if (canImport && importer != null && actionSupported(dropAction)) {
797 e.acceptDrop(dropAction);
798
799 try {
800 Transferable t = e.getTransferable();
801 e.dropComplete(importer.importData((JComponent) c, t));
802 } catch (RuntimeException re) {
803 e.dropComplete(false);
804 }
805 } else {
806 e.rejectDrop();
807 }
808 }
809
810 public void dropActionChanged(DropTargetDragEvent e) {
811 int dropAction = e.getDropAction();
812
813 if (canImport && actionSupported(dropAction)) {
814 e.acceptDrag(dropAction);
815 } else {
816 e.rejectDrag();
817 }
818 }
819 }
820
821 /**
822 * This is the default drag handler for drag and drop operations that
823 * use the <code>TransferHandler</code>.
824 */
825 private static class DragHandler
826 implements DragGestureListener, DragSourceListener {
827
828 private boolean scrolls;
829
830 // --- DragGestureListener methods -----------------------------------
831
832 /**
833 * a Drag gesture has been recognized
834 */
835 public void dragGestureRecognized(DragGestureEvent dge) {
836 JDNDAdapter c = (JDNDAdapter) dge.getComponent();
837 TransferHandler th = c.getTransferHandler();
838 Transferable t = th.createTransferable((JComponent) c);
839 if (t != null) {
840 scrolls = ((JComponent) c).getAutoscrolls();
841 ((JComponent) c).setAutoscrolls(false);
842 try {
843 dge.startDrag(null, t, this);
844 return;
845 } catch (RuntimeException re) {
846 ((JComponent) c).setAutoscrolls(scrolls);
847 }
848 }
849
850 th.exportDone((JComponent) c, null, NONE);
851 }
852
853 // --- DragSourceListener methods -----------------------------------
854
855 /**
856 * as the hotspot enters a platform dependent drop site
857 */
858 public void dragEnter(DragSourceDragEvent dsde) {
859 }
860
861 /**
862 * as the hotspot moves over a platform dependent drop site
863 */
864 public void dragOver(DragSourceDragEvent dsde) {
865 }
866
867 /**
868 * as the hotspot exits a platform dependent drop site
869 */
870 public void dragExit(DragSourceEvent dsde) {
871 }
872
873 /**
874 * as the operation completes
875 */
876 public void dragDropEnd(DragSourceDropEvent dsde) {
877 DragSourceContext dsc = dsde.getDragSourceContext();
878 JDNDAdapter c = (JDNDAdapter) dsc.getComponent();
879 if (dsde.getDropSuccess()) {
880 c.getTransferHandler().exportDone(
881 (JComponent) c,
882 dsc.getTransferable(),
883 dsde.getDropAction());
884 } else {
885 c.getTransferHandler().exportDone((JComponent) c, null, NONE);
886 }
887 ((JComponent) c).setAutoscrolls(scrolls);
888 }
889
890 public void dropActionChanged(DragSourceDragEvent dsde) {
891 }
892 }
893
894 private static class SwingDragGestureRecognizer
895 extends DragGestureRecognizer {
896
897 SwingDragGestureRecognizer(DragGestureListener dgl) {
898 super(DragSource.getDefaultDragSource(), null, NONE, dgl);
899 }
900
901 void gestured(JComponent c, MouseEvent e, int srcActions, int action) {
902 setComponent(c);
903 setSourceActions(srcActions);
904 appendEvent(e);
905 fireDragGestureRecognized(action, e.getPoint());
906 }
907
908 /**
909 * register this DragGestureRecognizer's Listeners with the Component
910 */
911 protected void registerListeners() {
912 }
913
914 /**
915 * unregister this DragGestureRecognizer's Listeners with the Component
916 *
917 * subclasses must override this method
918 */
919 protected void unregisterListeners() {
920 }
921
922 }
923
924 static final Action cutAction = new TransferAction("cut");
925 static final Action copyAction = new TransferAction("copy");
926 static final Action pasteAction = new TransferAction("paste");
927
928 static class TransferAction extends AbstractAction implements UIResource {
929
930 TransferAction(String name) {
931 super(name);
932 // Will cause the system clipboard state to be updated.
933 canAccessSystemClipboard = true;
934 canAccessSystemClipboard();
935 }
936
937 public void actionPerformed(ActionEvent e) {
938 Object src = e.getSource();
939 if (src instanceof JDNDAdapter) {
940 JDNDAdapter c = (JDNDAdapter) src;
941 TransferHandler th = c.getTransferHandler();
942 Clipboard clipboard = getClipboard((JComponent) c);
943 String name = (String) getValue(Action.NAME);
944 if ((clipboard != null) && (th != null) && (name != null)) {
945 if ("cut".equals(name)) {
946 th.exportToClipboard((JComponent) c, clipboard, MOVE);
947 } else if ("copy".equals(name)) {
948 th.exportToClipboard((JComponent) c, clipboard, COPY);
949 } else if ("paste".equals(name)) {
950 Transferable trans = clipboard.getContents(null);
951 if (trans != null) {
952 th.importData((JComponent) c, trans);
953 }
954 }
955 }
956 }
957 }
958
959 /**
960 * Returns the clipboard to use for cut/copy/paste.
961 */
962 private Clipboard getClipboard(JComponent c) {
963 if (canAccessSystemClipboard()) {
964 return c.getToolkit().getSystemClipboard();
965 }
966 Clipboard clipboard =
967 (Clipboard) sun.awt.AppContext.getAppContext().get(
968 SandboxClipboardKey);
969 if (clipboard == null) {
970 clipboard = new Clipboard("Sandboxed Component Clipboard");
971 sun.awt.AppContext.getAppContext().put(
972 SandboxClipboardKey,
973 clipboard);
974 }
975 return clipboard;
976 }
977
978 /**
979 * Returns true if it is safe to access the system Clipboard.
980 * If the environment is headless or the security manager
981 * does not allow access to the system clipboard, a private
982 * clipboard is used.
983 */
984 private boolean canAccessSystemClipboard() {
985 if (canAccessSystemClipboard) {
986 if (false) { //(GraphicsEnvironment.isHeadless()) {
987 canAccessSystemClipboard = false;
988 return false;
989 }
990
991 SecurityManager sm = System.getSecurityManager();
992 if (sm != null) {
993 try {
994 sm.checkSystemClipboardAccess();
995 return true;
996 } catch (SecurityException se) {
997 canAccessSystemClipboard = false;
998 return false;
999 }
1000 }
1001 return true;
1002 }
1003 return false;
1004 }
1005
1006 /**
1007 * Indicates if it is safe to access the system clipboard. Once false,
1008 * access will never be checked again.
1009 */
1010 private boolean canAccessSystemClipboard;
1011
1012 /**
1013 * Key used in app context to lookup Clipboard to use if access to
1014 * System clipboard is denied.
1015 */
1016 private static Object SandboxClipboardKey = new Object();
1017
1018 }
1019
1020 }
1021 ]]></echo>
1022 </target>
1023
1024 </project>
1025

  ViewVC Help
Powered by ViewVC 1.1.26