JTree cut and paste multiple child and ancestor nodes not function correct

Hello i'm creating a filebrowser for multimedia files (SDK 1.22) such as .jpg .gif .au .wav. (using Swing, JTree, DefaultMutableTreeNode)
The problem is I want to cut and paste single and multiple nodes (from current node til the last child "top-down") 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 (or after them) in the same level this file is put inside one of those
I tried much easier functions to cope with this problem but every time I solve one the "steps" the next problem appears...
The thing I don't want to do, is something like a LinkedList, Hashtable (to store the nodes)... because the MMed. filebrowser would need to much resources while browsing through a media library with (e.g.) thousands of files!
If someone has any idea to solve this problem I would be very pleased!
Thank you anyway by reading this ;)
// part of the code, if you want more detailed info
// @mail: [email protected]
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
}

You need to use a recursive copy method to do this. Here's a simple example of the copy. To do a cut you need to do a copy and then delete the source.
import java.io.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.*;
import java.util.*;
public class CopyTree extends JFrame
     Container cp;
     JTree cTree;
     JScrollPane spTree;
     DefaultTreeModel cModel;
     CNode cRoot;
     JMenuBar menuBar = new JMenuBar();
     JMenu editMenu = new JMenu("Edit");
     JMenuItem copyItem = new JMenuItem("Copy");
     JMenuItem pasteItem = new JMenuItem("Paste");
     TreePath [] sourcePaths;
     TreePath [] destPaths;
     // =====================================================================
     // constructors and public methods
     CopyTree()
          super("Copy Tree Example");
          addWindowListener(new WindowAdapter()
          {public void windowClosing(WindowEvent evt){System.exit(0);}});
          // edit menu
          copyItem.addActionListener(new ActionListener()
          {public void actionPerformed(ActionEvent e){mnuCopy();}});
          editMenu.add(copyItem);
          pasteItem.addActionListener(new ActionListener()
          {public void actionPerformed(ActionEvent e){mnuPaste();}});
          editMenu.add(pasteItem);
          menuBar.add(editMenu);
          setJMenuBar(menuBar);
          buildModel();
          cp = getContentPane();
          cTree = new JTree(cModel);
          spTree = new JScrollPane(cTree);
          cp.add(spTree, BorderLayout.CENTER);
          pack();
     static public void main(String [] args)
          new CopyTree().show();
     // =====================================================================
     // private methods - User Interface
     private void mnuCopy()
          sourcePaths = cTree.getSelectionPaths();
     private void mnuPaste()
          TreePath sp, dp;
          CNode sn,dn;
          int i;
          destPaths = cTree.getSelectionPaths();
          if(1 == destPaths.length)
               dp = destPaths[0];
               for(i=0; i< sourcePaths.length;i++)
                    sp = sourcePaths;
                    if(sp.isDescendant(dp) || dp.isDescendant(sp))
                         JOptionPane.showMessageDialog
                              (null, "source and destinations overlap","Paste", JOptionPane.ERROR_MESSAGE);
                         return;
               dn = (CNode)dp.getLastPathComponent(); // the node we will append our source to
               for(i=0; i< sourcePaths.length;i++)
                    sn = (CNode)sourcePaths[i].getLastPathComponent();
                    copyNode(sn,dn);
          else
               JOptionPane.showMessageDialog
                    (null, "multiple destinations not allowed","Paste", JOptionPane.ERROR_MESSAGE);
     // recursive copy method
     private void copyNode(CNode sn, CNode dn)
          int i;
          CNode scn = new CNode(sn);      // make a copy of the node
          // insert it into the model
          cModel.insertNodeInto(scn,dn,dn.getChildCount());
          // copy its children
          for(i = 0; i<sn.getChildCount();i++)
               copyNode((CNode)sn.getChildAt(i),scn);
     // ===================================================================
     // private methods - just a sample tree
     private void buildModel()
          int k = 0;
          cRoot = new CNode("root");
          cModel = new DefaultTreeModel(cRoot);
          CNode n1a = new CNode("n1a");
          cModel.insertNodeInto(n1a,cRoot,k++);
          CNode n1b = new CNode("n1b");
          cModel.insertNodeInto(n1b,cRoot,k++);
          CNode n1c = new CNode("n1c");
          cModel.insertNodeInto(n1c,cRoot,k++);
          CNode n1d = new CNode("n1d");
          cModel.insertNodeInto(n1d,cRoot,k++);
          k = 0;
          CNode n1a1 = new CNode("n1a1");
          cModel.insertNodeInto(n1a1,n1a,k++);
          CNode n1a2 = new CNode("n1a2");
          cModel.insertNodeInto(n1a2,n1a,k++);
          CNode n1a3 = new CNode("n1a3");
          cModel.insertNodeInto(n1a3,n1a,k++);
          CNode n1a4 = new CNode("n1a4");
          cModel.insertNodeInto(n1a4,n1a,k++);
          k = 0;
          CNode n1c1 = new CNode("n1c1");
          cModel.insertNodeInto(n1c1,n1c,k++);
          CNode n1c2 = new CNode("n1c2");
          cModel.insertNodeInto(n1c2,n1c,k++);
     // simple tree node with copy constructor
     class CNode extends DefaultMutableTreeNode
          private String name = "";
          // default constructor
          CNode(){this("");}
          // new constructor
          CNode(String n){super(n);}
          // copy constructor
          CNode(CNode c)
               super(c.getName());
          public String getName(){return (String)getUserObject();}
          public String toString(){return  getName();}

