Statistics
| Revision:

svn-gvsig-desktop / branches / gvSIG_1.11.0_Mejoras_gvSIG-EIEL / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toc / DnDJTree.java @ 35646

History | View | Annotate | Download (21.7 KB)

1
/*
2
 * Created on 02-jun-2004
3
 *
4
 */
5
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
6
 *
7
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27
 *   Av. Blasco Ib??ez, 50
28
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45
package com.iver.cit.gvsig.project.documents.view.toc;
46
/*
47
**  This is version II of DnDJTree. The first version allowed for what I
48
**  thought was a JDK oversight. However, we can set the cursor appropriately,
49
**  relative to whether the current cursor location is a valid drop target.
50
**
51
**  If this is your first time reading the source code. Just ignore the above
52
**  comment and ignore the "CHANGED" comments below. Otherwise, the
53
**  "CHANGED" comments will show where the code has changed.
54
**
55
**  Credit for finding this shortcoming in my code goes Laurent Hubert.
56
**  Thanks Laurent.
57
**
58
**  Rob. [ rkenworthy@hotmail.com ]
59
*/
60

    
61

    
62
import java.awt.Component;
63
import java.awt.Dimension;
64
import java.awt.Point;
65
import java.awt.datatransfer.Transferable;
66
import java.awt.datatransfer.UnsupportedFlavorException;
67
import java.awt.dnd.DnDConstants;
68
import java.awt.dnd.DragGestureEvent;
69
import java.awt.dnd.DragGestureListener;
70
import java.awt.dnd.DragGestureRecognizer;
71
import java.awt.dnd.DragSource;
72
import java.awt.dnd.DragSourceDragEvent;
73
import java.awt.dnd.DragSourceDropEvent;
74
import java.awt.dnd.DragSourceEvent;
75
import java.awt.dnd.DragSourceListener;
76
import java.awt.dnd.DropTarget;
77
import java.awt.dnd.DropTargetDragEvent;
78
import java.awt.dnd.DropTargetDropEvent;
79
import java.awt.dnd.DropTargetEvent;
80
import java.awt.dnd.DropTargetListener;
81
import java.awt.dnd.MouseDragGestureRecognizer;
82
import java.awt.event.InputEvent;
83
import java.awt.event.MouseEvent;
84
import java.io.IOException;
85
import java.util.ArrayList;
86

    
87
import javax.swing.JTree;
88
import javax.swing.SwingUtilities;
89
import javax.swing.event.TreeSelectionEvent;
90
import javax.swing.event.TreeSelectionListener;
91
import javax.swing.tree.DefaultMutableTreeNode;
92
import javax.swing.tree.DefaultTreeModel;
93
import javax.swing.tree.MutableTreeNode;
94
import javax.swing.tree.TreeCellRenderer;
95
import javax.swing.tree.TreeModel;
96
import javax.swing.tree.TreeNode;
97
import javax.swing.tree.TreePath;
98

    
99
import com.iver.andami.PluginServices;
100
import com.iver.cit.gvsig.fmap.layers.FLayer;
101
import com.iver.cit.gvsig.fmap.layers.FLayers;
102
import com.iver.cit.gvsig.project.documents.view.gui.View;
103
import com.iver.cit.gvsig.project.documents.view.toc.gui.TOCRenderer;
104
import com.iver.cit.gvsig.project.documents.view.toc.util.DropTargetInfo;
105

    
106
public class DnDJTree extends JTree
107
                  implements TreeSelectionListener,
108
                  DragGestureListener, DropTargetListener,
