/* This program is an implementation of Dijkstra's algorithm for the shortest pahts calculation. This program accepts the source node and the link state database, and generates the shortest path cost and the next hop to every node from the source node Author Basem Shihada version JDK 1.2.1 */ import javax.swing.JTable; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.table.AbstractTableModel; import javax.swing.JScrollPane; import javax.swing.JFrame; import javax.swing.SwingUtilities; import javax.swing.JOptionPane; import java.awt.*; import java.awt.event.*; public class Dijkstras extends JFrame implements ActionListener { private JLabel source, lsd; private JTextField sourceNode; private JTextArea result; private JButton shortestPath; private boolean DEBUG = true; private String link_s_d[][]; private String c[]; // the Confirmed List private String t[]; // the Tentative List String sourceRouter; String temp; int index=0; int value1=0; int indexValue=0; int keepTrak = 0; public Dijkstras() { super("Routing Data"); source = new JLabel("Enter the Source Node Letter"); sourceNode = new JTextField (20); lsd = new JLabel ("Enter the Link State DataBase"); shortestPath = new JButton ("The Shortest Paths"); shortestPath.addActionListener(this); result = new JTextArea("",5,20); MyTableModel myModel = new MyTableModel(); JTable table = new JTable(myModel); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); JPanel deck = new JPanel(); deck.setLayout( new GridLayout(3,0)); deck.add(source); deck.add(sourceNode); deck.add(lsd); JPanel deck1 = new JPanel(); deck1.setLayout( new GridLayout (2,0)); deck1.add(shortestPath); deck1.add(result); JScrollPane scrollPane0 = new JScrollPane(deck); getContentPane().add(scrollPane0, BorderLayout.NORTH); JScrollPane scrollPane = new JScrollPane(table); getContentPane().add(scrollPane, BorderLayout.CENTER); JScrollPane scrollPane03 = new JScrollPane(deck1); getContentPane().add(scrollPane03, BorderLayout.SOUTH); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); link_s_d = new String[7][7]; c = new String[7]; t = new String[7]; for(int i=0;i<7;i++){ for(int j=0;j<7;j++) link_s_d[i][j] = ""; } for(int i=0;i<7;i++) { c[i] = "Empty"; t[i] = "Empty"; } } public void actionPerformed (ActionEvent e) { if (e.getSource() == shortestPath) { sourceRouter = sourceNode.getText(); for(int i=0;link_s_d[0][i] != "";i++) if (link_s_d[0][i].compareTo(sourceRouter) == 0) { System.out.println(index = i); break; } // place an entrance for the source node in c list c[keepTrak] = sourceRouter.concat("0-"); result.append(c[keepTrak]+"\n"); //place the selected LSP to the T list for(int i=1;link_s_d[i][index].compareTo("") != 0;i++) { t[i-1] = link_s_d[i][index]; temp = t[i-1].valueOf(t[i-1].charAt(0)); t[i-1] = t[i-1].concat(temp); System.out.println(t[i-1]); } calculate(); } } class MyTableModel extends AbstractTableModel { String temp=null; final String[] columnNames = {"Router", "Router", "Router", "Router", "Router", "Router", "Router"}; final Object[][] data = { {"","","","","","",""}, {"","","","","","",""}, {"","","","","","",""}, {"","","","","","",""}, {"","","","","","",""}, {"","","","","","",""}, {"","","","","","",""} }; public int getColumnCount() { return columnNames.length; } public int getRowCount() { return data.length; } public String getColumnName(int col) { return columnNames[col]; } public Object getValueAt(int row, int col) { return data[row][col]; } /* * Don't need to implement this method unless your table's * editable. */ public boolean isCellEditable(int row, int col) { return true; } /* * Don't need to implement this method unless your table's * data can change. */ public void setValueAt(Object value, int row, int col) { if (DEBUG) { System.out.println("Setting value at " + row + "," + col + " to " + value + " (an instance of " + value.getClass() + ")"); } if (data[0][col] instanceof String) { //If we don't do something like this, the column //switches to contain Strings. //XXX: See TableEditDemo.java for a better solution!!! try { data[row][col] = new String((String)value); fireTableCellUpdated(row, col); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(Dijkstras.this, "The \"" + getColumnName(col) + "\" column accepts only integer values."); } } else { data[row][col] = value; fireTableCellUpdated(row, col); } if (DEBUG) { System.out.println("New value of data:"); printDebugData(); } } private void printDebugData() { int numRows = getRowCount(); int numCols = getColumnCount(); for (int i=0; i < numRows; i++) { System.out.print(" row " + i + ":"); for (int j=0; j < numCols; j++) { link_s_d[i][j] = (String) data[i][j]; System.out.print(" " + data[i][j]); } System.out.println(); } System.out.println("--------------------------"); } } public void calculate() { keepTrak++; value1 = Character.digit(t[0].charAt(1),10); for(int i=1; t[i] != "Empty" ;i++) { if (value1 >= (Character.digit(t[i].charAt(1),10))) { value1 = Character.digit(t[i].charAt(1),10); indexValue = i; } } c[keepTrak] = t[indexValue]; index = findIndex(c[keepTrak]); result.append(c[keepTrak]+"\n"); updateDistance(); } public void updateDistance() { String tTemp[]; String tt[]; int i=0; int j=0; tt = new String[7]; tTemp = new String[7]; for(i=0;i<7;i++) { tTemp[i] = "Empty"; tt[i] = "Empty"; } i = 0; while (t[i].compareTo("Empty") != 0) { if(c[keepTrak].charAt(0) != t[i].charAt(0)) { tTemp[i] = t[i]; t[i] = "Empty"; } else tTemp[i] = "Removed"; System.out.println(tTemp[i]); ++i; } // Place the values in the T list with the requiered update System.out.println(index); i = 1; j = 0; System.out.println(); while(link_s_d[i][index].compareTo("") != 0) { if(! exsist(i)) { value1 = Character.digit(c[keepTrak].charAt(1),10); tt[j] = link_s_d[i][index]; temp = temp.valueOf(c[keepTrak].charAt(2)); tt[j] = tt[j].concat(temp); value1 = value1 + Character.digit(tt[j].charAt(1),10); tt[j] = tt[j].replace(tt[j].charAt(1) , Character.forDigit(value1,10)); j++; } i++; } for(i=0;i<7;i++) t[i] = "Empty"; i=0; while (tt[i] != "Empty") { t[i] = tt[i]; i++; } j=0; while ( tTemp[j] != "Removed") { if(tTemp[j] != "Empty") { t[i] = tTemp[j]; i++; j++; } else j++; } for(i=0;t[i] != "Empty";i++) { for(j=0;t[j] != "Empty";j++) { if(i != j) { if(t[i].charAt(0) == t[j].charAt(0)) { if(Character.digit(t[i].charAt(1),10) >= Character.digit(t[j].charAt(1),10)) t[i] = "Removed"; else t[j] = "Removed"; } } } } for(i=0;t[i] != "Empty";i++) { if(t[i]=="Removed") for(int k=i;t[k] != "Empty";k++) t[k] = t[k+1]; } for(i=0;i<7;i++) System.out.println(t[i]); if(t[0] != "Empty") { temp= null; index=0; value1=0; indexValue=0; calculate(); } } public boolean exsist(int ii) { boolean ex = false; for(int j=0;c[j] != "Empty";j++){ if(c[j].charAt(0) == link_s_d[ii][index].charAt(0)) ex = true; } return ex; } public int findIndex(String s) { int ex = 10; for(int i=0; i<7 ;i++) { if(link_s_d[0][i].charAt(0) == s.charAt(0)) ex = i; } return ex; } public static void main(String[] args) { Dijkstras frame = new Dijkstras(); frame.pack(); frame.setVisible(true); } }