Adding actions to DefaultMutableTreeNode - file

I have created a "FileTree" class which displays a file system via a JTree, and only displays folders and ".xlsx" or ".xls" files. I want to be able to add a double-click action to the DefaultMutableTreeNodes if they are a ".xlsx" or ".xls" file. It doesnt seem possible to be able to add an ActionListener or a MouseListener to a DefaultMutableTreeNode, is there a way I can control the double-click action on these nodes?

I have a solution of sorts, its not the one I was looking for, but it does work.
I have added a MouseListener to the JTree, and then when the click count is 2, I check the event source is an instanceof the JTree, then call
Object comp = tree.getLastSelectedPathComponent();
if (comp instanceof FileTreeNode)
{
FileTreeNode ftn = (FileTreeNode) comp;
File file = ftn.getFile();
}
and then I can do whatever I want with the file. FileTreeNode is an extension of DefaultMutableTreeNode which contains the file at that node.

Related

Create folder in explicit directory AIR

Like the title says I want to create a folder in a specific directory with Adobe Air.
If I use static methods of File like File.userDirectory works fine but I need to give the choice to select the directory.
I am trying this:
file.addEventListener(Event.SELECT, dirSelected);
file.browseForDirectory("Select a directory");
function dirSelected(e:Event):void {
trace(file.nativePath);
file.resolvePath("new_folder");
file.createDirectory();
}
Nothing happens
"resolvePath: Creates a new File object with a path relative to this File object's path, based on the path parameter (a string)."
So:
var newDir:File = file.resolvePath("new_folder");
newDir.createDirectory();

JavaFX FileChooser not returning file extension for Windows

The following code works fine when returning a file on a mac since it automatically appends the
file extension to the name of the file.
On windows however i have to type in the extension of the file as part of the file name in order for it to return with that extension....even though the extension is selected in the 'save type as' pulldown menu.
is there a way to automatically append the extension to the name when returning a file from the filechooser on windows?
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(fileExtension.toUpperCase()+" files(*."+fileExtension+")", "*."+fileExtension);
fileChooser.getExtensionFilters().add(extFilter);
//Show save file dialog
final File file = fileChooser.showSaveDialog(MyStage.this);
I ran into the same issue. My solution was to make a new File, and append the file extension as a string in the File constructor.
If you want users to be able to select and overwrite an existing file, make sure and add a check to make sure the initial save file does not contain the particular extension already before appending or else you will get something like "test.xls.xls".
FileChooser fc = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("XLS File (*.xls)", "*.xls");
fc.getExtensionFilters().add(extFilter);
File save = fc.showSaveDialog(stage);
save = new File(save.getAbsolutePath()+".xls");
FileOutputStream fileOut = new FileOutputStream(save);

Can we restrict openfile dialog control to not to show shortcuts(.lnk files) despite setting different filter

I am using an openfiledialog control in c# to open ppt files by default.
But,despite setting the filter for the *.ppt,folder and programs shortcuts are also getting displayed which I want to avoid.
Isn't there some way to force the OpenFileDialog NOT to display shortcuts
like .lnk and .url files..
My code:
openFileDialog1.DefaultExt = "*.ppt";
openFileDialog1.Filter = "Powerpoint Presentations|*.ppt;*.pptx";
openFileDialog1.FilterIndex = 1;
openFileDialog1.DereferenceLinks = false;
openFileDialog1.Title = "Select a file.";
openFileDialog1.ShowDialog();
textBox1.Text = openFileDialog1.FileName;
Now,though my openfile dialog window filters the ppt files from all files but it also displays .lnk files which I want to avoid..
Please suggest the way to do so...

Silverlight: Programmatically open and read XAML file?

I found how to programmatically parse/load a XAML string by using XamlReader.Load(), however if I were to have a user drag/drop a ".xaml" file onto my Silverlight application, how would I go about parsing that file to a XAML file and reading in the contents as a string to serve up to the XamlReader.Load() method.
Or is there a more efficient/better way through using reflection?
I'd like to allow the user to use a "silverlight for dummies" silverlight designer (within reason) to design a simple control. (i.e. a label and a textbox).
Then save this or export it as a XAML file (or maybe I can simply have them save it as a TXT file?)
Then they'd drop this file into my Silverlight application and it would parse the text into a XamlReader.Load(), then I can add my newly programmatically created object to a listbox.
I have the logic for drag/drop file and for loading up the XAML string, but that middle point of getting the contents of a ".xaml" file is what I'm confusing myself on...
EDIT
#nicholas, that's exactly what I went with. I don't know what was going on yesterday but I think I just had a major 'brain-fart'.
This is how I ended up going: (still have some cleanup and refactoring, but this was to test)
IDataObject data = e.Data;
if (data.GetDataPresent(DataFormats.FileDrop))
{
FileInfo[] files = data.GetData(DataFormats.FileDrop) as FileInfo[];
if (files.Length > 1)
{
//TODO
}
else
{
FileInfo file = files[0];
extension = file.Extension;
string xaml = string.Empty;
using (Stream stream = file.OpenRead())
{
xaml = StreamUtils.StreamToString(stream);
}
if (!xaml.IsEmpty())
{
try
{
myListBox.Items.Add(XamlReader.Load(xaml);
}
catch (Exception ex)
{
//TODO
}
}
}
So you have a Drop event handler, from which you receive event args with Data property, an IDataObject. From MSDN you find out how to get a FileStream for the dropped file, which you can then load into a string (eg use StreamReader) to be parsed by XamlReader.Load().
If I understood you correctly there will be three different steps:
Parse the content of .xaml file or any type of file into a "string"
(it can be a .txt file or any other format)
Use XamlReader.Load() to generate an UI element out of this string
from step 1
Insert the UI element from step 2 to ListBox.Items collection
Does that help?

WPF List files from a folder

I want to get a number of songs from a folder and list their names in a WPF Listview.
I also want each item in the list view to be a draggable file and can be copied from the list to the desktop. I've achieved this on one button, using the code:
Point mpos = e.GetPosition(null);
Vector diff = this.start - mpos;
string[] files = new String[1];
files[0] = #"C:\Song1.mp3";
DragDrop.DoDragDrop(this, new DataObject(DataFormats.FileDrop, files),
DragDropEffects.Copy);
For that each item in the list needs to have a filepath string associated with it.
How do I:
1. Get the files from a folder and list them.
2. Associate with each one a filepath string for the dragging.
Thanks!
You can use Directory.GetFiles() to get all the file paths in a folder and then use Path.GetFileName() (or Path.GetFileNameWithoutExtension()) on each path returned to get just the file names.

Resources