src/main/java/xelfi/design/components/xawt/XTimer.java
author Jaroslav Tulach <jaroslav.tulach@xelfi.cz>
Tue, 17 Jan 2017 21:08:47 +0100
changeset 6 947ce1156833
parent 0 189280700bc7
permissions -rw-r--r--
Renaming setName to setDesignName and setSize to setDesignSize to avoid clash with added methods of java.awt.Component that return void
     1 /**
     2 * Xelfi
     3 */
     4 
     5 package xelfi.design.components.xawt;
     6 
     7 import java.awt.*;
     8 import java.io.*;
     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;
    14 
    15 /**
    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
    26 */
    27 
    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;
    35 
    36 	private int delayTime = 1000;
    37 	private boolean cycle = true;
    38 	private boolean autoStart = true;
    39 	
    40 //////////////////
    41 // constructors //
    42 //////////////////
    43 
    44 	public XTimer() { 
    45 		loadIcon();
    46 	}
    47 
    48 	public void setParentForm(XelfiDesignForm form) {
    49 		parentForm = form;
    50 	}
    51 
    52 	public XelfiDesignForm getParentForm() {
    53 		return parentForm;
    54 	}
    55 
    56 	public String getName() { return name; }
    57 	
    58 	public int setDesignName(String aName) { name = aName; return XFDesignConstants.RSP_OK; }
    59 	
    60 	public int getPropertyCount() { return propertyCount;	}
    61 	
    62 	public String[] getPropertyNames() {
    63 		String list[] = new String[propertyCount];
    64 		list[0] = "Delay Time";
    65 		list[1] = "Cycle";
    66 		list[2] = "Auto Start";
    67 		return list;
    68 	}	
    69 
    70 	public Object getProperty(int index) throws XFPropertyIndexOutOfBoundsException { 
    71 		switch (index) {
    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();
    76 		}
    77 	}
    78 			
    79 	public String getPropertyString(int index) throws 
    80 		XFPropertyIndexOutOfBoundsException { 
    81 		switch (index) {
    82 			case 0: return XIntegerPropertyType.viewString(getProperty(index)); 
    83 			case 1:
    84 			case 2: return XBooleanPropertyType.viewString(getProperty(index));
    85 			default: throw new XFPropertyIndexOutOfBoundsException();
    86 		}
    87 	}
    88 
    89 	public int getPropertyViewerType(int index) throws 
    90 		XFPropertyIndexOutOfBoundsException {
    91 		switch (index) {
    92 			case 0: return XFDesignConstants.PVT_INPUT_INT; 
    93 			case 1: 
    94 			case 2: return XFDesignConstants.PVT_CHECK;
    95 			default: throw new XFPropertyIndexOutOfBoundsException();
    96 		}
    97 	}
    98 
    99 	public String[] getPropertyValueNames(int index) throws 
   100 		XFPropertyIndexOutOfBoundsException {
   101 		switch (index) {
   102 			default: throw new XFPropertyIndexOutOfBoundsException();
   103 		}
   104 	}
   105 
   106 	public Object[] getPropertyValueValues(int index) throws 
   107 		XFPropertyIndexOutOfBoundsException {
   108 		switch (index) {
   109 			default: throw new XFPropertyIndexOutOfBoundsException();
   110 		}
   111 	}
   112 
   113 
   114 	public int setProperty(int index, Object value) throws 	
   115 		XFPropertyIndexOutOfBoundsException,
   116 		XFPropertyBadValueTypeException 
   117 	{ 
   118 		int ret = XFDesignConstants.RSP_OK;
   119 		int regen = XFDesignConstants.GEN_INIT;
   120 		
   121 		switch (index) { 
   122 			case 0: if (!(value instanceof Integer)) throw new XFPropertyBadValueTypeException();
   123 					delayTime = ((Integer)value).intValue();
   124 					if (delayTime <=0) {
   125 						delayTime = 1000;
   126 						ret = XFDesignConstants.RSP_DIFF_VALUE;
   127 					}
   128 					break;
   129 			case 1: if (!(value instanceof Boolean)) throw new XFPropertyBadValueTypeException();
   130 					cycle = ((Boolean)value).booleanValue();
   131 					break;
   132 			case 2: if (!(value instanceof Boolean)) throw new XFPropertyBadValueTypeException();
   133 					autoStart = ((Boolean)value).booleanValue();
   134 					break;
   135 			default: throw new XFPropertyIndexOutOfBoundsException();
   136 		}
   137 		if (parentForm != null)
   138 			parentForm.regenerateCode(this, regen);
   139 		
   140 		return ret;
   141 	}
   142 			
   143 	public void specialPropertyInput(Frame parent, int index) throws 	
   144 		XFPropertyIndexOutOfBoundsException, 
   145 		XFPropertyNoSpecialInputException { 
   146 		if ((index < 0) || (index >= propertyCount)) throw new XFPropertyIndexOutOfBoundsException();
   147 		switch (index) { 
   148 			default: throw new XFPropertyNoSpecialInputException();
   149 		}
   150 	}
   151 
   152 	
   153 	public boolean parentUpdate() { return false; }
   154 
   155 	public int getEventCount() { return eventCount; }
   156 	
   157 	public String[] getEventNames() {
   158 		String list[] = new String[eventCount];
   159 		list[0] = new String("OnTime"); 
   160 		return list;
   161 	}
   162 
   163 	public String getEvent(int index) throws 
   164 		XFPropertyIndexOutOfBoundsException {
   165 		if ((index>=0) && (index<eventCount)) {
   166 			if (events[index] == null)
   167 				return null;
   168 			else
   169 				return new String(events[index]); 
   170 		}
   171 		else throw new XFPropertyIndexOutOfBoundsException();
   172 	}		
   173 		
   174 	public void setEvent(int index, String value) throws 
   175 		XFPropertyIndexOutOfBoundsException {
   176 		if ((index>=0) && (index<eventCount)) {
   177 			events[index] = value;
   178 		}
   179 		else throw new XFPropertyIndexOutOfBoundsException();
   180 	}		
   181 
   182 	public Image getIcon() {
   183 		return icon; 
   184 	}
   185 
   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));
   191 	}
   192 	
   193 	public String getClassName() { return "Timer"; }
   194 	
   195 	public String[] neededImports() {
   196 		String list[] = new String[1];
   197 		list[0] = "xelfi.awt.Timer";
   198 		return list;
   199 	}
   200 
   201 	public String generateCode(int what) {
   202 		String gen = "";
   203 		switch (what) {
   204 			case XFDesignConstants.GEN_VARIABLE: 
   205 				gen = getClassName()+" "+ name + ";\n";
   206 				break;
   207 			case XFDesignConstants.GEN_INIT: 
   208 				gen = name + " = new " + getClassName() + "(this, " + delayTime + ", "+cycle+");\n";
   209 				break;
   210 			case XFDesignConstants.GEN_SETPROPS:
   211 				if (autoStart) 
   212 					gen = name + ".start();\n";
   213 				break;			
   214 		}
   215 		return gen;
   216 	}
   217 
   218 	public String generateEventCondition(int index) throws 
   219 	XFPropertyIndexOutOfBoundsException {
   220 		if (index==0){
   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)
   228 		}
   229 		else throw new XFPropertyIndexOutOfBoundsException();
   230 	}
   231 	
   232 	public void setSelection(int aSelection) {}
   233 
   234 	public int getSelection() { return 0; }
   235 
   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);
   241 	}
   242 	
   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);
   248 	}
   249 }