1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * ident "%Z%%M% %I% %E% SMI" 24 * 25 * Copyright (c) 2000 by Sun Microsystems, Inc. 26 * All rights reserved. 27 */ 28 29 /* 30 * Copyright (C) 1996 Active Software, Inc. 31 * All rights reserved. 32 * 33 * @(#) Op.java 1.15 - last change made 07/25/97 34 */ 35 36 package sunsoft.jws.visual.rt.type; 37 38 import sunsoft.jws.visual.rt.base.*; 39 import java.awt.Event; 40 import java.util.Hashtable; 41 42 /** 43 * Stores a single operation. An operation is an action that will 44 * take place when a particular kind of event comes in on the 45 * component that owns the operation. The operation has two main 46 * subparts, the filter and the action. The filter is used to 47 * determine which events or messages trigger the performance of the 48 * operation's action. This class also simultaneously acts as its own 49 * converter. 50 * 51 * @see OpFilter 52 * @see OpAction 53 * @version 1.15, 07/25/97 54 */ 55 public class Op extends Converter implements Cloneable { 56 57 // Register the converters 58 static { 59 Converter.addConverter(/* NOI18N */ 60 "sunsoft.jws.visual.rt.type.Op", 61 /* NOI18N */"sunsoft.jws.visual.rt.type.Op"); 62 Converter.addConverter(/* NOI18N */ 63 "[Lsunsoft.jws.visual.rt.type.Op;", 64 /* NOI18N */"sunsoft.jws.visual.rt.type.OpArrayConverter"); 65 } 66 67 /** 68 * The Root object under which this operation is held. 69 */ 70 public Root scope; 71 72 /** 73 * The name of the operation. 74 */ 75 public String name; 76 77 /** 78 * The filter of the operation. Used to determine 79 * whether a particular 80 * event should trigger the operation. 81 */ 82 public OpFilter filter; 83 84 /** 85 * The action to be taken by the operation. 86 */ 87 public OpAction action; 88 89 /** 90 * Constructs a new instance. 91 */ Op()92 public Op() { 93 } 94 95 /** 96 * Constructs a new instance given a scope (attribute 97 * manager tree root) 98 * in which to operate. 99 */ Op(Root scope)100 public Op(Root scope) { 101 this.scope = scope; 102 } 103 104 /** 105 * Returns true if the message given matches the 106 * filter for this operation. 107 */ matchMessage(Message msg)108 public boolean matchMessage(Message msg) { 109 if (msg.isAWT) 110 return filter.match(msg, (Event)msg.arg); 111 else 112 return filter.match(msg); 113 } 114 115 /** 116 * Returns true if this operation has code associated with it. 117 * The code will have been compiled in a separate section 118 * of the generated 119 * Ops class and must be specially called upon. 120 */ hasCode()121 public boolean hasCode() { 122 return (action != null && 123 action.actionType == OpAction.CODE); 124 } 125 126 /** 127 * Evokes the action of this operation if the message 128 * given matches with 129 * the operation's filter. 130 */ handleMessage(Message msg)131 public boolean handleMessage(Message msg) { 132 if (msg.isAWT) 133 return handleEvent(msg, (Event)msg.arg); 134 135 if (filter.match(msg)) { 136 action.invoke(msg.target, msg.arg, scope); 137 return true; 138 } else { 139 return false; 140 } 141 } 142 143 /** 144 * Evokes the action of this operation if the event 145 * given matches with 146 * the operation's filter. 147 */ handleEvent(Message msg, Event evt)148 public boolean handleEvent(Message msg, Event evt) { 149 if (filter.match(msg, evt)) { 150 action.invoke(msg.target, evt.arg, scope); 151 return true; 152 } else { 153 return false; 154 } 155 } 156 157 /** 158 * Returns a new copy of this operation. Makes new copies of the 159 * internal action and filter members as well. 160 */ clone()161 public Object clone() { 162 Op op; 163 164 try { 165 op = (Op)super.clone(); 166 } catch (CloneNotSupportedException e) { 167 // this shouldn't happen, since we are Cloneable 168 throw new InternalError(); 169 } 170 171 if (op.filter != null) 172 op.filter = (OpFilter)op.filter.clone(); 173 if (op.action != null) 174 op.action = (OpAction)op.action.clone(); 175 176 return op; 177 } 178 179 // 180 // Code generation 181 // 182 183 /** 184 * Appends the initialization code for this operation 185 * into the buffer 186 * given. 187 * 188 * @param name name of the operation 189 * @param buf buffer onto which the code should be appended 190 */ genInitCode(StringBuffer buf, String name)191 public void genInitCode(StringBuffer buf, String name) { 192 Global.newline(buf); 193 buf.append(/* NOI18N */" "); 194 buf.append(name); 195 buf.append(/* NOI18N */" = new Op(gui);"); 196 Global.newline(buf); 197 198 if (this.name != null) { 199 buf.append(/* NOI18N */" "); 200 buf.append(name); 201 buf.append(/* NOI18N */".name = "); 202 ListParser.quote(this.name, buf, true); 203 buf.append(/* NOI18N */";"); 204 Global.newline(buf); 205 } 206 207 if (filter != null) { 208 buf.append(/* NOI18N */" "); 209 buf.append(name); 210 buf.append(/* NOI18N */".filter = new OpFilter();"); 211 Global.newline(buf); 212 filter.genInitCode(buf, name + /* NOI18N */".filter"); 213 } 214 215 if (action != null) { 216 buf.append(/* NOI18N */" "); 217 buf.append(name); 218 buf.append(/* NOI18N */".action = new OpAction();"); 219 Global.newline(buf); 220 action.genInitCode(buf, name + /* NOI18N */".action"); 221 } 222 } 223 convertToString(Object obj, StringBuffer buf)224 public void convertToString(Object obj, StringBuffer buf) { 225 Op op = (Op)obj; 226 227 if (op == null) 228 return; 229 230 buf.append(/* NOI18N */"{"); 231 newline(buf); 232 incrIndent(); 233 234 if (op.name != null) { 235 indent(buf); 236 buf.append(/* NOI18N */"name "); 237 ListParser.quote(op.name, buf, false); 238 newline(buf); 239 } 240 241 if (op.filter != null) { 242 indent(buf); 243 buf.append(/* NOI18N */"filter "); 244 op.filter.convertToString(op.filter, buf); 245 newline(buf); 246 } 247 248 if (op.action != null) { 249 indent(buf); 250 buf.append(/* NOI18N */"action "); 251 op.action.convertToString(op.action, buf); 252 newline(buf); 253 } 254 255 decrIndent(); 256 indent(buf); 257 buf.append(/* NOI18N */"}"); 258 } 259 convertFromString(String s)260 public Object convertFromString(String s) { 261 Op op = new Op(); 262 convertFromString(s, op); 263 return op; 264 } 265 convertFromString(String s, Op op)266 public void convertFromString(String s, Op op) { 267 Hashtable table = ListParser.makeListTable(s); 268 269 op.name = (String)table.get(/* NOI18N */"name"); 270 271 s = (String)table.get(/* NOI18N */"filter"); 272 if (s != null) { 273 op.filter = new OpFilter(); 274 op.filter.convertFromString(s, op.filter); 275 } 276 277 s = (String)table.get(/* NOI18N */"action"); 278 if (s != null) { 279 op.action = new OpAction(); 280 op.action.convertFromString(s, op.action); 281 } 282 } 283 284 /** 285 * Returns true if this type should be displayed in an editor. 286 * 287 * For the attribute editor, a return value of false means that the 288 * the textfield will be hidden. 289 * 290 * @return false 291 */ viewableAsString()292 public boolean viewableAsString() { 293 return (false); 294 } 295 } 296