JTree cut single and multiple child and ancestor nodes not function the right way!

J

Johnny

I'm creating a filebrowser for multimedia files such as .jpg .gif .au
..wav.
The problem is I want to cut single and multiple nodes which partly
functions:
single nodes and multiple nodes with only one folder per hierarchy
level;
Not function:
- multiple folders in the same level -> the former order gets lost
- if there is a file (MMed. document) between folders in the same
level
this file is put inside one of those
I tried much easier functions (read javaDocs, searched the web,
javasoft, google...) to cope with this problem but every time I solve
one the "steps" the next problem appears...
The only way I don't want to go, is something like a LinkedList,
Hashtable... because the MMed. filebrowser would need to much
resources while browsing through a media library e.g. with thousands
of files!
(I made descriptions for variables and methods i know)

If anyone has any idea to solve this problem I would be V E R Y
pleased!

Thank you anyway for reading this ;)


/** following code **/

// import statements
import java.util.Enumeration;
import javax.swing.*;
import javax.swing.tree.*;

// var declaration
static Enumeration en;
static DefaultMutableTreeNode jTreeModel, lastCopiedNode, dmt, insert,
insertParent, insertSameFolder;
static String varCut;
static int index= 0;
static int counter = 0;


/* cut function */
if (actionCommand.equals ("cut"))
{
// get the selected node (DefaultMutableTreeNode)
lastCopiedNode = (DefaultMutableTreeNode)
selPath.getLastPathComponent ();

// get the nodes in an Enumeration array (in top- down order)
en = dmt.preorderEnumeration();
// the way to make sure if it is a cut or a copied node
varCut = "cut";
}


/* paste function */
if (actionCommand.equals ("paste"))
{
// node is cut
if (varCut == "cut")
{
// is necessary for first time same folder case
insertParent = dmt;

// getting the nodes out of the array
while(en.hasMoreElements())
{
// cast the Object to DefaultMutableTreeNode
// and get stored (next) node
insert = (DefaultMutableTreeNode) en.nextElement();

// check if the node is a catalogue when getRepresentation()
// is a function of my node creating class (to know if it is
// a folder or a file)
if (insert.getRepresentation().equals("catalogue"))
{
// check if a "folder" node is inserted
if (index == 1)
{
counter = 0;
index = 0;
System.out.println ("***index and counter reset***");
}
// the node is in the same folder
// check if the folder is in the same hierarchy level
// -> in this case the insertParent has to remain
if (insert.getLevel() == insertParent.getLevel())
{
// this is necessary to get right parent folder
insertSameFolder = (Knoten) insert.getParent();

jTreeModel.insertNodeInto (insert, insertSameFolder, index);


System.out.println (">>>sameFolderCASE- insert"+counter+"> " +
String.valueOf(insert) +
"\ninsertTest -> " + String.valueOf(insertTest)
);

// set insertParent folder to the new createded one
insertParent = insert;

index ++;
}
else // the node is a subfolder
{
// insertNode
jTreeModel.insertNodeInto (insert, insertParent, index);

// set insertParent folder to the new createded one
insertParent = insert;

System.out.println (">>>subFolderCASE- insertParent"+counter+">
" + String.valueOf(insertParent) +
"\ninsertParent -> " + String.valueOf(insertParent)
);

index ++;
}
}
else // the node is a file
{
// insertNode
jTreeModel.insertNodeInto (insert, insertParent, counter);

System.out.println (">>>fileCASE insert "+counter+"> " +
String.valueOf(insert) +
"\ninsertParent -> " + String.valueOf(insertParent)
);
counter ++;
}

}

// reset index and counter for the next loop
index = 0;
counter = 0;
}
// reset cut var
varCut = null;
// remove the node (automatically deletes subfolders and files)
dmt.removeNodeFromParent (lastCopiedNode);


// if node is a copied one
if varCut == null)
{
// insert copied node the same way as for cut one's
// to make it possible to copy more than one node
}
}

/** end of code **/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top