Not everything written in Tweek must use Java, C++, IDL, and CORBA. The Tweek Java GUI is a generalized Bean-loading environment, and as such, its main focus is to load JavaBeans and present them to the user. Those Beans may take advantage of CORBA, but they are not required to do so. In this section, we show another sample Bean that is much simpler than the previous example.
The Bean shown in this example can open and close multiple text files. To do this, the Bean implements the interface org.vrjuggler.tweek.beans.FileLoader. The Java code will be shown in separate pieces to make it easier to understand. The full code is in $TWEEK_BASE_DIR/share/test/FileOpenTestBean/fileopentestbean/FileOpenTestBean.java. An example makefile can be found in the section called “File Loader”. within Appendix A, Compiling Example Code..
As in the previous example, we focus on the imported classes first. They are as follows:
package fileopentestbean;import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.*;
import org.vrjuggler.tweek.services.ExtensionFileFilter;
import org.vrjuggler.tweek.beans.FileLoader;
![]()
Next, we show the class declaration and the member variables.
public class FileOpenTestBean extends JPanelimplements java.io.Serializable, FileLoader
{ // Methods ... private int openFileCount = 0;
private BorderLayout mMainLayout = new BorderLayout(); private JLabel mBeanTitle = new JLabel(); private JTabbedPane mTextContainer = new JTabbedPane();
}
Now that we have the basics for the class, we can start implementing the FileLoader interface. Of the methods that must be implemented, the most complex is openRequested(). It will be shown last because of its length. We will begin instead with the simplest methods.
1 public String getFileType()
{
return "Text";
}
5
public boolean canOpenMultiple()
{
return true;
}
10
public boolean canSave()
{
return false;
}
15
public boolean saveRequested()
{
return false;
}
20
public boolean closeRequested()
{
mTextContainer.remove(mTextContainer.getSelectedComponent());
openFileCount--;
25 return true;
}
public int getOpenFileCount ()
{
30 return openFileCount;
}The above are all the methods of the FileLoader interface except openRequested(). We are now ready to move on to it.
1 public boolean openRequested()
{
// Initialize this to false since a lot of things can go wrong in the
// process of opening files. Once the file is opened and read
5 // successfully, this can be changed to true.
boolean opened = false;
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(false);
10 chooser.setDialogTitle("Text File Loader");
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// Only load .txt files.
ExtensionFileFilter filter = new ExtensionFileFilter("Text Files");
15 filter.addExtension("txt");
chooser.addChoosableFileFilter(filter);
int status = chooser.showOpenDialog(this);
20 if ( status == JFileChooser.APPROVE_OPTION )
{
File file = chooser.getSelectedFile();
if ( file.canRead() )
25 {
try
{
// Read the contents of the file into a byte[] object.
FileInputStream input_file = new FileInputStream(file);
30 byte[] file_data = new byte[(int) file.length()];
input_file.read(file_data);
// Create a text area to hold the contents of the file.
JTextArea text_area = new JTextArea();
35 text_area.setEditable(false);
text_area.insert(new String(file_data), 0);
// Create a scroll pane to hold the text area; add it to the
// tabbed pane with all the other previously loaded scroll
40 // panes; and make the new scroll pane the selected component.
JScrollPane text_comp = new JScrollPane(text_area);
mTextContainer.add(text_comp, file.getName());
mTextContainer.setSelectedComponent(text_comp);
45 // Our work is done.
openFileCount++;
opened = true;
}
catch (java.io.FileNotFoundException ex)
50 {
JOptionPane.showMessageDialog(null, "Cannot find '" +
file.getAbsolutePath() + "'",
"Read Error",
JOptionPane.ERROR_MESSAGE);
55 }
catch (java.io.IOException ex)
{
JOptionPane.showMessageDialog(null, "Error reading from '" +
file.getAbsolutePath() + "':" +
60 ex.getMessage(), "Read Error",
JOptionPane.ERROR_MESSAGE);
}
}
else
65 {
JOptionPane.showMessageDialog(null, "Cannot read from file '" +
file.getAbsolutePath() + "'",
"Read Error",
JOptionPane.ERROR_MESSAGE);
70 }
}
return opened;
}This Bean uses no CORBA code and does not require C++ code to act as a peer. This may be the case for many Panel Beans written for Tweek. Of course, this Bean could be extended to open files that are then handed off to C++ code through CORBA.
The XML file for this Bean is very simple. It simply lists the Bean file information and puts the Bean at the root of the Bean tree. The full file is shown in Example 6.7, “FileOpenTestBean.xml”.
Example 6.7. FileOpenTestBean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beanlist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.vrjuggler.org/tweek/xsd/1.1/beanlist.xsd">
<guipanel name="File Open Test Bean">
<file name="${TWEEK_BASE_DIR}/bin/beans/FileOpenTestBean.jar"
class="fileopentestbean.FileOpenTestBean" />
<tree path="/" />
</guipanel>
</beanlist>