我有一个Eclipse E4应用程序,它与不同服务器有多个连接。现在,当打开MPart时,该部件将调用服务器来接收他的数据。此外,当MPart关闭时,客户端将关闭发送给服务器,以便客户端知道客户端不需要更新数据。
现在我需要找到一种方法让MParts知道客户端正在关闭。这样他们就不会向服务器发送任何消息。这将加速客户端的关闭。
当用户点击关闭按钮时,如何将关闭命令发送到Mpart?
答案 0 :(得分:1)
使用事件代理向部件发送消息。
在关机代码中发送一个事件:
function CSVToArray( strData, strDelimiter ){
// Check to see if the delimiter is defined. If not,
// then default to comma.
strDelimiter = (strDelimiter || ",");
// Create a regular expression to parse the CSV values.
var objPattern = new RegExp(
(
// Delimiters.
"(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
// Quoted fields.
"(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
// Standard fields.
"([^\"\\" + strDelimiter + "\\r\\n]*))"
),
"gi"
);
// Create an array to hold our data. Give the array
// a default empty first row.
var arrData = [[]];
// Create an array to hold our individual pattern
// matching groups.
var arrMatches = null;
// Keep looping over the regular expression matches
// until we can no longer find a match.
while (arrMatches = objPattern.exec( strData )){
// Get the delimiter that was found.
var strMatchedDelimiter = arrMatches[ 1 ];
// Check to see if the given delimiter has a length
// (is not the start of string) and if it matches
// field delimiter. If id does not, then we know
// that this delimiter is a row delimiter.
if (
strMatchedDelimiter.length &&
strMatchedDelimiter !== strDelimiter
){
// Since we have reached a new row of data,
// add an empty row to our data array.
arrData.push( [] );
}
var strMatchedValue;
// Now that we have our delimiter out of the way,
// let's check to see which kind of value we
// captured (quoted or unquoted).
if (arrMatches[ 2 ]){
// We found a quoted value. When we capture
// this value, unescape any double quotes.
strMatchedValue = arrMatches[ 2 ].replace(
new RegExp( "\"\"", "g" ),
"\""
);
} else {
// We found a non-quoted value.
strMatchedValue = arrMatches[ 3 ];
}
// Now that we have our value string, let's add
// it to the data array.
arrData[ arrData.length - 1 ].push( strMatchedValue );
}
// Return the parsed data.
return( arrData );
}
@Inject
IEventBroker eventBroker;
eventBroker.send("my/topic/shutdown", data);
是您要与shudown事件关联的任何数据。
data
只是该活动的唯一ID。
使用"my/topic/shutdown"
方法同步发送事件,使用send
异步发送。
每个部分都可以通过以下方式订阅活动:
post
@Inject
@Optional
public void shutdown(@EventTopic("my/topic/shutdown") Event event)
{
....
}
是Event
如果您希望保证在UI线程中运行该方法,也可以使用org.osgi.service.event.Event
。
要处理对应用程序“关闭”按钮的单击,您需要将@UIEventTopic
的实现放入主窗口的Eclipse上下文中。您可以在生命周期类中执行此操作(如果有的话)。应用程序启动完成事件适用于此:
org.eclipse.e4.ui.workbench.modeling.IWindowCloseHandler