Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
DragExchange.java
1 
16 package net.squiz.matrix.ui;
17 
18 import java.awt.datatransfer.*;
19 import net.squiz.matrix.matrixtree.MatrixTreeTransferable;
20 import java.awt.*;
21 import java.util.*;
22 import javax.swing.tree.*;
23 
24 public class DragExchange {
25 
26  private static MatrixTreeTransferable transfer;
27  private static Draggable dragSource;
28  private static boolean inExchange = false;
29  // cannot instantiate
30  private DragExchange() {}
31 
32  public static void setTransferable(Draggable dragSource, MatrixTreeTransferable transfer) {
33  if (inExchange)
34  throw new IllegalStateException("There is already an exchange open by " + dragSource.getClass());
35  DragExchange.transfer = transfer;
36  DragExchange.dragSource = dragSource;
37  inExchange = true;
38  }
39 
40  public static MatrixTreeTransferable getTransferable() {
41  if (!inExchange)
42  throw new IllegalStateException("There is no exchange open");
43  return transfer;
44  }
45 
46  public static Draggable getSource() {
47  if (!inExchange)
48  throw new IllegalStateException("There is no exchange open");
49  return dragSource;
50  }
51 
52  public static TreePath[] getDragPaths() {
53  if (!inExchange)
54  throw new IllegalStateException("There is no exchange open");
55  java.util.List paths = null;
56  try {
57  paths = (java.util.List) transfer.getTransferData(
58  MatrixTreeTransferable.TREE_NODE_FLAVOUR);
59  } catch (UnsupportedFlavorException ufe) {
60  // GUIUtilties.error();
61  ufe.printStackTrace();
62  } catch (java.io.IOException ioe) {
63  // GUIUtilties.error();
64  ioe.printStackTrace();
65  }
66  return (TreePath[]) paths.toArray(new TreePath[paths.size()]);
67  }
68 
69  public static Image translateImage(Draggable draggable) {
70  if (!inExchange)
71  throw new IllegalStateException("There is no exchange open");
72  TreePath[] pathsArr = getDragPaths();
73 
74  return draggable.getDragImage(pathsArr);
75  }
76 
77  public static void completeExchange() {
78  if (!inExchange)
79  throw new IllegalStateException("There is no exchange open");
80  transfer = null;
81  dragSource = null;
82  inExchange = false;
83  }
84 }