import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.naming.*;
import javax.naming.directory.*;
import com.novell.java.io.DataAccessable;
import com.novell.java.io.NInputStream;
import com.novell.java.io.NOutputStream;
public class DataPopupHandler
implements PopupHandler
{
public DataPopupHandler ()
{
}
public void popupRequested (
JFrame frame,
JPopupMenu popup,
Object obj)
{
Context ctx;
if (obj instanceof ContextTreeNode)
{
ctx = ((ContextTreeNode) obj).getThisContext ();
if ((null != ctx) &&
(ctx instanceof DirContext) &&
(ctx instanceof DataAccessable))
new DataActionListener (obj.toString(), (DirContext) ctx, popup);
}
}
}
class DataActionListener
implements ActionListener
{
String fileName;
DirContext fileCtx;
JMenuItem dataMenuItem;
private static int buttonHeight = 25;
public DataActionListener () {}
public DataActionListener (
String fileName,
DirContext fileCtx,
JPopupMenu popup)
{
this.fileName = fileName;
this.fileCtx = fileCtx;
dataMenuItem = new JMenuItem ("View Contents");
dataMenuItem.setToolTipText ("Text/hex viewer");
dataMenuItem.addActionListener (this);
popup.add (dataMenuItem);
}
public void actionPerformed (
ActionEvent event)
{
Object object = event.getSource ();
if (object == dataMenuItem)
data_Action (event);
}
void data_Action (
ActionEvent event)
{
DataViewFrame frame = new DataViewFrame (fileName, fileCtx);
Point p = ((Component) event.getSource ()).getLocation ();
p.translate (buttonHeight, buttonHeight);
frame.setLocation (p);
frame.show ();
}
}
class DataViewFrame
extends JFrame
implements ActionListener
{
Context fileCtx;
private static int emptySpace = 5;
private static int buttonHeight = 25;
private static int buttonWidth = 80;
private static int mainPaneHeight = 300;
private static int mainPaneWidth = 580;
JButton asciiButton;
JButton hexButton;
JButton saveButton;
JButton closeButton;
JScrollPane fileScrollPane;
JTextArea fileTextArea;
JList fileHexList;
DefaultListModel fileListModel;
static final int hexDisplayByteCount = 16;
private boolean textView;
public DataViewFrame (
String fileName,
DirContext fileCtx)
{
super (fileName);
this.fileCtx = fileCtx;
getContentPane ().setLayout (null);
setSize (
mainPaneWidth + (emptySpace * 2),
mainPaneHeight + buttonHeight + (emptySpace * 2));
fileScrollPane = new JScrollPane ();
fileScrollPane.setBounds (
emptySpace,
emptySpace,
mainPaneWidth - (emptySpace * 2),
mainPaneHeight - (buttonHeight + (emptySpace * 3)));
getContentPane ().add (fileScrollPane);
asciiButton = new JButton ("ASCII");
asciiButton.setToolTipText ("Display the data in ASCII form");
asciiButton.setBounds (
emptySpace,
mainPaneHeight - (buttonHeight + emptySpace),
buttonWidth,
buttonHeight);
asciiButton.addActionListener (this);
getContentPane ().add (asciiButton);
hexButton = new JButton ("Hex");
hexButton.setToolTipText ("Display the data in hexidecimal form");
hexButton.setBounds (
(buttonWidth + emptySpace) + emptySpace,
mainPaneHeight - (buttonHeight + emptySpace),
buttonWidth,
buttonHeight);
hexButton.addActionListener (this);
getContentPane ().add (hexButton);
saveButton = new JButton ("Save");
saveButton.setToolTipText ("Save the data");
saveButton.setBounds (
(buttonWidth + emptySpace) * 2 + emptySpace,
mainPaneHeight - (buttonHeight + emptySpace),
buttonWidth,
buttonHeight);
saveButton.addActionListener (this);
getContentPane ().add (saveButton);
closeButton = new JButton ("Close");
closeButton.setToolTipText ("Close this window");
closeButton.setBounds (
(buttonWidth + emptySpace) * 3 + emptySpace,
mainPaneHeight - (buttonHeight + emptySpace),
buttonWidth,
buttonHeight);
closeButton.addActionListener (this);
getContentPane ().add (closeButton);
textView = false;
showText ();
}
public void actionPerformed (
ActionEvent event)
{
Object object = event.getSource ();
if (object == asciiButton)
asciiButton_Action (event);
else if (object == hexButton)
hexButton_Action (event);
else if (object == saveButton)
saveButton_Action (event);
else if (object == closeButton)
closeButton_Action (event);
}
void asciiButton_Action (
ActionEvent event)
{
setVisible (false);
showText ();
setVisible (true);
}
void hexButton_Action (
ActionEvent event)
{
setVisible (false);
showHex ();
setVisible (true);
}
void saveButton_Action (
ActionEvent event)
{
Cursor saveCursor = getCursor ();
setCursor (Cursor.getPredefinedCursor (Cursor.WAIT_CURSOR));
try
{
DataOutputStream out =
new DataOutputStream (
new NOutputStream ((DataAccessable) fileCtx));
out.writeBytes (fileTextArea.getText ());
out.close ();
}
catch (Throwable e)
{
}
setCursor (saveCursor);
}
void closeButton_Action (
ActionEvent event)
{
dispose ();
}
void showText ()
{
if (textView)
return;
textView = true;
if (null != fileHexList)
fileScrollPane.remove (fileHexList);
if (null == fileTextArea)
{
fileTextArea = new JTextArea ();
fileTextArea.setEditable (true);
String newLine = System.getProperty ("line.separator");
boolean finished = false;
String s = null;
try
{
BufferedReader in =
new BufferedReader (
new InputStreamReader (
new NInputStream ((DataAccessable) fileCtx)));
s = in.readLine ();
while (s != null)
{
fileTextArea.append (s + newLine);
s = in.readLine ();
}
in.close ();
}
catch (IOException e)
{
fileTextArea.append (newLine + "<<< Interrupted >>>" + newLine);
(new MessageBox (
"Error",
"Unable to read file:\n" +
Util.getExceptionTrace (e))).show ();
}
catch (Throwable e)
{
(new MessageBox (
"Error",
"Unable to read file:\n" +
Util.getExceptionTrace (e))).show ();
}
}
fileScrollPane.getViewport().add (fileTextArea);
}
void showHex ()
{
if (!textView)
return;
textView = false;
if (null != fileTextArea)
fileScrollPane.getViewport ().remove (fileTextArea);
if (null == fileHexList)
{
fileListModel = new DefaultListModel ();
fileHexList = new JList (fileListModel);
fileHexList.setFont (new Font ("Courier", Font.PLAIN, 12));
try
{
BufferedReader in =
new BufferedReader (
new InputStreamReader (
new NInputStream ((DataAccessable) fileCtx)));
char a[] = new char [hexDisplayByteCount];
char disp[] = new char[12 + a.length * 3 + 1 + a.length];
char hex[] = { '0','1','2','3','4','5','6','7',
'8','9','A','B','C','D','E','F' };
int numBytesRead = 0;
int totalBytes = 0;
boolean finished = false;
int maxTextWidth = 0, dispWidth;
while (!finished)
{
int i = 0;
for (int j = 8; j > 0; j--)
{
disp[i++] = hex[(totalBytes >>> (4*j)) & 0x0F];
}
disp[i++] = ':';
disp[i++] = ' ';
numBytesRead = in.read (a, 0, a.length);
if (numBytesRead < a.length)
finished = true;
for (int j = 0; j < a.length; j++)
{
if (j < numBytesRead)
{
disp[i++] = hex[(a[j] >>> 4) & 0x0F];
disp[i++] = hex[(a[j] >>> 0) & 0x0F];
}
else
{
disp[i++] = ' ';
disp[i++] = ' ';
}
if (j == (a.length / 2) - 1)
{
disp[i++] = ' ';
disp[i++] = !finished ? '-' : ' ';
}
disp[i++] = ' ';
}
disp[i++] = ' ';
for (int j = 0; j < a.length; j++)
{
if (j < numBytesRead)
{
if (a[j] < (char) 32 || a[j] > (char) 127)
disp[i++] = '.';
else
disp[i++] = (char) a[j];
}
else
disp[i++] = ' ';
}
totalBytes += numBytesRead;
fileListModel.addElement (new String (disp));
}
in.close ();
}
catch (IOException e)
{
fileListModel.addElement ("<<< Interrupted >>>");
(new MessageBox (
"Error",
"Unable to read file:\n" +
Util.getExceptionTrace (e))).show ();
}
catch (Throwable e)
{
(new MessageBox (
"Error",
"Unable to read file:\n" +
Util.getExceptionTrace (e))).show ();
}
}
fileScrollPane.getViewport ().add (fileHexList);
}
}