Pivot includes support for easily adding local file system access to an application. The following example demonstrates the use of the FileBrowserSheet class:
It allows the user to browse the local file system using one of four supported modes:
Open - select a single file to open
Open Multiple - select multiple files to open
Save As - select a file name to save as
Save To - select a folder to save to
The BXML source for the application is as follows:
<filebrowsing:FileBrowsing title="File Browsing" maximized="true" xmlns:bxml="http://pivot.apache.org/bxml" xmlns:filebrowsing="org.apache.pivot.tutorials.filebrowsing" xmlns="org.apache.pivot.wtk"> <bxml:define> <ButtonGroup bxml:id="fileBrowserSheetModeGroup"/> </bxml:define> <Border styles="{padding:6}"> <BoxPane orientation="vertical" styles="{spacing:6}"> <Label text="Mode:" styles="{font:{bold:true}}"/> <RadioButton buttonData="Open" buttonGroup="$fileBrowserSheetModeGroup" selected="true"> <userData mode="open"/> </RadioButton> <RadioButton buttonData="Open Multiple" buttonGroup="$fileBrowserSheetModeGroup"> <userData mode="open_multiple"/> </RadioButton> <RadioButton buttonData="Save As" buttonGroup="$fileBrowserSheetModeGroup"> <userData mode="save_as"/> </RadioButton> <RadioButton buttonData="Save To" buttonGroup="$fileBrowserSheetModeGroup"> <userData mode="save_to"/> </RadioButton> <PushButton bxml:id="openSheetButton" buttonData="Open Sheet"/> </BoxPane> </Border> </filebrowsing:FileBrowsing>
The Java source code is shown below. When the user presses the "Open Sheet" button, it creates a FileBrowserSheet using the selected mode and opens the sheet. When the sheet is closed, it simply displays an alert to the user reflecting the selection:
package org.apache.pivot.tutorials.filebrowsing; import java.io.File; import java.net.URL; import org.apache.pivot.beans.BXML; import org.apache.pivot.beans.Bindable; import org.apache.pivot.collections.ArrayList; import org.apache.pivot.collections.Map; import org.apache.pivot.collections.Sequence; import org.apache.pivot.util.Resources; import org.apache.pivot.wtk.Alert; import org.apache.pivot.wtk.Button; import org.apache.pivot.wtk.ButtonGroup; import org.apache.pivot.wtk.ButtonPressListener; import org.apache.pivot.wtk.FileBrowserSheet; import org.apache.pivot.wtk.ListView; import org.apache.pivot.wtk.MessageType; import org.apache.pivot.wtk.PushButton; import org.apache.pivot.wtk.Sheet; import org.apache.pivot.wtk.SheetCloseListener; import org.apache.pivot.wtk.Style; import org.apache.pivot.wtk.Window; public class FileBrowsing extends Window implements Bindable { @BXML private ButtonGroup fileBrowserSheetModeGroup = null; @BXML private PushButton openSheetButton = null; @Override public void initialize(Map<String, Object> namespace, URL location, Resources resources) { openSheetButton.getButtonPressListeners().add(new ButtonPressListener() { @Override public void buttonPressed(Button button) { Button selection = fileBrowserSheetModeGroup.getSelection(); String mode = (String)selection.getUserData().get("mode"); FileBrowserSheet.Mode fileBrowserSheetMode = FileBrowserSheet.Mode.valueOf(mode.toUpperCase()); final FileBrowserSheet fileBrowserSheet = new FileBrowserSheet(); if (fileBrowserSheetMode == FileBrowserSheet.Mode.SAVE_AS) { fileBrowserSheet.setSelectedFile(new File(fileBrowserSheet.getRootDirectory(), "New File")); } fileBrowserSheet.setMode(fileBrowserSheetMode); fileBrowserSheet.open(FileBrowsing.this, new SheetCloseListener() { @Override public void sheetClosed(Sheet sheet) { if (sheet.getResult()) { Sequence<File> selectedFiles = fileBrowserSheet.getSelectedFiles(); ListView listView = new ListView(); listView.setListData(new ArrayList<File>(selectedFiles)); listView.setSelectMode(ListView.SelectMode.NONE); listView.getStyles().put(Style.backgroundColor, null); Alert.alert(MessageType.INFO, "You selected:", listView, FileBrowsing.this); } else { Alert.alert(MessageType.INFO, "You didn't select anything.", FileBrowsing.this); } } }); } }); } }
Note that, internally, FileBrowserSheet uses an instance of the org.apache.pivot.wtk.FileBrowser class to perform the actual file system navigation. It is actually possible to use this component directly within an application to create a custom file browser; however, a discussion of the FileBrowser class is outside the scope of this tutorial.
Next: Windows