Solved

How to select multiple files and/or folders at the same time in a GUI by using SWT?

Posted on 2013-06-18
12
542 Views
Last Modified: 2013-08-27
Hi,
I have a GUI and in this GUI when you click on a icon, it should open a dialog box and you should be able to select files and/or folders.

The following code can do this this for files and I can convert this to work for files by using FileDialog instead of DirectoryDialog. But I couldn't make it work both for files and directories at the same time.

Can you please help?

The code is below:

 import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolItem;


public class ToolBarListener implements Listener {
    
    private Shell shell;

    @Override
    public void handleEvent(Event event) {
        ToolItem toolItem = (ToolItem) event.widget;
        if (toolItem == Gui.item1) {
            System.out.println("item1 clicked...");
            DirectoryDialog dlg = new DirectoryDialog(shell);
            String dir = dlg.open();
            System.out.println("chosen dir : " + dir);
        } else if (toolItem == Gui.item13) {
            Gui.swichTabChecks();
        } else if (toolItem == Gui.item23) {
            Gui.swichTabResults();
        } else if (toolItem == Gui.item3333333) {
            Gui.swichTabFiles();
        }
    }

    public void setShell(Shell shell) {
        this.shell = shell;
    }

}

Open in new window

0
Comment
Question by:Tolgar
  • 8
  • 3
12 Comments
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
Given that you've put only Java as a tag, but given that I know zero about SWT, in Java a file can be both a file, or a directory, so this ought to be the first thing to check - that you are in fact not seeing both already.
0
 
LVL 16

Expert Comment

by:Valeri
Comment Utility
this piece of code will do the work:

} else if (toolItem == Gui.item111) {
                  FileDialog fd = new FileDialog(shell, SWT.MULTI);
                  String firstFile = fd.open();
                  if (firstFile != null) {
                        String[] selectedFiles = fd.getFileNames();
                        for (int ii = 0; ii < selectedFiles.length; ii++ ) {
                              System.out.println("selected file : " + selectedFiles[ii]);
                        }
                  }
            }
0
 

Author Comment

by:Tolgar
Comment Utility
So, now I have this piece of code with the addition of your suggestion and with this code, I cannot select the files  and the folders by using the same icon.

What it does is, it selects the folders because the DirectoryDialog is before the FileDialog in the if statement.

If I remove this piece of if statement then the new code that you suggested can only select multiple files but not the folders.

This is the code I have:

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolItem;


public class ToolBarListener implements Listener {
    
    private Shell shell;

    @Override
    public void handleEvent(Event event) {
        ToolItem toolItem = (ToolItem) event.widget;
        if (toolItem == Gui.item1) {
            System.out.println("item1 clicked...");
            DirectoryDialog dlg = new DirectoryDialog(shell);
            String dir = dlg.open();
            System.out.println("chosen dir : " + dir);
        } else if (toolItem == Gui.item13) {
            Gui.swichTabChecks();
        } else if (toolItem == Gui.item23) {
            Gui.swichTabResults();
        } else if (toolItem == Gui.item3333333) {
            Gui.swichTabFiles();
        } else if (toolItem == Gui.item1) {
            FileDialog fd = new FileDialog(shell, SWT.MULTI);
            String firstFile = fd.open();
            if (firstFile != null) {
                  String[] selectedFiles = fd.getFileNames();
                  for (int ii = 0; ii < selectedFiles.length; ii++ ) {
                        System.out.println("selected file : " + selectedFiles[ii]);
                  }
            }
      } 
    }

    public void setShell(Shell shell) {
        this.shell = shell;
    }

}

Open in new window


My requirement is to select multiple files and/or directories by using the same icon. Also, files and directories should be selected at the same time in one shot.

So as an example, I have the following files and folders in directory A.

dirA
   dir1
   dir2
   dir3
   fileX
   fileY
   fileZ

Open in new window


I should be able to select the following files and folders in dirA directory in one shot by using the same icon on the UI:

dir1
dir3
fileY
fileZ

Open in new window


I would appreciate if you can help me with that.

Thanks,
0
 
LVL 16

Expert Comment

by:Valeri
Comment Utility
You can't do this with standart SWT widgets. There are only file dialog and directory dialog.
You need to develop your own custom widget.
I found exactly what you need here:
http://www.eclipse.org/articles/Article-SWT-Virtual/Virtual-in-SWT.html
0
 

Author Comment

by:Tolgar
Comment Utility
Hi,
Thank you for your reply. Can you please tell me in more detail how I will use the cod that you sent me in the link?

Currently, this is the code that I use to select directories. How should I change this code/

   
     if (toolItem == Gui.item1) {
            System.out.println("item1 clicked...");
            DirectoryDialog dlg = new DirectoryDialog(shell);
            String dir = dlg.open();
            System.out.println("chosen dir : " + dir);

Open in new window

0
 

Author Comment

by:Tolgar
Comment Utility
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Tolgar
Comment Utility
Hi Valeri,
If you have time, can you please take a look at my other questions?

Thanks,
0
 
LVL 16

Accepted Solution

by:
Valeri earned 500 total points
Comment Utility
take a look at the attached file. if you run it, you can choose multiple files and folder in the same time. when you close the component selected files and folders are dumped to the console. You can format the output according to your needs.
FileTree.java
0
 

Author Comment

by:Tolgar
Comment Utility
Hi Valeri,
I created the file you sent me, FileTree.java.

I added the following line to ToolBarListener.java:
        if (toolItem == Gui.item1) {
            Gui.selectFileFolders();
        }

Open in new window


I added this listener in Gui.java:
       
item1.addListener(SWT.Selection, toolBarListener);

Open in new window



Then I created this function in Gui.java:

    public static void selectFileFolders() {
        
    } 

Open in new window


But I don't know what to write in it.

Can you please help?
0
 

Author Comment

by:Tolgar
Comment Utility
@Valeri: I can open a new email thread if you prefer.
0
 

Author Comment

by:Tolgar
Comment Utility
Hi Valeri,
Can you please take a look at this question?

http://www.experts-exchange.com/Programming/Languages/Java/Q_28221229.html

Thanks,
0
 

Author Comment

by:Tolgar
Comment Utility
Hi Valeri,
Can you please take a look at this open question:

http://www.experts-exchange.com/Programming/Languages/Java/Q_28221229.html

Thanks,
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now