对于设计用于打开多个文档的eclipse RCP文本编辑器应用程序,我已经合并了一个工具栏按钮来显示不可打印的字符(即制表符,空格和CR)。
为了显示这些字符,我添加/删除了jface painter WhitespaceCharacterPainter:
sv.addPainter(whitespaceCharacterPainter);
该按钮适用于单个部件,但在PartStack中有多个文档(由部件描述符调用创建),只有创建的最后一个部分显示/隐藏不可打印的字符。 创建新零件时,其他零件仍处于最后状态。 用户在部件之间切换或单击工具栏上的按钮。
我按下代码以在按下按钮时以及当零件成为所选零件时更新零件(通过相应选项卡上的ckick)。按钮hanlder。
我很欣赏任何暗示。
//Code...
/** Indicates the status of the WhiteSpaceCharacterPainter button on the toolbar for this part. */
private boolean wsToolBarButtonStatus = false;
public StyledText st;
public SourceViewer sv;
@Inject MPart parte;
//Code...
/**
* Update the part when the user push the WhiteSpace toolbar button.
* Sets the sv.addPainter(whitespaceCharacterPainter)
* or sv.removePainter(whitespaceCharacterPainter);
*
* @param newButtonStatus Receives the status of the button from the IEventBroker
*/
@Inject
@Optional
public void updatePartByWSButton(@UIEventTopic(NastranEditorEventConstants.WHITE_CHARACTERS_STATUS) boolean newButtonStatus) {
final MElementContainer<MUIElement>container = parte.getParent();
if (parte.equals((MPart)container.getSelectedElement())){
System.out.println("EL PART ES EL SELECTED ELEMENT cuando se aprieta el boton\t" + parte.getLabel());
//if(wsToolBarButtonStatus != newButtonStatus)
wsToolBarButtonStatus = newButtonStatus;
if(wsToolBarButtonStatus){
sv.addPainter(whitespaceCharacterPainter);
System.out.println("addPainter......");
}
else{
try{
sv.removePainter(whitespaceCharacterPainter);
System.out.println("removePainter.....");
}catch (NullPointerException e) {
//e.printStackTrace();
}
}
}
}
/**
* Update the WhiteSpace toolbar button when the part is active.
* Only the active part is updated
*
* @param activePart
*/
@Inject
@Optional
public void updateWSButtonByPart(@Named(IServiceConstants.ACTIVE_PART) MPart activePart) {
if (parte.equals(activePart)) {
System.out.println("EL PART ACTIVO TIENE LABEL:\t" + parte.getLabel());
MToolItem item = (MToolItem) modelService.find("es.robes.nastraneditor.toolbarbuttons.whitespacespainterbutton",app);
item.setSelected(wsToolBarButtonStatus);
if(wsToolBarButtonStatus){
this.sv.addPainter(whitespaceCharacterPainter);
System.out.println("addPainter......");
}
else{
try{
this.sv.removePainter(whitespaceCharacterPainter);
System.out.println("removePainter......");
}catch (NullPointerException e) {
//e.printStackTrace();
}
}
}
}
按钮处理程序:
public class WhiteSpacesHandler {
boolean status;
@Execute
public void execute(final MToolItem item, IEventBroker broker) {
System.out.println("MToolItem element ID: "+ item.getElementId());
if (item.isSelected()){
System.out.println("Item is selected");
status = true;
//broker.post(NastranEditorEventConstants.WHITE_CHARACTERS_ENABLED, status);
}
else{
System.out.println("Item is not selected");
status = false;
}
broker.post(NastranEditorEventConstants.WHITE_CHARACTERS_STATUS, status);
}
}