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 JPanel
implements 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()
25 );
openFileCount--;
return true;
}
30 public int getOpenFileCount ()
{
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
5 // and read 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 =
15 new ExtensionFileFilter("Text Files");
filter.addExtension("txt");
chooser.addChoosableFileFilter(filter);
int status = chooser.showOpenDialog(this);
20
if ( status == JFileChooser.APPROVE_OPTION )
{
File file = chooser.getSelectedFile();
25 if ( file.canRead() )
{
try
{
// Read the contents of the file into a byte[]
30 // object.
FileInputStream input_file =
new FileInputStream(file);
byte[] file_data = new byte[(int) file.length()];
input_file.read(file_data);
35
// Create a text area to hold the contents of the
// file.
JTextArea text_area = new JTextArea();
text_area.setEditable(false);
40 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 panes; and make the new
45 // scroll pane the selected component.
JScrollPane text_comp = new JScrollPane(text_area);
mTextContainer.add(text_comp, file.getName());
mTextContainer.setSelectedComponent(text_comp);
50 // Our work is done.
openFileCount++;
opened = true;
}
catch (java.io.FileNotFoundException ex)
55 {
JOptionPane.showMessageDialog(
null,
"Cannot find '" + file.getAbsolutePath() + "'",
"Read Error", JOptionPane.ERROR_MESSAGE
60 );
}
catch (java.io.IOException ex)
{
JOptionPane.showMessageDialog(
65 null,
"Error reading from '" + file.getAbsolutePath() +
"':" + ex.getMessage(),
"Read Error", JOptionPane.ERROR_MESSAGE
);
70 }
}
else
{
JOptionPane.showMessageDialog(
75 null,
"Cannot read from file '" +
file.getAbsolutePath() + "'",
"Read Error", JOptionPane.ERROR_MESSAGE
);
80 }
}
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}/share/tweek/beans/FileOpenTestBean.jar"
class="fileopentestbean.FileOpenTestBean" />
<tree path="/" />
</guipanel>
</beanlist>