2

I'm trying to hide a SWT shell when the Display is minimized. I'm missing something and would be most thankful for any help.

Additional Info: This shell is actually a popup that gets drawn when the user clicks on a composite. In the end, my goal is to hide this popup-shell when the composite is not visible (user minimized the window or switched between windows, say with Alt+Tab for example).

Here's my code:

static Shell middleClickNodeInfoShell ;
static Label nodeIdLabel ;

void init(){
    ... 
    /** Focused node on middle click*/
    middleClickNodeInfoShell = new Shell(Display.getDefault(), SWT.BORDER | SWT.MODELESS);
    middleClickNodeInfoShell.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false));
    middleClickNodeInfoShell.setLayout(createNoMarginLayout(1, false));

    nodeIdLabel = new Label(middleClickNodeInfoShell, SWT.NONE);
    Display.getDefault().addListener(SWT.Iconify,new Listener() {
        @Override
        public void handleEvent(Event arg0) {
            // TODO Auto-generated method stub
            middleClickNodeInfoShell.setVisible(false);
        }
    });
}   

@Override
public boolean onMouseClicked(Button button, ScreenPosition screenPos,
            final GeoPosition arg2) {
    ... 

    nodeIdLabel.setText("Node Id: "+node.getId());
    middleClickNodeInfoShell.setLocation(pos.getX()+displayX,pos.getY()+displayY+30);
    middleClickNodeInfoShell.setVisible(true);
    middleClickNodeInfoShell.pack();
}
Baz
  • 36,440
  • 11
  • 68
  • 94
Mihai Galos
  • 1,707
  • 1
  • 19
  • 38
  • I guess this is not complete code, post complete code and add more details to post – Chandrayya G K Mar 14 '14 at 07:34
  • Hey Chandrayya, I added some more code. Cheers. – Mihai Galos Mar 14 '14 at 07:54
  • This code WILL not compile on OTHER users machine as it is incomplete. Add the MORE code so that we directly copy your code and test,verify and fix the problem. – Chandrayya G K Mar 14 '14 at 07:57
  • 1
    This code is part of an OSGI environment with proprietary bundles. I stripped out the propietary parts. With the original code you cannot compile either. Please avoid using caps lock unless appropriate. The question is straightforward: Display a popup when mouse is on a visible composite, hide it when the composite is no longer visible. – Mihai Galos Mar 14 '14 at 08:02
  • I think you will have to take care of not just Iconify event, what if the composite is not visible by scrolling. Your code could lead to memory leak as you are adding listener to the Display and dont remove it when composite is disposed. – sambi reddy Mar 14 '14 at 15:54

1 Answers1

1

Here is sample code that will help you do figure out what you are looking for

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setSize(300, 200);
    shell.setText("Shell Example");
    shell.setLayout(new RowLayout());

    final Button button = new Button(shell, SWT.PUSH);
    button.setText("Click Me");


    final Shell tip = new Shell(shell,SWT.MODELESS);
    tip.setLayout(new FillLayout());
    Label lbl = new Label(tip, SWT.NONE);
    lbl.setText("***tooltip***");
    tip.pack();
    shell.addControlListener(new ControlListener() {
      @Override
      public void controlResized(ControlEvent e) {
        changeTipLocation(display, button, tip);
      }
      @Override
      public void controlMoved(ControlEvent e) {
        changeTipLocation(display, button, tip);
      }
    });

    button.addSelectionListener(new SelectionAdapter() {

      public void widgetSelected(SelectionEvent event) {
        changeTipLocation(display, button, tip);
        tip.open();
      }

    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();

  }

  private static void changeTipLocation(final Display display, final Button button, final Shell tip) {

    Rectangle bounds = button.getBounds();
    Point loc = button.getLocation();
    tip.setLocation(display.map(button, null, new Point(loc.x+bounds.width, loc.y+bounds.height)));
  }
sambi reddy
  • 3,065
  • 1
  • 13
  • 10