505d0b338f5a5b85537757a3e3c8a2753609cac4
[jalviewjs.git] / src / javajs / awt / Component.java
1 package javajs.awt;
2
3 import javajs.api.GenericColor;
4 import javajs.util.CU;
5
6 abstract public class Component {
7
8   protected boolean visible;  
9   protected boolean enabled = true;
10   protected String text;    
11   protected String name;
12   protected int width;
13   protected int height;
14   protected String id;
15
16   protected Object parent;
17   
18   public void setParent(Object p) {
19     parent = p;
20   }
21
22   protected Object mouseListener;
23
24   private GenericColor bgcolor;
25
26   protected Component(String type) {
27     id = newID(type);
28     if (type == null)
29       return;
30     /**
31      * @j2sNative
32      *            SwingController.register(this, type);
33      */
34     {
35     }
36
37   }
38   
39   public static String newID(String type) {
40     return type + ("" + Math.random()).substring(3, 10);
41   }
42
43   abstract public String toHTML();
44   
45   public void setBackground(GenericColor color) {
46     bgcolor = color;
47   }
48
49   public void setText(String text) {
50     this.text = text;
51     /**
52      * @j2sNative
53      * 
54      * SwingController.setText(this);
55      * 
56      */
57     {
58     }
59   }
60
61   public void setName(String name) {
62     this.name = name;
63   }
64
65   public String getName() {
66     return name;
67   }
68   
69   public Object getParent() {
70     return parent;
71   }
72   
73   public void setPreferredSize(Dimension dimension) {
74     this.width = dimension.width;
75     this.height = dimension.height;   
76   }
77
78   public void addMouseListener(Object listener) {
79     mouseListener = listener;
80   }
81
82   public String getText() {
83     return text;
84   }
85
86   public boolean isEnabled() {
87     return enabled;
88   }
89   
90   public void setEnabled(boolean enabled) {
91     this.enabled = enabled;
92     /**
93      * @j2sNative
94      * 
95      * SwingController.setEnabled(this);
96      * 
97      */
98     {}
99   }
100
101   public boolean isVisible() {
102     return visible;
103   }
104
105   public void setVisible(boolean visible) {
106     this.visible = visible;
107     /**
108      * @j2sNative
109      * 
110      * SwingController.setVisible(this);
111      * 
112      */
113     {}
114   }
115
116   public int getHeight() {
117     return height;
118   }
119
120   public int getWidth() {
121     return width;
122   }
123
124   protected int minWidth = 30;
125   protected int minHeight = 30;
126
127   public void setMinimumSize(Dimension d) {
128     minWidth = d.width;
129     minHeight = d.height;
130   }
131
132   public int getSubcomponentWidth() {
133     return width;
134   }
135   
136   public int getSubcomponentHeight() {
137     return height;
138   }
139   
140   protected int renderWidth;
141   protected int renderHeight;
142
143   protected String getCSSstyle(int defaultPercentW, int defaultPercentH) {
144     int width = (renderWidth > 0 ? renderWidth : getSubcomponentWidth());
145     int height = (renderHeight > 0 ? renderHeight : getSubcomponentHeight());
146     return (width > 0 ? "width:" + width +"px;" : defaultPercentW > 0 ? "width:"+defaultPercentW+"%;" : "")
147     + (height > 0 ?"height:" + height + "px;" : defaultPercentH > 0 ? "height:"+defaultPercentH+"%;" : "")
148     + (bgcolor == null ? "" : "background-color:" + CU.toCSSString(bgcolor) + ";");
149   }
150   
151   public void repaint() {
152     // for inheritance
153   }
154
155 }