Je n'ai pas compris ce code
Salut tout le monde, j'ai vu ce code dans un cours java, mais je n'ai pas compris le code de la classe TreeTransferHandler et plus précisément la méthode importData.
Voici le code :
La classe MyTransferHandler
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
public class MyTransferHandler extends TransferHandler{
public boolean canImport(TransferHandler.TransferSupport info) {
if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) {
return false;
}
return true;
}
public boolean importData(TransferHandler.TransferSupport support){
if(!canImport(support))
return false;
Transferable data = support.getTransferable();
String str = "";
try {
str = (String)data.getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
JLabel lab = (JLabel)support.getComponent();
lab.setText(str);
return false;
}
protected void exportDone(JComponent c, Transferable t, int action){
if(action == MOVE){
JLabel lab = (JLabel)c;
String text = lab.getText();
int indice = Integer.parseInt(text.substring(text.length()-1, text.length()));
lab.setText(text.substring(0, text.length()-1) + (++indice));
}
}
protected Transferable createTransferable(JComponent c) {
return new StringSelection(((JLabel)c).getText());
}
public int getSourceActions(JComponent c) {
return MOVE;
}
} |
La classe TreeTransferHandler
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
public class TreeTransferHandler extends TransferHandler{
JTree tree;
public TreeTransferHandler(JTree tree){
this.tree = tree;
}
public boolean canImport(TransferHandler.TransferSupport info) {
if (!info.isDataFlavorSupported(DataFlavor.stringFlavor))
return false;
return true;
}
public boolean importData(TransferHandler.TransferSupport support){
if(!canImport(support))
return false;
//On récupère l'endroit du drop via un objet approprié
JTree.DropLocation dl = (JTree.DropLocation)support.getDropLocation();
//Les informations afin de pouvoir créer un nouvel élément
TreePath path = dl.getPath();
int index = dl.getChildIndex();
//Comme pour le JLabel, on récupère les données
Transferable data = support.getTransferable();
String str = "";
try {
str = (String)data.getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//On peut maintenant ajouter le nud
DefaultMutableTreeNode nouveau = new DefaultMutableTreeNode(str);
//On déduit le nud parent via le chemin
DefaultMutableTreeNode parent = (DefaultMutableTreeNode)path.getLastPathComponent();
DefaultTreeModel model = (DefaultTreeModel)this.tree.getModel();
index = (index == -1) ? model.getChildCount(path.getLastPathComponent()) : index ;
model.insertNodeInto(nouveau, parent, index);
tree.makeVisible(path.pathByAddingChild(nouveau));
tree.scrollPathToVisible(path);
return true;
}
public int getSourceActions(JComponent c) {
return COPY_OR_MOVE;
}
} |
La classe TreeDragDemo
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
public class TreeDragDemo extends JFrame{
JTree tree;
public TreeDragDemo(){
setTitle("Drag'n Drop avec un JLabel !");
setSize(400, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pan = new JPanel();
pan.setLayout(new GridLayout(1, 1));
pan.setBackground(Color.white);
JLabel srcLib = new JLabel("Source de drag : ", JLabel.RIGHT);
JLabel src = new JLabel("Noeud 1");
//------------------------------------------------------
//On utilise notre nouvel objet MyTransferHandle
src.setTransferHandler(new MyTransferHandler());
src.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
JComponent lab = (JComponent)e.getSource();
TransferHandler handle = lab.getTransferHandler();
handle.exportAsDrag(lab, e, TransferHandler.MOVE);
}
});
//------------------------------------------------------
JLabel destLib = new JLabel("Destination de drag : ", JLabel.RIGHT);
JTextField dest = new JTextField();
dest.setDragEnabled(true);
tree = new JTree(getModel());
tree.setDragEnabled(true);
tree.setTransferHandler(new TreeTransferHandler(tree));
pan.add(src);
pan.add(new JScrollPane(tree));
//Pour le choix des actions
JComboBox combo = new JComboBox();
combo.addItem("USE_SELECTION");
combo.addItem("ON");
combo.addItem("INSERT");
combo.addItem("ON_OR_INSERT");
combo.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent event) {
String value = event.getItem().toString();
if(value.equals("USE_SELECTION"))
tree.setDropMode(DropMode.USE_SELECTION);
if(value.equals("ON"))
tree.setDropMode(DropMode.ON);
if(value.equals("INSERT"))
tree.setDropMode(DropMode.INSERT);
if(value.equals("ON_OR_INSERT"))
tree.setDropMode(DropMode.ON_OR_INSERT);
}
});
add(pan, BorderLayout.CENTER);
add(combo, BorderLayout.SOUTH);
setVisible(true);
}
private TreeModel getModel(){
DefaultMutableTreeNode root = new DefaultMutableTreeNode("SDZ");
DefaultMutableTreeNode forum = new DefaultMutableTreeNode("Forum");
forum.add(new DefaultMutableTreeNode("C++"));
forum.add(new DefaultMutableTreeNode("Java"));
forum.add(new DefaultMutableTreeNode("PHP"));
DefaultMutableTreeNode tuto = new DefaultMutableTreeNode("Tutoriel");
tuto.add(new DefaultMutableTreeNode("Tutoriel"));
tuto.add(new DefaultMutableTreeNode("Programmation"));
tuto.add(new DefaultMutableTreeNode("Mapping"));
root.add(tuto);
root.add(forum);
return new DefaultTreeModel(root);
}
public static void main(String[] args){
new TreeDragDemo();
}
} |
J'ai besoin de votre aide svp . Merci beaucoup !