109
                  DragSourceListener {
110

    
111
  protected ArrayList m_Listeners = new ArrayList();
112

    
113
  private static DnDJTree oDnDtocOrigin = null;
114
  private static DnDJTree oDnDtocDestination = null;
115
  /** Stores the parent Frame of the component */
116
  // private Frame Parent = null;
117

    
118
  /** Stores the selected node info */
119
  protected TreePath SelectedTreePath = null;
120
  protected DefaultMutableTreeNode SelectedNode = null;
121

    
122
  /** Variables needed for DnD */
123
  private DragSource dragSource = null;
124
  //private DragSourceContext dragSourceContext = null;
125
  private DropTarget dropTarget;
126
  //private ArrayList treeListeners=new ArrayList();
127
  //private ArrayList dropListeners=new ArrayList();
128
 // private TreeModel model1;
129
  
130
  
131
  /** Constructor
132
  @param root The root node of the tree
133
  @param parent Parent JFrame of the JTree */
134
  public DnDJTree(TreeModel treeModel) {
135
    super(treeModel);
136
    // Parent = parent;
137

    
138
    //addTreeSelectionListener(this);
139

    
140
                /* ********************** CHANGED ********************** */
141
    dragSource = DragSource.getDefaultDragSource() ;
142
                /* ****************** END OF CHANGE ******************** */
143

    
144
    DragGestureRecognizer dgr =
145
      dragSource.createDefaultDragGestureRecognizer(
146
        this,                             //DragSource
147
        DnDConstants.ACTION_COPY_OR_MOVE, //specifies valid actions
148
        this                              //DragGestureListener
149
      );
150

    
151

    
152
    /* Eliminates right mouse clicks as valid actions - useful especially
153
     * if you implement a JPopupMenu for the JTree
154
     */
155
    dgr.setSourceActions(dgr.getSourceActions() & ~InputEvent.BUTTON3_MASK);
156

    
157
    /* First argument:  Component to associate the target with
158
     * Second argument: DropTargetListener
159
    */
160
    //DropTarget dropTarget = new DropTarget(this, this);
161
    setDropTarget();
162
  }
163
  public void invalidateListeners(){
164
          removeDropListener();
165
          removeTreeListener();
166
  }
167
  private void addDropListener(){
168
          dropTarget= new DropTarget(this, this);
169
  }
170
  private void removeDropListener(){
171
          dropTarget=null;
172
  }
173
  private void addTreeListener(){
174
          addTreeSelectionListener(this);
175
  }
176
  private void removeTreeListener(){
177
          removeTreeSelectionListener(this);
178
  }
179
  public void setDropTarget(){
180
      // TODO: COMENTADO POR FJP
181
          /* com.iver.andami.ui.mdiManager.View[] views=PluginServices.getMDIManager().getAllViews();
182
          for(int i=0;i<views.length;i++){
183
                  if (views[i] instanceof View){
184
                         // model1=((View)views[i]).getTOC().getTree().getModel();
185
                          ((View)views[i]).getTOC().getTree().removeTreeListener();
186
                          ((View)views[i]).getTOC().getTree().addTreeListener();
187
                          ((View)views[i]).getTOC().getTree().removeDropListener();
188
                          ((View)views[i]).getTOC().getTree().addDropListener();
189
                  } */
190
                  addTreeListener();
191
                  addDropListener();
192
          // }
193
         ////////// new DropTarget(this, this);
194
  }
195
  /** Returns The selected node */
196
  public DefaultMutableTreeNode getSelectedNode() {
197
    return SelectedNode;
198
  }
199

    
200
  ///////////////////////// Interface stuff ////////////////////
201

    
202

    
203
  /** DragGestureListener interface method */
204
  public void dragGestureRecognized(DragGestureEvent e) {
205
          if (((MouseEvent)((MouseDragGestureRecognizer)e.getSource()).getTriggerEvent()).getButton()==MouseEvent.BUTTON3){
206
                  return;
207
          }
208
    //Get the selected node
209
    DefaultMutableTreeNode dragNode = getSelectedNode();
210
    if (dragNode != null) {
211

    
212

    
213
      if (!(dragNode.getUserObject() instanceof Transferable)) return;
214

    
215
//    Get the Transferable Object
216
      Transferable transferable = (Transferable) dragNode.getUserObject();
217

    
218
                        /* ********************** CHANGED ********************** */
219

    
220
      //Select the appropriate cursor;
221
      // Cursor cursor = DragSource.DefaultCopyNoDrop;
222
      int action = e.getDragAction();
223
      /* if (action == DnDConstants.ACTION_MOVE)
224
        cursor = DragSource.DefaultMoveDrop; */
225

    
226

    
227
      //In fact the cursor is set to NoDrop because once an action is rejected
228
      // by a dropTarget, the dragSourceListener are no more invoked.
229
      // Setting the cursor to no drop by default is so more logical, because
230
      // when the drop is accepted by a component, then the cursor is changed by the
231
      // dropActionChanged of the default DragSource.
232
                        /* ****************** END OF CHANGE ******************** */
233

    
234
      //begin the drag
235
      oDnDtocOrigin = this;
236
      dragSource.startDrag(e, null, transferable, this);
237
      
238
      System.out.println("+++ dragGestureRecognized");
239
    }
240
  }
241

    
242
  /** DragSourceListener interface method */
243
  public void dragDropEnd(DragSourceDropEvent dsde) {
244
          showEvent("DRAG DROP END", dsde);
245
          clearGraphicTarget();
246
  }
247

    
248
  /** DragSourceListener interface method */
249
  public void dragEnter(DragSourceDragEvent dsde) {
250
          showEvent("DRAG ENTER", dsde);
251
                /* ********************** CHANGED ********************** */
252
      // System.err.println("dragOver" + dsde.getDragSourceContext().getComponent());
253

    
254
                /* ****************** END OF CHANGE ******************** */
255
  }
256

    
257
  /** DragSourceListener interface method */
258
  public void dragOver(DragSourceDragEvent dsde) {
259
      Point screen_loc = dsde.getLocation();
260
      Point loc = (Point) screen_loc.clone();
261
      SwingUtilities.convertPointFromScreen(loc, this);
262
      // TreePath auxDragTarget = getPathForLocation(loc.x, loc.y);
263

    
264
      TreePath auxDragTarget = getPathForLocation(loc.x, loc.y);
265
      if (auxDragTarget == null) {
266
              return;
267
      }
268
          TreeCellRenderer tcr = getCellRenderer();
269
          TOCRenderer tocr = null;
270
          if (tcr instanceof TOCRenderer) {
271
                  tocr = (TOCRenderer) tcr;
272
          } else {
273
                  return;
274
          }
275
          
276

    
277
      DefaultMutableTreeNode dmtn =
278
              (DefaultMutableTreeNode) auxDragTarget.getLastPathComponent();
279
      
280
      Object obj = dmtn.getUserObject();
281
      if (obj instanceof ITocItem) {
282
              ITocItem toc_it = (ITocItem) obj;
283
              DropTargetInfo dti = new DropTargetInfo(toc_it, screen_loc);
284
              tocr.setGraphicIndicatorInfo(dti);
285
              this.repaint();
286
      }
287
  }
288

    
289
  private void clearGraphicTarget() {
290
          TreeCellRenderer tcr = getCellRenderer();
291
          TOCRenderer tocr = null;
292
          if (tcr instanceof TOCRenderer) {
293
                  tocr = (TOCRenderer) tcr;
294
                  tocr.setGraphicIndicatorInfo(null);
295
          } else {
296
                  return;
297
          }
298

    
299
          
300
  }
301
  
302
  
303
  /** DragSourceListener interface method */
304
  public void dropActionChanged(DragSourceDragEvent dsde) {
305
          showEvent("DROP ACT CHANGED", dsde);
306
  }
307

    
308
  /** DragSourceListener interface method */
309
  public void dragExit(DragSourceEvent dsde) {
310
          showEvent("DRAG EXIT", dsde);
311
  }
312

    
313
  private void showEvent(String msg, DragSourceEvent ev) {
314
          
315
          System.out.println("+++ " + msg + ": " + ev.getDragSourceContext().getComponent());
316
      Point loc = ev.getLocation();
317
      SwingUtilities.convertPointFromScreen(loc, this);
318
      TreePath destinationPath = getPathForLocation(loc.x, loc.y);
319
          System.out.println("    dest (" + loc.x + " , " + loc.y + "): " +
320
                          (destinationPath == null ? "NULL" : destinationPath.toString()));
321
  }
322
  
323
  /** DropTargetListener interface method - What we do when drag is released */
324
  public void drop(DropTargetDropEvent e) {
325
    try {
326
      Transferable tr = e.getTransferable();
327
      //flavor not supported, reject drop
328
      if (!tr.isDataFlavorSupported( TocItemBranch.INFO_FLAVOR)) e.rejectDrop();
329
      //cast into appropriate data type
330
      TocItemBranch childInfo =
331
        (TocItemBranch) tr.getTransferData( TocItemBranch.INFO_FLAVOR );
332
      //get new parent node
333
      Point loc = e.getLocation();
334
      TreePath destinationPath = getPathForLocation(loc.x, loc.y);
335
      
336
      
337
          System.out.println("+++ " + "DROP");
338
          System.out.println("    dest (" + loc.x + " , " + loc.y + "): " +
339
                          (destinationPath == null ? "NULL" : destinationPath.toString()));
340

    
341
          
342

    
343
      final String msg = testDropTarget(destinationPath, SelectedTreePath);
344
      if (msg != null) {
345
          /* if (testSameComponent())
346
          { */
347
              e.rejectDrop();
348
              SwingUtilities.invokeLater(new Runnable() {
349
                  public void run() {
350
                      System.err.println(msg);
351
                  }
352
              });
353
              clearGraphicTarget();
354
              return;
355
          /* }
356
          else
357
          {
358
              // TODO: Por ahora solo dejamos mover, no copiar
359
              // TODO: ?Y qu? pasa si la vista a la que vamos
360
              // no tiene la misma proyecci?n? MEJOR DESHABILITO ESTO POR AHORA
361
              if (e.getDropAction() == DnDConstants.ACTION_MOVE)
362
                  dropRoot(SelectedNode);
363
              else
364
                  e.rejectDrop();
365
              return;
366
          } */
367
      }
368
      if (!testSameComponent(e))
369
      {
370
          e.rejectDrop();
371
          clearGraphicTarget();
372
          return;
373
      }
374
      int oldPos,newPos;
375
          //boolean isContainer=false;
376

    
377
          DefaultMutableTreeNode nodoTocado =
378
        (DefaultMutableTreeNode) destinationPath.getLastPathComponent();
379
          //        get old parent node
380
      DefaultMutableTreeNode oldParent = (DefaultMutableTreeNode) getSelectedNode().getParent();
381
      if (nodoTocado.getParent().equals(getSelectedNode())){
382
              clearGraphicTarget();
383
              return;
384
      }
385
      //oldParent.setUserObject(new TocItemBranch(((TocItemBranch)getSelectedNode().getUserObject()).getLayer().getParentLayer()));
386
          if (oldParent==null) {
387
                  clearGraphicTarget();
388
                  return;
389
          }
390
                  
391
      oldPos = oldParent.getIndex(getSelectedNode());
392
          // Para no tener en cuenta los nodos de s?mbolos:
393
      if (!(nodoTocado.getUserObject() instanceof TocItemBranch)){
394
                      nodoTocado = (DefaultMutableTreeNode) nodoTocado.getParent();
395
      }
396

    
397
      ///posActual = oldParent.getIndex(getSelectedNode());
398
      //Destino
399
          DefaultMutableTreeNode destParent=null;
400

    
401
          if (((TocItemBranch)nodoTocado.getUserObject()).getLayer() instanceof FLayers){
402
                  //isContainer=true;
403
                  newPos=0;
404
                  destParent=nodoTocado;
405
      }else{//Si donde se deja la capa seleccionada no es un contenedor de capas.
406
                destParent= (DefaultMutableTreeNode)nodoTocado.getParent();
407
        newPos=destParent.getIndex(nodoTocado);
408
      }
409

    
410

    
411

    
412

    
413
      int action = e.getDropAction();
414
      boolean copyAction = (action == DnDConstants.ACTION_COPY);
415

    
416
      //make new child node
417
      DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(childInfo);
418
          if (getSelectedNode().getAllowsChildren()){
419
                  int childs=getSelectedNode().getChildCount();
420

    
421
                  for (int i=0;i<childs;i++){
422
                          newChild.add((MutableTreeNode)getSelectedNode().getChildAt(0));
423
                  }
424
          }
425

    
426
          try {
427
        if (!copyAction){
428
                oldParent.remove(getSelectedNode());
429
            destParent.insert(newChild,newPos);
430
            // newParent.add(newChild);
431
        }
432
        if (copyAction) e.acceptDrop (DnDConstants.ACTION_COPY);
433
        else e.acceptDrop (DnDConstants.ACTION_MOVE);
434
      }
435
      catch (java.lang.IllegalStateException ils) {
436
        e.rejectDrop();
437
      }
438

    
439
      e.getDropTargetContext().dropComplete(true);
440

    
441
      //expand nodes appropriately - this probably isnt the best way...
442

    
443
      // TODO: COMENTADO POR FJP
444
      /* com.iver.andami.ui.mdiManager.View[] views=PluginServices.getMDIManager().getAllViews();
445
          for(int i=0;i<views.length;i++){
446
                  if (views[i] instanceof View){
447
                          ((DefaultTreeModel)((View)views[i]).getTOC().getTree().getModel()).reload(oldParent);
448
                  }
449
          } */
450

    
451

    
452

    
453
     // ((DefaultTreeModel)model1).reload(oldParent);
454
      DefaultTreeModel model = (DefaultTreeModel) getModel();
455

    
456
          model.reload(getSelectedNode().getRoot());
457
          FLayers lpo=null;
458
          FLayers lpd=null;
459

    
460
          if (oldParent.getUserObject() instanceof TocItemBranch){
461
          lpo=(FLayers)((TocItemBranch)oldParent.getUserObject()).getLayer();
462
          //lpd=(FLayers)((TocItemBranch)destParent.getUserObject()).getLayer();
463
          }/*else if (((TocItemBranch)nodoTocado.getUserObject()).getLayer().getParentLayer()!=null){
464
                  lpo=((TocItemBranch)nodoTocado.getUserObject()).getLayer().getParentLayer();
465
          }*/else{
466
                  lpo=(FLayers)((TocItemBranch)getSelectedNode().getUserObject()).getLayer().getParentLayer();
467
          }
468
                  if (destParent.getUserObject() instanceof TocItemBranch){
469
                  lpd=(FLayers)((TocItemBranch)destParent.getUserObject()).getLayer();
470
                  }else{
471
                  lpd=((TocItemBranch)nodoTocado.getUserObject()).getLayer().getParentLayer();
472
                  }
473

    
474

    
475
          if (destParent.equals(oldParent)){
476
                  callListeners(oldPos,newPos,lpd);
477
          }else{
478
                  callListeners(lpo,lpd,((TocItemBranch)newChild.getUserObject()).getLayer());
479
          }
480
          }
481

    
482
    catch (IOException io) { e.rejectDrop(); }
483
    catch (UnsupportedFlavorException ufe) {e.rejectDrop();}
484
    
485
    clearGraphicTarget();
486
    
487
  } //end of method
488
public void dropRoot(TreeNode tn){
489
        int oldPos,newPos;
490
        DefaultMutableTreeNode nodoTocado =
491
                (DefaultMutableTreeNode) tn;
492
                  //        get old parent node
493
            if (getSelectedNode()==null)return;
494
                DefaultMutableTreeNode oldParent = (DefaultMutableTreeNode) getSelectedNode().getParent();
495
                  if (oldParent!=null){
496
              oldPos = oldParent.getIndex(getSelectedNode());
497
              //Destino
498
                  DefaultMutableTreeNode destParent=null;
499
                  newPos=0;
500
                  destParent=nodoTocado;
501

    
502
              //make new child node
503
              DefaultMutableTreeNode newChild = (DefaultMutableTreeNode)getSelectedNode().clone();
504
              oldParent.remove(getSelectedNode());
505
              destParent.insert(newChild,newPos);
506

    
507
              com.iver.andami.ui.mdiManager.IWindow[] views=PluginServices.getMDIManager().getAllWindows();
508
                  for(int i=0;i<views.length;i++){
509
                          if (views[i] instanceof View){
510
                                  ((DefaultTreeModel)((View)views[i]).getTOC().getTree().getModel()).reload(oldParent);
511
                          }
512
                  }
513
             // ((DefaultTreeModel)model1).reload(oldParent);
514
              DefaultTreeModel model = (DefaultTreeModel) getModel();
515
                  model.reload(destParent);
516
                  FLayers lpo=null;
517
                  FLayers lpd=null;
518

    
519
                  lpo=(FLayers)((TocItemBranch)getSelectedNode().getUserObject()).getLayer().getParentLayer();
520
                  for(int i=0;i<views.length;i++){
521
                          if (views[i] instanceof View){
522
                                  if (((View)views[i]).getTOC().getTree().equals(this)){
523
                                          lpd= ((View)views[i]).getMapControl().getMapContext().getLayers();
524
                                  }
525
                          }
526
                  }
527
                  if (destParent.equals(oldParent)){
528
                          callListeners(oldPos,newPos,lpd);
529
                  }else{
530
                          callListeners(lpo,lpd,((TocItemBranch)newChild.getUserObject()).getLayer());
531
                  }
532
                  }
533
        }
534

    
535
  /** DropTaregetListener interface method */
536
  public void dragEnter(DropTargetDragEvent e) {
537
  }
538

    
539
  /** DropTaregetListener interface method */
540
  public void dragExit(DropTargetEvent e) {
541
  }
542

    
543
  /** DropTaregetListener interface method */
544
  public void dragOver(DropTargetDragEvent e) {
545
                /* ********************** CHANGED ********************** */
546
    //set cursor location. Needed in setCursor method
547
    Point cursorLocationBis = e.getLocation();
548
        TreePath destinationPath =
549
      getPathForLocation(cursorLocationBis.x, cursorLocationBis.y);
550

    
551

    
552
    // if destination path is okay accept drop...
553

    
554
    if (testSameComponent(e))
555
    {
556
        String msg = testDropTarget(destinationPath, SelectedTreePath);
557
        if ( msg == null) {
558
            e.acceptDrag(DnDConstants.ACTION_MOVE) ;
559
            return;
560
        }
561

    
562
    }
563
    // ...otherwise reject drop
564
    // else {
565
        // System.err.println(e.getDropTargetContext().getComponent());
566

    
567
        // if (testSameComponent(e))
568
            e.rejectDrag() ;
569
        /* else
570
            e.acceptDrag(DnDConstants.ACTION_MOVE); */
571
    // }
572
                /* ****************** END OF CHANGE ******************** */
573
  }
574

    
575
  /** DropTaregetListener interface method */
576
  public void dropActionChanged(DropTargetDragEvent e) {
577
  }
578
  private void setSelectedNode(DefaultMutableTreeNode smtn){
579
          if (smtn!=null)
580
          SelectedNode=smtn;
581
  }
582

    
583
  /** TreeSelectionListener - sets selected node */
584
  public void valueChanged(TreeSelectionEvent evt) {
585
    SelectedTreePath = evt.getNewLeadSelectionPath();
586
    /* com.iver.andami.ui.mdiManager.View[] views=PluginServices.getMDIManager().getAllViews();
587
          for(int i=0;i<views.length;i++){
588
                  if (views[i] instanceof View){
589
                          if (SelectedTreePath == null) {
590
                                ((View)views[i]).getTOC().getTree().setSelectedNode(null);
591
                          }else{
592
                                   ((View)views[i]).getTOC().getTree().setSelectedNode((DefaultMutableTreeNode)SelectedTreePath.getLastPathComponent());
593
                          }
594
                  }
595
          } */
596
          if (SelectedTreePath == null){
597
                  setSelectedNode(null);
598
          }else{
599
                  setSelectedNode((DefaultMutableTreeNode)SelectedTreePath.getLastPathComponent());
600
          }
601
  }
602

    
603
  /** Convenience method to test whether drop location is valid
604
  @param destination The destination path
605
  @param dropper The path for the node to be dropped
606
  @return null if no problems, otherwise an explanation
607
  */
608
  private String testDropTarget(TreePath destination, TreePath dropper) {
609
    //Typical Tests for dropping
610

    
611
    //Test 1.
612
    boolean destinationPathIsNull = destination == null;
613
    if (destinationPathIsNull)
614
      return "Invalid drop location.";
615

    
616
    //Test 2.
617
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) destination.getLastPathComponent();
618
    if ( !node.getAllowsChildren() )
619
      return "This node does not allow children";
620

    
621
    if (destination.equals(dropper))
622
      return "Destination cannot be same as source";
623

    
624
    //Test 3.
625
    /* if ( dropper.isDescendant(destination))
626
       return "Destination node cannot be a descendant.";
627

628
    //Test 4.
629
    if ( dropper.getParentPath().equals(destination))
630
       return "Destination node cannot be a parent."; */
631

    
632
    return null;
633
  }
