/* Copyright (C) 2002 J. M. Spivey */ import java.awt.*; import java.awt.event.*; public class Editor extends Frame { Machine machine; Loader loader; ColourSpot colrSpot[][] = new ColourSpot[machine.NSTATES][machine.NCOLORS]; LabelSpot turnSpot[][] = new LabelSpot[machine.NSTATES][machine.NCOLORS]; LabelSpot nextSpot[][] = new LabelSpot[machine.NSTATES][machine.NCOLORS]; Panel panel = new Panel(new VariGridLayout(machine.NSTATES+1, machine.NCOLORS+1, 1)); Panel buttons = new Panel(new FlowLayout()); public static String turns[] = { "Left", "Right", "Ahead" }; public Editor(Machine machine, Loader loader) { super("Turmite editor"); this.machine = machine; this.loader = loader; panel.setBackground(Color.darkGray); add(panel, "Center"); add(buttons, "South"); addMargin(new Label("State", Label.CENTER)); for (int i = 0; i < machine.NCOLORS; i++) { addMargin(new ColourLabel(i)); } String[] states = new String[machine.NSTATES]; for (int i = 0; i < machine.NSTATES; i++) states[i] = Integer.toString(i+1); for (int i = 0; i < machine.NSTATES; i++) { addMargin(new Label(Integer.toString(i+1), Label.CENTER)); for (int j = 0; j < machine.NCOLORS; j++) { Panel cell = new Panel(new VariGridLayout(1, 3)); panel.add(cell); final int ii = i, jj = j; cell.add(colrSpot[i][j] = new ColourSpot() { public void changed(int color) { Editor.this.machine.setDraw(ii, jj, color); } }); cell.add(turnSpot[i][j] = new LabelSpot(turns, 50) { public void changed(int n) { Editor.this.machine.setMove(ii, jj, n); } }); cell.add(nextSpot[i][j] = new LabelSpot(states, 20) { public void changed(int n) { Editor.this.machine.setTran(ii, jj, n); } }); } } addButton("Save", new ActionListener() { public void actionPerformed(ActionEvent e) { save(); } }); reload(); pack(); } public void reload() { for (int i = 0; i < machine.NSTATES; i++) { for (int j = 0; j < machine.NCOLORS; j++) { colrSpot[i][j].setColor(machine.getDraw(i, j)); turnSpot[i][j].setChoice(machine.getMove(i, j)); nextSpot[i][j].setChoice(machine.getTran(i, j)); } } } public void save() { String s = machine.descriptor(); System.out.println(s); loader.setSaved(s); } private void addMargin(Component c) { if (! (c instanceof ColourLabel)) c.setBackground(Color.lightGray); panel.add(c); } private void addButton(String label, ActionListener action) { Button b = new Button(label); if (action != null) b.addActionListener(action); buttons.add(b); } class ColourLabel extends Label { private int color; public ColourLabel(int color) { setAlignment(CENTER); setColor(color); } public void setColor(int color) { this.color = color; setText(Arena.colname[color]); setBackground(Arena.color[color]); repaint(); } public Dimension getPreferredSize() { Dimension pref = super.getPreferredSize(); return new Dimension(50, pref.height); } } abstract class ColourSpot extends ColourLabel { PopupMenu popup = new PopupMenu(); public ColourSpot() { super(Machine.field); popup.add(new ColourChange(Machine.field)); popup.add(new ColourChange(Machine.red)); popup.add(new ColourChange(Machine.green)); popup.add(new ColourChange(Machine.yellow)); add(popup); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { popup.show(ColourSpot.this, e.getX(), e.getY()); } }); } public abstract void changed(int color); private class ColourChange extends MenuItem implements ActionListener { int color; ColourChange(int color) { super(Arena.colname[color]); this.color = color; addActionListener(this); } public void actionPerformed(ActionEvent e) { setColor(color); changed(color); } } } abstract class LabelSpot extends Label { String choice[]; int width; PopupMenu popup = new PopupMenu(); public LabelSpot(String choice[], int width) { this.choice = choice; this.width = width; setAlignment(CENTER); setBackground(Color.lightGray); for (int i = 0; i < choice.length; i++) popup.add(new LabelChange(i, choice[i])); add(popup); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { popup.show(LabelSpot.this, e.getX(), e.getY()); } }); } public void setChoice(int n) { setText(choice[n]); repaint(); } public abstract void changed(int n); private class LabelChange extends MenuItem implements ActionListener { int n; LabelChange(int n, String text) { super(text); this.n = n; addActionListener(this); } public void actionPerformed(ActionEvent e) { setChoice(n); changed(n); } } public Dimension getPreferredSize() { Dimension pref = super.getPreferredSize(); return new Dimension(width, pref.height); } } }