import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.naming.*;
import com.novell.service.file.nw.*;
import com.novell.service.file.nw.naming.*;
public class FileSpecialHandler extends Frame
implements NSISpecialHandler,
WindowListener,
ActionListener
{
static final int width = 500;
static final int height = 400;
static final int hexDisplayByteCount = 16;
FileDirContext fileCtx;
String fileName;
Button hexButton = new Button ("Hex");
Button textButton = new Button ("Text");
MenuItem saveText = new MenuItem ("&Save");
MenuItem closeWindow = new MenuItem ("Close");
TextArea fileTextArea = null;
public FileSpecialHandler ()
{
}
public boolean initialize (Name name, Object obj)
{
if (obj instanceof FileDirContext)
{
fileCtx = (FileDirContext) obj;
fileName = name.get (name.size() - 1);
setTitle (fileName);
}
else
{
new NSIMessageBox ("Error", "Object is not a file context.");
return (false);
}
Menu m = new Menu ("&File");
m.add (saveText);
m.addSeparator ();
m.add (closeWindow);
MenuBar mb = new MenuBar ();
mb.add (m);
setMenuBar (mb);
addWindowListener (this);
hexButton.addActionListener (this);
textButton.addActionListener (this);
saveText.addActionListener (this);
closeWindow.addActionListener (this);
return (true);
}
public void handleObject ()
{
showHex ();
show ();
}
void showHex ()
{
this.removeAll ();
Panel listPanel = new Panel ();
List fileContentsList = new List ();
fileContentsList.setMultipleMode (false);
fileContentsList.setFont (new Font ("Courier", Font.PLAIN, 12));
listPanel.setLayout (new BorderLayout ());
listPanel.add ("Center", fileContentsList);
Panel buttonPanel = new Panel ();
buttonPanel.add (textButton);
listPanel.add ("South", buttonPanel);
add ("Center", listPanel);
pack ();
this.setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
try
{
BufferedReader in =
new BufferedReader(
new InputStreamReader(new NFileInputStream(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;
fileContentsList.addItem (new String (disp));
dispWidth = fileContentsList.getFontMetrics (
fileContentsList.getFont ()).
charsWidth (disp, 0, disp.length);
if (maxTextWidth < dispWidth)
maxTextWidth = dispWidth;
}
in.close ();
setSize (maxTextWidth + 40, height);
}
catch (IOException e)
{
System.out.println ("Unable to process file");
System.out.println ("Unexpected io exception " + e);
e.printStackTrace ();
}
this.setCursor (Cursor.getDefaultCursor());
}
void showText ()
{
this.removeAll ();
fileTextArea = new TextArea ();
fileTextArea.setEditable (true);
Panel buttonPanel = new Panel ();
buttonPanel.add (hexButton);
setLayout (new BorderLayout ());
add ("Center", fileTextArea);
add ("South", buttonPanel);
pack ();
this.setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
String newLine = System.getProperty ("line.separator");
try
{
BufferedReader in =
new BufferedReader (
new InputStreamReader(new NFileInputStream(fileCtx)));
boolean finished = false;
String s = null;
try
{
s = in.readLine ();
while (s != null)
{
fileTextArea.append (s + newLine);
s = in.readLine ();
}
}
catch (EOFException e)
{
fileTextArea.append (newLine + newLine +
"<<< Interrupted >>>" + newLine);
}
in.close ();
setSize (width, height);
show ();
}
catch (IOException e)
{
System.out.println ("Unable to process file");
System.out.println ("Unexpected io exception " + e);
e.printStackTrace ();
}
this.setCursor (Cursor.getDefaultCursor());
}
public void actionPerformed (
ActionEvent evt)
{
Object target = evt.getSource();
if (target == hexButton)
{
setVisible (false);
showHex ();
setVisible (true);
}
else if (target == textButton)
{
setVisible (false);
showText ();
setVisible (true);
}
else if (target == saveText)
{
try
{
this.setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
DataOutputStream out =
new DataOutputStream(new NFileOutputStream(fileCtx));
out.writeBytes (fileTextArea.getText ());
out.close ();
this.setCursor (Cursor.getDefaultCursor());
}
catch (IOException e)
{
System.out.println ("Unable to process file");
System.out.println ("Unexpected io exception " + e);
e.printStackTrace ();
}
}
else if (target == closeWindow)
{
dispose ();
}
}
public void windowClosed (
WindowEvent event)
{
}
public void windowDeiconified (
WindowEvent event)
{
}
public void windowIconified (
WindowEvent event)
{
}
public void windowActivated (
WindowEvent event)
{
}
public void windowDeactivated (
WindowEvent event)
{
}
public void windowOpened (
WindowEvent event)
{
}
public void windowClosing (
WindowEvent event)
{
dispose ();
}
}