2012-09-17 25 views
5

Estoy tratando de cambiar el cursor cuando comienzo una operación. Quiero que el cursor se muestre ocupado hasta que la operación finalice. Tengo una clase separada para el OperationListener. No estoy seguro de cómo asignar el cursor?SWT - Mostrando un Cursor ocupado

llamada desde la clase AplotBaseDialog

listOp.addOperationListener(new MyOperationListener(this) { 
    etc.... 
} 

clase separada

public abstract class MyOperationListener implements InterfaceAIFOperationListener { 
    Cursor busyCursor = null; 
    AplotBaseDialog w = null; 

    public MyOperationListener(AplotBaseDialog win) { 

    **Should this not be getCurrent?** 
    busyCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_WAIT); 

    w = win; 
    } // end constructor 

    public void startOperation(String startMessage) { 
    ** should this be getting a Shell form the AplotBaseDialog? ** 
    w.???.setCursor(busyCursor); 

    } // end startOperation() 

    public void endOperation() { 
    try { 
     endOperationImpl(); 
    } 
    finally { 
     w.???.setCursor(null); 
    } 
    } // end endOperation() 

    abstract protected void endOperationImpl(); 
} // end class MyOperationListener 

EDITAR

plotButton.addSelectionListener(new SelectionAdapter() { 
     public void widgetSelected(SelectionEvent e) { 
      BusyIndicator.showWhile(Display.getDefault(), new Runnable(){ 
       public void run(){ 
        startPrinterListOperation(); 
       } 
      }); 
     } 
    }); 

EDITAR

private void startPrinterListOperation() { 

    listOp = new AplotPrinterListOperation(appReg.getString("aplot.message.GETPRINTERLIST"), session); 
    listOp.addOperationListener(new MyOperationListener(this) { 
    public void endOperationImpl() { 
     try { 
      printers.clear(); 
      printers.addAll((ArrayList<PrinterProfile>) listOp.getPrinters()); 
      Display.getDefault().asyncExec(new Runnable() { 
       public void run() { 

       showAplotPlotterDialog(); 
       } 
      }); 
     } 
     finally { 

      listOp.removeOperationListener(this); 
      listOp = null; 
     } 
    } 
    }); 
    session.queueOperation(listOp); 


} // end startPrinterListOperation() 

Respuesta

4

Esto funciona para mí:

private static boolean wait = false; 

private static Cursor cursor = null; 

public static void main(String[] args) { 
    final Display display = new Display(); 
    final Shell shell = new Shell(display); 
    shell.setLayout(new FillLayout()); 

    Button button = new Button(shell, SWT.PUSH); 
    button.setText("Change cursor"); 

    button.addListener(SWT.Selection, new Listener() { 

     @Override 
     public void handleEvent(Event arg0) { 
      wait = !wait; 

      if(cursor != null) 
       cursor.dispose(); 

      cursor = wait ? new Cursor(display, SWT.CURSOR_WAIT) : new Cursor(display, SWT.CURSOR_ARROW); 

      shell.setCursor(cursor); 
     } 
    }); 


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

    if(cursor != null) 
     cursor.dispose(); 
} 

acaba de establecer el cursor de su cáscara que contiene través setCursor(Display, int).

Si no desea cambiar el cursor para el shell completo, use un Composite o Control de su elección.


Recuerde que usted tiene que dispose() cada Cursor ejemplo se crea a sí mismo.

+0

Básicamente, simplemente agregué las líneas anteriores a mi método de operación, ya que comenzó justo antes de que se abriera el cuadro de diálogo y parece que funciona. Realmente desearía haber descubierto cómo hacerlo funcionar en la clase de operación abstracta. Pero esto parece tener el mismo efecto. - Gracias – jkteater

4

Usted podría utilizar

BusyIndicator.showWhile(Display.getDefault(), new Runnable(){ 

    public void run(){ 
     //your code 
    } 
    }); 
+0

Intenté poner eso en mi acción de botón, pero no hizo nada. Por favor, consulte más arriba – jkteater

+0

¿podría publicar lo que está haciendo en startPrinterListOperation()? ¿Ves alguna excepción? –

+0

Sin excepciones: consulte el código anterior – jkteater