Similar Messages

  • IPhone 4 vibration, wifi, and calling will not function correctly?

    I have had my iPhone 4 for 6 months now, and I have dropped it a couple of times, but a few days after I dropped it nothing happened. My first problem started in May when I noticed that my phone could not reach the wifi at home or other places quickly as it normally could and when I would go into other rooms I would lose the connection quickly.This made me have to use my 3g more often and I only have a limit of 300MB, I normally do not use it, because of the wifi i have around,but it is getting harder. 2-3 days ago I called the DATA (*3282#) in my phone and got 64.89 out of 300MB. I later used my 3G for awhile, so a day later i called DATA and the same number (64.89 out of 300MB) comes on again. I knew this was a mistake so I called it again and the same number shows up. I KNOW FOR SURE I USED MY 3G, so I dont know why this keeps popping up. Everything else channged (texting and calling) On top of that my vibration started to not work in early June and it's difficult for me to know when someone has texted or called. I have tried going into settings and choosing a different type of vibration, but it does not vibrate overall and yes it is turned on. My version right now is 6.1. Will upgrading help? Everything else works fine, but is there a way I can fix this or do I have to go the Apple Store? I'm also in AT&T.

    For the data usage,
    I have AT&T as well and I found that the Data usage only updates about once a day even though texting updated instantly.
    As for the vibration,
    The vibration motor thing may be broken.
    But I am not sure.

  • Closed a window on final cut and i can not re open it

    closed a window on final cut and i can not re open it. i tryed many things. i went to window and it wouldn't let me select it (timeline). what do i do? help asap!

    Locate a Sequence in the Browser (not a clip or an image, but a Sequence) and double-click it.
    -DH

  • My audio and cam is not functioning anymore.my laptop model is Satellite A200-130.what shall i do?

    im having a problem with my laptop.the audio and cam is not functioning.i dont know why/
    please help me what to do.do you think program files about these were deleted?

    Probably best to post in the Toshiba Support Forums Europe.
    -Jerry

  • When opening iTunes I continue to get the same messages, 'The program can't start because AVFoundationCF.dll is missing' and 'iTunes was not installed correctly. Error 7 (Windows error 126)' I've uninstalled, reinstalled many times - nothing! help please.

    When opening iTunes on my new Windows 7 PC I continue to get the same messages, 'The program can't start because AVFoundationCF.dll is missing' and 'iTunes was not installed correctly. Error 7 (Windows error 126)' I've uninstalled and reinstalled many times to no avail, even tried it on a different user account and still nothing. I recently installed Panda Internet Security 2012 (about 12 days ago), initially iTunes worked fine as usual but within the last few days I've been  unable to open it with the same messages appearing. I've read through some of these pages and tried everything that's been advised, uninstalling and reinstalling while internet and laptop security is swithched off, downloading tools to completely remove anything related to iTunes, tried it in Safe Mode, nothing has worked. I've also noticed that in the start menu when I click on iTunes, it says '(empty)' where it should have the logo and ability to start the program, does this mean it doesn't download + install properly each time I've tried? Any help would be enormously appreciated.

    Doublechecking. Have you also tried the following user tip?
    Troubleshooting issues with iTunes for Windows updates

  • I have a windows 7 64x SP1 and every time I install iTunes 12 I get this message when I try to ponen it:"This copy of Itunes is damaged and it has not been correctly installe. Please, install iTunes again.(-42404)". The instalation process runs smoot

    I have a windows 7 64x SP1.
    Every time I install iTunes 12 I get this message when I try to open it:"This copy of iTunes is damaged and it has not been correctly installed. Please, install iTunes again.(-42404)".
    The instalation process runs smoothly without any error message and iTunes has been installed in my computer before. The problem started with the update no. 12. Any idea what's going on? Thank you

    if itunes will not run, you probably have mangled windows badly
    one feature of windows 8 is the refresh that saves libraries,
    because you are using windows 7, you will need to backup all files to another storage device

  • Suddenly 2 of my apps (Facebook and Skype) will not function on my ipod. They just close down when I touch the ikon.

    Suddenly 2 of my apps (Facebook and Skype) will not function on my ipod touch. They just close down when I touch the ikons.

    If that does not work try resetting your iPod:
    Reset iPod touch:  Press and hold the On/Off Sleep/Wake button and the Home
    button at the same time for at least ten seconds, until the Apple logo appears.

  • My phone was left in a car overnight and now is not working correctly, screen looks funny and I cannot make calls? Will they be able to assist in  a store?

    My phone was left in a car overnight and now is not working correctly, screen looks funny and I cannot make calls? Will they be able to assist in  a store?

    Make an appointment at any Apple store, see what the genius bar says/does.
    You don't metion whether the car was hot or cold.

  • I have a 27" iMac with 16GB of factory installed SDRAM and 1T harddrive.  It is telling me the harddrive is full and is not functioning correctly.  How can I adjust so that all files are managed on the harddrive rather than the SDRAM.  (BTW - design flaw)

    Processor 3.4 GHz Intel Core i7
    Memory  16 GB 1333 MHz DDR3
    I have a 27" iMac with 16GB of factory installed SDRAM and 1T harddrive.  It is telling me the harddrive is full and is not functioning correctly.  How can I adjust so that all files are managed on the harddrive rather than the SDRAM.  (BTW - design flaw here.)
    Older Macs with a single Harddrive would simply expand OS management on the drive, and I think I understand the new Fusion Drive to do just that, but how do I get this product that I spent a great deal of money on to be more than a pretty screen?

    I was confused by this statement
    It is telling me the harddrive is full and is not functioning correctly.
    OS X manages ram and when you run out, it create swapfiles to extend your ram on the HDD (Hard Disk Drive). The OS itself, takes about 16 gig of space and that too resides on the HDD.  Your 16 gig of RAM is really a temporary space that holds things from the HDD so the processor can work on them.  There is no design flaw between your ram and HDD.  Something else is going on and there are a lot of smart people here to help you figure out what that "something else" is.
    I would do three things.  First I would create a backup, backups are important.  Second, I would reboot into recovery and repair my HDD.  Lastly while still booted in recovery I would repair permissions.
    Can you capture a screen shot of the exact error you are getting?

  • Measurement and Automation Explorer not installed correctly

    I have a trial version of LabView 8.5.  Upon trying to change the Instrument I/O Assistant, I get "Measurement and Automation Explorer not installed correctly.  Please install from driver CD."  Well, I don't have the CD since this was a trial download.
    Basically, what I am trying to do is this:  I want to pull 4 variables/tags from an Allen Bradley PLC and log them to an Excel spreadsheet.  I am using RSLinx to communicate with the PLC, and LabView to compile the info. 
    Any help would be appreciated.

    Any Ideas?  I have the program running right now, and I'm kinda in a bind.  I also tried RSView32 Works to log the info, but there isn't enough flexibility in logging.  I need to log flow rate test results from a large automated machine.  The mahcine does a number of operations, but we are most concerned with these four tags (since it conducts four tests at once).  The problem is, the cycle time varies with the lot of product.  Some products require more time as the process is more delicate.  So, I can't use a time-interval logging method.
    As for variable change, I can't do that either because it records the 4 test results in series; not all at the same time.  That, and if the test is aborted (no tube), the previous test result value stays in the tag until there is a successful test.
    I found a variable that changes once per cycle.  I'd like to log the four tags once per cycle.
    BUT I can't do anything until I get the I/O Wizard working. 

  • I just downloaded Photoshop Elements 13 and IMPORT is not functional.    What is wrong?

    I just downloaded Photoshop Elements 13 and IMPORT is not functional.    I reinstalled the printer /scanner and still not functional.   My computer is recognizing the printer.   What is wrong?

    I guess the first thing you now need to do is to get the latest drivers for your printer because as you say you uninstalled it from your machine and now reinstalled it again.  I don't know why you thought printer should have been uninstall;led but anyway, please reinstall the driver and then see if it is recognized by your operating system and try scanning something as a test from outside the PSE13 before going back to PSE13 and trying to scan your real image.
    Please post back so that Barbara or R_Kelly can help you as I don't use Apple Mac.

  • Elements 12 Organizer not functioning correctly uploading files and viewing.

    Elements 12 Organizer does not function correctly.  It will upload new files but will not allow me to view correctly or edit.  I attempts to reconnect files.  When I attempt to do this manually it states that the files are already there.  Very frustrating and Adobe representatives will not assist me.

    Windows Media Player 12 is installed.
    Windows Media Player
    Version 12.0.9600.16384
    Operating System
    Version:
    6.3.9600
    System Locale:
    en-GB
    Service Pack:
    0.0
    User Locale:
    en-GB
    Build Lab:
    9600.winblue_gdr.131030-1505
    Geo ID:
    United Kingdom
    Type:
    Workstation
    DRM Version:
    11.0.9600.16384
    Architecture:
    x86
    Indiv Version:
    2.9.0.1
    Processors:
    8
    Video codecs are:
    Type
    Name
    Format
    Binary
    Version
    ICM
    Microsoft YUV
    UYVY
    msyuv.dll
    6.3.9600.16384
    ICM
    Intel IYUV codec
    IYUV
    iyuv_32.dll
    6.3.9600.16384
    ICM
    Cinepak Codec by Radius
    cvid
    iccvid.dll
    1.10.0.12
    ICM
    Microsoft YUV
    UYVY
    msyuv.dll
    6.3.9600.16384
    ICM
    Toshiba YUV Codec
    Y411
    tsbyuv.dll
    6.3.9600.16384
    ICM
    Microsoft YUV
    UYVY
    msyuv.dll
    6.3.9600.16384
    ICM
    Intel IYUV codec
    IYUV
    iyuv_32.dll
    6.3.9600.16384
    ICM
    Microsoft RLE
    MRLE
    msrle32.dll
    6.3.9600.16384
    ICM
    Microsoft Video 1
    MSVC
    msvidc32.dll
    6.3.9600.16384
    ICM
    proDAD Codec
    pDAD
    DMO
    Mpeg4s Decoder DMO
    mp4s, MP4S, m4s2, M4S2, MP4V, mp4v, XVID, xvid, DIVX, DX50
    mp4sdecd.dll
    6.3.9600.16384
    DMO
    WMV Screen decoder DMO
    MSS1, MSS2
    wmvsdecd.dll
    6.3.9600.16384
    DMO
    WMVideo Decoder DMO
    WMV1, WMV2, WMV3, WMVA, WVC1, WMVP, WVP2, VC1S
    wmvdecod.dll
    6.3.9600.16384
    DMO
    Mpeg43 Decoder DMO
    mp43, MP43
    mp43decd.dll
    6.3.9600.16384
    DMO
    Mpeg4 Decoder DMO
    MPG4, mpg4, mp42, MP42
    mpg4decd.dll
    6.3.9600.16384
    Windows Media Player plays .mps files without problems
    Quick Time Player v7.7.4 installed (at suggestion from adobe premiere organiser). Will not play .mps files
    Adobe Premiere 12 organiser will not play .mps files, editor will play and edit them.
    Media Player Classic not installed

  • Safari says "i cannot open the page because the server cannot be found" I get this on Safari and Mail does not function. Apps work like a dream. All Windows OS are screaming. Any thoughts?

    Safari says "i cannot open the page because the server cannot be found" I get this on Safari and Mail does not function. Apps work like a dream. All Windows OS are screaming. Any thoughts?

    You can check this thread for the answer you are looking for.
    https://discussions.apple.com/thread/3685247?start=0&tstart=0

  • I downloaded an update yesterday and it did not install correctly..gives me error... now I cant even open itunes...should I uninstall I tunes totally and do a brand new install?

    I downloaded a new version of I tunes yesterday and it did not install correctly- I cannot open I tunes, I have tried to download again, install again, repair and nothing... should I uninstall ITunes completely and reinstall fresh? what happens to my music collection, images, etc?

    Hi bezoya,
    Thanks for using Apple Support Communities.  If iTunes is not launching after an update, I would recommend trying a thorough reinstall as described here:
    Issues installing iTunes or QuickTime for Windows
    http://support.apple.com/kb/ht1926
    Cheers,
    - Ari

  • I've recently downloaded Lion and find my Microsoft excel, word, entourage and powerpoint do not function. It says these functions are not now supported. Why and how do I get to use them again?

    I've recently downloaded Lion and find my Microsoft excel, word, entourage and powerpoint do not function. It says these functions are not now supported. Why and how do I get to use them again?

    philippnoe wrote:
    What a "great" Program which is supporting many nice but not mandatory things but is not supporting a Program which is used day by day from many users ... and especially this Program is also sold officially by apple !!!
    Yeah!, Why, Lion won't even run my old DOS programs! 

Maybe you are looking for

  • Delivery control based on customer's GRN

    Dear Gurus, We follow Scheduling agreement (LP) process. We have a requirement like, when we do the PGI of the delivery (along with the commercial and excise invoices), the goods goes to Transporter's warehouse and kept there. Whenever customer ask t

  • Input and Output VAT

    Dear Friends, One advise needed. I have created 2 VAT G/L Accounts via FS00 i.e. Input VAT ( which is updated in Purchase Process at the time of MIRO ) (ii) Output VAT( which is updated in Sales Process at the time of VF01 ) Both are balanace Sheet a

  • Tabular from problems while migrated from APEX 3.0 to 4.0.2

    We are planning to upgrade our existing applications from 3.0 to 4.0, but I have noticed a few things about the tabular forms. 1. The wizard generated tabular form with checkboxes (and "select all" checkbox in the heading) functionality stopped worki

  • Script-Quotation

    Hi Friends,       i want to get 10 fields  in my form from  from table vbap( since it is not defined in my driver program) then how can i use perform statement in my form . its urgent, please help me. regards, desha.

  • Can you apply titles over Photos?

    At the end of my video, I added the credits introducing the cast of the play...I created a clip of the name of the actor followed by a clip of a photo of same But is there a way to apply the text directly on to the photo clip? Thanks!