|
|
< Day Day Up > |
|
22.2 Implementation Notes22.2.1 Flexible LayoutThe FileExplorer shell uses FormLayout to arrange its children. Initially, the table, the tree, and the sash between them are attached to the tool bar at the top and the status label at the bottom. The View menu allows you to hide the tool bar or status label. This is accomplished by changing the attachment of the table, tree, and sash to be relative to the edge of the shell, then hiding the tool bar or label. For example, here is the code that toggles the visibility of the tool bar.
if (toolItem.getSelection ()) {
FormData treeData = (FormData) tree.getLayoutData ();
treeData.top = new FormAttachment (toolBar);
FormData sashData = (FormData) sash.getLayoutData ();
sashData.top = new FormAttachment (toolBar);
FormData tableData = (FormData) table.getLayoutData ();
tableData.top = new FormAttachment (toolBar);
} else {
FormData treeData = (FormData) tree.getLayoutData ();
treeData.top = new FormAttachment (0);
FormData sashData = (FormData) sash.getLayoutData ();
sashData.top = new FormAttachment (0);
FormData tableData = (FormData) table.getLayoutData ();
tableData.top = new FormAttachment (0);
}
toolBar.setVisible (toolItem.getSelection ());
shell.layout ();
Notice that the selected state of the item is queried to decide whether the tool bar should be shown or hidden. Notice also the explicit call to layout() to cause the appearance to change. 22.2.2 Drag-and-DropIn SWT, drag-and-drop (we say "D&D") support is implemented using native drag-and-drop facilities. The package that implements this support is org.eclipse.swt.dnd. D&D support is implemented in an application by creating a DragSource for the control that items will be dragged from and a DragTarget for the control they will be dragged to. The example creates a DragSource and a DragTarget for both the table and the tree, allowing items to be moved by dragging them to either control. Where things start to get interesting, however, is that because the D&D support uses the native facilities, you can also drag files between FileExplorer and the native file browsers on platforms that support this.
In the FileExplorer source, the DragSource and DragTarget instances are created by the following methods.
Looking at these methods should provide a good start toward working with the drag-and-drop support. 22.2.3 Application LaunchingMost modern operating systems provide support for mapping between identifiers for content types (such as MIME types or file extensions) and the native applications that are capable of manipulating content of those types. In SWT, this application launching support is implemented by the Program class in the package org.eclipse.swt.program. An instance of class Program represents a specific external program that is capable of handling one or more content types. Static methods in class Program are provided that return the available programs and the content types for which there are mappings. The simplest way to use this support is to launch the content file.
The FileExplorer example launches the selected file when the table receives a DefaultSelection event. The handler for this is implemented in the following method.
The code to launch the file is trivial and looks like this.
String name = file.getAbsolutePath ();
if (!Program.launch (name)) {
MessageBox dialog =
new MessageBox (shell, SWT.ICON_ERROR | SWT.OK);
dialog.setText (shell.getText ());
dialog.setMessage (
getMessage (
"Could not launch \"{0}\".",
new Object [] {name}));
dialog.open ();
}
This is the simplest way to use the Program class. Class Program provides other capabilities that we have not included here, and it is worth looking at its API if you get a chance. For example, you can use the findProgram() method to return an instance of Program, then ask this program for an image to display. This would allow you, for example, to show a specific image for each type of file that is shown in the table. 22.2.4 MultithreadingThe FileExplorer application uses multithreading to allow different file operations to run in parallel. These operations include deleting a file or hierarchy, copying a file or hierarchy, and filling the table in the background. These operations are described by a hierarchy of classes under the abstract class FileOperation.[2]
FileOperation always runs in a separate thread. The user interface will wait until either the operation completes or the expectedTime (in FileOperation) has passed. The user interface then calls the isDone method to see whether the operation has completed. This allows us to support both operations that complete quickly (without notification in the user interface) and longer running operations that display a progress dialog. Progress is reported to the user interface by calling the following method.
Look at the code for DeleteOperation to see a simple operation. 22.2.5 In-Place EditingNative tables and trees sometimes provide built-in support for directly editing the contents of an item that they are displaying. This support is typically quite limited. Because this support was either not available or not sufficiently powerful across all platforms, the SWT implementers chose to provide a separate, platform-neutral, in-place editing mechanism, rather than attempt to manifest the native capabilities.[3] The classes that provide this capability are found in the org.eclipse.swt.custom package. The class ControlEditor has subclasses TreeEditor and TableEditor[4] that allow an arbitrary control to be laid out "on top of" a field in a tree or table. Application code can use this to build an in-place editor by doing the following.
Depending on the application, you may also have to disable accelerators for the application menu and tool bar to prevent unexpected behavior. In the FileExplorer example, renaming of files and directories is implemented using in-place editing. The code that does this is found in the following.
This method is called from either rename(TreeItem item) or rename(TableItem item). These methods simply construct an instance of the appropriate editor class, then call the above method to do the work. 22.2.6 Other Design NotesHere are some other areas of the code that are worth looking at.
|
|
|
< Day Day Up > |
|