Revision 2078 branches/gvSIG_03_SLD/applications/appgvSIG/src/com/iver/cit/gvsig/gui/toc/DnDJTree.java
DnDJTree.java | ||
---|---|---|
88 | 88 |
import javax.swing.event.TreeSelectionListener; |
89 | 89 |
import javax.swing.tree.DefaultMutableTreeNode; |
90 | 90 |
import javax.swing.tree.DefaultTreeModel; |
91 |
import javax.swing.tree.MutableTreeNode; |
|
91 | 92 |
import javax.swing.tree.TreeModel; |
92 | 93 |
import javax.swing.tree.TreePath; |
93 | 94 |
|
95 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
|
96 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
|
97 |
|
|
94 | 98 |
public class DnDJTree extends JTree |
95 | 99 |
implements TreeSelectionListener, |
96 | 100 |
DragGestureListener, DropTargetListener, |
... | ... | |
215 | 219 |
public void drop(DropTargetDropEvent e) { |
216 | 220 |
try { |
217 | 221 |
Transferable tr = e.getTransferable(); |
218 |
|
|
219 | 222 |
//flavor not supported, reject drop |
220 | 223 |
if (!tr.isDataFlavorSupported( TocItemBranch.INFO_FLAVOR)) e.rejectDrop(); |
221 |
|
|
222 |
|
|
223 | 224 |
//cast into appropriate data type |
224 | 225 |
TocItemBranch childInfo = |
225 | 226 |
(TocItemBranch) tr.getTransferData( TocItemBranch.INFO_FLAVOR ); |
226 |
|
|
227 | 227 |
//get new parent node |
228 | 228 |
Point loc = e.getLocation(); |
229 | 229 |
TreePath destinationPath = getPathForLocation(loc.x, loc.y); |
... | ... | |
239 | 239 |
}); |
240 | 240 |
return; |
241 | 241 |
} |
242 |
int oldPos,newPos; |
|
243 |
boolean isContainer=false; |
|
242 | 244 |
|
243 |
int posActual,newPos; |
|
244 |
|
|
245 |
|
|
246 |
DefaultMutableTreeNode nodoTocado = |
|
245 |
DefaultMutableTreeNode nodoTocado = |
|
247 | 246 |
(DefaultMutableTreeNode) destinationPath.getLastPathComponent(); |
248 |
|
|
249 |
// Para no tener en cuenta los nodos de s?mbolos: |
|
250 |
if (!(nodoTocado.getUserObject() instanceof TocItemBranch)) |
|
251 |
{ |
|
247 |
// get old parent node |
|
248 |
DefaultMutableTreeNode oldParent = (DefaultMutableTreeNode) getSelectedNode().getParent(); |
|
249 |
oldPos = oldParent.getIndex(getSelectedNode()); |
|
250 |
// Para no tener en cuenta los nodos de s?mbolos: |
|
251 |
if (!(nodoTocado.getUserObject() instanceof TocItemBranch)){ |
|
252 | 252 |
nodoTocado = (DefaultMutableTreeNode) nodoTocado.getParent(); |
253 | 253 |
} |
254 |
|
|
255 |
//get old parent node |
|
256 |
DefaultMutableTreeNode oldParent = (DefaultMutableTreeNode) getSelectedNode().getParent(); |
|
257 |
posActual = oldParent.getIndex(getSelectedNode()); |
|
258 |
newPos = oldParent.getIndex(nodoTocado); |
|
259 | 254 |
|
255 |
///posActual = oldParent.getIndex(getSelectedNode()); |
|
256 |
//Destino |
|
257 |
DefaultMutableTreeNode destParent=null; |
|
258 |
|
|
259 |
if (((TocItemBranch)nodoTocado.getUserObject()).getLayer() instanceof FLayers){ |
|
260 |
isContainer=true; |
|
261 |
newPos=0; |
|
262 |
destParent=nodoTocado; |
|
263 |
}else{//Si donde se deja la capa seleccionada no es un contenedor de capas. |
|
264 |
destParent= (DefaultMutableTreeNode)nodoTocado.getParent(); |
|
265 |
newPos=destParent.getIndex(nodoTocado); |
|
266 |
} |
|
267 |
|
|
260 | 268 |
|
269 |
|
|
261 | 270 |
|
262 | 271 |
int action = e.getDropAction(); |
263 | 272 |
boolean copyAction = (action == DnDConstants.ACTION_COPY); |
264 | 273 |
|
265 | 274 |
//make new child node |
266 | 275 |
DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(childInfo); |
267 |
|
|
268 |
try { |
|
276 |
if (getSelectedNode().getAllowsChildren()){ |
|
277 |
int childs=getSelectedNode().getChildCount(); |
|
278 |
|
|
279 |
for (int i=0;i<childs;i++){ |
|
280 |
newChild.add((MutableTreeNode)getSelectedNode().getChildAt(0)); |
|
281 |
} |
|
282 |
} |
|
283 |
|
|
284 |
try { |
|
269 | 285 |
if (!copyAction) oldParent.remove(getSelectedNode()); |
270 |
oldParent.insert(newChild,newPos);
|
|
286 |
destParent.insert(newChild,newPos);
|
|
271 | 287 |
// newParent.add(newChild); |
272 | 288 |
|
273 | 289 |
if (copyAction) e.acceptDrop (DnDConstants.ACTION_COPY); |
... | ... | |
276 | 292 |
catch (java.lang.IllegalStateException ils) { |
277 | 293 |
e.rejectDrop(); |
278 | 294 |
} |
279 |
|
|
295 |
|
|
280 | 296 |
e.getDropTargetContext().dropComplete(true); |
281 | 297 |
|
282 | 298 |
//expand nodes appropriately - this probably isnt the best way... |
283 | 299 |
DefaultTreeModel model = (DefaultTreeModel) getModel(); |
284 | 300 |
model.reload(oldParent); |
285 |
|
|
286 |
callListeners(posActual, newPos); |
|
301 |
|
|
302 |
model.reload(destParent); |
|
303 |
if (oldParent.getUserObject() instanceof TocItemBranch){ |
|
304 |
FLayers lpo=(FLayers)((TocItemBranch)oldParent.getUserObject()).getLayer(); |
|
305 |
FLayers lpd=(FLayers)((TocItemBranch)destParent.getUserObject()).getLayer(); |
|
306 |
|
|
307 |
if (destParent.equals(oldParent)){ |
|
308 |
callListeners(oldPos,newPos,lpd); |
|
309 |
///callListeners((FLayers)((TocItemBranch)oldParent.getUserObject()).getLayer(),(FLayers)((TocItemBranch)destParent.getUserObject()).getLayer(),((TocItemBranch)newChild.getUserObject()).getLayer(),newPos); |
|
310 |
}else{ |
|
311 |
callListeners(lpo,lpd,((TocItemBranch)newChild.getUserObject()).getLayer()); |
|
312 |
/// callListeners(newPos,newPos+1); |
|
313 |
} |
|
314 |
} |
|
315 |
///callListeners(posActual, newPos); |
|
287 | 316 |
/* model.reload(newParent); |
288 | 317 |
TreePath parentPath = new TreePath(newParent.getPath()); |
289 | 318 |
expandPath(parentPath); */ |
... | ... | |
383 | 412 |
public boolean removeOrderListener(ITocOrderListener arg0) { |
384 | 413 |
return m_Listeners.remove(arg0); |
385 | 414 |
} |
386 |
private void callListeners(int oldPos, int newPos) |
|
415 |
private void callListeners(int oldPos, int newPos,FLayers lpd)
|
|
387 | 416 |
{ |
388 | 417 |
for (int i=0; i < m_Listeners.size(); i++) |
389 | 418 |
{ |
390 | 419 |
ITocOrderListener listener = (ITocOrderListener) m_Listeners.get(i); |
391 |
listener.orderChanged(oldPos, newPos); |
|
420 |
listener.orderChanged(oldPos, newPos,lpd);
|
|
392 | 421 |
} |
393 | 422 |
} |
394 |
|
|
423 |
|
|
424 |
private void callListeners(FLayers lpo,FLayers lpd,FLayer ls){ |
|
425 |
for (int i=0; i < m_Listeners.size(); i++) |
|
426 |
{ |
|
427 |
ITocOrderListener listener = (ITocOrderListener) m_Listeners.get(i); |
|
428 |
listener.parentChanged(lpo,lpd,ls); |
|
429 |
} |
|
430 |
} |
|
395 | 431 |
|
396 | 432 |
} //end of DnDJTree |
Also available in: Unified diff