JAL-1807 Bob's first commit -- Applet loaded; needs image
[jalview.git] / src / javajs / swing / AbstractButton.java
1 package javajs.swing;
2
3 import javajs.awt.Component;
4
5 import javajs.api.SC;
6
7 public abstract class AbstractButton extends JComponent implements SC {
8
9   Object itemListener;
10   Object applet;
11   String htmlName;
12   boolean selected;
13   
14   private SC popupMenu;
15
16   private String icon;
17
18   protected AbstractButton(String type) {
19     super(type);
20     enabled = true;
21   }
22   
23   @Override
24   public void setSelected(boolean selected) {
25     this.selected = selected;
26     /**
27      * @j2sNative
28      * 
29      * SwingController.setSelected(this);
30      * 
31      */
32     {
33     }
34   }
35
36   @Override
37   public boolean isSelected() {
38     return selected;
39   }
40   
41   @Override
42   public void addItemListener(Object listener) {
43     itemListener = listener;
44   }
45   
46   @Override
47   public Object getIcon() {
48     return icon;
49   }
50   
51   @Override
52   public void setIcon(Object icon) {
53     this.icon = (String) icon;
54   }
55
56   @Override
57   public void init(String text, Object icon, String actionCommand, SC popupMenu) {
58     this.text = text;
59     this.icon = (String) icon;
60     this.actionCommand = actionCommand;
61     this.popupMenu = popupMenu;
62     /**
63      * @j2sNative
64      * 
65      *  SwingController.initMenuItem(this);
66      *  
67      */
68     {
69     }
70   }
71  
72   public SC getTopPopupMenu() {
73     // note that JMenu.getPopupMenu refers to ITSELF, not the main one)
74     return popupMenu;
75   }
76   
77   @Override
78   public void add(SC item) {
79     addComponent((Component) item);
80   }
81
82   @Override
83   public void insert(SC subMenu, int index) {
84     // JMenu, JPopupMenu only, but implemented here as well
85     // for simplicity
86     insertComponent((Component) subMenu, index);
87   }
88
89   @Override
90   public Object getPopupMenu() {
91     // JMenu only
92     return null;
93   }
94
95   protected String getMenuHTML() {
96     String label = (this.icon != null ? this.icon
97         : this.text != null ? this.text 
98          : null);
99     String s = (label == null ? "" : "<li><a>" + label + "</a>"
100       + htmlMenuOpener("ul"));
101     int n = getComponentCount();
102     if (n > 0)
103       for(int i = 0; i < n; i++)
104         s += getComponent(i).toHTML();
105     if (label != null)
106       s += "</ul></li>";
107     return s;
108   }
109
110   protected String htmlMenuOpener(String type) {
111     return "<" + type + " id=\"" + this.id + "\"" + (this.enabled ? "" : getHtmlDisabled()) + ">";
112   }
113
114   protected String getHtmlDisabled() {
115     return " disabled=\"disabled\"";
116   }  
117
118 }