Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Populate swt.combo with list of filenames?
Populate swt.combo with list of filenames? [message #278839] Sat, 08 January 2005 08:43 Go to next message
Eclipse User
Originally posted by: mike_devlin.fastmail.fm

Hiya

I have a combo in a New File Wizard which I want to populate with a list
of templates (ie. actually a list files contained in a particular
directory).

How can I populate my combo?

TIA

mD.
Re: Populate swt.combo with list of filenames? [message #278852 is a reply to message #278839] Sat, 08 January 2005 18:56 Go to previous messageGo to next message
Adam Kiezun is currently offline Adam Kiezun
Messages: 219
Registered: July 2009
Senior Member
Mike wrote:
> Hiya
>
> I have a combo in a New File Wizard which I want to populate with a list
> of templates (ie. actually a list files contained in a particular
> directory).
>
> How can I populate my combo?

can you be more specific? what is it that you have in your hand? what is
it that you try to do? what did you try to do? etc

in particular, does the following work?

1. get the list of names of files in the directory
2. use Combo.setItems to set the contents of the combo

/adam
/adam
Re: Populate swt.combo with list of filenames? [message #278867 is a reply to message #278852] Sun, 09 January 2005 13:40 Go to previous messageGo to next message
Eclipse User
Originally posted by: mike_devlin.fastmail.fm

>> I have a combo in a New File Wizard which I want to populate with a
>> list of templates (ie. actually a list files contained in a particular
>> directory).
>>
>> How can I populate my combo?
>
>
> can you be more specific? what is it that you have in your hand? what is
> it that you try to do? what did you try to do? etc
>
> in particular, does the following work?
>
> 1. get the list of names of files in the directory
> 2. use Combo.setItems to set the contents of the combo
>
> /adam

Adam

It's part 1 I'm having trouble with...

I have a project folder containing a directory called templates. That
directory contains a number of template files from which the NewWizard
will create a new file for me to work with.

I have all this working fine using a text box that the user manually
enters the template name into.

I have successfully added a Combo to the Wizard and can populate it with
sample data using a simple piece of code:

for (int i = 1; i < 11; i++)
{
combo.add(i+".) element ");
}

My difficulty (which seems to permuate all my Eclipse development at the
moment) is related to file management issues - something that seems much
more complicated and confusing in Eclipse than it needs to be (IMHO)...

My particular problem here is how to pull a list of files in a
particular folder of a project in my Workspace so I can add them to my
Combo?

TIA

mD
Re: Populate swt.combo with list of filenames? [message #278868 is a reply to message #278867] Sun, 09 January 2005 13:57 Go to previous messageGo to next message
Eclipse User
Originally posted by: akiezun.mit.remove.edu

>> in particular, does the following work?
>>
>> 1. get the list of names of files in the directory
>> 2. use Combo.setItems to set the contents of the combo
>>
>> /adam
>
> Adam
>
> It's part 1 I'm having trouble with...

afaik, simply calling IFolder.members always worked for me.

hth
/adam
Re: Populate swt.combo with list of filenames? [message #278871 is a reply to message #278868] Sun, 09 January 2005 16:14 Go to previous messageGo to next message
Eclipse User
Originally posted by: mike_devlin.fastmail.fm

>>> 1. get the list of names of files in the directory
>>
>> It's part 1 I'm having trouble with...
>
> afaik, simply calling IFolder.members always worked for me.
>

Cool. I appreciate your help on this.

So how do I get to IFolder? I think this is the part of File/Folder
management I'm getting confused with.

In the past I've used:

if (selection!=null && selection.isEmpty()==
false && selection instanceof IStructuredSelection) {
IStructuredSelection ssel = (IStructuredSelection)selection;
if (ssel.size()>1) return;
Object obj = ssel.getFirstElement();
if (obj instanceof IResource) {
if (obj instanceof IContainer)
container = (IContainer)obj;
else
container = ((IResource)obj).getParent();

Which gives me the Project folder...

Then what? :o(

Cheers.
PS: Perhaps Eclipse needs a
GetWorkspaceFolder("projectname\subdir\subdir2") type function get
around all these shinanigans? ;o)
Re: Populate swt.combo with list of filenames? [message #278874 is a reply to message #278871] Sun, 09 January 2005 17:53 Go to previous messageGo to next message
Adam Kiezun is currently offline Adam Kiezun
Messages: 219
Registered: July 2009
Senior Member
> Which gives me the Project folder...

so what kind of object do you have? IProject?
IProject is an IContainer so members() should work too

> Then what? :o(
>
> Cheers.
> PS: Perhaps Eclipse needs a
> GetWorkspaceFolder("projectname\subdir\subdir2") type function get
> around all these shinanigans? ;o)

If that's all you want, then why does simply using
ResourcesPlugin.getWorkspace().getRoot()...
not work for you?

/adam
Re: Populate swt.combo with list of filenames? [message #278893 is a reply to message #278874] Mon, 10 January 2005 09:46 Go to previous messageGo to next message
Eclipse User
Originally posted by: mike_devlin.fastmail.fm

>
> If that's all you want, then why does simply using
> ResourcesPlugin.getWorkspace().getRoot()...
> not work for you?

Well this is my latest attempt <sob>

IWorkspace workspace = ResourcesPlugin.getWorkspace();

//IPath tp = (IPath) workspace.getRoot().findMember("templates");
final IPath DOC_PATH = new Path("test/templates");

IFile[] files = root.findFilesForLocation(DOC_PATH);
int len = files.length;
for (int i = 0; i < len; i++) {
templateCombo.add(files[i].toString());
}

Where files ends up being empty ("test" is my project folder and
"templates" is a subfolder containing the files I want to pull ie:
"c:\eclipse\workspace\test\templates").

What should DOC_PATH be for this to work? This should be a relative path
since "c:\eclipse\workspace" could be anywhere...

I've been using DOC_PATH for testing (to try and work out how to do
this), given that the "templates" folder name is constant, how do I
assign DOC_PATH dynamically?

Thanks.
mD.
Re: Populate swt.combo with list of filenames? [message #278899 is a reply to message #278893] Mon, 10 January 2005 10:00 Go to previous messageGo to next message
Adam Kiezun is currently offline Adam Kiezun
Messages: 219
Registered: July 2009
Senior Member
> IFile[] files = root.findFilesForLocation(DOC_PATH);
>
>(...)
> What should DOC_PATH be for this to work? This should be a relative path
> since "c:\eclipse\workspace" could be anywhere...

The documentation is clear about this. In the call to
findFilesForLocation, the argument is an absolute path.
see (link may get broken)
http://help.eclipse.org/help30/index.jsp?topic=/org.eclipse. platform.doc.isv/reference/api/org/eclipse/core/resources/IW orkspaceRoot.html

so i understand correctly that you already tried
root.getProject("test").getFolder("templates").members()
and it failed?

/adam
Re: Populate swt.combo with list of filenames? [message #278929 is a reply to message #278899] Mon, 10 January 2005 15:33 Go to previous messageGo to next message
Eclipse User
Originally posted by: mike_devlin.fastmail.fm

Adam

Thanks for the pointers. I now seem to have something that works. Here's
my code in case others can get use from it:

IContainer container = null;

if (selection!=null && selection.isEmpty()==false
&& selection instanceof IStructuredSelection) {
IStructuredSelection ssel = (IStructuredSelection)selection;
if (ssel.size()>1) return;
Object obj = ssel.getFirstElement();
if (obj instanceof IResource) {
if (obj instanceof IContainer)
container = (IContainer)obj;
else
container = ((IResource)obj).getParent();
//The code above, I understand, will give me the project I have
//currently selected in the navigator??
try {
//returns an array of resources in templates
IResource[] res = ((IProject)
container).getFolder("templates").members();
int len = res.length;
for (int i = 0; i < len; i++) {
//scan through array and populate Combo if the file ext.
//is *.template
if(res[i] instanceof IFile) {
IFile file = (IFile) res[i];
if (file.getFileExtension() != null &&
file.getFileExtension().equals("template")) {
//return actual filename without path etc.
templateCombo.add(file.getName());
}
}
}
} catch (CoreException e) {/* ignore*/}
}
}

No doubt this code could be tidier. Being an eclipse novice, I'm just
happy it works, elegance can come l8ter ;o)

mD
Re: Populate swt.combo with list of filenames? [message #278933 is a reply to message #278929] Mon, 10 January 2005 16:26 Go to previous message
Adam Kiezun is currently offline Adam Kiezun
Messages: 219
Registered: July 2009
Senior Member
Mike wrote:
> Adam
>
> Thanks for the pointers. I now seem to have something that works. Here's
> my code in case others can get use from it:

No problem, glad I could help.
Now, don't ignore exceptions and do Extract Method a bunch of time and
you'll be all set.

/adam
Previous Topic:CVS server version
Next Topic:Console for interaction in eclipse??
Goto Forum:
  


Current Time: Thu Jul 31 09:38:38 EDT 2014

Powered by FUDForum. Page generated in 0.01753 seconds