JAL-1807 update
[jalviewjs.git] / src / javajs / swing / JPanel.java
1 package javajs.swing;
2
3 import javajs.awt.BorderLayout;
4 import javajs.awt.LayoutManager;
5 import javajs.util.SB;
6
7 public class JPanel extends JComponent {
8
9         //private LayoutManager layoutManager;
10
11         private Grid grid;
12
13         private int nElements;
14         private JComponent last;
15         
16         
17         /**
18          * @param manager  ignored. we just use the layout designations with a grid
19          */
20         public JPanel(LayoutManager manager) {
21                 super("JP");
22                 //this.layoutManager = manager;
23                 grid = new Grid(10,10);
24         }
25
26         public void add(JComponent btn, Object c) {
27                 last = (++nElements == 1 ? btn : null);
28                 if (c instanceof String) {
29                         if (c.equals(BorderLayout.NORTH))
30                                 c = new GridBagConstraints(0, 0, 3, 1, 0, 0, GridBagConstraints.CENTER,
31                                                 0, null, 0, 0);
32                         else if (c.equals(BorderLayout.SOUTH))
33                                 c = new GridBagConstraints(0, 2, 3, 1, 0, 0, GridBagConstraints.CENTER,
34                                                 0, null, 0, 0);
35                         else if (c.equals(BorderLayout.EAST))
36                                 c = new GridBagConstraints(2, 1, 1, 1, 0, 0, GridBagConstraints.EAST,
37                                                 0, null, 0, 0);
38                         else if (c.equals(BorderLayout.WEST))
39                                 c = new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.WEST,
40                                                 0, null, 0, 0);
41                         else
42                                 c = new GridBagConstraints(1, 1, 1, 1, 0, 0, GridBagConstraints.CENTER,
43                                                 0, null, 0, 0);
44                 }
45                 grid.add(btn, (GridBagConstraints) c);
46         }
47         
48         @Override
49         public String toHTML() {
50                 if (last != null) {
51                         // only one element
52                         grid = new Grid(1, 1);
53                         grid.add(last, new GridBagConstraints(0, 0, 1, 1, 0, 0,
54                                         GridBagConstraints.CENTER, 0, null, 0, 0));
55                         last = null;
56                 }
57                 SB sb = new SB();
58                 sb.append("\n<div id='" + id + "' class='JPanel' style='"
59                                 + getCSSstyle(100, 100) + "'>\n");
60                 sb.append("\n<span id='" + id + "_minimizer' style='width:" + minWidth
61                                 + "px;height:" + minHeight + "px;'>");
62                 sb.append(grid.toHTML(id));
63                 sb.append("</span>");
64                 sb.append("\n</div>\n");
65                 return sb.toString();
66         }
67 }