Populate swt.combo with list of filenames? [message #278839] |
Sat, 08 January 2005 13:43 |
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 #278867 is a reply to message #278852] |
Sun, 09 January 2005 18:40 |
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 18:57 |
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 21:14 |
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 #278893 is a reply to message #278874] |
Mon, 10 January 2005 14:46 |
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 #278929 is a reply to message #278899] |
Mon, 10 January 2005 20:33 |
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
|
|
|
|
Powered by
FUDForum. Page generated in 0.02593 seconds