summaryrefslogtreecommitdiffstats
path: root/lib/example/RadTool.java
diff options
context:
space:
mode:
Diffstat (limited to 'lib/example/RadTool.java')
-rw-r--r--lib/example/RadTool.java647
1 files changed, 647 insertions, 0 deletions
diff --git a/lib/example/RadTool.java b/lib/example/RadTool.java
new file mode 100644
index 0000000..8ef0c55
--- /dev/null
+++ b/lib/example/RadTool.java
@@ -0,0 +1,647 @@
1package example;
2
3
4
5import java.awt.*;
6import java.awt.event.*;
7import java.util.*;
8import javax.swing.*;
9import layout.TableLayout;
10import layout.TableLayoutConstraints;
11import support.GeneralDialog;
12
13
14
15public class RadTool extends Frame implements ActionListener
16{
17
18
19
20 private JTextField textfieldColumnNumber;
21 private JTextField textfieldColumnSize;
22 private JTextField textfieldRowNumber;
23 private JTextField textfieldRowSize;
24 private JButton buttonAddColumn;
25 private JButton buttonRemoveColumn;
26 private JButton buttonResizeColumn;
27 private JButton buttonAddRow;
28 private JButton buttonRemoveRow;
29 private JButton buttonResizeRow;
30 private JButton buttonShowLayout;
31 private JButton buttonGenerateCode;
32 private TextArea textArea;
33 private JPanel panel;
34 private TableLayout layout;
35 private ArrayList columnHeader;
36 private ArrayList rowHeader;
37 private ArrayList boxList;
38
39
40
41 public static void main (String args[])
42 {
43 new RadTool();
44 }
45
46
47
48 /**
49 * Constructs the user interface.
50 */
51
52 public RadTool()
53 {
54 // Create frame
55 super("Example of Dynamic Rows and Columns");
56
57 // Create a TableLayout for the frame
58 double b = 10;
59 double s = 5;
60 double f = TableLayout.FILL;
61 double p = TableLayout.PREFERRED;
62 double size[][] =
63 {{b, p, s, p, s, p, s, p, s, p, s, p, s, p, f, b},
64 {f, s, p, s, p, s, 100, s, p, b}};
65
66 setLayout (new TableLayout(size));
67
68 // Create static controls
69 JLabel labelColumn = new JLabel("Column");
70 JLabel labelColumnSize = new JLabel("Size");
71 textfieldColumnNumber = new JTextField(2);
72 textfieldColumnSize = new JTextField(2);
73 buttonAddColumn = new JButton("Add");
74 buttonRemoveColumn = new JButton("Remove");
75 buttonResizeColumn = new JButton("Resize");
76
77 JLabel labelRow = new JLabel("Row");
78 JLabel labelRowSize = new JLabel("Size");
79 textfieldRowNumber = new JTextField(2);
80 textfieldRowSize = new JTextField(5);
81 buttonAddRow = new JButton("Add");
82 buttonRemoveRow = new JButton("Remove");
83 buttonResizeRow = new JButton("Resize");
84
85 textArea = new TextArea();
86 columnHeader = new ArrayList();
87 rowHeader = new ArrayList();
88 boxList = new ArrayList();
89 buttonShowLayout = new JButton("Show Layout");
90 buttonGenerateCode = new JButton("Generate Code");
91
92 // Add static controls
93 add (labelColumn, " 1, 2");
94 add (textfieldColumnNumber, " 3, 2");
95 add (labelColumnSize, " 5, 2");
96 add (textfieldColumnSize, " 7, 2");
97 add (buttonAddColumn, " 9, 2");
98 add (buttonRemoveColumn, "11, 2");
99 add (buttonResizeColumn, "13, 2");
100
101 add (labelRow, " 1, 4");
102 add (textfieldRowNumber, " 3, 4");
103 add (labelRowSize, " 5, 4");
104 add (textfieldRowSize, " 7, 4");
105 add (buttonAddRow, " 9, 4");
106 add (buttonRemoveRow, "11, 4");
107 add (buttonResizeRow, "13, 4");
108
109 add (textArea, "1, 6, 14, 6");
110 add (buttonShowLayout, "1, 8, 7, 6");
111 add (buttonGenerateCode, "9, 8, 12, 8");
112
113 // Listen for button events
114 buttonAddColumn.addActionListener (this);
115 buttonRemoveColumn.addActionListener (this);
116 buttonResizeColumn.addActionListener (this);
117 buttonAddRow.addActionListener (this);
118 buttonRemoveRow.addActionListener (this);
119 buttonResizeRow.addActionListener (this);
120 buttonShowLayout.addActionListener (this);
121 buttonGenerateCode.addActionListener (this);
122
123 // Add a panel for RadTool controls
124 panel = new JPanel();
125 panel.setBackground (Color.white);
126 add (panel, "0, 0, 15, 0");
127
128 // Create the layout manager for the panel
129 double size2[][] = {{p, -1}, {p, -1}};
130 layout = new TableLayout(size2);
131 panel.setLayout (layout);
132 updateHeader();
133 updateBox();
134
135 // Allow user to close the window to terminate the program
136 addWindowListener
137 (new WindowAdapter()
138 {
139 public void windowClosing (WindowEvent e)
140 {
141 System.exit (0);
142 }
143 }
144 );
145
146 // Show frame
147 setBackground (Color.lightGray);
148 setBounds (100, 100, 500, 400);
149 show();
150 }
151
152
153
154 /**
155 * Handles all action events.
156 */
157
158 public void actionPerformed (ActionEvent e)
159 {
160 // Get row and column information from text fields
161 int row = getInt(textfieldRowNumber);
162 int col = getInt(textfieldColumnNumber);
163 double rowSize = getDouble(textfieldRowSize);
164 double colSize = getDouble(textfieldColumnSize);
165
166 // Get source of the event
167 Object source = e.getSource();
168
169 try
170 {
171 // Update layout
172 if (source == buttonAddColumn)
173 layout.insertColumn (col, colSize);
174 else if (source == buttonRemoveColumn)
175 layout.deleteColumn (col);
176 else if (source == buttonResizeColumn)
177 layout.setColumn (col, colSize);
178 else if (source == buttonAddRow)
179 layout.insertRow (row, rowSize);
180 else if (source == buttonRemoveRow)
181 layout.deleteRow (row);
182 else if (source == buttonResizeRow)
183 layout.setRow (row, rowSize);
184
185 // Update headers, etc. to reflect layout's change
186 updateHeader();
187 updateBox();
188
189 // Layout and repaint panel since the layout has changed
190 panel.doLayout();
191 panel.repaint();
192
193 // Update layout's description
194 textArea.setText(layout.toString());
195
196 // Generate code if desired
197 if (source == buttonGenerateCode)
198 generateCode();
199 }
200 catch (Throwable error)
201 {
202 error.printStackTrace();
203 textArea.setText (error.toString());
204 }
205 }
206
207
208
209 /**
210 * Converts the text in a text field to an integer.
211 */
212
213 private int getInt (JTextField field)
214 {
215 int value = 0;
216
217 try
218 {
219 value = Integer.parseInt(field.getText());
220 }
221 catch (NumberFormatException e) {}
222
223 return value;
224 }
225
226
227
228 /**
229 * Converts the text in a text field to a double.
230 */
231
232 private double getDouble (JTextField field)
233 {
234 double value = -1.0;
235
236 try
237 {
238 value = Double.parseDouble(field.getText());
239 }
240 catch (NumberFormatException e) {}
241
242 return value;
243 }
244
245
246
247 /**
248 * Updates all the row and columns headers by bruce force. The headers
249 * are removed and then recreated based on the current layout configuration.
250 */
251
252 private void updateHeader ()
253 {
254 TableLayoutConstraints c = new TableLayoutConstraints
255 (0, 0, 0, 0, TableLayout.FULL, TableLayout.FULL);
256
257 double size[] = layout.getColumn();
258
259 for (int i = columnHeader.size() - 1; i >= 0; i--)
260 {
261 JButton header = (JButton) columnHeader.remove(i);
262 panel.remove(header);
263 }
264
265 for (int i = 0; i < size.length; i++)
266 {
267 c.col1 = c.col2 = i;
268 JButton header = new JButton("" + i + ": " + size[i]);
269 columnHeader.add(i, header);
270 panel.add (header, c);
271 }
272
273 c.col1 = c.col2 = 0;
274 size = layout.getRow();
275
276 for (int i = rowHeader.size() - 1; i >= 0; i--)
277 {
278 JButton header = (JButton) rowHeader.remove(i);
279 panel.remove(header);
280 }
281
282 for (int i = 0; i < size.length; i++)
283 {
284 c.row1 = c.row2 = i;
285 JButton header = new JButton("" + i + ": " + size[i]);
286 rowHeader.add(i, header);
287 panel.add (header, c);
288 }
289 }
290
291
292
293 /**
294 * Updates the boxes used to illustrate cells. This is a brute force,
295 * unoptimized method.
296 */
297
298 private void updateBox ()
299 {
300 for (int i = boxList.size() - 1; i >= 0; i--)
301 {
302 Box box = (Box) boxList.remove(i);
303 panel.remove(box);
304 }
305
306 TableLayoutConstraints constraint = new TableLayoutConstraints
307 (0, 0, 0, 0, TableLayout.FULL, TableLayout.FULL);
308
309 double col[] = layout.getColumn();
310 double row[] = layout.getRow();
311
312 for (int c = 1; c < col.length; c++)
313 for (int r = 1; r < row.length; r++)
314 {
315 constraint.col1 = constraint.col2 = c;
316 constraint.row1 = constraint.row2 = r;
317 Box box = new Box();
318 panel.add (box, constraint);
319 }
320 }
321
322
323
324 /**
325 * Generates code based on the current layout and Smiley controls.
326 */
327
328 private void generateCode ()
329 {
330 String code =
331 "import java.awt.*;\n" +
332 "import java.awt.event.*;\n" +
333 "import layout.TableLayout;\n\n" +
334
335 "public class MyClass\n" +
336 "{\n\n" +
337
338 " public static void main (String args[])\n" +
339 " {\n" +
340 " Frame frame = new Frame(\"MyTitle\");\n" +
341 " frame.setBounds (100, 100, 300, 300);\n\n" +
342
343 " double size[][] =\n" +
344 " {{";
345
346 double size[] = layout.getColumn();
347
348 if (size.length > 0)
349 code += size[0];
350
351 for (int i = 1; i < size.length; i++)
352 code += ", " + size[i];
353
354 code += "}, // Columns\n" +
355 " {";
356
357 size = layout.getRow();
358
359 if (size.length > 0)
360 code += size[0];
361
362 for (int i = 1; i < size.length; i++)
363 code += ", " + size[i];
364
365 code += "}}; // Rows\n\n" +
366 " frame.setLayout (new TableLayout(size));\n\n" +
367 " Button button;\n";
368
369 Component component[] = panel.getComponents();
370
371 for (int i = 0; i < component.length; i++)
372 {
373 if (component[i] instanceof Smiley)
374 {
375 TableLayoutConstraints c = layout.getConstraints(component[i]);
376 String constraint = "" + c.col1 + ", " + c.row1 + ", ";
377
378 if ((c.col1 == c.col2) && (c.row1 == c.row2))
379 {
380 String h[] = {"L", "C", "F", "R"};
381 String v[] = {"T", "C", "F", "B"};
382 constraint += h[c.hAlign] + ", " + v[c.vAlign];
383 }
384 else
385 constraint += c.col2 + ", " + c.row2;
386
387 code +=
388 " button = new Button(\"" + constraint + "\");\n" +
389 " frame.add (button, \"" + constraint + "\");\n";
390 }
391 }
392
393 code +=
394 "\n" +
395 " frame.addWindowListener\n" +
396 " (new WindowAdapter()\n" +
397 " {\n" +
398 " public void windowClosing (WindowEvent e)\n" +
399 " {\n" +
400 " System.exit (0);\n" +
401 " }\n" +
402 " }\n" +
403 " );\n\n" +
404 " frame.show();\n" +
405 " }\n" +
406 "}\n";
407
408 textArea.setText (code);
409 }
410
411
412
413 /**
414 * This inner class is a component that looks like a box.
415 */
416
417 public class Box extends Component implements MouseListener
418 {
419 public Box ()
420 {
421 super();
422 addMouseListener(this);
423 }
424
425 public void update (Graphics g)
426 {
427 paint (g);
428 }
429
430 public void paint (Graphics g)
431 {
432 Dimension d = getSize();
433 g.setColor (Color.black);
434 g.drawRect (0, 0, d.width - 1, d.height - 1);
435 }
436
437 public void mouseExited(java.awt.event.MouseEvent mouseEvent) {}
438 public void mousePressed(java.awt.event.MouseEvent mouseEvent) {}
439 public void mouseClicked(java.awt.event.MouseEvent mouseEvent) {}
440 public void mouseEntered(java.awt.event.MouseEvent mouseEvent) {}
441
442 public void mouseReleased(java.awt.event.MouseEvent mouseEvent)
443 {
444 TableLayoutConstraints c = layout.getConstraints(this);
445 Smiley smiley = new Smiley();
446 Container container = getParent();
447 container.add(smiley, c, 0);
448 container.doLayout();
449 }
450 }
451
452
453
454 /**
455 * This inner class is a component that looks like a smiley face.
456 */
457
458 public class Smiley extends Component implements MouseListener
459 {
460 public Smiley ()
461 {
462 super();
463 addMouseListener (this);
464 }
465
466 public Dimension getPreferredSize ()
467 {
468 return new Dimension(64, 64);
469 }
470
471 public void update (Graphics g)
472 {
473 paint (g);
474 }
475
476 public void paint (Graphics g)
477 {
478 Dimension d = getSize();
479 int width_1_8 = d.width >> 3;
480 int width_7_8 = d.width - width_1_8;
481 int width_1_4 = d.width >> 2;
482 int width_3_4 = d.width - width_1_4;
483 int width_5_8 = width_3_4 - width_1_8;
484 int width_1_2 = d.width >> 1;
485 int height_1_8 = d.height >> 3;
486 int height_7_8 = d.height - height_1_8;
487 int height_1_4 = d.height >> 2;
488 int height_3_4 = d.height - height_1_4;
489 int height_5_8 = height_7_8 - height_1_4;
490
491 g.setColor (Color.yellow);
492 g.fillArc (0, 0, d.width - 1, d.height - 1, 0, 360);
493 g.setColor (Color.blue);
494 g.fillArc (width_1_4, height_1_4, width_1_8, height_1_8, 0, 360);
495 g.fillArc (width_5_8, height_1_4, width_1_8, height_1_8, 0, 360);
496 g.setColor (Color.red);
497 g.fillArc (width_1_4, height_5_8, width_1_2, height_1_4, 180, 180);
498 }
499
500 public void mouseExited(java.awt.event.MouseEvent mouseEvent) {}
501 public void mousePressed(java.awt.event.MouseEvent mouseEvent) {}
502 public void mouseClicked(java.awt.event.MouseEvent mouseEvent) {}
503 public void mouseEntered(java.awt.event.MouseEvent mouseEvent) {}
504
505 public void mouseReleased(java.awt.event.MouseEvent mouseEvent)
506 {
507 // Get constraints applied to this Smiley
508 TableLayoutConstraints c = layout.getConstraints(this);
509
510 // Create controls
511 Panel panel = new Panel();
512
513 int numRow = layout.getNumRow();
514 int numColumn = layout.getNumColumn();
515
516 Choice choiceCol1 = new Choice();
517 Choice choiceCol2 = new Choice();
518 Choice choiceRow1 = new Choice();
519 Choice choiceRow2 = new Choice();
520
521 for (int i = 1; i < numColumn; i++)
522 {
523 choiceCol1.add ("" + i);
524 choiceCol2.add ("" + i);
525 }
526
527 for (int i = 1; i < numRow; i++)
528 {
529 choiceRow1.add ("" + i);
530 choiceRow2.add ("" + i);
531 }
532
533 choiceCol1.select (c.col1 - 1);
534 choiceCol2.select (c.col2 - 1);
535 choiceRow1.select (c.row1 - 1);
536 choiceRow2.select (c.row2 - 1);
537
538 Choice choiceAlignH = new Choice();
539 choiceAlignH.add ("Left");
540 choiceAlignH.add ("Center");
541 choiceAlignH.add ("Full");
542 choiceAlignH.add ("Right");
543 choiceAlignH.select (c.hAlign);
544
545 Choice choiceAlignV = new Choice();
546 choiceAlignV.add ("Top");
547 choiceAlignV.add ("Center");
548 choiceAlignV.add ("Full");
549 choiceAlignV.add ("Bottom");
550 choiceAlignV.select (c.vAlign);
551
552 JLabel labelCol1 = new JLabel("Column 1");
553 JLabel labelCol2 = new JLabel("Column 2");
554 JLabel labelRow1 = new JLabel("Row 1");
555 JLabel labelRow2 = new JLabel("Row 2");
556 JLabel labelAlignH = new JLabel("Horizontal Justification");
557 JLabel labelAlignV = new JLabel("Vertical Justification");
558 labelAlignH.setHorizontalAlignment (JLabel.RIGHT);
559 labelAlignV.setHorizontalAlignment (JLabel.RIGHT);
560
561 // Create layout
562 double f = TableLayout.FILL;
563 double p = TableLayout.PREFERRED;
564 double s = 10;
565
566 double size[][] =
567 {{f, p, s, p, s, p, s, p, f},
568 {p, s, p, s, p, s, p, s, p, s, p}};
569
570 TableLayout panelLayout = new TableLayout(size);
571 panel.setLayout (panelLayout);
572
573 // Add controls
574 panel.add (labelCol1, "1, 0, R, B");
575 panel.add (choiceCol1, "3, 0, L, B");
576 panel.add (labelRow1, "5, 0, R, B");
577 panel.add (choiceRow1, "7, 0, L, B");
578 panel.add (labelCol2, "1, 2, R, B");
579 panel.add (choiceCol2, "3, 2, L, B");
580 panel.add (labelRow2, "5, 2, R, B");
581 panel.add (choiceRow2, "7, 2, L, B");
582 panel.add (labelAlignH, "0, 4, 3, 4");
583 panel.add (choiceAlignH, "5, 4, 7, 4");
584 panel.add (labelAlignV, "0, 6, 3, 6");
585 panel.add (choiceAlignV, "5, 6, 7, 6");
586
587 // Prompt user
588 String button[] = {"Update Smiley", "Remove Smiley", "Cancel"};
589
590 GeneralDialog dialog = new GeneralDialog
591 (RadTool.this, "Update Smiley", "", button, null, panel);
592
593 String answer = dialog.promptUser();
594
595 // Update constraints applied to this Smiley
596 if (answer.equals("Update Smiley"))
597 {
598 // Get columns
599 int col1 = choiceCol1.getSelectedIndex() + 1;
600 int col2 = choiceCol2.getSelectedIndex() + 1;
601 int row1 = choiceRow1.getSelectedIndex() + 1;
602 int row2 = choiceRow2.getSelectedIndex() + 1;
603
604 // Make sure col1 < col2
605 if (col1 > col2)
606 {
607 int temp = col1;
608 col1 = col2;
609 col2 = temp;
610 }
611
612 // Make sure row1 < row2
613 if (row1 > row2)
614 {
615 int temp = row1;
616 row1 = row2;
617 row2 = temp;
618 }
619
620 // Apply new constraints
621 c.col1 = col1;
622 c.col2 = col2;
623 c.row1 = row1;
624 c.row2 = row2;
625 c.hAlign = choiceAlignH.getSelectedIndex();
626 c.vAlign = choiceAlignV.getSelectedIndex();
627 layout.setConstraints (this, c);
628
629 // Repaint and layout container since layout has changed
630 Container container = getParent();
631 container.doLayout();
632 container.repaint();
633 }
634 // Remove Smiley
635 else if (answer.equals("Remove Smiley"))
636 {
637 Container container = getParent();
638 container.remove (this);
639 container.doLayout();
640 container.repaint();
641 }
642 }
643 }
644
645
646
647}