¿Detectar de manera programada la maximización / minimización de ventanas ??? Eclipse RCP

As the title shows, I want to add a listener to my rcp user interface in order to detect maximization and minimization. Actually, it not that my real purpose, but I think it is a way to solve my problem. I have a view with some shapes in the center, and I wonna keep the drawing exactly in the center even if the window is resized. To do so, I used the following listener :

public void createPartControl(final Composite parent) {
    display = parent.getDisplay();
    white= display.getSystemColor(SWT.COLOR_WHITE);
    parent.setLayout(new FillLayout(SWT.VERTICAL));
    final ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    sc.setMinHeight(100);
    sc.setMinWidth(100);
    sc.setSize(565, 305);
    final Composite child = new Composite(sc,SWT.NONE);   
    child.setLayout(new FillLayout());
    // Set child as the scrolled content of the ScrolledComposite
    sc.setContent(child);
    child.setBackground(white);
    gc = new GC(child);
    parent.addListener (SWT.Resize,  new Listener () {
        public void handleEvent (Event e) {
            x = child.getBounds().width/2;
            y = child.getBounds().height/2;
            child.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent event) {
                    dessin(gc);  // draw my shapes
    }
    });
}

everything goes well except when I maximize the window and then minimize it, in this case I loose the drawing (it is in the corner). Any idea please? I'm I thinking in the right way?

preguntado el 27 de agosto de 11 a las 15:08

There seem to be missing some vital code in the example. But... when the resize event is seen for the parent, the child need not necessarily be resized yet. -

Oh yeah, you are right, I have to add the listener to the child and not the parent.. thanks -

3 Respuestas

The two events to detect minimization and un-minimization (not necessarily maximization) are Iconify y Deiconify which only occur on the Shell. See the javadocs for Cáscara.

Respondido 28 ago 11, 16:08

Consider moving the resize event is seen for the parent, as the child need not necessarily be resized yet.

Respondido 28 ago 11, 22:08

In order to keep something in the center of something else all you need is the SWT.Resize event, so this question is a classic case of the Problema XY. (Except that the OP in this case seems to already suspect that this may be an XY Problem.)

However, many people arrive at this question with a legitimate need to programmatically detect window minimized / maximized / restored events, for the following reason:

If you want to be able to save the bounds of your application window on exit, you cannot just save whatever is returned by Shell.getBounds(), because your application may be terminated while minimized or maximized or fullscreen, in which case its bounds should not be persisted. What should be persisted is the minimized/normal/maximized/fullscreen state of the shell, (I call it "posture",) and the bounds of the shell last time its posture was "normal". So, essentially, you need to keep track of when the posture is "normal", and for that you need to have a "posture changed" event.

The problem is that when SWT issues the "deiconified" event, it has not calculated the bounds of the shell yet, so the value that you get in that case is bogus.

So, here is the solution to that:

You are going to need a method which recalculates the posture as follows:

private void recalculatePosture()
{
    Posture posture = swtShell.getFullScreen()? Posture.FULLSCREEN 
        : swtShell.getMinimized()? Posture.MINIMIZED
        : swtShell.getMaximized()? Posture.MAXIMIZED
        : Posture.NORMAL;
    if( posture != previousPosture )
    {
        issue event...
        previousPosture = posture;
    }
}

In order to generate the "maximized", "restored (from maximized)" and "fullscreen" events you can use Shell.addListener() para escuchar el SWT.Move y SWT.Resize event, and invoke recalculatePosture() cuando ocurren.

In order to generate the "minimized" event you can use the shellIconified() método de la ShellListener as @the.duckman said, and again, invoke recalculatePosture().

In order to generate the "restored (from minimized)" event, you need to do the following in your ShellListener:

@Override
protected void onShellDeiconified( ShellEvent e )
{
    display.asyncExec( () -> recalculatePosture() );
}

This will cause the recalculation of posture a short time Si lo envía después the 'deiconified' event, at which point SWT will have gotten around to properly calculating the bounds of the shell.

Respondido 14 Abr '17, 08:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.