Renaming setName to setDesignName and setSize to setDesignSize to avoid clash with added methods of java.awt.Component that return void
5 package xelfi.design.components.xawt;
9 import xelfi.design.components.*;
10 import xelfi.design.forms.XelfiDesignForm;
11 import xelfi.design.components.property.*;
12 import xelfi.design.util.*;
13 import xelfi.util.XelfiPath;
16 * This is the first non-visual component (except menu).
17 * It is a design class for class Timer, which is class that once or periodically sends
18 * message to its owner with given period time.
19 * It has 3 properties:
20 * DelayTime ... time in ms for the timer
21 * Cycle ... if false then the timer ticks oly once/ if true then it works periodically
22 * AutoStart ... if true then the timer is strted right after it is created
23 * (or better the code for starting it is generated right after the creation code)
24 * @version 2.13, 15 Mar 1997
25 * @author Ian Formanek, Petr Hamernik
28 public class XTimer implements XelfiDesignComponent {
29 private static int propertyCount = 3;
30 private static int eventCount = 1;
31 private String events[] = new String[eventCount];
32 private String name = "";
33 private Image icon = null;
34 private XelfiDesignForm parentForm = null;
36 private int delayTime = 1000;
37 private boolean cycle = true;
38 private boolean autoStart = true;
48 public void setParentForm(XelfiDesignForm form) {
52 public XelfiDesignForm getParentForm() {
56 public String getName() { return name; }
58 public int setDesignName(String aName) { name = aName; return XFDesignConstants.RSP_OK; }
60 public int getPropertyCount() { return propertyCount; }
62 public String[] getPropertyNames() {
63 String list[] = new String[propertyCount];
64 list[0] = "Delay Time";
66 list[2] = "Auto Start";
70 public Object getProperty(int index) throws XFPropertyIndexOutOfBoundsException {
72 case 0: return new Integer(delayTime);
73 case 1: return new Boolean(cycle);
74 case 2: return new Boolean(autoStart);
75 default: throw new XFPropertyIndexOutOfBoundsException();
79 public String getPropertyString(int index) throws
80 XFPropertyIndexOutOfBoundsException {
82 case 0: return XIntegerPropertyType.viewString(getProperty(index));
84 case 2: return XBooleanPropertyType.viewString(getProperty(index));
85 default: throw new XFPropertyIndexOutOfBoundsException();
89 public int getPropertyViewerType(int index) throws
90 XFPropertyIndexOutOfBoundsException {
92 case 0: return XFDesignConstants.PVT_INPUT_INT;
94 case 2: return XFDesignConstants.PVT_CHECK;
95 default: throw new XFPropertyIndexOutOfBoundsException();
99 public String[] getPropertyValueNames(int index) throws
100 XFPropertyIndexOutOfBoundsException {
102 default: throw new XFPropertyIndexOutOfBoundsException();
106 public Object[] getPropertyValueValues(int index) throws
107 XFPropertyIndexOutOfBoundsException {
109 default: throw new XFPropertyIndexOutOfBoundsException();
114 public int setProperty(int index, Object value) throws
115 XFPropertyIndexOutOfBoundsException,
116 XFPropertyBadValueTypeException
118 int ret = XFDesignConstants.RSP_OK;
119 int regen = XFDesignConstants.GEN_INIT;
122 case 0: if (!(value instanceof Integer)) throw new XFPropertyBadValueTypeException();
123 delayTime = ((Integer)value).intValue();
126 ret = XFDesignConstants.RSP_DIFF_VALUE;
129 case 1: if (!(value instanceof Boolean)) throw new XFPropertyBadValueTypeException();
130 cycle = ((Boolean)value).booleanValue();
132 case 2: if (!(value instanceof Boolean)) throw new XFPropertyBadValueTypeException();
133 autoStart = ((Boolean)value).booleanValue();
135 default: throw new XFPropertyIndexOutOfBoundsException();
137 if (parentForm != null)
138 parentForm.regenerateCode(this, regen);
143 public void specialPropertyInput(Frame parent, int index) throws
144 XFPropertyIndexOutOfBoundsException,
145 XFPropertyNoSpecialInputException {
146 if ((index < 0) || (index >= propertyCount)) throw new XFPropertyIndexOutOfBoundsException();
148 default: throw new XFPropertyNoSpecialInputException();
153 public boolean parentUpdate() { return false; }
155 public int getEventCount() { return eventCount; }
157 public String[] getEventNames() {
158 String list[] = new String[eventCount];
159 list[0] = new String("OnTime");
163 public String getEvent(int index) throws
164 XFPropertyIndexOutOfBoundsException {
165 if ((index>=0) && (index<eventCount)) {
166 if (events[index] == null)
169 return new String(events[index]);
171 else throw new XFPropertyIndexOutOfBoundsException();
174 public void setEvent(int index, String value) throws
175 XFPropertyIndexOutOfBoundsException {
176 if ((index>=0) && (index<eventCount)) {
177 events[index] = value;
179 else throw new XFPropertyIndexOutOfBoundsException();
182 public Image getIcon() {
186 private void loadIcon() {
187 String sep = new String(System.getProperty("file.separator"));
188 StringBuffer s = new StringBuffer(XelfiPath.get() + sep +
189 "design" + sep + "components" + sep + "xawt" + sep + "timer.gif");
190 icon = Toolkit.getDefaultToolkit().getImage(new String(s));
193 public String getClassName() { return "Timer"; }
195 public String[] neededImports() {
196 String list[] = new String[1];
197 list[0] = "xelfi.awt.Timer";
201 public String generateCode(int what) {
204 case XFDesignConstants.GEN_VARIABLE:
205 gen = getClassName()+" "+ name + ";\n";
207 case XFDesignConstants.GEN_INIT:
208 gen = name + " = new " + getClassName() + "(this, " + delayTime + ", "+cycle+");\n";
210 case XFDesignConstants.GEN_SETPROPS:
212 gen = name + ".start();\n";
218 public String generateEventCondition(int index) throws
219 XFPropertyIndexOutOfBoundsException {
221 return XFDesignOptions.eventVarName+".target == "+name;
222 // Note: if the component had multiple events then the condition would look like:
223 // evt.target == name && "NameOfEvent".equalsIgnoreCase((String)evt.arg)
224 // where evt is (in this example only) result of XFDesignOptions.eventVarName
225 // and the NameOfEvent differentiates between different events
226 // This scheme must be, of course, supported by the component class (it must create the event the way,
227 // that into event.target it puts reference to itself (this) and into event.arg string name of the event)
229 else throw new XFPropertyIndexOutOfBoundsException();
232 public void setSelection(int aSelection) {}
234 public int getSelection() { return 0; }
236 public void loadFromStream(DataInput stream) throws IOException {
237 name = stream.readUTF();
238 delayTime = XIntegerPropertyType.loadFromStream(stream).intValue();
239 cycle = XBooleanPropertyType.loadBFromStream(stream);
240 autoStart = XBooleanPropertyType.loadBFromStream(stream);
243 public void saveToStream(DataOutput stream) throws IOException {
244 stream.writeUTF(name);
245 XIntegerPropertyType.saveToStream(new Integer(delayTime), stream);
246 XBooleanPropertyType.saveToStream(cycle, stream);
247 XBooleanPropertyType.saveToStream(autoStart, stream);