00001 package org.gel.mauve.dcj;
00002
00003 import java.util.*;
00004 import java.awt.*;
00005 import java.awt.event.*;
00006 import javax.swing.*;
00007 import javax.swing.tree.*;
00008 import javax.swing.text.*;
00009 import java.applet.Applet;
00015 public class DCJWindow extends JWindow {
00016
00017 TextArea input, output, log, ops;
00018
00019 int fWIDTH = 600;
00020
00021 int fHEIGHT = 300;
00022
00023 Panel toptopPanel;
00024
00025 CardLayout cards;
00026
00027 String box;
00028
00029 String pattern;
00030
00031 String defaultInput = ""
00032 + "1 -32 17 2 23 12 3 20 6 30 7 8 21 31 24 9 -10 -18 11 33 -28 19 14 34 13 25 4 22 -29 26 5 35 -15 -27 -16 -36 $,\n1 30 7 2 23 12 3 -32 6 8 21 31 9 -10 11 19 14 18 33 -13 -5 -22 -4 -25 -20 -36 17 -26 34 -16 -35 15 -24 -27 29 -28 $,\n1 30 7 2 23 12 3 -32 6 8 21 31 9 -10 11 19 14 18 33 28 -29 27 24 -15 35 16 -34 26 -17 36 20 25 4 22 5 13 $,\n1 25 2 23 17 12 3 20 6 15 30 27 31 18 -19 -9 -21 -8 -7 33 -28 10 11 32 -4 -24 -13 -34 -14 22 -29 26 5 35 -16 -36 $,\n1 25 2 23 17 12 3 20 6 15 30 27 31 18 -19 -9 -21 -8 -7 33 -28 10 11 32 -4 -24 -13 -34 -14 26 5 35 -22 -29 -16 -36 $,\n1 34 13 24 28 15 10 9 4 7 11 17 16 19 2 36 35 20 21 -29 -25 -27 -12 -30 -18 -14 -26 -6 -32 31 8 -33 -3 22 5 23 $,\n1 34 13 24 15 10 28 9 4 7 11 17 16 19 2 36 35 20 21 -29 -25 -27 -12 -30 -18 -14 26 -6 -32 -33 -3 31 8 22 5 23 $,\n1 17 2 12 -19 -9 -21 -8 -7 33 -32 -11 -10 28 -4 -25 -24 -13 -34 -14 -26 -16 -36 -35 -29 -20 -18 3 23 15 30 27 22 6 31 5 $,\n1 27 2 17 36 20 3 29 10 11 35 12 30 21 9 19 18 28 33 7 8 16 26 14 34 13 24 15 32 25 4 22 23 6 31 5 $,\n1 16 26 17 20 2 21 13 6 9 15 28 34 10 7 35 18 14 32 27 36 4 12 23 25 31 5 22 30 29 19 11 24 3 33 8 $,\n1 35 10 30 29 11 24 3 23 15 25 27 26 7 14 36 4 19 12 22 20 2 21 13 6 16 32 28 17 34 9 18 31 5 33 8";
00033
00034 public static void startDCJ (String s1) {
00035 DCJWindow w = new DCJWindow ();
00036 w.performDCJ (s1);
00037 }
00038
00039 private Vector parseInput (String s) {
00040 StringTokenizer token = new StringTokenizer (s, ",");
00041 StringTokenizer newtoken;
00042 Vector v = new Vector ();
00043 String st;
00044 while (token.hasMoreTokens ()) {
00045 st = (token.nextToken ().trim ());
00046 if (st.length () > 0)
00047 v.add (st);
00048 }
00049 return v;
00050
00051 }
00052
00053 public void performDCJ (String s) {
00054 build ();
00055
00056 String box2 = "";
00057 output.setText (box2);
00058 log.setText (box2);
00059 StringBuffer boxa = new StringBuffer (256);
00060 StringBuffer boxb = new StringBuffer ();
00061 StringBuffer boxc = new StringBuffer ();
00062 Vector v = parseInput (s.trim ());
00063 DCJ d;
00064 int [][] distances = new int [v.size ()] [v.size ()];
00065 for (int i = 0; i < v.size (); i++) {
00066 distances[i][i] = 0;
00067 }
00068 for (int i = 0; i < v.size (); i++) {
00069 for (int j = i + 1; j < v.size (); j++) {
00070 boxa.append (i
00071 + " to "
00072 + j
00073 + "\n"
00074 + (d = new DCJ (new StringTokenizer ((String) v
00075 .elementAt (i), "$"), new StringTokenizer (
00076 (String) v.elementAt (j), "$"))).getLog ());
00077 distances[i][j] = d.getCount ();
00078 distances[j][i] = d.getCount ();
00079 boxc.append ("\n" + i + " to " + j + "\n");
00080 boxc.append (d.getOpBuf ());
00081 }
00082 }
00083
00084
00085
00086
00087
00088
00089 output.setText (boxb.toString ());
00090 ops.setText (boxc.toString ());
00091 log.setText (boxa.toString ());
00092
00093
00094 }
00095
00096 public void build () {
00097 JFrame frame = new JFrame ("DCJ");
00098 frame.setSize (fWIDTH, fHEIGHT);
00099 JPanel content = new JPanel (new BorderLayout ());
00100 frame.getContentPane ().add (content, BorderLayout.CENTER);
00101 box = "";
00102 setLayout (new BorderLayout ());
00103
00104 Panel topPanel = new Panel ();
00105
00106 topPanel.setLayout (new BorderLayout ());
00107
00108 cards = new CardLayout ();
00109 toptopPanel = new Panel ();
00110 toptopPanel.setLayout (cards);
00111 topPanel.add (toptopPanel, BorderLayout.CENTER);
00112
00113 Panel toplowerPanel = new Panel ();
00114 GridLayout butts = new GridLayout (1, 0);
00115 butts.setHgap (50);
00116 toplowerPanel.setLayout (butts);
00117 topPanel.add (toplowerPanel, BorderLayout.SOUTH);
00118
00119 Button Bout = new Button ("matrix");
00120 Button Bop = new Button ("operations");
00121 Button Blog = new Button ("log");
00122 toplowerPanel.add (Bout);
00123 toplowerPanel.add (Bop);
00124 toplowerPanel.add (Blog);
00125
00126 ChangeCards cc = new ChangeCards ();
00127
00128 Bout.addActionListener (cc);
00129 Blog.addActionListener (cc);
00130 Bop.addActionListener (cc);
00131
00132 add (topPanel, BorderLayout.NORTH);
00133 content.add (topPanel, BorderLayout.CENTER);
00134
00135
00136 output = new TextArea (box, 25, 40);
00137 output.setEditable (false);
00138 output.setFont (new Font ("monospaced", Font.ITALIC, 12));
00139 toptopPanel.add ("matrix", output);
00140 cards.show (toptopPanel, "matrix");
00141
00142 ops = new TextArea (box, 25, 40);
00143 ops.setEditable (false);
00144 ops.setFont (new Font ("monospaced", Font.PLAIN, 12));
00145 toptopPanel.add ("operations", ops);
00146
00147 log = new TextArea ("", 25, 40);
00148 log.setEditable (false);
00149 log.setFont (new Font ("monospaced", Font.PLAIN, 12));
00150 toptopPanel.add ("log", log);
00151
00152
00154 Panel textInputField = new Panel ();
00155 Panel forTheButtons = new Panel ();
00156 textInputField.setLayout (new BorderLayout ());
00157 forTheButtons.setLayout (new GridLayout (1, 0));
00158 add (textInputField);
00159
00160 Button submitB = new Button ("Submit");
00161 Button clear = new Button ("Clear");
00162 forTheButtons.add (submitB);
00163 forTheButtons.add (clear);
00164 textInputField.add (forTheButtons, BorderLayout.SOUTH);
00165
00166 input = new TextArea ();
00167 textInputField.add (input);
00168
00169 ClearData clearit = new ClearData ();
00170 SubmitData submitButton = new SubmitData ();
00171
00172 submitB.addActionListener (submitButton);
00173 clear.addActionListener (clearit);
00174
00175 input.setText (defaultInput);
00177 output.setText ("");
00178 frame.setVisible (true);
00179 }
00180
00181 private class ChangeCards implements ActionListener {
00182 public void actionPerformed (ActionEvent e) {
00183 if (e.getActionCommand () == "matrix") {
00184 cards.show (toptopPanel, "matrix");
00185 }
00186 if (e.getActionCommand () == "log") {
00187 cards.show (toptopPanel, "log");
00188 }
00189 if (e.getActionCommand () == "operations") {
00190 cards.show (toptopPanel, "operations");
00191 }
00192 }
00193 }
00194
00195 private class ClearData implements ActionListener {
00196 public void actionPerformed (ActionEvent e) {
00197 input.setText ("");
00198 output.setText ("");
00199 log.setText ("");
00200 }
00201 }
00202
00203 private class SubmitData implements ActionListener {
00204
00205 private Vector parseInput (String s) {
00206 StringTokenizer token = new StringTokenizer (s, ",");
00207 StringTokenizer newtoken;
00208 Vector v = new Vector ();
00209 String st;
00210 while (token.hasMoreTokens ()) {
00211 st = (token.nextToken ().trim ());
00212 if (st.length () > 0)
00213 v.add (st);
00214 }
00215 return v;
00216
00217 }
00218
00219 public void actionPerformed (ActionEvent e) {
00220 String box2 = "";
00221 output.setText (box2);
00222 log.setText (box2);
00223 StringBuffer boxa = new StringBuffer (256);
00224 StringBuffer boxb = new StringBuffer ();
00225 StringBuffer boxc = new StringBuffer ();
00226 Vector v = parseInput ((input.getText ()).trim ());
00227 DCJ d;
00228 int [][] distances = new int [v.size ()] [v.size ()];
00229 for (int i = 0; i < v.size (); i++) {
00230 distances[i][i] = 0;
00231 }
00232 for (int i = 0; i < v.size (); i++) {
00233 for (int j = i + 1; j < v.size (); j++) {
00234 boxa.append (i
00235 + " to "
00236 + j
00237 + "\n"
00238 + (d = new DCJ (new StringTokenizer ((String) v
00239 .elementAt (i), "$"), new StringTokenizer (
00240 (String) v.elementAt (j), "$"))).getLog ());
00241 distances[i][j] = d.getCount ();
00242 distances[j][i] = d.getCount ();
00243 boxc.append ("\n" + i + " to " + j + "\n");
00244 boxc.append (d.getOpBuf ());
00245 }
00246 }
00247 for (int i = 0; i < v.size (); i++) {
00248 for (int j = 0; j < v.size (); j++) {
00249 boxb.append (distances[i][j] + " ");
00250 }
00251 boxb.append ("\n");
00252 }
00253 output.setText (boxb.toString ());
00254 ops.setText (boxc.toString ());
00255 log.setText (boxa.toString ());
00256
00257 }
00258
00259 }
00260
00261 }