chiark / gitweb /
Generate special fake keypresses from menu options.
[sgt-puzzles.git] / PuzzleApplet.java
1 /*
2  * PuzzleApplet.java: NestedVM applet for the puzzle collection
3  */
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.awt.image.BufferedImage;
7 import java.util.*;
8 import javax.swing.*;
9 import javax.swing.border.BevelBorder;
10 import javax.swing.Timer;
11 import java.util.List;
12
13 import org.ibex.nestedvm.Runtime;
14
15 public class PuzzleApplet extends JApplet implements Runtime.CallJavaCB {
16
17     private static final long serialVersionUID = 1L;
18
19     private static final int CFG_SETTINGS = 0, CFG_SEED = 1, CFG_DESC = 2,
20             LEFT_BUTTON = 0x0200, MIDDLE_BUTTON = 0x201, RIGHT_BUTTON = 0x202,
21             LEFT_DRAG = 0x203, MIDDLE_DRAG = 0x204, RIGHT_DRAG = 0x205,
22             LEFT_RELEASE = 0x206, CURSOR_UP = 0x209, CURSOR_DOWN = 0x20a,
23             CURSOR_LEFT = 0x20b, CURSOR_RIGHT = 0x20c, MOD_CTRL = 0x1000,
24             MOD_SHFT = 0x2000, MOD_NUM_KEYPAD = 0x4000, ALIGN_VCENTRE = 0x100,
25             ALIGN_HCENTRE = 0x001, ALIGN_HRIGHT = 0x002, C_STRING = 0,
26             C_CHOICES = 1, C_BOOLEAN = 2;
27
28     private JFrame mainWindow;
29
30     private JMenu typeMenu;
31     private JMenuItem[] typeMenuItems;
32     private int customMenuItemIndex;
33
34     private JMenuItem solveCommand;
35     private Color[] colors;
36     private JLabel statusBar;
37     private PuzzlePanel pp;
38     private Runtime runtime;
39     private String[] puzzle_args;
40     private Graphics2D  gg;
41     private Timer timer;
42     private int xarg1, xarg2, xarg3;
43     private int[] xPoints, yPoints;
44     private BufferedImage[] blitters = new BufferedImage[512];
45     private ConfigDialog dlg;
46
47     static {
48         try {
49             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
50         } catch (Exception ex) {
51             ex.printStackTrace();
52         }
53     }
54
55     public void init() {
56         try {
57             Container cp = getContentPane();
58             cp.setLayout(new BorderLayout());
59             runtime = (Runtime) Class.forName("PuzzleEngine").newInstance();
60             runtime.setCallJavaCB(this);
61             JMenuBar menubar = new JMenuBar();
62             JMenu jm;
63             menubar.add(jm = new JMenu("Game"));
64             addMenuItemCallback(jm, "New", "jcallback_newgame_event");
65             addMenuItemCallback(jm, "Restart", "jcallback_restart_event");
66             addMenuItemCallback(jm, "Specific...", "jcallback_config_event", CFG_DESC);
67             addMenuItemCallback(jm, "Random Seed...", "jcallback_config_event", CFG_SEED);
68             jm.addSeparator();
69             addMenuItemCallback(jm, "Undo", "jcallback_undo_event");
70             addMenuItemCallback(jm, "Redo", "jcallback_redo_event");
71             jm.addSeparator();
72             solveCommand = addMenuItemCallback(jm, "Solve", "jcallback_solve_event");
73             solveCommand.setEnabled(false);
74             if (mainWindow != null) {
75                 jm.addSeparator();
76                 addMenuItemCallback(jm, "Exit", "jcallback_quit_event");
77             }
78             menubar.add(typeMenu = new JMenu("Type"));
79             typeMenu.setVisible(false);
80             menubar.add(jm = new JMenu("Help"));
81             addMenuItemCallback(jm, "About", "jcallback_about_event");
82             setJMenuBar(menubar);
83             cp.add(pp = new PuzzlePanel(), BorderLayout.CENTER);
84             pp.addKeyListener(new KeyAdapter() {
85                 public void keyPressed(KeyEvent e) {
86                     int key = -1;
87                     int shift = e.isShiftDown() ? MOD_SHFT : 0;
88                     int ctrl = e.isControlDown() ? MOD_CTRL : 0;
89                     switch (e.getKeyCode()) {
90                     case KeyEvent.VK_LEFT:
91                     case KeyEvent.VK_KP_LEFT:
92                         key = shift | ctrl | CURSOR_LEFT;
93                         break;
94                     case KeyEvent.VK_RIGHT:
95                     case KeyEvent.VK_KP_RIGHT:
96                         key = shift | ctrl | CURSOR_RIGHT;
97                         break;
98                     case KeyEvent.VK_UP:
99                     case KeyEvent.VK_KP_UP:
100                         key = shift | ctrl | CURSOR_UP;
101                         break;
102                     case KeyEvent.VK_DOWN:
103                     case KeyEvent.VK_KP_DOWN:
104                         key = shift | ctrl | CURSOR_DOWN;
105                         break;
106                     case KeyEvent.VK_PAGE_UP:
107                         key = shift | ctrl | MOD_NUM_KEYPAD | '9';
108                         break;
109                     case KeyEvent.VK_PAGE_DOWN:
110                         key = shift | ctrl | MOD_NUM_KEYPAD | '3';
111                         break;
112                     case KeyEvent.VK_HOME:
113                         key = shift | ctrl | MOD_NUM_KEYPAD | '7';
114                         break;
115                     case KeyEvent.VK_END:
116                         key = shift | ctrl | MOD_NUM_KEYPAD | '1';
117                         break;
118                     default:
119                         if (e.getKeyCode() >= KeyEvent.VK_NUMPAD0 && e.getKeyCode() <=KeyEvent.VK_NUMPAD9) {
120                             key = MOD_NUM_KEYPAD | (e.getKeyCode() - KeyEvent.VK_NUMPAD0+'0');
121                         }
122                     break;
123                     }
124                     if (key != -1) {
125                         runtimeCall("jcallback_key_event", new int[] {0, 0, key});
126                     }
127                 }
128                 public void keyTyped(KeyEvent e) {
129                     runtimeCall("jcallback_key_event", new int[] {0, 0, e.getKeyChar()});
130                 }
131             });
132             pp.addMouseListener(new MouseAdapter() {
133                 public void mouseReleased(MouseEvent e) {
134                     mousePressedReleased(e, true);
135                 }
136                 public void mousePressed(MouseEvent e) {
137                     pp.requestFocus();
138                     mousePressedReleased(e, false);
139                 }
140                 private void mousePressedReleased(MouseEvent e, boolean released) {
141                     int button;
142                     if ((e.getModifiers() & (InputEvent.BUTTON2_MASK | InputEvent.SHIFT_MASK)) != 0)
143                         button = MIDDLE_BUTTON;
144                     else if ((e.getModifiers() & (InputEvent.BUTTON3_MASK | InputEvent.ALT_MASK)) != 0)
145                         button = RIGHT_BUTTON;
146                     else if ((e.getModifiers() & (InputEvent.BUTTON1_MASK)) != 0)
147                         button = LEFT_BUTTON;
148                     else
149                         return;
150                     if (released)
151                         button += LEFT_RELEASE - LEFT_BUTTON;
152                     runtimeCall("jcallback_key_event", new int[] {e.getX(), e.getY(), button});
153                 }
154             });
155             pp.addMouseMotionListener(new MouseMotionAdapter() {
156                 public void mouseDragged(MouseEvent e) {
157                     int button;
158                     if ((e.getModifiers() & (InputEvent.BUTTON2_MASK | InputEvent.SHIFT_MASK)) != 0)
159                         button = MIDDLE_DRAG;
160                     else if ((e.getModifiers() & (InputEvent.BUTTON3_MASK | InputEvent.ALT_MASK)) != 0)
161                         button = RIGHT_DRAG;
162                     else
163                         button = LEFT_DRAG;
164                     runtimeCall("jcallback_key_event", new int[] {e.getX(), e.getY(), button});
165                 }
166             });
167             pp.addComponentListener(new ComponentAdapter() {
168                 public void componentResized(ComponentEvent e) {
169                     handleResized();
170                 }
171             });
172             pp.setFocusable(true);
173             pp.requestFocus();
174             timer = new Timer(20, new ActionListener() {
175                 public void actionPerformed(ActionEvent e) {
176                     runtimeCall("jcallback_timer_func", new int[0]);
177                 }
178             });
179             String gameid;
180             try {
181                 gameid = getParameter("game_id");
182             } catch (java.lang.NullPointerException ex) {
183                 gameid = null;
184             }
185             if (gameid == null) {
186                 puzzle_args = null;
187             } else {
188                 puzzle_args = new String[2];
189                 puzzle_args[0] = "puzzle";
190                 puzzle_args[1] = gameid;
191             }
192             SwingUtilities.invokeLater(new Runnable() {
193                 public void run() {
194                     runtime.start(puzzle_args);
195                     runtime.execute();
196                 }
197             });
198         } catch (Exception ex) {
199             ex.printStackTrace();
200         }
201     }
202
203     public void destroy() {
204         SwingUtilities.invokeLater(new Runnable() {
205             public void run() {
206                 runtime.execute();
207                 if (mainWindow != null) {
208                     mainWindow.dispose();
209                     System.exit(0);
210                 }
211             }
212         });
213     }
214
215     protected void handleResized() {
216         pp.createBackBuffer(pp.getWidth(), pp.getHeight(), colors[0]);
217         runtimeCall("jcallback_resize", new int[] {pp.getWidth(), pp.getHeight()});
218     }
219
220     private JMenuItem addMenuItemCallback(JMenu jm, String name, final String callback, final int arg) {
221         return addMenuItemCallback(jm, name, callback, new int[] {arg}, false);
222     }
223
224     private JMenuItem addMenuItemCallback(JMenu jm, String name, final String callback) {
225         return addMenuItemCallback(jm, name, callback, new int[0], false);
226     }
227
228     private JMenuItem addMenuItemCallback(JMenu jm, String name, final String callback, final int[] args, boolean checkbox) {
229         JMenuItem jmi;
230         if (checkbox)
231             jm.add(jmi = new JCheckBoxMenuItem(name));
232         else
233         jm.add(jmi = new JMenuItem(name));
234         jmi.addActionListener(new ActionListener() {
235             public void actionPerformed(ActionEvent e) {
236                 runtimeCall(callback, args);
237             }
238         });
239         return jmi;
240     }
241
242     protected void runtimeCall(String func, int[] args) {
243         if (runtimeCallWithResult(func, args) == 42 && mainWindow != null) {
244             destroy();
245         }
246     }
247
248     protected int runtimeCallWithResult(String func, int[] args) {
249         try {
250             return runtime.call(func, args);
251         } catch (Runtime.CallException ex) {
252             ex.printStackTrace();
253             return 42;
254         }
255     }
256
257     private void buildConfigureMenuItem() {
258         if (typeMenu.isVisible()) {
259             typeMenu.addSeparator();
260         } else {
261             typeMenu.setVisible(true);
262         }
263         typeMenuItems[customMenuItemIndex] =
264             addMenuItemCallback(typeMenu, "Custom...",
265                                 "jcallback_config_event",
266                                 new int[] {CFG_SETTINGS}, true);
267     }
268
269     private void addTypeItem
270         (JMenu targetMenu, String name, int newId, final int ptrGameParams) {
271
272         typeMenu.setVisible(true);
273         typeMenuItems[newId] =
274             addMenuItemCallback(targetMenu, name,
275                                 "jcallback_preset_event",
276                                 new int[] {ptrGameParams}, true);
277     }
278
279     private void addTypeSubmenu
280         (JMenu targetMenu, String name, int newId) {
281
282         JMenu newMenu = new JMenu(name);
283         newMenu.setVisible(true);
284         typeMenuItems[newId] = newMenu;
285         targetMenu.add(newMenu);
286     }
287
288     public int call(int cmd, int arg1, int arg2, int arg3) {
289         try {
290             switch(cmd) {
291             case 0: // initialize
292                 if (mainWindow != null) mainWindow.setTitle(runtime.cstring(arg1));
293                 if ((arg2 & 1) != 0) buildConfigureMenuItem();
294                 if ((arg2 & 2) != 0) addStatusBar();
295                 if ((arg2 & 4) != 0) solveCommand.setEnabled(true);
296                 colors = new Color[arg3];
297                 return 0;
298             case 1: // configure Type menu
299                 if (arg1 == 0) {
300                     // preliminary setup
301                     typeMenuItems = new JMenuItem[arg2 + 2];
302                     typeMenuItems[arg2] = typeMenu;
303                     customMenuItemIndex = arg2 + 1;
304                     return arg2;
305                 } else if (xarg1 != 0) {
306                     addTypeItem((JMenu)typeMenuItems[arg2],
307                                 runtime.cstring(arg1), arg3, xarg1);
308                 } else {
309                     addTypeSubmenu((JMenu)typeMenuItems[arg2],
310                                    runtime.cstring(arg1), arg3);
311                 }
312                 return 0;
313             case 2: // MessageBox
314                 JOptionPane.showMessageDialog(this, runtime.cstring(arg2), runtime.cstring(arg1), arg3 == 0 ? JOptionPane.INFORMATION_MESSAGE : JOptionPane.ERROR_MESSAGE);
315                 return 0;
316             case 3: // Resize
317                 pp.setPreferredSize(new Dimension(arg1, arg2));
318                 if (mainWindow != null) mainWindow.pack();
319                 handleResized();
320                 if (mainWindow != null) mainWindow.setVisible(true);
321                 return 0;
322             case 4: // drawing tasks
323                 switch(arg1) {
324                 case 0:
325                     String text = runtime.cstring(arg2);
326                     if (text.equals("")) text = " ";
327                     statusBar.setText(text);
328                     break;
329                 case 1:
330                     gg = pp.backBuffer.createGraphics();
331                     if (arg2 != 0 || arg3 != 0 ||
332                         arg2 + xarg2 != getWidth() ||
333                         arg3 + xarg3 != getHeight()) {
334                         int left = arg2, right = arg2 + xarg2;
335                         int top = arg3, bottom = arg3 + xarg3;
336                         int width = getWidth(), height = getHeight();
337                         gg.setColor(colors != null ? colors[0] : Color.black);
338                         gg.fillRect(0, 0, left, height);
339                         gg.fillRect(right, 0, width-right, height);
340                         gg.fillRect(0, 0, width, top);
341                         gg.fillRect(0, bottom, width, height-bottom);
342                         gg.setClip(left, top, right-left, bottom-top);
343                     }
344                     break;
345                 case 2: gg.dispose(); pp.repaint(); break;
346                 case 3: gg.setClip(arg2, arg3, xarg1, xarg2); break;
347                 case 4:
348                     if (arg2 == 0 && arg3 == 0) {
349                         gg.setClip(0, 0, getWidth(), getHeight());
350                     } else {
351                         gg.setClip(arg2, arg3, getWidth()-2*arg2, getHeight()-2*arg3);
352                     }
353                     break;
354                 case 5:
355                     gg.setColor(colors[xarg3]);
356                     gg.fillRect(arg2, arg3, xarg1, xarg2);
357                     break;
358                 case 6:
359                     gg.setColor(colors[xarg3]);
360                     gg.drawLine(arg2, arg3, xarg1, xarg2);
361                     break;
362                 case 7:
363                     xPoints = new int[arg2];
364                     yPoints = new int[arg2];
365                     break;
366                 case 8:
367                     if (arg3 != -1) {
368                         gg.setColor(colors[arg3]);
369                         gg.fillPolygon(xPoints, yPoints, xPoints.length);
370                     }
371                     gg.setColor(colors[arg2]);
372                     gg.drawPolygon(xPoints, yPoints, xPoints.length);
373                     break;
374                 case 9:
375                     if (arg3 != -1) {
376                         gg.setColor(colors[arg3]);
377                         gg.fillOval(xarg1-xarg3, xarg2-xarg3, xarg3*2, xarg3*2);
378                     }
379                     gg.setColor(colors[arg2]);
380                     gg.drawOval(xarg1-xarg3, xarg2-xarg3, xarg3*2, xarg3*2);
381                     break;
382                 case 10:
383                     for(int i=0; i<blitters.length; i++) {
384                         if (blitters[i] == null) {
385                             blitters[i] = new BufferedImage(arg2, arg3, BufferedImage.TYPE_3BYTE_BGR);
386                             return i;
387                         }
388                     }
389                     throw new RuntimeException("No free blitter found!");
390                 case 11: blitters[arg2] = null; break;
391                 case 12:
392                     timer.start(); break;
393                 case 13:
394                     timer.stop(); break;
395                 }
396                 return 0;
397             case 5: // more arguments
398                 xarg1 = arg1;
399                 xarg2 = arg2;
400                 xarg3 = arg3;
401                 return 0;
402             case 6: // polygon vertex
403                 xPoints[arg1]=arg2;
404                 yPoints[arg1]=arg3;
405                 return 0;
406             case 7: // string
407                 gg.setColor(colors[arg2]);
408                 {
409                     String text = runtime.utfstring(arg3);
410                     Font ft = new Font((xarg3 & 0x10) != 0 ? "Monospaced" : "Dialog",
411                             Font.PLAIN, 100);
412                     int height100 = this.getFontMetrics(ft).getHeight();
413                     ft = ft.deriveFont(arg1 * 100 / (float)height100);
414                     FontMetrics fm = this.getFontMetrics(ft);
415                     int asc = fm.getAscent(), desc = fm.getDescent();
416                     if ((xarg3 & ALIGN_VCENTRE) != 0)
417                         xarg2 += asc - (asc+desc)/2;
418                     int wid = fm.stringWidth(text);
419                     if ((xarg3 & ALIGN_HCENTRE) != 0)
420                         xarg1 -= wid / 2;
421                     else if ((xarg3 & ALIGN_HRIGHT) != 0)
422                         xarg1 -= wid;
423                     gg.setFont(ft);
424                     gg.drawString(text, xarg1, xarg2);
425                 }
426                 return 0;
427             case 8: // blitter_save
428                 Graphics g2 = blitters[arg1].createGraphics();
429                 g2.drawImage(pp.backBuffer, 0, 0, blitters[arg1].getWidth(), blitters[arg1].getHeight(),
430                         arg2, arg3, arg2 + blitters[arg1].getWidth(), arg3 + blitters[arg1].getHeight(), this);
431                 g2.dispose();
432                 return 0;
433             case 9: // blitter_load
434                 gg.drawImage(blitters[arg1], arg2, arg3, this);
435                 return 0;
436             case 10: // dialog_init
437                 dlg= new ConfigDialog(this, runtime.cstring(arg1));
438                 return 0;
439             case 11: // dialog_add_control
440                 {
441                     int sval_ptr = arg1;
442                     int ival = arg2;
443                     int ptr = xarg1;
444                     int type=xarg2;
445                     String name = runtime.cstring(xarg3);
446                     switch(type) {
447                     case C_STRING:
448                         dlg.addTextBox(ptr, name, runtime.cstring(sval_ptr));
449                         break;
450                     case C_BOOLEAN:
451                         dlg.addCheckBox(ptr, name, ival != 0);
452                         break;
453                     case C_CHOICES:
454                         dlg.addComboBox(ptr, name, runtime.cstring(sval_ptr), ival);
455                     }
456                 }
457                 return 0;
458             case 12:
459                 dlg.finish();
460                 dlg = null;
461                 return 0;
462             case 13: // tick a menu item
463                 if (arg1 < 0) arg1 = customMenuItemIndex;
464                 for (int i = 0; i < typeMenuItems.length; i++) {
465                     if (typeMenuItems[i] instanceof JCheckBoxMenuItem) {
466                         ((JCheckBoxMenuItem)typeMenuItems[i]).setSelected
467                             (arg1 == i);
468                     }
469                 }
470                 return 0;
471             default:
472                 if (cmd >= 1024 && cmd < 2048) { // palette
473                     colors[cmd-1024] = new Color(arg1, arg2, arg3);
474                 }
475             if (cmd == 1024) {
476                 pp.setBackground(colors[0]);
477                 if (statusBar != null) statusBar.setBackground(colors[0]);
478                 this.setBackground(colors[0]);
479             }
480             return 0;
481             }
482         } catch (Throwable ex) {
483             ex.printStackTrace();
484             System.exit(-1);
485             return 0;
486         }
487     }
488
489     private void addStatusBar() {
490         statusBar = new JLabel("test");
491         statusBar.setBorder(new BevelBorder(BevelBorder.LOWERED));
492         getContentPane().add(BorderLayout.SOUTH,statusBar);
493     }
494
495     // Standalone runner
496     public static void main(String[] args) {
497         final PuzzleApplet a = new PuzzleApplet();
498         JFrame jf = new JFrame("Loading...");
499         jf.getContentPane().setLayout(new BorderLayout());
500         jf.getContentPane().add(a, BorderLayout.CENTER);
501         a.mainWindow=jf;
502         a.init();
503         a.start();
504         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
505         jf.addWindowListener(new WindowAdapter() {
506             public void windowClosing(WindowEvent e) {
507                 a.stop();
508                 a.destroy();
509             }
510         });
511         jf.setVisible(true);
512     }
513
514     public static class PuzzlePanel extends JPanel {
515
516         private static final long serialVersionUID = 1L;
517         protected BufferedImage backBuffer;
518
519         public PuzzlePanel() {
520             setPreferredSize(new Dimension(100,100));
521             createBackBuffer(100,100, Color.black);
522         }
523
524         public void createBackBuffer(int w, int h, Color bg) {
525             if (w > 0 && h > 0) {
526                 backBuffer = new BufferedImage(w,h, BufferedImage.TYPE_3BYTE_BGR);
527                 Graphics g = backBuffer.createGraphics();
528                 g.setColor(bg);
529                 g.fillRect(0, 0, w, h);
530                 g.dispose();
531             }
532         }
533
534         protected void paintComponent(Graphics g) {
535             g.drawImage(backBuffer, 0, 0, this);
536         }
537     }
538
539     public static class ConfigComponent {
540         public int type;
541         public int configItemPointer;
542         public JComponent component;
543
544         public ConfigComponent(int type, int configItemPointer, JComponent component) {
545             this.type = type;
546             this.configItemPointer = configItemPointer;
547             this.component = component;
548         }
549     }
550
551     public class ConfigDialog extends JDialog {
552
553         private GridBagConstraints gbcLeft = new GridBagConstraints(
554                 GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE, 1, 1,
555                 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE,
556                 new Insets(0, 0, 0, 0), 0, 0);
557         private GridBagConstraints gbcRight = new GridBagConstraints(
558                 GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE,
559                 GridBagConstraints.REMAINDER, 1, 1.0, 0,
560                 GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
561                 new Insets(5, 5, 5, 5), 0, 0);
562         private GridBagConstraints gbcBottom = new GridBagConstraints(
563                 GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE,
564                 GridBagConstraints.REMAINDER, GridBagConstraints.REMAINDER,
565                 1.0, 1.0, GridBagConstraints.CENTER,
566                 GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);
567
568         private static final long serialVersionUID = 1L;
569         private List components = new ArrayList();
570
571         public ConfigDialog(JApplet parent, String title) {
572             super(JOptionPane.getFrameForComponent(parent), title, true);
573             getContentPane().setLayout(new GridBagLayout());
574         }
575
576         public void addTextBox(int ptr, String name, String value) {
577             getContentPane().add(new JLabel(name), gbcLeft);
578             JComponent c = new JTextField(value, 25);
579             getContentPane().add(c, gbcRight);
580             components.add(new ConfigComponent(C_STRING, ptr, c));
581         }
582
583
584         public void addCheckBox(int ptr, String name, boolean selected) {
585             JComponent c = new JCheckBox(name, selected);
586             getContentPane().add(c, gbcRight);
587             components.add(new ConfigComponent(C_BOOLEAN, ptr, c));
588         }
589
590         public void addComboBox(int ptr, String name, String values, int selected) {
591             getContentPane().add(new JLabel(name), gbcLeft);
592             StringTokenizer st = new StringTokenizer(values.substring(1), values.substring(0,1));
593             JComboBox c = new JComboBox();
594             c.setEditable(false);
595             while(st.hasMoreTokens())
596                 c.addItem(st.nextToken());
597             c.setSelectedIndex(selected);
598             getContentPane().add(c, gbcRight);
599             components.add(new ConfigComponent(C_CHOICES, ptr, c));
600         }
601
602         public void finish() {
603             JPanel buttons = new JPanel(new GridLayout(1, 2, 5, 5));
604             getContentPane().add(buttons, gbcBottom);
605             JButton b;
606             buttons.add(b=new JButton("OK"));
607             b.addActionListener(new ActionListener() {
608                 public void actionPerformed(ActionEvent e) {
609                     save();
610                     dispose();
611                 }
612             });
613             getRootPane().setDefaultButton(b);
614             buttons.add(b=new JButton("Cancel"));
615             b.addActionListener(new ActionListener() {
616                 public void actionPerformed(ActionEvent e) {
617                     dispose();
618                 }
619             });
620             setDefaultCloseOperation(DISPOSE_ON_CLOSE);
621             pack();
622             setLocationRelativeTo(null);
623             setVisible(true);
624         }
625         private void save() {
626             for (int i = 0; i < components.size(); i++) {
627                 ConfigComponent cc = (ConfigComponent) components.get(i);
628                 switch(cc.type) {
629                 case C_STRING:
630                     JTextField jtf = (JTextField)cc.component;
631                     runtimeCall("jcallback_config_set_string", new int[] {cc.configItemPointer, runtime.strdup(jtf.getText())});
632                     break;
633                 case C_BOOLEAN:
634                     JCheckBox jcb = (JCheckBox)cc.component;
635                     runtimeCall("jcallback_config_set_boolean", new int[] {cc.configItemPointer, jcb.isSelected()?1:0});
636                     break;
637                 case C_CHOICES:
638                     JComboBox jcm = (JComboBox)cc.component;
639                     runtimeCall("jcallback_config_set_choice", new int[] {cc.configItemPointer, jcm.getSelectedIndex()});
640                     break;
641                 }
642             }
643             runtimeCall("jcallback_config_ok", new int[0]);
644         }
645     }
646 }