import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.util.StringTokenizer; import java.util.*; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; public class DiagnosticGUI extends JPanel implements ActionListener { JLabel label; JFrame frame; public DiagnosticGUI(JFrame frame) { super(new BorderLayout()); this.frame = frame; JButton start = new JButton("click here to troubleshoot"); start.addActionListener(this); add(start, BorderLayout.CENTER); } public void actionPerformed(ActionEvent e) { try { String inFileName = ("Diagnostic.txt"); FileReader reader = new FileReader(inFileName); BufferedReader ins = new BufferedReader(reader); convertFile(ins); String outFileName = JOptionPane.showInputDialog("Output file:"); FileWriter outStream = new FileWriter(outFileName); PrintWriter outs = new PrintWriter(outStream); Node root = buildTree(0); help(root, outs); ins.close(); } catch (Exception ex) { System.out.println("exception caught in constructor"); ex.printStackTrace(); } } private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Diagnostic"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DiagnosticGUI newContentPane = new DiagnosticGUI(frame); newContentPane.setOpaque(true); frame.setContentPane(newContentPane); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { try { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } catch (Exception e) { } } public void help(Node cur, PrintWriter pw) { if (cur != null) { int next; String n = "0"; if (cur.children.size() > 1) /* * n = JOptionPane.showInputDialog(cur.msg + " (enter number)\n" + * cur.printChildren()); */ n = (String) JOptionPane.showInputDialog(frame, cur.msg + " (pick one)\n", "Customized Dialog", JOptionPane.PLAIN_MESSAGE, null, cur.getChildren(), cur.getChildren()[0]); else JOptionPane.showMessageDialog(frame, cur.msg); pw.println(cur.num + "\t" + cur.msg); pw.println(cur.printChildren()); try { System.out.println("ni is: " + n); StringTokenizer st = new StringTokenizer(n, " "); String nn = st.nextToken(); next = Integer.parseInt(nn); ArrayList kids = cur.children; Node nd; for (int i = 0; i < kids.size(); i++) { nd = (Node) kids.get(i); if (nd == null) break; if (nd.num == next) { if (nd.children.size() > 1) { help(nd, pw); } else { help((Node) nd.children.get(0), pw); } } } } catch (Exception e) { System.out.println("invalid input!!"); } } pw.close(); } public ArrayList orderedFile = new ArrayList(); public void convertFile(BufferedReader br) { String s; try { while ((s = br.readLine()) != null) { orderedFile.add(s); } } catch (IOException ex) { System.out.println("I/O error " + ex.getMessage()); ex.printStackTrace(); } } public Node buildTree(int pos) { if (pos == -1) return null; StringTokenizer st = new StringTokenizer((String) orderedFile.get(pos), "\t"); int num = Integer.parseInt(st.nextToken()); String msg = st.nextToken(); ArrayList children = new ArrayList(); ; String kids = st.nextToken(); StringTokenizer st2 = new StringTokenizer(kids, ","); while (st2.hasMoreElements()) { children.add(buildTree(Integer.parseInt(st2.nextToken()))); } return new Node(num, msg, children); } public class Node { public int num; public String msg; public ArrayList children; public Node(int n, String m, ArrayList c) { num = n; msg = m; children = c; } public String printChildren() { String s = "\n"; for (int i = 0; i < this.children.size(); i++) { if (this.children.get(i) != null) s = s + ((Node) this.children.get(i)).num + " " + ((Node) this.children.get(i)).msg + "\n"; } return s; } public Object[] getChildren(){ String s = this.printChildren(); StringTokenizer st = new StringTokenizer(s, "\n"); int i = 0; Object[] o = new Object[st.countTokens()]; while (st.hasMoreElements()){ o[i] = st.nextElement(); i++; } return o; } public void printit() { System.out.println(); System.out.println(); System.out.println("Node: " + num); System.out.println(msg); for (int i = 0; i < children.size(); i++) { if ((Node) children.get(i) == null) { System.out.println("done"); break; } ((Node) children.get(i)).printit(); } } } }