Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 68367 invoked from network); 9 Sep 2005 23:10:18 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 9 Sep 2005 23:10:18 -0000 Received: (qmail 46161 invoked by uid 500); 9 Sep 2005 23:10:15 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 46134 invoked by uid 500); 9 Sep 2005 23:10:15 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 46115 invoked by uid 99); 9 Sep 2005 23:10:14 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 09 Sep 2005 16:10:14 -0700 X-ASF-Spam-Status: No, hits=-9.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Fri, 09 Sep 2005 16:10:25 -0700 Received: (qmail 68342 invoked by uid 65534); 9 Sep 2005 23:10:12 -0000 Message-ID: <20050909231012.68341.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r279909 - in /directory/sandbox/trunk/proxy/src/java/main/org/apache/ldap/proxy/gui: ConnectDialog.java MainFrame.java Date: Fri, 09 Sep 2005 23:10:11 -0000 To: commits@directory.apache.org From: elecharny@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: elecharny Date: Fri Sep 9 16:10:05 2005 New Revision: 279909 URL: http://svn.apache.org/viewcvs?rev=279909&view=rev Log: Updated with Jocelyn modifications Added: directory/sandbox/trunk/proxy/src/java/main/org/apache/ldap/proxy/gui/ConnectDialog.java Modified: directory/sandbox/trunk/proxy/src/java/main/org/apache/ldap/proxy/gui/MainFrame.java Added: directory/sandbox/trunk/proxy/src/java/main/org/apache/ldap/proxy/gui/ConnectDialog.java URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/proxy/src/java/main/org/apache/ldap/proxy/gui/ConnectDialog.java?rev=279909&view=auto ============================================================================== --- directory/sandbox/trunk/proxy/src/java/main/org/apache/ldap/proxy/gui/ConnectDialog.java (added) +++ directory/sandbox/trunk/proxy/src/java/main/org/apache/ldap/proxy/gui/ConnectDialog.java Fri Sep 9 16:10:05 2005 @@ -0,0 +1,186 @@ +package org.apache.ldap.proxy.gui; + +import java.awt.Frame; +import java.awt.GridLayout; +import java.awt.HeadlessException; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JTextField; + +public class ConnectDialog extends JDialog implements ActionListener +{ + + private final static String TITLE = "Connect Parameter"; + + private JTextField localPortTF; + + private JTextField portTF; + + private JTextField hostTF; + + private final static JLabel localPortLabel = new JLabel( "Local Port", JLabel.CENTER ); + + private final static JLabel hostLabel = new JLabel( "Host name", JLabel.CENTER ); + + private final static JLabel portLabel = new JLabel( "Port", JLabel.CENTER ); + + private JButton cancelBut = new JButton( "Cancel" ); + + private JButton okBut = new JButton( "OK" ); + + private int currentLocalPortValue = 1389; + + private int currentPortValue = 389; + + private String currentHostValue = "localhost"; + + public final static int DEFAULT_LOCAL_PORT = 1389; + + public final static String DEFAULT_HOST = "localhost"; + + public final static int DEFAULT_PORT = 389; + + public ConnectDialog(Frame arg0) throws HeadlessException + { + super( arg0, true ); + setTitle( TITLE ); + init(); + } + + public ConnectDialog(Frame arg0, int localPort, String host, int port) throws HeadlessException + { + super( arg0, true ); + setTitle( TITLE ); + setCurrentLocalPortValue( localPort ); + setCurrentHostValue( host ); + setCurrentPortValue( port ); + init(); + } + + /** + * Initialize JDialog + */ + private void init() + { + JPanel principalPane = new JPanel(); + principalPane.setLayout( new BoxLayout( principalPane, BoxLayout.Y_AXIS ) ); + principalPane.add( new JLabel( "Set the Ports & Host parameter", JLabel.CENTER ) ); + + JPanel tfPane = new JPanel( new GridLayout( 2, 3 ) ); + localPortTF = new JTextField( "" + getCurrentLocalPortValue() ); + hostTF = new JTextField( getCurrentHostValue() ); + portTF = new JTextField( "" + getCurrentPortValue() ); + tfPane.add( localPortLabel ); + tfPane.add( hostLabel ); + tfPane.add( portLabel ); + tfPane.add( localPortTF ); + tfPane.add( hostTF ); + tfPane.add( portTF ); + principalPane.add( tfPane ); + + JPanel butPane = new JPanel(); + butPane.setLayout( new BoxLayout( butPane, BoxLayout.X_AXIS ) ); + butPane.add( cancelBut ); + butPane.add( okBut ); + okBut.addActionListener( this ); + cancelBut.addActionListener( this ); + okBut.setActionCommand( "OK" ); + cancelBut.setActionCommand( "cancel" ); + principalPane.add( butPane ); + + this.getContentPane().add( principalPane ); + // to quit only by the OK or Cancel button + this.setDefaultCloseOperation( JDialog.DO_NOTHING_ON_CLOSE ); + this.setBounds( getParent().getX() + 50, getParent().getY() + 50, 600, 200 ); + + } + + public String getCurrentHostValue() + { + return currentHostValue; + } + + public void setCurrentHostValue( String currentHostValue ) + { + this.currentHostValue = currentHostValue; + } + + public int getCurrentLocalPortValue() + { + return currentLocalPortValue; + } + + public void setCurrentLocalPortValue( int currentLocalPortValue ) + { + this.currentLocalPortValue = currentLocalPortValue; + } + + public void setCurrentLocalPortValue( String currentLocalPortValue ) + { + try + { + this.currentLocalPortValue = Integer.parseInt( currentLocalPortValue ); + ; + } + catch ( NumberFormatException nfe ) + { + JOptionPane.showMessageDialog( this, new JLabel( "New Local Port not set to " + currentLocalPortValue + + ". Set to default value : " + DEFAULT_LOCAL_PORT + "." ) ); + this.currentLocalPortValue = DEFAULT_LOCAL_PORT; + } + } + + public int getCurrentPortValue() + { + return currentPortValue; + } + + public void setCurrentPortValue( int currentPortValue ) + { + this.currentPortValue = currentPortValue; + } + + public void setCurrentPortValue( String currentPortValue ) + { + try + { + this.currentPortValue = Integer.parseInt( currentPortValue ); + } + catch ( NumberFormatException nfe ) + { + JOptionPane.showMessageDialog( this, new JLabel( "New Port not set to " + currentPortValue + + ". Set to default value : " + DEFAULT_PORT + "." ) ); + this.currentPortValue = DEFAULT_PORT; + } + } + + public void actionPerformed( ActionEvent action ) + { + String butName = ( (JButton) action.getSource() ).getActionCommand(); + System.out.println( butName ); + + if ( butName.equals( "OK" ) ) + { + System.out.println( "click on OK" ); + setCurrentLocalPortValue( localPortTF.getText() ); + setCurrentHostValue( hostTF.getText() ); + setCurrentPortValue( portTF.getText() ); + setVisible( false ); + + } + else + { + System.out.println( "click on Cancel" ); + setVisible( false ); + } + + } + +} \ No newline at end of file Modified: directory/sandbox/trunk/proxy/src/java/main/org/apache/ldap/proxy/gui/MainFrame.java URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/proxy/src/java/main/org/apache/ldap/proxy/gui/MainFrame.java?rev=279909&r1=279908&r2=279909&view=diff ============================================================================== --- directory/sandbox/trunk/proxy/src/java/main/org/apache/ldap/proxy/gui/MainFrame.java (original) +++ directory/sandbox/trunk/proxy/src/java/main/org/apache/ldap/proxy/gui/MainFrame.java Fri Sep 9 16:10:05 2005 @@ -1,630 +1,755 @@ -/* - * Copyright 2005 The Apache Software Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -package org.apache.ldap.proxy.gui; - -import org.apache.asn1new.ldap.pojo.LdapMessage; -import org.apache.ldap.proxy.impl.LdapMessageWithPDU; -import org.apache.ldap.proxy.impl.LdapProxy; - -import com.thoughtworks.xstream.XStream; - -import java.awt.Color; -import java.awt.Font; -import java.awt.HeadlessException; -import java.awt.Rectangle; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.KeyEvent; - -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; - -import java.net.UnknownHostException; - -import javax.swing.ImageIcon; -import javax.swing.JCheckBoxMenuItem; -import javax.swing.JFrame; -import javax.swing.JMenu; -import javax.swing.JMenuBar; -import javax.swing.JMenuItem; -import javax.swing.JScrollPane; -import javax.swing.JSplitPane; -import javax.swing.JTextPane; -import javax.swing.JTree; -import javax.swing.KeyStroke; -import javax.swing.event.TreeExpansionEvent; -import javax.swing.event.TreeExpansionListener; -import javax.swing.event.TreeSelectionEvent; -import javax.swing.event.TreeSelectionListener; -import javax.swing.text.BadLocationException; -import javax.swing.text.DefaultStyledDocument; -import javax.swing.text.SimpleAttributeSet; -import javax.swing.text.Style; -import javax.swing.text.StyleConstants; -import javax.swing.text.StyleContext; -import javax.swing.tree.DefaultMutableTreeNode; -import javax.swing.tree.DefaultTreeCellRenderer; -import javax.swing.tree.DefaultTreeModel; -import javax.swing.tree.TreeNode; -import javax.swing.tree.TreeSelectionModel; - -/** - * A Ldap Proxy GUI - * - * @author Apache Directory Project - */ -public class MainFrame extends JFrame implements TreeSelectionListener, TreeExpansionListener -{ - // ~ Static fields/initializers - // ----------------------------------------------------------------- - // Shut down the Serialization warning ... - public static final long serialVersionUID = 1L; - - /** frame itSelf */ - private MainFrame itSelf; - - /** Frame and Application name */ - public static final String FRAME_NAME = "LDAP Proxy GUI"; - - /** Default Frame default Size */ - public static final Rectangle FRAME_SIZE = new Rectangle( 100, 100, 1000, 500 ); - - // ~ Instance fields - // ---------------------------------------------------------------------------- - - // ALL Panel - /** The splitted panel */ - private JSplitPane principalPanel; - - /** DOCUMENT ME! */ - private JScrollPane jspIn; - - /** DOCUMENT ME! */ - private JScrollPane jspOut; - - /** DOCUMENT ME! */ - private JTree messageTree; - - /** DOCUMENT ME! */ - private JTextPane textOut; - - // List of Menu - /** The menu bar */ - private JMenuBar menuBar = new JMenuBar(); - - /** The file menu */ - private JMenu fileMenu = new JMenu( "File" ); - - /** The edit menu */ - private JMenu editMenu = new JMenu( "edit" ); - - /** The "New" menu item */ - private JMenuItem newMenuItem = new JMenuItem( "New..." ); - - /** The "Load" menu item */ - private JMenuItem loadMenuItem = new JMenuItem( "load" ); - - /** The "Save" menu item */ - private JMenuItem saveMenuItem = new JMenuItem( "Save" ); - - /** The "Quit" menu item */ - private JMenuItem quitMenuItem = new JMenuItem( "Quit" ); - - /** DOCUMENT ME! */ - private JCheckBoxMenuItem pduView = new JCheckBoxMenuItem( "View PDU" ); - - /** DOCUMENT ME! */ - private LdapProxy ldapProxy; - - /** DOCUMENT ME! */ - private DefaultStyledDocument docOut; - - private String[] args; - - private DefaultMutableTreeNode top; - - private DefaultMutableTreeNode ldapMessageTree; - - private DefaultTreeModel treeModel; - - // STYLE - private SimpleAttributeSet boldItalicRedText; - - private SimpleAttributeSet boldText; - - private SimpleAttributeSet boldItalicBlueText; - - private SimpleAttributeSet pduStyle; - - final static String SEPARATOR = "\n====================================\n"; - - // ~ Constructors - // ------------------------------------------------------------------------------- - - /** - * Creates a new MainFrame object. - * - * @throws HeadlessException - * DOCUMENT ME! - * @throws UnknownHostException - * DOCUMENT ME! - * @throws IOException - * DOCUMENT ME! - */ - public MainFrame() throws HeadlessException, UnknownHostException, IOException - { - super(); - initFrame(); - - } - - // ~ Methods - // ------------------------------------------------------------------------------------ - - public void initFrame() - { - setItSelf( this ); - setTitle( FRAME_NAME ); - setBounds( FRAME_SIZE ); - setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); - setJMenuBar( initMenuBar() ); - top = new DefaultMutableTreeNode( "Start" ); - treeModel = new DefaultTreeModel( top ); - messageTree = new JTree( treeModel ); - ldapMessageTree = new DefaultMutableTreeNode( "Start Ldap message" ); - - messageTree.addTreeExpansionListener( this ); - messageTree.addTreeSelectionListener( this ); - - messageTree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION ); - initRenderTree(); - messageTree.setBackground( Color.lightGray ); - messageTree.putClientProperty( "JTree.lineStyle", "None" ); - - initStyleForEditing(); - - messageTree.setEditable( false ); - // messageTree.setRootVisible(false); - - textOut = new JTextPane( docOut ); - textOut.setEditable( false ); - - jspIn = new JScrollPane(); - jspIn.getViewport().add( messageTree ); - - jspOut = new JScrollPane(); - jspOut.getViewport().add( textOut ); - - principalPanel = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, jspIn, jspOut ); - principalPanel.setOneTouchExpandable( true ); - principalPanel.setDividerLocation( 500 ); - - getContentPane().add( principalPanel ); - } - - public void initStyleForEditing() - { - // DEFAULT STYLE - Style def = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE ); - docOut = new DefaultStyledDocument(); - // other style could be define later // COMMENT TO BE DELETED AFTER DEV - - boldItalicRedText = new SimpleAttributeSet(); - StyleConstants.setForeground( boldItalicRedText, Color.red ); - StyleConstants.setBold( boldItalicRedText, true ); - StyleConstants.setItalic( boldItalicRedText, true ); - StyleConstants.setFontSize( boldItalicRedText, 12 ); - - boldItalicBlueText = new SimpleAttributeSet(); - StyleConstants.setForeground( boldItalicBlueText, Color.blue ); - StyleConstants.setBold( boldItalicBlueText, true ); - StyleConstants.setItalic( boldItalicBlueText, true ); - StyleConstants.setFontSize( boldItalicBlueText, 12 ); - - boldText = new SimpleAttributeSet(); - StyleConstants.setFontSize( boldText, 14 ); - StyleConstants.setBold( boldText, true ); - - pduStyle = new SimpleAttributeSet(); - StyleConstants.setFontSize( pduStyle, 12 ); - StyleConstants.setBold( pduStyle, true ); - StyleConstants.setForeground( pduStyle, Color.DARK_GRAY ); - - } - - /** - * DOCUMENT ME! - */ - public void launchLdapProxy( String[] args ) - { - ldapProxy = new LdapProxy(); - ldapProxy.setMainFrame( this ); - // ldapProxy.setTextIn( messageTree ); - // ldapProxy.setTextOut( textOut ); - - ldapProxy.likeMain( args ); - - } - - /** Returns an ImageIcon, or null if the path was invalid. */ - protected static ImageIcon createImageIcon( String path ) - { - // java.net.URL imgURL = MainFrame.class.getResource(path); - try - { - return new ImageIcon( path ); - } - catch ( Exception e ) - { - return null; - } - } - - /** - * DOCUMENT ME! - */ - public void initRenderTree() - { - ImageIcon leafIcon = createImageIcon( "./images/gnome-icone-001.gif" ); - ImageIcon icon = createImageIcon( "./images/gnome-icone-002.gif" ); - if ( ( leafIcon != null ) && ( icon != null ) ) - { - DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer(); - renderer.setLeafIcon( leafIcon ); - messageTree.setCellRenderer( renderer ); - renderer.setFont( new Font( "treeFont", Font.BOLD, 12 ) ); - renderer.setBackgroundNonSelectionColor( Color.lightGray ); - renderer.setBackgroundSelectionColor( Color.white ); - renderer.setOpenIcon( icon ); - renderer.setClosedIcon( icon ); - } - - } - - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - private JMenuBar initMenuBar() - { - fileMenu.add( newMenuItem ); - fileMenu.add( loadMenuItem ); - fileMenu.add( saveMenuItem ); - fileMenu.addSeparator(); - fileMenu.add( quitMenuItem ); - newMenuItem.addActionListener( new ActionListener() - { - public void actionPerformed( ActionEvent action ) - { - // top.removeAllChildren(); - // TO DO BETTER !!!!! - top = new DefaultMutableTreeNode( "Start" ); - treeModel.setRoot( top ); - } - } ); - saveMenuItem.addActionListener( new ActionListener() - { - public void actionPerformed( ActionEvent action ) - { - try - { - FileWriter fw = new FileWriter( new File( "./dest/testsauv.xml" ) ); - for ( int i = 0; i < ldapMessageTree.getChildCount(); i++ ) - { - DefaultMutableTreeNode tmpNode = (DefaultMutableTreeNode) ldapMessageTree.getChildAt( i ); - String st = toXML( tmpNode.getUserObject(), fw ) + "\n"; - try - { - docOut.insertString( docOut.getLength(), st, boldText ); - } - catch ( BadLocationException ble ) - { - } - for ( int j = 0; j < tmpNode.getChildCount(); j++ ) - { - st = toXML( ( (DefaultMutableTreeNode) tmpNode.getChildAt( j ) ).getUserObject(), fw ) - + "\n"; - try - { - docOut.insertString( docOut.getLength(), st, boldItalicBlueText ); - } - catch ( BadLocationException ble ) - { - } - } - } - - } - catch ( IOException e ) - { - } - } - } ); - quitMenuItem.addActionListener( new ActionListener() - { - public void actionPerformed( ActionEvent action ) - { - System.out.println( "EXIT" ); - try - { - if ( ldapProxy != null ) - ldapProxy.stop(); - } - catch ( Exception e ) - { - e.printStackTrace(); - } - - System.exit( 0 ); - } - } ); - - quitMenuItem.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_Q, ActionEvent.CTRL_MASK ) ); - saveMenuItem.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_S, ActionEvent.CTRL_MASK ) ); - - editMenu.add( pduView ); - - menuBar.add( fileMenu ); - menuBar.add( editMenu ); - return menuBar; - } - - /** - * DOCUMENT ME! - */ - public void run() - { - this.setVisible( true ); - this.launchLdapProxy( args ); - } - - /** - * DOCUMENT ME! - * - * @param args - */ - public static void main( String[] args ) throws Exception - { - MainFrame mf = new MainFrame(); - mf.args = args; - mf.run(); - } - - public JTree getMessageTree() - { - return messageTree; - } - - public DefaultMutableTreeNode getTop() - { - return top; - } - - public DefaultMutableTreeNode getLdapMessageTree() - { - return ldapMessageTree; - } - - public void setLdapMessageTree( DefaultMutableTreeNode ldapMessageTree ) - { - this.ldapMessageTree = ldapMessageTree; - } - - public SimpleAttributeSet getStyleByMessageType( int type ) - { - SimpleAttributeSet styledType; - - switch ( type ) - { - case 0 : - styledType = boldItalicBlueText; - break; - case 1 : - styledType = boldItalicBlueText; - break; - case 2 : - styledType = boldItalicBlueText; - break; - case 3 : - styledType = boldItalicRedText; - break; - case 4 : - styledType = boldItalicBlueText; - break; - case 5 : - styledType = boldItalicBlueText; - break; - case 6 : - styledType = boldItalicBlueText; - break; - case 7 : - styledType = boldItalicBlueText; - break; - case 8 : - styledType = boldItalicBlueText; - break; - case 9 : - styledType = boldItalicBlueText; - break; - case 10 : - styledType = boldItalicBlueText; - break; - case 11 : - styledType = boldItalicBlueText; - break; - case 12 : - styledType = boldItalicBlueText; - break; - case 13 : - styledType = boldItalicBlueText; - break; - case 14 : - styledType = boldItalicBlueText; - break; - case 15 : - styledType = boldItalicBlueText; - break; - case 16 : - styledType = boldItalicBlueText; - break; - case 17 : - styledType = boldItalicRedText; - break; - case 18 : - styledType = boldItalicBlueText; - break; - case 19 : - styledType = boldItalicBlueText; - break; - case -1 : - styledType = boldItalicBlueText; - break; - - default : - styledType = boldItalicBlueText; - break; - } - return styledType; - } - - public String toXML( Object ob, FileWriter fw ) - { - try - { - XStream xs = new XStream(); - xs.toXML( ob, fw ); - String st = xs.toXML( ob ); - System.out.println( st ); - return st; - } - catch ( Exception e ) - { - return "NOTHING"; - } - } - - public MainFrame getItSelf() - { - return itSelf; - } - - public void setItSelf( MainFrame itSelf ) - { - this.itSelf = itSelf; - } - - public void treeCollapsed( TreeExpansionEvent tee ) - { - messageTree.setSelectionPath( tee.getPath() ); - // messageTree.setSelectionRow(0); - - } - - public void treeExpanded( TreeExpansionEvent tee ) - { - // messageTree.setSelectionRow(0); - messageTree.setSelectionPath( tee.getPath() ); - } - - public void valueChanged( TreeSelectionEvent tse ) - { - textOut.setText( "" ); - // there is only 2 depth level in thoses Tree. - DefaultMutableTreeNode node = (DefaultMutableTreeNode) messageTree.getLastSelectedPathComponent(); - if ( node.equals( top ) ) - { - System.out.println( "First Element Selected!!" ); - return; - } - if ( node == null ) - { - System.out.println( "Can't Find Node !!! NULL" ); - return; - - } - int index = top.getIndex( node ); - int count = 0; - int childrenCount = top.getChildCount(); - while ( ( index == -1 ) && ( count < childrenCount ) ) - { - index = top.getChildAt( count ).getIndex( node ); - count++; - } - count--; - if ( index == -1 ) - { - System.out.println( "Can't Find Node !!! index -1" ); - return; - } - Object nodeInfo = node.getUserObject(); - System.out.println( "============================================" ); - System.out.println( "NODE selected " + index + " :::: " + nodeInfo.toString() ); - TreeNode ldapNode; - if ( !node.isLeaf() ) - { - // First Level - ldapNode = ldapMessageTree.getChildAt( index ); - } - else - { - // second Level we take before the good sub tree to get the - // good leaf by its own index - ldapNode = ldapMessageTree.getChildAt( count ).getChildAt( index ); - } - - System.out.println( "LDAP message associate ::: " - + ( (LdapMessageWithPDU) ( (DefaultMutableTreeNode) ldapNode ).getUserObject() ).getLdapMessage() - .toString() ); - System.out.println( "============================================" ); - - // textOut.setLogicalStyle(textOut.getStyle("regular")); - // textOut.setText(textOut.getText() + ldapNode.toString()); - try - { - LdapMessage ldapmess = ( (LdapMessageWithPDU) ( (DefaultMutableTreeNode) ldapNode ).getUserObject() ) - .getLdapMessage(); - int type = ldapmess.getMessageType(); - SimpleAttributeSet currentStyle = getStyleByMessageType( type ); - docOut.insertString( docOut.getLength(), node.toString() + "\n", boldText ); - if ( node.isLeaf() ) - { - docOut.insertString( docOut.getLength(), ldapmess.toString(), currentStyle ); - if ( pduView.getState() ) - { - docOut.insertString( docOut.getLength(), SEPARATOR - + " PDU :: \n " - + ( (LdapMessageWithPDU) ( (DefaultMutableTreeNode) ldapNode ).getUserObject() ) - .getDumpBytes() + SEPARATOR, pduStyle ); - } - } - - } - catch ( Exception e ) - {// BadLocationException ble) { - System.err.println( "Can not write data in Editor!!" ); - } - - } - - public DefaultTreeModel getTreeModel() - { - return treeModel; - } - -} +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.ldap.proxy.gui; + +import org.apache.asn1new.ldap.pojo.LdapMessage; +import org.apache.ldap.proxy.impl.LdapMessageWithPDU; +import org.apache.ldap.proxy.impl.LdapProxy; + +import com.thoughtworks.xstream.XStream; + +import java.awt.Color; +import java.awt.Font; +import java.awt.HeadlessException; +import java.awt.Rectangle; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import java.net.UnknownHostException; + +import javax.swing.ImageIcon; +import javax.swing.JCheckBoxMenuItem; +import javax.swing.JFileChooser; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; +import javax.swing.JScrollPane; +import javax.swing.JSplitPane; +import javax.swing.JTextPane; +import javax.swing.JTree; +import javax.swing.KeyStroke; +import javax.swing.event.TreeExpansionEvent; +import javax.swing.event.TreeExpansionListener; +import javax.swing.event.TreeSelectionEvent; +import javax.swing.event.TreeSelectionListener; +import javax.swing.filechooser.FileFilter; +import javax.swing.text.DefaultStyledDocument; +import javax.swing.text.SimpleAttributeSet; +import javax.swing.text.Style; +import javax.swing.text.StyleConstants; +import javax.swing.text.StyleContext; +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.DefaultTreeCellRenderer; +import javax.swing.tree.DefaultTreeModel; +import javax.swing.tree.TreeNode; +import javax.swing.tree.TreeSelectionModel; + +/** + * A Ldap Proxy GUI + * + * @author Apache Directory Project + */ +public class MainFrame extends JFrame implements TreeSelectionListener, TreeExpansionListener +{ + // ~ Static fields/initializers + // ----------------------------------------------------------------- + // Shut down the Serialization warning ... + public static final long serialVersionUID = 1L; + + /** frame itSelf */ + private MainFrame itSelf; + + /** Frame and Application name */ + public static final String FRAME_NAME = "LDAP Proxy GUI"; + + /** Default Frame default Size */ + public static final Rectangle FRAME_SIZE = new Rectangle( 100, 100, 1000, 500 ); + + // ~ Instance fields + // ---------------------------------------------------------------------------- + + // JDialog for connection parameters. + /** our own JDialog to set connection ports and host parameters */ + private ConnectDialog connectDialog; + + // ALL Panel + /** The splitted panel */ + private JSplitPane principalPanel; + + /** scrollpane for tree */ + private JScrollPane jspIn; + + /** rigth scroll pane that contains the editorpane */ + private JScrollPane jspOut; + + /** Jtree describing the proxy stream */ + private JTree messageTree; + + /** detail pane for each tree entry */ + private JTextPane textOut; + + // List of Menu + /** The menu bar */ + private JMenuBar menuBar = new JMenuBar(); + + /** The file menu */ + private JMenu fileMenu = new JMenu( "File" ); + + /** The edit menu */ + private JMenu editMenu = new JMenu( "edit" ); + + /** The "New" menu item */ + private JMenuItem newMenuItem = new JMenuItem( "New..." ); + + /** The "Load" menu item */ + private JMenuItem loadMenuItem = new JMenuItem( "load" ); + + /** The "Save" menu item */ + private JMenuItem saveMenuItem = new JMenuItem( "Save" ); + + /** The "Quit" menu item */ + private JMenuItem quitMenuItem = new JMenuItem( "Quit" ); + + /** check box to view or not the PDU in the right detail panel */ + private JCheckBoxMenuItem pduView = new JCheckBoxMenuItem( "View PDU" ); + + /** an instance of the Proxy */ + private LdapProxy ldapProxy; + + /** the basic default style document, use to construct the other style */ + private DefaultStyledDocument docOut; + + /** args that are passed during the launching of the Proxy */ + private String[] args; + + /** The root of the visible tree */ + private DefaultMutableTreeNode top; + + /** + * the root of the assiociate tree that contains all LdapMessage and PDU + * associated + */ + private DefaultMutableTreeNode ldapMessageTree; + + /** The Viewing tree Model */ + private DefaultTreeModel treeModel; + + // STYLE use in the rigth detail Editor Pane + + /** bold style using for header before detail of the ldap message */ + private SimpleAttributeSet boldStyle; + + /** italic style using for error or Unknown message */ + private SimpleAttributeSet italicStyle; + + /** request style */ + private SimpleAttributeSet requestStyle; + + /** response style */ + private SimpleAttributeSet responseStyle; + + /** PDU Style */ + private SimpleAttributeSet pduStyle; + + /** Standard separator using to include PDU */ + final static String SEPARATOR = "\n====================================\n"; + + public final static String DEFAULT_SAVE_EXTENTION_NAME = ".lpml"; + + // ~ Constructors + // ------------------------------------------------------------------------------- + + /** + * Creates a new MainFrame object. + * + * @throws HeadlessException + * if no title for the frame! (never happen) + * @throws UnknownHostException + * if the host using for the proxy launching is unrecognize + * @throws IOException + * if the port for proxy socket is allready open + */ + public MainFrame() throws HeadlessException, UnknownHostException, IOException + { + super(); + initFrame(); + + } + + // ~ Methods + // ------------------------------------------------------------------------------------ + /** + * DOCUMENT ME! + */ + public void initFrame() + { + + // its own Parameters + setItSelf( this ); + setTitle( FRAME_NAME ); + setBounds( FRAME_SIZE ); + setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); + + // Dialog + connectDialog = new ConnectDialog( this ); + + setJMenuBar( initMenuBar() ); + top = new DefaultMutableTreeNode( "Start" ); + treeModel = new DefaultTreeModel( top ); + messageTree = new JTree( treeModel ); + ldapMessageTree = new DefaultMutableTreeNode( "Start Ldap message" ); + + messageTree.addTreeExpansionListener( this ); + messageTree.addTreeSelectionListener( this ); + + messageTree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION ); + initRenderTree(); + messageTree.setBackground( Color.lightGray ); + messageTree.putClientProperty( "JTree.lineStyle", "None" ); + + initStyleForEditing(); + + messageTree.setEditable( false ); + // messageTree.setRootVisible(false); + + textOut = new JTextPane( docOut ); + textOut.setEditable( false ); + + jspIn = new JScrollPane(); + jspIn.getViewport().add( messageTree ); + + jspOut = new JScrollPane(); + jspOut.getViewport().add( textOut ); + + principalPanel = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, jspIn, jspOut ); + principalPanel.setOneTouchExpandable( true ); + principalPanel.setDividerLocation( 500 ); + + getContentPane().add( principalPanel ); + + } + + /** + * init all the style using for editing detail of a Ldap Message + */ + public void initStyleForEditing() + { + // DEFAULT STYLE + Style def = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE ); + docOut = new DefaultStyledDocument(); + // other style could be define later // COMMENT TO BE DELETED AFTER DEV + + requestStyle = new SimpleAttributeSet(); + StyleConstants.setForeground( requestStyle, Color.red ); + StyleConstants.setBold( requestStyle, true ); + StyleConstants.setFontSize( requestStyle, 12 ); + + responseStyle = new SimpleAttributeSet(); + StyleConstants.setForeground( responseStyle, Color.blue ); + StyleConstants.setBold( responseStyle, true ); + StyleConstants.setFontSize( responseStyle, 12 ); + + boldStyle = new SimpleAttributeSet(); + StyleConstants.setFontSize( boldStyle, 14 ); + StyleConstants.setBold( boldStyle, true ); + + italicStyle = new SimpleAttributeSet(); + StyleConstants.setFontSize( boldStyle, 14 ); + StyleConstants.setItalic( boldStyle, true ); + + pduStyle = new SimpleAttributeSet(); + StyleConstants.setFontSize( pduStyle, 12 ); + StyleConstants.setBold( pduStyle, true ); + StyleConstants.setForeground( pduStyle, Color.DARK_GRAY ); + + } + + /** + * DOCUMENT ME! + */ + public void launchLdapProxy() + { + ldapProxy = new LdapProxy(); + ldapProxy.setMainFrame( this ); + ldapProxy.likeMain( args ); + + } + + /** + * DOCUMENT ME! + */ + public void launchLdapProxy( int localPort, String host, int port ) + { + ldapProxy = new LdapProxy(); + ldapProxy.setMainFrame( this ); + ldapProxy.likeMain( localPort, host, port ); + + } + + /** + * Returns an ImageIcon, or null if the path was invalid. + */ + protected static ImageIcon createImageIcon( String path ) + { + // java.net.URL imgURL = MainFrame.class.getResource(path); + try + { + return new ImageIcon( path ); + } + catch ( Exception e ) + { + return null; + } + } + + /** + * DOCUMENT ME! + */ + public void initRenderTree() + { + ImageIcon leafIcon = createImageIcon( "./images/gnome-icone-001.gif" ); + ImageIcon icon = createImageIcon( "./images/gnome-icone-002.gif" ); + if ( ( leafIcon != null ) && ( icon != null ) ) + { + DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer(); + renderer.setLeafIcon( leafIcon ); + messageTree.setCellRenderer( renderer ); + renderer.setFont( new Font( "treeFont", Font.BOLD, 12 ) ); + renderer.setBackgroundNonSelectionColor( Color.lightGray ); + renderer.setBackgroundSelectionColor( Color.white ); + renderer.setOpenIcon( icon ); + renderer.setClosedIcon( icon ); + } + + } + + /** + * initialise the menu of the GUI + * + * @return JMenuBar + */ + private JMenuBar initMenuBar() + { + fileMenu.add( newMenuItem ); + fileMenu.add( loadMenuItem ); + fileMenu.add( saveMenuItem ); + fileMenu.addSeparator(); + fileMenu.add( quitMenuItem ); + newMenuItem.addActionListener( new ActionListener() + { + public void actionPerformed( ActionEvent action ) + { + // top.removeAllChildren(); + // TO DO BETTER !!!!! + // top = new DefaultMutableTreeNode("Start"); + // treeModel.setRoot(top); + JOptionPane.showMessageDialog( getItSelf(), new JLabel( "This Menu don't run actually!! TO DO ..." ) ); + } + } ); + saveMenuItem.addActionListener( new ActionListener() + { + public void actionPerformed( ActionEvent action ) + { + try + { + JFileChooser jfc = new JFileChooser(); + jfc.addChoosableFileFilter( new FileFilter() + { + public boolean accept( File f ) + { + if ( f.isDirectory() ) + { + return false; + } + if ( f.getName().endsWith( MainFrame.DEFAULT_SAVE_EXTENTION_NAME ) ) + { + return true; + } + return false; + } + + public String getDescription() + { + return "*" + MainFrame.DEFAULT_SAVE_EXTENTION_NAME; + } + + } ); + + int retrunValue = jfc.showSaveDialog( getItSelf() ); + + if ( retrunValue == JFileChooser.APPROVE_OPTION ) + { + File f; + + if ( jfc.getSelectedFile().getName().endsWith( MainFrame.DEFAULT_SAVE_EXTENTION_NAME ) ) + { + f = jfc.getSelectedFile(); + } + else + { + f = new File( jfc.getSelectedFile().getAbsoluteFile() + + MainFrame.DEFAULT_SAVE_EXTENTION_NAME ); + } + + FileWriter fw = new FileWriter( f ); + + for ( int i = 0; i < ldapMessageTree.getChildCount(); i++ ) + { + DefaultMutableTreeNode tmpNode = (DefaultMutableTreeNode) ldapMessageTree.getChildAt( i ); + String st = toXML( tmpNode.getUserObject(), fw ) + "\n"; + + for ( int j = 0; j < tmpNode.getChildCount(); j++ ) + { + st = toXML( ( (DefaultMutableTreeNode) tmpNode.getChildAt( j ) ).getUserObject(), fw ) + + "\n"; + } + } + } + } + catch ( IOException e ) + { + JOptionPane.showMessageDialog( getItSelf(), new JLabel( "Can't save the tree!!" ) ); + } + } + } ); + quitMenuItem.addActionListener( new ActionListener() + { + public void actionPerformed( ActionEvent action ) + { + System.out.println( "EXIT" ); + try + { + if ( ldapProxy != null ) + ldapProxy.stop(); + } + catch ( Exception e ) + { + e.printStackTrace(); + } + + System.exit( 0 ); + } + } ); + + quitMenuItem.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_Q, ActionEvent.CTRL_MASK ) ); + saveMenuItem.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_S, ActionEvent.CTRL_MASK ) ); + + editMenu.add( pduView ); + + menuBar.add( fileMenu ); + menuBar.add( editMenu ); + return menuBar; + } + + /** + * First runing method, set visible the GUI and launch Proxy + */ + public void run() + { + this.setVisible( true ); + if ( args.length > 2 ) + { + this.launchLdapProxy(); + } + else + { + connectDialog.setVisible( true ); + this.launchLdapProxy( connectDialog.getCurrentLocalPortValue(), connectDialog.getCurrentHostValue(), + connectDialog.getCurrentPortValue() ); + } + + } + + /** + * launching method + * + * @param args + */ + public static void main( String[] args ) throws Exception + { + MainFrame mf = new MainFrame(); + mf.args = args; + mf.run(); + } + + /** + * DOCUMENT ME! + */ + public JTree getMessageTree() + { + return messageTree; + } + + /** + * DOCUMENT ME! + */ + public DefaultMutableTreeNode getTop() + { + return top; + } + + /** + * DOCUMENT ME! + */ + public DefaultMutableTreeNode getLdapMessageTree() + { + return ldapMessageTree; + } + + /** + * DOCUMENT ME! + */ + public void setLdapMessageTree( DefaultMutableTreeNode ldapMessageTree ) + { + this.ldapMessageTree = ldapMessageTree; + } + + /** + * DOCUMENT ME! + */ + public SimpleAttributeSet getStyleByMessageType( int type ) + { + SimpleAttributeSet styledType; + + switch ( type ) + { + case 0 : + // ABANDON REQUEST + styledType = requestStyle; + break; + case 1 : + // ADD REQUEST + styledType = requestStyle; + break; + case 2 : + // ADD RESPONSE + styledType = responseStyle; + break; + case 3 : + // BIND REQUEST + styledType = requestStyle; + break; + case 4 : + // BIND RESPONSE + styledType = responseStyle; + break; + case 5 : + // COMPARE REQUEST + styledType = requestStyle; + break; + case 6 : + // COMPARE RESPONSE + styledType = responseStyle; + break; + case 7 : + // DEL REQUEST + styledType = requestStyle; + break; + case 8 : + // DEL RESPONSE + styledType = responseStyle; + break; + case 9 : + // EXTENDED REQUEST + styledType = requestStyle; + break; + case 10 : + // EXTENDED RESPONSE + styledType = responseStyle; + break; + case 11 : + // MODIFYDN REQUEST + styledType = requestStyle; + break; + case 12 : + // MODIFYDN RESPONSE + styledType = responseStyle; + break; + case 13 : + // MODIFY REQUEST + styledType = requestStyle; + break; + case 14 : + // MODIFY RESPONSE + styledType = responseStyle; + break; + case 15 : + // SEARCH REQUEST + styledType = requestStyle; + break; + case 16 : + // SEARCH RESULT DONE + styledType = responseStyle; + break; + case 17 : + // SEARCH RESULT ENTRY + styledType = responseStyle; + break; + case 18 : + // SEARCH RESULT REFERENCE + styledType = responseStyle; + break; + case 19 : + // UNBIND REQUEST + styledType = requestStyle; + break; + case -1 : + // UNKNOWN + styledType = italicStyle; + break; + + default : + // UNKNOWN + styledType = italicStyle; + break; + } + return styledType; + } + + public String toXML( Object ob, FileWriter fw ) + { + try + { + XStream xs = new XStream(); + xs.toXML( ob, fw ); + String st = xs.toXML( ob ); + // System.out.println(st); + return st; + } + catch ( Exception e ) + { + return "NOTHING"; + } + } + + public MainFrame getItSelf() + { + return itSelf; + } + + public void setItSelf( MainFrame itSelf ) + { + this.itSelf = itSelf; + } + + public void treeCollapsed( TreeExpansionEvent tee ) + { + messageTree.setSelectionPath( tee.getPath() ); + // messageTree.setSelectionRow(0); + + } + + public void treeExpanded( TreeExpansionEvent tee ) + { + // messageTree.setSelectionRow(0); + messageTree.setSelectionPath( tee.getPath() ); + } + + public void valueChanged( TreeSelectionEvent tse ) + { + textOut.setText( "" ); + // there is only 2 depth level in thoses Tree. + DefaultMutableTreeNode node = (DefaultMutableTreeNode) messageTree.getLastSelectedPathComponent(); + if ( node.equals( top ) ) + { + System.out.println( "First Element Selected!!" ); + return; + } + if ( node == null ) + { + System.out.println( "Can't Find Node !!! NULL" ); + return; + + } + int index = top.getIndex( node ); + int count = 0; + int childrenCount = top.getChildCount(); + while ( ( index == -1 ) && ( count < childrenCount ) ) + { + index = top.getChildAt( count ).getIndex( node ); + count++; + } + count--; + if ( index == -1 ) + { + System.out.println( "Can't Find Node !!! index -1" ); + return; + } + Object nodeInfo = node.getUserObject(); + System.out.println( "============================================" ); + System.out.println( "NODE selected " + index + " :::: " + nodeInfo.toString() ); + TreeNode ldapNode; + if ( !node.isLeaf() ) + { + // First Level + ldapNode = ldapMessageTree.getChildAt( index ); + } + else + { + // second Level we take before the good sub tree to get the + // good leaf by its own index + ldapNode = ldapMessageTree.getChildAt( count ).getChildAt( index ); + } + + System.out.println( "LDAP message associate ::: " + + ( (LdapMessageWithPDU) ( (DefaultMutableTreeNode) ldapNode ).getUserObject() ).getLdapMessage() + .toString() ); + System.out.println( "============================================" ); + + // textOut.setLogicalStyle(textOut.getStyle("regular")); + // textOut.setText(textOut.getText() + ldapNode.toString()); + try + { + LdapMessage ldapmess = ( (LdapMessageWithPDU) ( (DefaultMutableTreeNode) ldapNode ).getUserObject() ) + .getLdapMessage(); + int type = ldapmess.getMessageType(); + SimpleAttributeSet currentStyle = getStyleByMessageType( type ); + docOut.insertString( docOut.getLength(), node.toString() + "\n", boldStyle ); + if ( node.isLeaf() ) + { + docOut.insertString( docOut.getLength(), ldapmess.toString(), currentStyle ); + if ( pduView.getState() ) + { + docOut.insertString( docOut.getLength(), SEPARATOR + + " PDU :: \n " + + ( (LdapMessageWithPDU) ( (DefaultMutableTreeNode) ldapNode ).getUserObject() ) + .getDumpBytes() + SEPARATOR, pduStyle ); + } + } + + } + catch ( Exception e ) + {// BadLocationException ble) { + System.err.println( "Can not write data in Editor!!" ); + } + + } + + public DefaultTreeModel getTreeModel() + { + return treeModel; + } + +} \ No newline at end of file