/** * 获取数据对象缓存。 * * @param dataObjectDescriptor * @param keyName * @param key * @return */ public DataObject getDataObject(Thing dataObjectDescriptor, String keyName, Object key) { if (dataObjectDescriptor.getBoolean("cacheRelationReadnone")) { DataObject dataObject = new DataObject(dataObjectDescriptor.getMetadata().getPath()); dataObject.put(keyName, key); dataObject.setInited(false); return dataObject; } else { String descriptorPath = dataObjectDescriptor.getMetadata().getPath(); Map<Object, DataObjectCacheEntry> dataObjectCache = caches.get(descriptorPath); if (dataObjectCache == null) { dataObjectCache = new HashMap<Object, DataObjectCacheEntry>(); caches.put(descriptorPath, dataObjectCache); } DataObjectCacheEntry entry = dataObjectCache.get(key); if (entry != null) { return entry.dataObject; } // 重置缓存 DataObject dataObject = new DataObject(descriptorPath); dataObject.put(keyName, key); dataObject.setInited(false); // 保存缓存 if (dataObjectDescriptor.getInt("cacheRelationMaxSize") <= 0 || dataObjectCache.size() < dataObjectDescriptor.getInt("cacheRelationMaxSize")) { entry = new DataObjectCacheEntry(); entry.dataObject = dataObject; dataObjectCache.put(key, entry); } return dataObject; } }
/** * 创建。 * * @param actionContext * @return */ public static Thing create(ActionContext actionContext) { Thing self = (Thing) actionContext.get("self"); // 任务列表 List<Thing> tasks = new ArrayList<Thing>(); Thing tasksThing = self.getThing("Tasks@0"); if (tasksThing != null) { tasks.addAll(tasksThing.getChilds()); } // 进度条和标签 String progressBarVarName = self.getStringBlankAsNull("progressBarVarName"); ProgressBar progressBar = null; if (progressBarVarName != null) { progressBar = (ProgressBar) actionContext.get(progressBarVarName); } String labelVarName = self.getStringBlankAsNull("labelVarName"); Label label = null; if (labelVarName != null) { label = (Label) actionContext.get(labelVarName); } // 创建新的事物 Thing thing = new Thing(); thing.put("descriptors", self.getMetadata().getPath()); // 设置原来的事物为描述者,用于继承行为 // 创建实例 TaskMonitor monitor = new TaskMonitor(self, tasks, progressBar, label, actionContext); thing.setData(TaskMonitor.monitor_name, monitor); // 保存变量 actionContext.getScope(0).put(self.getMetadata().getName(), thing); return thing; }
/** * 根据事物的定义转化Map数据中的数据类型。。 * * @param thing * @param data * @param exludeNull 是否删除null数据 * @return */ @SuppressWarnings("unchecked") public static Map convertMap(Thing thing, Map data, boolean excludeNull) { Map<String, Object> d = new HashMap<String, Object>(); if (thing == null || data == null) return d; List childs = thing.getAllChilds("attribute"); for (int i = 0; i < childs.size(); i++) { try { Thing child = (Thing) childs.get(i); String type = child.getString("type"); String name = child.getMetadata().getName(); putValue(child, d, name, data, excludeNull); // 如果存在范围的,也转换 if (UtilData.isScopeType(type)) { putValue(child, d, name + "Start", data, excludeNull); putValue(child, d, name + "End", data, excludeNull); } } catch (Exception e) { e.printStackTrace(); } } // 遍历子节点,如果子节点数目唯一,那么应该存在一个名称为子节点名的map,该map保存子节点的信息,如果子节点数目是多个那么可能映射成List for (Iterator iter = thing.getAllChilds("thing").iterator(); iter.hasNext(); ) { Thing child = (Thing) iter.next(); Object o = data.get(child.getMetadata().getName()); if (o != null && o instanceof Map) { Map childMap = (Map) o; // 如果内容不为空且有数据,那么加入查询 if (childMap != null && childMap.size() > 0) { Map childMapData = convertMap(child, childMap, excludeNull); if (childMapData.size() != 0) { d.put(child.getMetadata().getName(), childMapData); } } } else if (o != null && o instanceof List) { List list = (List) o; List datas = new ArrayList(); for (Object ol : list) { if (ol instanceof Map) { Map childMapData = convertMap(child, (Map) ol, excludeNull); if (childMapData.size() != 0) { datas.add(childMapData); } } } d.put(child.getMetadata().getName(), datas); } } return d; }
@Override public Thing getThing(String thingName) { // 需要先从缓存中读取,否则当遍历iterator时每次都会读取新的,是否和World中的缓存重复呢? Thing thing = ThingCache.get(thingName); if (thing != null) { if (thing.getMetadata().isRemoved()) { // 如事物的文件被外部改动或事物已给标记为删除,需要重新读取 thing = null; } else { return thing; } } thing = doLoadThing(thingName); if (thing != null) { for (ThingManagerListener listener : listeners) { listener.loaded(this, thing); } for (ThingManagerListener listener : World.getInstance().getThingManagerListeners(getName())) { listener.loaded(this, thing); } } return thing; }
public static SplitPane create(ActionContext actionContext) throws OgnlException { Thing self = (Thing) actionContext.get("self"); Actor firstWidget = null; Actor secondWidget = null; Skin skin = null; String styleName = self.getStringBlankAsNull("styleName"); String firstWidgetStr = self.getStringBlankAsNull("firstWidget"); if (firstWidgetStr != null) { firstWidget = (Actor) actionContext.get(firstWidgetStr); } String secondWidgetStr = self.getStringBlankAsNull("secondWidget"); if (secondWidgetStr != null) { secondWidget = (Actor) actionContext.get(secondWidgetStr); } if (firstWidget == null) { firstWidget = createActor(self, "FirstWidget", actionContext); } if (secondWidget == null) { secondWidget = createActor(self, "SecondWidget", actionContext); } skin = (Skin) actionContext.get(self.getString("skin")); SplitPane group = new SplitPane(firstWidget, secondWidget, self.getBoolean("vertical"), skin, styleName); actionContext.getScope(0).put(self.getMetadata().getName(), group); init(self, group, actionContext); return group; }
public static JInternalFrame create(ActionContext actionContext) { // 变量 Thing self = (Thing) actionContext.get("self"); Container parent = (Container) actionContext.get("parent"); // 创建 JInternalFrame comp = new JInternalFrame(); if (parent != null) { parent.add(comp); } // 初始化 init(comp, self, parent, actionContext); // 创建子节点 try { actionContext.push().put("parent", comp); for (Thing child : self.getChilds()) { child.doAction("create", actionContext); } } finally { actionContext.pop(); } // 放置和返回变量 actionContext.getScope(0).put(self.getMetadata().getName(), comp); return comp; }
/** * 获取指定的工作流。 * * @param workflowThing * @param workflowId * @param actionContext * @return */ public static Workflow getWorkflow( Thing workflowThing, String workflowId, ActionContext actionContext) { Workflow workflow = workflows.get(workflowThing.getMetadata().getPath() + ":" + workflowId); if (workflow == null) {} return workflow; }
public static File getFile(ActionContext actionContext) throws OgnlException { Thing self = (Thing) actionContext.get("self"); Object obj = UtilData.getData(self, "file", actionContext); if (obj instanceof File) { return (File) obj; } else if (obj instanceof String) { return new File((String) obj); } else { throw new ActionException(obj + "is not a file, path=" + self.getMetadata().getPath()); } }
@SuppressWarnings("unchecked") public static void onReconfig(ActionContext actionContext) { Thing self = (Thing) actionContext.get("self"); Table table = (Table) self.get("table"); // 删除所有数据 table.removeAll(); // 删除所有列 for (TableColumn column : table.getColumns()) { column.dispose(); } // 重新组建表格的列 Thing store = (Thing) actionContext.get("store"); Thing dataObject = (Thing) store.get("dataObject"); if (dataObject == null) { return; } List<Thing> columns = new ArrayList<Thing>(); for (Thing attr : (List<Thing>) dataObject.get("attribute@")) { if (attr.getBoolean("viewField") && attr.getBoolean("showInTable") && attr.getBoolean("gridEditor")) { int style = SWT.NONE; String gridAlign = attr.getString("gridAlign"); if ("left".equals(gridAlign)) { style = style | SWT.LEFT; } else if ("right".equals(gridAlign)) { style = style | SWT.RIGHT; } else if ("center".equals(gridAlign)) { style = style | SWT.CENTER; } TableColumn column = new TableColumn(table, style); column.setText(attr.getMetadata().getLabel()); int width = attr.getInt("gridWidth"); if (width > 0) { column.setWidth(width); } column.setResizable(attr.getBoolean("gridResizable")); column.setMoveable(true); columns.add(attr); } } self.set("columns", columns); // 初始化数据,如果存在 self.doAction("onLoaded", actionContext); }
public static Object showGenerateFileName(ActionContext actionContext) { Thing self = (Thing) actionContext.get("self"); StringBuffer tName = new StringBuffer(); // def project = self.getMetadata().getProject(); // tName.append(project == null ? "null" : project.getName()); tName.append("/" + self.getRoot().getMetadata().getPath().hashCode()); tName.append("/" + self.getMetadata().getPath().hashCode()); tName.append("/" + self.getMetadata().getName()); tName.append(".ftl"); // println tName.toString(); return tName.toString(); }
/** * 返回当前所有工作流多定义的事物列表。 * * @return */ public static List<Thing> getWorkflowThings() { List<Thing> things = new ArrayList<Thing>(); Map<String, String> thingMap = new HashMap<String, String>(); for (String key : workflows.keySet()) { Workflow workflow = workflows.get(key); Thing thing = workflow.getThing(); String path = thing.getMetadata().getPath(); if (thingMap.get(path) == null) { things.add(thing); thingMap.put(path, path); } } return things; }
public static void init(ActionContext actionContext) { World world = World.getInstance(); // import xworker.util.ThingOgnlAccessor; // 初始化Ognl读Thing的类 // ThingOgnlAccessor.init(); System.out.println("init world=" + world); // 重新设置元事物,Java代码中定义的元事物是最简单的,有更多功能的更好一些 Thing metaThing = world.getThing("xworker.lang.MetaThing").detach(); // 保留元事物的路径 metaThing.getMetadata().setPath("xworker.lang.MetaThing"); metaThing.initChildPath(); world.metaThing = metaThing; // 检查全局配置是否存在 Thing globalConfig = world.getThing("_local.xworker.config.GlobalConfig"); if (globalConfig == null) { globalConfig = new Thing("xworker.ide.config.decriptors.GlobalConfig"); globalConfig.put("name", "GlobalConfig"); globalConfig.copyTo("_local", "_local.xworker.config"); } // 注册事物 ThingRegistor.regist( "_xworker_thing_attribute_editor_config", "xworker.lang.attributeEditors.EditorConfig"); ThingRegistor.regist( "_xworker_thing_attribute_editor_CodeEditor", "xworker.swt.xworker.CodeEditor"); ThingRegistor.regist("_xworker_thing_attribute_editor_GridData", "xworker.swt.layout.GridData"); ThingRegistor.regist( "_xworker_thing_attribute_editor_CodeEditor", "xworker.swt.xworker.CodeEditor"); ThingRegistor.regist( "_xworker_thing_attribute_editor_HtmlEditor", "xworker.swt.xworker.HtmlEditor"); ThingRegistor.regist( "_xworker_thing_attribute_editor_openDataListener", "xworker.ide.worldExplorer.swt.shareScript.ThingEditor/@scripts/@openDataListener"); ThingRegistor.regist("_xworker.swt_model", "xworker.swt.model.Model"); ThingRegistor.regist("_xworker_globalConfig", "_local.xworker.config.GlobalConfig"); // log.info("ThingRegister regiseted all"); // log.info("ThingRegister class=" + ThingRegistor.getThings()); // 执行其他项目的初始化 World.getInstance().runActionAsync("xworker.ide.config.Project/@actions/@initBackground", null); }
public static Object create(ActionContext actionContext) { World world = World.getInstance(); Thing self = (Thing) actionContext.get("self"); Action styleAction = world.getAction("xworker.swt.widgets.Composite/@scripts/@getStyles"); int style = (Integer) styleAction.run(actionContext); Composite parent = (Composite) actionContext.get("parent"); Spinner spinner = new Spinner(parent, style); // 父类的初始化方法 Bindings bindings = actionContext.push(null); bindings.put("control", spinner); bindings.put("self", self); try { Action initAction = world.getAction("xworker.swt.widgets.Control/@actions/@init"); initAction.run(actionContext); } finally { actionContext.pop(); } spinner.setDigits(self.getInt("digits", 0)); spinner.setIncrement(self.getInt("increment", 1)); String maximum = self.getString("maximum"); if (maximum != null && !"".equals(maximum)) spinner.setMaximum(self.getInt("maximum", 1000000)); String minimun = self.getString("minimun"); if (minimun != null && !"".equals(minimun)) spinner.setMinimum(self.getInt("maximum", -100000)); String pageIncrement = self.getString("pageIncrement"); if (pageIncrement != null && !"".equals(pageIncrement)) spinner.setPageIncrement(self.getInt("pageIncrement", 10)); String selection = self.getString("selection"); if (selection != null && !"".equals(selection)) spinner.setSelection(self.getInt("selection", 1)); // 保存变量和创建子事物 actionContext.getScope(0).put(self.getString("name"), spinner); actionContext.peek().put("parent", spinner); for (Thing child : self.getAllChilds()) { child.doAction("create", actionContext); } actionContext.peek().remove("parent"); Designer.attach(spinner, self.getMetadata().getPath(), actionContext); return spinner; }
public static RemoveListenerAction create(ActionContext actionContext) { Thing self = (Thing) actionContext.get("self"); RemoveListenerAction ac = Actions.action(RemoveListenerAction.class); if (self.getStringBlankAsNull("capture") != null) { ac.setCapture(self.getBoolean("capture")); } EventListener listener = (EventListener) actionContext.get("listener"); ac.setListener(listener); Actor actor = (Actor) actionContext.get("actor"); ac.setActor(actor); actionContext.getScope(0).put(self.getMetadata().getName(), ac); return ac; }
public static Object create(ActionContext actionContext) { Thing self = (Thing) actionContext.get("self"); int style = SWT.NONE; String selfStyle = self.getString("style"); if ("HORIZONTAL".equals(selfStyle)) { style |= SWT.HORIZONTAL; } else if ("VERTICAL".equals(selfStyle)) { style |= SWT.VERTICAL; } if (self.getBoolean("SMOOTH")) style |= SWT.SMOOTH; if (self.getBoolean("BORDER")) style |= SWT.BORDER; if (self.getBoolean("INDETERMINATE")) style |= SWT.INDETERMINATE; Composite parent = (Composite) actionContext.get("parent"); ProgressBar bar = new ProgressBar(parent, style); // 父类的初始化方法 Bindings bindings = actionContext.push(null); bindings.put("control", bar); try { self.doAction("super.init", actionContext); } finally { actionContext.pop(); } bar.setMaximum(self.getInt("maximum", 100)); bar.setMinimum(self.getInt("minimum", 0)); bar.setSelection(self.getInt("selection", 0)); // 保存变量和创建子事物 actionContext.getScope(0).put(self.getString("name"), bar); actionContext.peek().put("parent", bar); for (Thing child : self.getAllChilds()) { child.doAction("create", actionContext); } actionContext.peek().remove("parent"); Designer.attach(bar, self.getMetadata().getPath(), actionContext); return bar; }
public static ColorAction create(ActionContext actionContext) throws OgnlException { Thing self = (Thing) actionContext.get("self"); ColorAction action = Actions.action(ColorAction.class); Color startColor = (Color) self.getObject("startColor", actionContext); if (startColor != null) { action.setColor(startColor); } Color endColor = (Color) self.getObject("endColor", actionContext); if (endColor != null) { action.setEndColor(endColor); } TemporalActionActions.init(self, action, actionContext); actionContext.getScope(0).put(self.getMetadata().getName(), action); return action; }
public static void iteratorLines(ActionContext actionContext) throws IOException { Thing self = (Thing) actionContext.get("self"); File file = (File) self.doAction("getFile", actionContext); if (file == null) { throw new ActionException( "IteratorLines: file is null, path=" + self.getMetadata().getPath()); } FileInputStream fin = new FileInputStream(file); try { BufferedReader br = new BufferedReader(new InputStreamReader(fin)); String line = null; boolean trim = self.getBoolean("trim"); boolean continueOnBlank = self.getBoolean("continueOnBlank"); Bindings bindings = actionContext.push(); bindings.isVarScopeFlag = true; try { bindings.put("file", file); while ((line = br.readLine()) != null) { if (trim) { line = line.trim(); } if (continueOnBlank && "".equals(line)) { continue; } bindings.put("line", line); self.doAction("handleLine", actionContext); } } finally { actionContext.pop(); } br.close(); } finally { fin.close(); } }
@SuppressWarnings("unchecked") public static void run(ActionContext actionContext) { Thing self = (Thing) actionContext.get("self"); DataObject monitorTask = (DataObject) actionContext.get("monitorTask"); // DataObject monitorTaskTask = (DataObject) actionContext.get("monitorTaskTask"); List<DataObject> resources = (List<DataObject>) actionContext.get("resources"); MonitorContext monitorContext = (MonitorContext) actionContext.get("monitorContext"); Random random = new Random(); for (DataObject resource : resources) { String dataNames = resource.getString("param1"); double minValue = resource.getDouble("param2"); double maxValue = resource.getDouble("param3"); boolean toInt = resource.getBoolean("param4"); for (String dataName : dataNames.split("[,]")) { double v = random.nextDouble(); v = (maxValue - minValue) * v + minValue; DataObject data = new DataObject("xworker.app.monitor.dataobjects.MonitorData"); data.put("taskThingPath", self.getMetadata().getPath()); data.put("resourceId", resource.getString("resId")); data.put("dataName", dataName); if (toInt) { long lv = (long) v; data.put("value", String.valueOf(lv)); } else { data.put("value", String.valueOf(v)); } data.put("grabStartTime", monitorContext.getStartTime()); data.put("grabEndTime", new Date()); data.put("monitorId", monitorContext.monitor.get("id")); data.put("monitorTaskId", monitorTask.get("id")); data.put("monitorTaskResId", resource.get("id")); MonitorDataSaveTask.addCreateData(data); } } }
public TaskMonitor( Thing thing, List<Thing> tasks, ProgressBar progressBar, Label label, ActionContext actionContext) { this.thing = thing; this.tasks = tasks; this.progressBar = progressBar; this.label = label; this.actionContext = actionContext; taskTotalCount = tasks.size(); dynamicTask = thing.getBoolean("dynamicTask"); if (!dynamicTask) { setProgressBarMaximun(tasks.size()); } Thread th = new Thread(this, thing.getMetadata().getName()); th.setDaemon(true); th.start(); }