634

    
635
  private boolean testSameComponent(DropTargetEvent e)
636
  {
637
      oDnDtocDestination = this;
638
      return (oDnDtocOrigin == oDnDtocDestination);
639
  }
640
        /**
641
         * @param arg0
642
         * @return
643
         */
644
        public boolean addOrderListener(ITocOrderListener arg0) {
645
                return m_Listeners.add(arg0);
646
        }
647
        /**
648
         * @param arg0
649
         * @return
650
         */
651
        public boolean removeOrderListener(ITocOrderListener arg0) {
652
                return m_Listeners.remove(arg0);
653
        }
654
        private void callListeners(int oldPos, int newPos,FLayers lpd)
655
        {
656
//                lpd.getMapContext().clearAllCachingImageDrawnLayers();
657
                for (int i=0; i < m_Listeners.size(); i++)
658
                {
659
                        ITocOrderListener listener = (ITocOrderListener) m_Listeners.get(i);
660
                        listener.orderChanged(oldPos, newPos,lpd);
661
                }
662
        }
663

    
664
  private void callListeners(FLayers lpo,FLayers lpd,FLayer ls){
665
//          lpo.getMapContext().clearAllCachingImageDrawnLayers();
666
          for (int i=0; i < m_Listeners.size(); i++)
667
                {
668
                        ITocOrderListener listener = (ITocOrderListener) m_Listeners.get(i);
669
                        listener.parentChanged(lpo,lpd,ls);
670
                }
671
  }
672

    
673
} //end of DnDJTree