import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import java.io.*;
import javax.naming.*;
import javax.naming.directory.*;
public class NSIBrowserFrame extends Frame
implements WindowListener,
ActionListener,
ItemListener
{
public static final int width = 420;
public static final int height = 400;
NSIBrowserFrame parent;
Properties props;
Context initCtx;
CompositeName currName;
Context currCtx;
Vector currCtxVector = new Vector ();
Label initCtxLabel;
Label currentPathLabel;
List nameList;
Vector nameListVector;
CheckboxMenuItem listClassesItem = new CheckboxMenuItem ("List Classes");
CheckboxMenuItem listBindingsItem = new CheckboxMenuItem ("List Bindings");
public boolean doListClasses = false;
public boolean doListBindings = false;
Button attrButton;
Button lookupButton;
Button specialButton;
static final String jndiInitCtxPropShort = "Init Ctx:";
NSIBrowserFrame (
String title,
NSIBrowserFrame parent,
Properties props)
{
super (title);
this.parent = parent;
this.props = props;
if (null != parent)
{
currCtx = parent.currCtx;
initCtx = currCtx;
currName = new CompositeName ();
}
MenuBar mainMenu = new MenuBar ();
listClassesItem.addItemListener(this);
listBindingsItem.addItemListener(this);
Menu file = new Menu ("File");
file.add ("Exit");
file.addActionListener(this);
mainMenu.add (file);
Menu find = new Menu ("Find");
find.add ("Lookup...");
find.addActionListener(this);
mainMenu.add (find);
Menu options = new Menu ("Options");
options.add (listClassesItem);
options.add (listBindingsItem);
options.add ("-");
options.add ("Initial Context...");
if (null == parent)
options.add ("Save Settings");
options.addActionListener(this);
mainMenu.add (options);
Menu help = new Menu ("Help");
help.add ("About...");
help.addActionListener(this);
mainMenu.add (help);
setMenuBar (mainMenu);
attrButton = new Button ("Attributes");
attrButton.addActionListener(this);
lookupButton = new Button ("Lookup");
lookupButton.addActionListener(this);
specialButton = new Button ("Special");
specialButton.addActionListener(this);
Panel listPanel = new Panel ();
listPanel.setLayout (new BorderLayout ());
nameList = new List ();
nameList.addActionListener(this);
nameListVector = new Vector ();
nameList.setMultipleMode (false);
listPanel.add ("Center", nameList);
Panel buttonPanel = new Panel ();
buttonPanel.setLayout (new GridLayout (1, 0));
buttonPanel.add (attrButton);
buttonPanel.add (lookupButton);
buttonPanel.add (specialButton);
listPanel.add ("South", buttonPanel);
add ("Center", listPanel);
currentPathLabel = new Label ();
add ("North", currentPathLabel);
initCtxLabel = new Label ();
add ("South", initCtxLabel);
addWindowListener(this);
pack ();
}
public void actionPerformed (
ActionEvent evt)
{
Frame f;
Object what = evt.getSource ();
if ("Exit".equals (evt.getActionCommand ()))
{
dispose ();
if (null == parent)
System.exit (0);
}
else if ("Lookup...".equals (evt.getActionCommand ()))
{
f = new NSILookupFrame (this, initCtx, currCtx);
f.setSize (NSILookupFrame.width, NSILookupFrame.height);
f.show ();
}
else if ("Initial Context...".equals (evt.getActionCommand ()))
{
f = new NSIInitialContextFrame (this);
f.setSize (
NSIInitialContextFrame.width,
NSIInitialContextFrame.height);
f.show ();
}
else if ("Save Settings".equals (evt.getActionCommand ()))
{
NSIBrowser.saveConfigFile ();
}
else if ("About...".equals (evt.getActionCommand ()))
{
new NSIMessageBox ("About", "NSIBrowser by Novell Inc.");
}
else if (what == attrButton)
{
showAttributes ();
}
else if (what == lookupButton)
{
lookupContext ();
}
else if (what == specialButton)
{
callSpecialHandler ();
}
else if (what == nameList)
{
int itemIndex = nameList.getSelectedIndex ();
if (-1 != itemIndex)
{
SortableString sortStr;
String atomicName;
sortStr = (SortableString) nameListVector.elementAt (itemIndex);
atomicName = sortStr.getKeyStr ();
Context tempCtx;
if (atomicName.equals (".."))
{
try
{
currName.remove (
currName.size () - 1);
}
catch (InvalidNameException e)
{
System.out.println ("Unexpected naming exception " + e);
e.printStackTrace ();
}
int stackSize = currCtxVector.size ();
currCtx = (Context) currCtxVector.elementAt (stackSize - 1);
currCtxVector.removeElementAt (stackSize - 1);
}
else
{
try
{
currName.add (atomicName);
currCtxVector.addElement (currCtx);
tempCtx = (Context) currCtx.lookup (atomicName);
currCtx = tempCtx;
}
catch (NameNotFoundException e)
{
try
{
tempCtx = (Context) currCtx.lookup ("/" + atomicName);
currCtx = tempCtx;
}
catch (Exception e2)
{
System.out.println ("Unable to update current context");
System.out.println ("Unexpected exception " + e2);
e2.printStackTrace ();
}
}
catch (NamingException e)
{
System.out.println ("Unexpected naming exception " + e);
e.printStackTrace ();
}
}
updateList ();
}
}
}
public void itemStateChanged(
ItemEvent evt)
{
Object what = evt.getItem ();
if ("List Classes".equals (what))
{
doListClasses = ! doListClasses;
doListBindings = false;
listClassesItem.setState (doListClasses);
listBindingsItem.setState (doListBindings);
updateList ();
}
else if ("List Bindings".equals (what))
{
doListBindings = ! doListBindings;
doListClasses = false;
listClassesItem.setState (doListClasses);
listBindingsItem.setState (doListBindings);
updateList ();
}
}
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 ();
if (null == parent)
System.exit (0);
}
public void setProperties (
Properties props)
{
this.props = props;
}
public synchronized void updateList ()
{
int sortStart = 0;
int lastElement;
SortableString sortStr;
if (null == currCtx)
throw new NullPointerException ("currCtx");
this.setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
nameListVector.removeAllElements ();
if (0 != currName.size ())
currentPathLabel.setText (currName.toString ());
else
currentPathLabel.setText ("[root]");
if (0 != currName.size ())
{
sortStart = 1;
nameListVector.addElement (new SortableString (".."));
}
try
{
if (true == doListBindings)
{
NamingEnumeration bindingEnum;
Binding binding;
bindingEnum = currCtx.listBindings ("");
while (true == bindingEnum.hasMoreElements ())
{
binding = (Binding) bindingEnum.next ();
sortStr = new SortableString (
binding.getName (),
binding.getClassName ());
nameListVector.addElement (sortStr);
}
try
{
bindingEnum = currCtx.listBindings ("/");
while (true == bindingEnum.hasMoreElements ())
{
binding = (Binding) bindingEnum.next ();
sortStr = new SortableString (
binding.getName (),
binding.getClassName ());
nameListVector.addElement (sortStr);
}
}
catch (NameNotFoundException e)
{
System.out.println ("Next naming system not supported (Bindings)");
}
}
else
{
NamingEnumeration ncEnum;
NameClassPair ncPair;
ncEnum = currCtx.list ("");
while (true == ncEnum.hasMoreElements ())
{
ncPair = (NameClassPair) ncEnum.next ();
if (doListClasses)
sortStr = new SortableString (
ncPair.getName (),
ncPair.getClassName ());
else
sortStr = new SortableString (ncPair.getName ());
nameListVector.addElement (sortStr);
}
try
{
ncEnum = currCtx.list ("/");
if (null == ncEnum)
throw new NameNotFoundException ("/");
while (true == ncEnum.hasMoreElements ())
{
ncPair = (NameClassPair) ncEnum.next ();
if (doListClasses)
sortStr = new SortableString (
ncPair.getName (),
ncPair.getClassName ());
else
sortStr = new SortableString (ncPair.getName ());
nameListVector.addElement (sortStr);
}
}
catch (NameNotFoundException e)
{
System.out.println ("Next naming system not supported (NameClassPairs)");
}
}
}
catch (NamingException e)
{
System.out.println ("Unable to update name list");
System.out.println ("Unexpected naming exception : " + e);
e.printStackTrace ();
}
lastElement = nameListVector.size() - 1;
QuickSort.sort(nameListVector, sortStart, lastElement);
nameList.setVisible(false);
nameList.removeAll ();
nameList.setVisible(true);
for (int i = 0; i <= lastElement; i++)
{
sortStr = (SortableString) nameListVector.elementAt (i);
nameList.addItem (sortStr.toString ());
}
this.setCursor (Cursor.getDefaultCursor());
}
public void resetInitCtx ()
{
String initPropVal;
initCtx = null;
currCtx = null;
currName = null;
currName = new CompositeName ();
initPropVal = props.getProperty (NSIBrowser.jndiInitCtxProp);
if ((null != initPropVal) && (false == initPropVal.equals ("")))
{
try
{
initCtx = new InitialContext ( props );
}
catch (NamingException e)
{
}
}
currCtx = initCtx;
if ((null == initPropVal) || (true == initPropVal.equals ("")))
initCtxLabel.setText (jndiInitCtxPropShort + "[not set]");
else
initCtxLabel.setText (jndiInitCtxPropShort + initPropVal);
updateList ();
}
public void showAttributes ()
{
String atomicName;
int itemIndex = nameList.getSelectedIndex ();
if (-1 == itemIndex)
{
atomicName = "";
}
else
{
SortableString sortStr;
sortStr = (SortableString) nameListVector.elementAt (itemIndex);
atomicName = sortStr.getKeyStr ();
}
if (atomicName.equals (".."))
{
new NSIMessageBox ("Error", "No attributes for '..'");
return;
}
try
{
Object obj = currCtx.lookup (atomicName);
if (obj instanceof DirContext)
{
Frame f;
f = new NSIAttributeFrame ((DirContext) obj);
f.setSize (NSIAttributeFrame.width, NSIAttributeFrame.height);
f.show ();
}
else
new NSIMessageBox ("Error", atomicName + " is not a DirContext");
}
catch (NamingException e)
{
System.out.println ("Unable to lookup " + atomicName);
System.out.println ("Unexpected naming exception : " + e);
e.printStackTrace ();
}
}
public void lookupContext ()
{
String atomicName;
int itemIndex = nameList.getSelectedIndex ();
if (-1 == itemIndex)
{
atomicName = "";
}
else
{
SortableString sortStr;
sortStr = (SortableString) nameListVector.elementAt (itemIndex);
atomicName = sortStr.getKeyStr ();
}
if (atomicName.equals (".."))
{
new NSIMessageBox ("Error", "Cannot perform lookup on '..'");
return;
}
try
{
Object obj = currCtx.lookup (atomicName);
if (obj instanceof Context)
startNewContextFrame (
currName.toString () + atomicName,
(Context) obj);
else
new NSIMessageBox ("Error", atomicName + " is not a DirContext");
}
catch (NamingException e)
{
System.out.println ("Unable to lookup " + atomicName);
System.out.println ("Unexpected naming exception : " + e);
e.printStackTrace ();
}
}
public void startNewContextFrame (
String name,
Context ctx)
{
NSIBrowserFrame f;
Context tempCtx = currCtx;
currCtx = ctx;
f = new NSIBrowserFrame (
name,
this,
props);
f.setSize (width, height);
f.show ();
f.updateList ();
currCtx = tempCtx;
}
public void callSpecialHandler ()
{
String atomicName;
int itemIndex = nameList.getSelectedIndex ();
if (-1 == itemIndex)
{
atomicName = "";
}
else
{
SortableString sortStr;
sortStr = (SortableString) nameListVector.elementAt (itemIndex);
atomicName = sortStr.getKeyStr ();
}
if (atomicName.equals (".."))
{
new NSIMessageBox ("Error", "No attributes for '..'");
return;
}
try
{
Object obj = currCtx.lookup (atomicName);
Properties props = new Properties ();
String fileProp = "jnos.browser.handler.com.novell.service.file.nw.naming.FileDirContext";
props.put (fileProp, "FileSpecialHandler");
String handlerPropName = "jnos.browser.handler." + obj.getClass ().getName ();
String handlerClassName = props.getProperty (handlerPropName);
if (null == handlerClassName || handlerClassName.equals (""))
new NSIMessageBox ("Error", "No handler registered");
else try
{
Object handlerObj = Class.forName (handlerClassName).newInstance ();
if (handlerObj instanceof NSISpecialHandler)
{
NSISpecialHandler handler = (NSISpecialHandler) handlerObj;
CompositeName tempName = new CompositeName ();
tempName.addAll (currName);
tempName.add (atomicName);
this.setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
if (handler.initialize (tempName, obj))
handler.handleObject ();
else
new NSIMessageBox ("Error", "Unable to initialize handler");
this.setCursor (Cursor.getDefaultCursor());
}
else
{
new NSIMessageBox ("Error", "Special object handler not " +
"registered for this object type (" +
obj.getClass ().getName () + ")");
}
}
catch (Exception e)
{
System.out.println ("Unable to instantiate handler");
System.out.println ("Unexpected exception " + e);
e.printStackTrace ();
}
}
catch (NamingException e)
{
System.out.println ("Unable to lookup " + atomicName);
System.out.println ("Unexpected naming exception : " + e);
e.printStackTrace ();
}
}
}
class ImageCanvas extends Canvas
{
int scale = 10;
Image image;
Image scaledImage;
ImageCanvas(Image image)
{
this.image = image;
prepareImage(image, this);
}
public void paint(Graphics g)
{
update(g);
}
public void update(Graphics g)
{
int w = image.getWidth(this);
int h = image.getHeight(this);
if (w >= 0 || h >= 0)
{
if (g.drawImage(image, 0, 0, this) && scaledImage == null)
{
if (image.getProperty("comment", this) !=
Image.UndefinedProperty)
{
System.out.println(
image.getProperty("comment", this));
}
int[] pixels = new int[w * h];
try
{
PixelGrabber pg = new PixelGrabber(
image, 0, 0, w, h, pixels, 0, w);
pg.grabPixels();
if ((pg.status() & ImageObserver.ABORT) != 0)
{
System.err.println("Error while fetching image");
System.exit(1);
}
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
int d = 0, s = 0;
int[] newpixels = new int[w * h * scale * scale];
for (int i = 0; i < h; i++)
{
for (int j = 0; j < scale; j++)
{
for (int k = 0; k < w; k++)
{
for (int l = 0; l < scale; l++)
{
newpixels[d++] = pixels[i*w + k];
}
}
}
}
scaledImage = getToolkit().createImage(
new MemoryImageSource(w*scale, h*scale,
ColorModel.getRGBdefault(), newpixels, 0, w*scale));
}
}
if (scaledImage != null)
{
g.drawImage(scaledImage, w, 0, this);
}
}
}