/* * ************************************************* * COMPRESS IPv6 ADDRESS * .project: tutorial about IPv6 Address * Eli pentser * ************************************************* */ import java.awt.*; import java.awt.event.*; import java.text.*; import javax.swing.*; public class Ipv6 extends JApplet implements ActionListener, KeyListener { // objects private JLabel colon[]; private JTextField octet[]; private JButton comp,clear; private JTextArea output; private int i, count; String display = ""; // set up applet's GUI public void init() { count=0; // get content pane and set its layout to FlowLayout Container container = getContentPane(); container.setLayout( new FlowLayout() ); container.setBackground(Color.white); colon=new JLabel[7]; octet=new JTextField[8]; for(i=0;i<7;i++) { colon[i]=new JLabel(":"); octet[i] = new JTextField( 4 ); octet[i].setText(""); container.add( octet[i] ); container.add(colon[i]); octet[i].addKeyListener(this); } octet[0].requestFocus(true); octet[i] = new JTextField( 4 ); octet[i].setText(""); container.add( octet[i] ); octet[i].addKeyListener(this); output=new JTextArea(6,30); output.setForeground(new Color(255,128,64)); output.setText("\nEnter unabbreviated IPv6 Address.\nnote: in an unabbreviated IPv6 Address, each octet has 4 HEXA DIGITS.\nHEXA DIGIT: 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f (Lower Case).\n"); output.setEditable(false); container.add(output); comp=new JButton("Compress IP address"); comp.setBackground(new Color(128,0,128)); comp.setForeground(Color.white); comp.addActionListener(this); container.add(comp); clear=new JButton("Clear"); clear.setBackground(new Color(128,0,128)); clear.setForeground(Color.white); clear.setEnabled(false); clear.addActionListener(this); container.add(clear); } public void actionPerformed( ActionEvent actionEvent ) { int i; String str; if( (actionEvent.getSource())==comp) { comp.setEnabled(false); clear.setEnabled(true); output.setText(""); if (getInfo()) { compress(); } } else if(actionEvent.getSource()==clear) { clear.setEnabled(false); comp.setEnabled(true); for(i=0;i<8;i++) { octet[i].setText(""); if(octet[i].getBackground()==Color.red) octet[i].setBackground(Color.white); } output.setText("\nEnter unabbreviated IPv6 Address.\nnote: in an unabbreviated IPv6 Address, each octet has 4 HEXA DIGITS\nHEXA DIGIT: 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f (Lower Case)\n"); octet[0].requestFocus(true); } } // end method actionPerformed /** ******************************************************************************************* * getInfo method: * cheak the given Ipv6 if the address are ligal? * if the address are ligal- return true. * if the address are not ligal- return false **********************************************************************************************/ public boolean getInfo() { int i,j,k; boolean flag; String str; String ligal="0123456789abcdef"; for(i=0;i<8;i++) { str=octet[i].getText(); if(str.length()!=4) { octet[i].setBackground(Color.red); output.append("\nEach Octet must be 16 bits! (4 HEXA DIGITS)\n"); return false; } for(j=0;j1) display+=':'; i--; if(count>1) flag=true; } else if( str[i].equals("") && flag ) // if null Appearance and we got before null group. { while( i<7 && str[i].equals("") ) { display+='0'; display+=':'; i++; } if(i==7 && str[i].equals("") ) { display+='0'; break; } i--; } else // if not null Appearance we present the number. { while( i<7 && !str[i].equals("") ) { display+=str[i]; display+=':'; i++; } if(i==7) { if( !str[i].equals("") ) display+=str[i]; else if (str[i].equals("") ) display+='0'; break; } i--; } } // display the Compress address. output.append("The compressed form:\t"+ display +"\n"); } public void keyPressed(KeyEvent evt){ int i=0; count++; if(count == 4) { for (;i < 8 ;i++) { if (evt.getComponent().equals(octet[i])){ if (i < 7) octet[i + 1].requestFocus(true); if(i==7) comp.requestFocus(true); } } count=0; } } public void keyReleased(KeyEvent evt) { } public void keyTyped(KeyEvent evt) { } } // end class Ipv6 /***************************************************************************** /* all rights belong to Eli pentser: * Hadassah college - department of computer science *Class D2 ****************************************************************************/