Package gui.dialogs.tuningpanels

Source Code of gui.dialogs.tuningpanels.InputDialog$InputData

package gui.dialogs.tuningpanels;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Scrollable;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


/**
* Esta clase implementa un di�logo simple para el ingreso de datos
* referentes a un objeto reconocido en el escenario.
*/
public class InputDialog extends Dialog
{

  private Shell      dialogShell;
  private Composite    backgroundComposite;
  private Scrollable    inputField;
  private Label      message;
  private Composite    messageComposite;
  private Composite    buttonsComposite;
  private Button      colorButton;
  private Button      okButton;
  private Listener    okListener;
  private Listener    colorListener;
  private Canvas      colorCanvas;
  private Color       color;
  private InputData    returnData;

 
  public class InputData implements Cloneable
  {
    public String  textData;
    public Color  colorData;
   
    public Object clone()
    {
      try
      {
        InputData copy  = (InputData)super.clone();
        copy.textData  = new String( textData );
        copy.colorData  = new Color( null , colorData.getRGB() );
              return copy;
          }
      catch (CloneNotSupportedException e)
      {
              throw new Error("Nunca deber�a ocurrir, porque implementamos Cloneable");
          }
    }
  }

 
 
  public InputDialog(Shell parent, int style)
  {
    super(parent, style);
    returnData = new InputData();
  }

 
  /**
   * Abre el di�logo y, cuando el mismo es cerrado, retorna una instancia
   * {@link InputData} con los datos ingresados.
   * @param text    t�tulo del di�logo.
   * @param msg    texto que se mostrar� en el di�logo.
   * @param c      color que se mostrar� en el di�logo.
   * @param items    listado de �tems para selecci�n (si este par�metro es
   *           nulo, entonces permite el ingreso a trav�s de un cuadro
   *           de texto).
   * @return      datos ingresados.
   */
  public synchronized InputData open(String text, String msg, Color c, String[] items)
  {
    if ( c == null )
      color = new Color(null, 255, 255, 255);
    else
      color = c;
   
    try
    {
     
      if ( text == null  ||  msg == null )
        return null;
     
      Shell parent = getParent();
      dialogShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);

      {
        FillLayout dialogShellLayout = new FillLayout(org.eclipse.swt.SWT.HORIZONTAL);
        dialogShell.setLayout(dialogShellLayout);
        dialogShell.setSize(200, 140);
        dialogShell.setText(text);       
      }
      {
        backgroundComposite = new Composite(dialogShell, SWT.NONE);
        GridLayout backgroundCompositeLayout = new GridLayout();
        backgroundCompositeLayout.numColumns = 2;
        backgroundComposite.setLayout(backgroundCompositeLayout);
        {
          GridData colorCanvasLData = new GridData();
          colorCanvasLData.horizontalAlignment  = GridData.FILL;
          colorCanvasLData.verticalAlignment    = GridData.FILL;
          colorCanvasLData.horizontalIndent    = 7;
          colorCanvasLData.verticalIndent      = 5;
          colorCanvas = new Canvas(backgroundComposite, SWT.NONE);
          colorCanvas.setLayoutData(colorCanvasLData);
          colorCanvas.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent evt) {
              canvasPaintControl(evt);
            }
          });
        }
        {
          messageComposite = new Composite(backgroundComposite, SWT.NONE);
          GridLayout messageCompositeLayout = new GridLayout();
          messageCompositeLayout.makeColumnsEqualWidth = true;
          GridData messageCompositeLData = new GridData();
          messageCompositeLData.horizontalAlignment = GridData.FILL;
          messageCompositeLData.verticalAlignment = GridData.FILL;
          messageCompositeLData.grabExcessHorizontalSpace = true;
          messageComposite.setLayoutData(messageCompositeLData);
          messageComposite.setLayout(messageCompositeLayout);
          {
            message = new Label(messageComposite, SWT.WRAP);
            GridData messageLData = new GridData();
            messageLData.widthHint = 167;
            messageLData.heightHint = 31;
            messageLData.horizontalAlignment = GridData.FILL;
            messageLData.grabExcessHorizontalSpace = true;
            message.setLayoutData(messageLData);
            message.setText(msg);
          }
          {
            GridData inputLData = new GridData();
            inputLData.grabExcessHorizontalSpace = true;
            inputLData.horizontalAlignment = GridData.FILL;
           
            if ( items != null )
            {
              // Recibi� una lista de �tems. Entonces, uso un combo.
              inputField = new CCombo(messageComposite, SWT.NONE);
              inputField.setBackground(new Color(null, 255,255,255));
             
              ((CCombo)inputField).setEditable(false);
              for (int i=0; i<items.length; i++)
                ((CCombo)inputField).add( items[i] );

              ((CCombo)inputField).select( 0 );
            }
            else
            {
              // Se usa un campo editable.
              inputField = new Text(messageComposite, SWT.NONE);
              inputField.setBackground(new Color(null, 255,255,255));
              ((Text)inputField).addModifyListener(
                  new ModifyListener()
                  {
                    public void modifyText(ModifyEvent e)
                    {
                      if ( ((Text)inputField).getText().length() > 0 )
                        okButton.setEnabled( true );
                      else
                        okButton.setEnabled( false );
                    }
                  });
            }

            inputField.setFocus();
            inputField.setLayoutData(inputLData);
            inputField.addKeyListener(
              new KeyAdapter()
              {
                public void keyPressed(KeyEvent evt)
                {
                  if ( (evt.keyCode == SWT.CR  ||  evt.keyCode == SWT.KEYPAD_CR) )
                  {
                    // Presionaron <enter> y hay algo cargado.
                    // Se cierra el di�logo.
                   
                    if ( inputField instanceof CCombo )
                    {
                      returnData.textData    = ((CCombo)inputField).getText();
                      returnData.colorData  = color;
                      dialogShell.close();
                    }
                    else if ( inputField instanceof Text  &&  ((Text)inputField).getText().length() > 0 )
                    {
                      returnData.textData    = ((Text)inputField).getText();
                      returnData.colorData  = color;
                      dialogShell.close();
                    }
                  }
                }
              });
           
          }
        }
        {
          buttonsComposite = new Composite(backgroundComposite, SWT.NONE);
          RowLayout buttonsCompositeLayout = new RowLayout(org.eclipse.swt.SWT.HORIZONTAL);
          buttonsCompositeLayout.justify = true;
          GridData buttonsCompositeLData = new GridData();
          buttonsCompositeLData.horizontalSpan = 2;
          buttonsCompositeLData.horizontalAlignment = GridData.FILL;
          buttonsCompositeLData.heightHint = 30;
          buttonsComposite.setLayoutData(buttonsCompositeLData);
          buttonsComposite.setLayout(buttonsCompositeLayout);
          colorListener = new Listener()
          {
            public void handleEvent(Event event)
            {
              // Abre el "color picker"
              ColorDialog cd = new ColorDialog(dialogShell, SWT.NONE);
              cd.setText("Sample Color Dialog");
              RGB rgb = cd.open();
              if ( rgb != null )
                color = new Color(dialogShell.getDisplay(), rgb);
              colorCanvas.redraw();
            }
          };
          okListener = new Listener()
          {
            public void handleEvent(Event event)
            {
              if ( inputField instanceof CCombo )
                returnData.textData = ((CCombo)inputField).getText();
              else if ( inputField instanceof Text )
                returnData.textData = ((Text)inputField).getText();
              returnData.colorData = color;
              dialogShell.close();
            }
          };
          {
            if ( c == null )
            {
              colorButton = new Button(buttonsComposite, SWT.PUSH
                | SWT.CENTER);
              RowData colorButton2LData = new RowData();
              colorButton2LData.width = 66;
              colorButton2LData.height = 23;
              colorButton.setLayoutData(colorButton2LData);
              colorButton.setText("Color...");
              colorButton.addListener(SWT.Selection, colorListener);
            }
          }
          {
            okButton = new Button(buttonsComposite, SWT.PUSH
              | SWT.CENTER);
            RowData okButton2LData = new RowData();
            okButton2LData.width = 66;
            okButton2LData.height = 23;
            okButton.setLayoutData(okButton2LData);
            okButton.setText("Ok");
            okButton.addListener(SWT.Selection, okListener);
            if (items == null)
              okButton.setEnabled(false);
          }
        }
      }

      dialogShell.open();

      Display display = dialogShell.getDisplay();
      while (!dialogShell.isDisposed())
        if (!display.readAndDispatch())
          display.sleep();
     
    }
    catch (Exception e)
    {
      // TODO: qu� hacer ??
      return null;
    }

    return (InputData)returnData.clone();
  }
 
 
  protected void canvasPaintControl(PaintEvent evt)
  {
    evt.gc.setBackground( color );
    evt.gc.fillRoundRectangle(0, 0, 50, 50, 2, 2);
  }

}
TOP

Related Classes of gui.dialogs.tuningpanels.InputDialog$InputData

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.