Eclipse RCP text editor application with WhitespaceCharacterPainter and ToolbarButton

77 Views Asked by At

For an eclipse RCP text editor application, designed to open multiple documents, I have incorporated a toolbar button to show non-printable characters, (i.e. tab, space and CR).

To show these characters, I add/remove the jface painter WhitespaceCharacterPainter:

sv.addPainter(whitespaceCharacterPainter);

The button works fine with a single part, but when in the PartStack there are more than one document (created by part descriptor calls), only the last part created shows/hides the non-printable characters. Other parts remain in their last status when a new part is created. The user switch between parts or click on the toolbar butoon.

I have attched the code to update the part when the button is pushed and when the part becomes the selected part (by ckick on the correponding tab). Also the button hanlder.

I appreciatte any hint.

//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();
            }
        } 
    }
}

The button handler:

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);
    }
}
0

There are 0 best solutions below