The table-of-content library defines a window displaying a tree in an explorer-like style. This library is programmed by refining its base-class toc_window. We will introduce this library using an example exploring the filesystem. A screendump of this application is in figure 21.
|Figure 21 : Exploring the filesystem|
:- pce_autoload(toc_window, library(pce_toc)). :- pce_autoload(report_dialog, library(pce_report)). :- pce_begin_class(explorer, frame, "Explore the filesystem"). initialise(E, Dir:directory) :-> "Explore from directory":: send_super(E, initialise, 'Simple explorer'), send(E, append, new(DH, directory_hierarchy(Dir))), send(new(view), right, DH), send(new(report_dialog), below, DH). open_node(E, Node:file) :-> "Show file content of opened node":: get(E, member, view, View), send(View, load, Node). :- pce_end_class. :- pce_begin_class(directory_hierarchy, toc_window, "Browser for a directory-hierarchy"). initialise(FB, Root:directory) :-> send(FB, send_super, initialise), get(Root, name, Name), send(FB, root, toc_folder(Name, Root)). expand_node(FB, D:directory) :-> "Called if a node is to be expanded":: new(SubDirsNames, chain), new(FileNames, chain), send(D, scan, FileNames, SubDirsNames), get(SubDirsNames, map, ?(D, directory, @arg1), SubDirs), send(SubDirs, for_all, message(FB, son, D, create(toc_folder, @arg1?name, @arg1))), get(FileNames, map, ?(D, file, @arg1), SubFiles), send(SubFiles, for_all, message(FB, son, D, create(toc_file, @arg1?base_name, @arg1))). open_node(FB, Node:file) :-> "Called if a file is double-clicked":: send(FB?frame, open_node, Node). :- pce_end_class.
Programming is achieved by subclassing toc_window and in some cases the support classes toc_folder and toc_file, representing expandable and leaf-nodes.
Each node is assigned an identifier, a unique reference to the node. In the example below we used file and directory objects for this purpose. The identifier is the second argument to the creation of the node. When omitted, the node is turned into an identifier of itself. This distinction is used to hide the existence of graphical node objects for users of the basic functionality.
Below we describe the important methods of this package. We start with the virtual methods on class toc_window that should be refined by most applications.
[+]sign or double-clicked a toc_folder. This method is normally refined to add sub-nodes for Id to the current node using `toc_window
->son'. If the implementation of toc_window is activated at the end the window will scroll such that as much as possible of the subtree below Id is visible.
->open_node and therefore the action following select_node should execute quickly.
The methods below are used for general querying and manipulation of the hierarchy.
->initialise to get a sensible initial hierarchy.
The classes toc_folder and toc_file are summarised below. Subclassing may be used to modify interaction and/or store additional information with the node.