Package tool.editors.cdf

Source Code of tool.editors.cdf.AttributeDialog

package tool.editors.cdf;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.ResourceManager;

import tool.model.ToolAttribute;
import tool.model.ToolComponent;
import tool.model.ToolType;
import tool.model.ToolWindowAttribute;
import tool.model.validation.ToolNameValidator;
import tool.model.validation.ToolNameValidatorWithBrackets;
import tool.search.SelectType;

public class AttributeDialog extends TitleAreaDialog{
  private DataBindingContext m_bindingContext;
  private Text nameText;
  private Text typeText;
  private Button btnCheckButton;
  private Button lookupButton;
  private ToolAttribute attribute;
 
  public static int show(ToolAttribute attribute){
    Shell shell = Display.getDefault().getActiveShell();
    AttributeDialog dialog = new AttributeDialog(shell, attribute);
    return dialog.open();
  }
  private AttributeDialog(Shell parentShell, ToolAttribute attribute) {
    super(parentShell);
    setTitleImage(ResourceManager.getPluginImage("Tool", "icons/private_attribute.gif"));
    setTitle("Attribute");
    this.attribute = attribute;
  }

  @Override
  protected Control createDialogArea(Composite parent) {
    setTitleImage(ResourceManager.getPluginImage("Tool", "icons/private_attribute.gif"));
    setTitle("Attribute");
    Composite area = (Composite) super.createDialogArea(parent);
    Composite myComposite = new Composite(area, SWT.NONE);
    myComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    GridLayout mylayout = new GridLayout();
    mylayout.horizontalSpacing = 8;
    mylayout.numColumns = 3;
    mylayout.marginHeight = 1;
    mylayout.marginWidth = 1;
    myComposite.setLayout(mylayout);
    Label nameLabel = new Label(myComposite, SWT.NONE);
    nameLabel.setText("Name");
    nameText = new Text(myComposite, SWT.BORDER);
    GridData gd_nameText = new GridData(GridData.FILL_HORIZONTAL);
    gd_nameText.widthHint = 198;
    nameText.setLayoutData(gd_nameText);
    nameText.setText("");
    new Label(myComposite, SWT.NONE);
   
    Label lblType = new Label(myComposite, SWT.NONE);
    lblType.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    lblType.setText("Type");
   
    typeText = new Text(myComposite, SWT.BORDER);
    typeText.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
   
    lookupButton = new Button(myComposite, SWT.NONE);
    lookupButton.setImage(ResourceManager.getPluginImage("Tool", "icons/look_up.gif"));
    lookupButton.setToolTipText("Find Type");
    lookupButton.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent e) {
        String typeString = getAttribute().getType();
        String[] parts = typeString.split("\\.");
        ToolComponent type = ToolType.findType(getAttribute().getProject(), parts[0].trim(), parts[1].trim());
        if (type != null){
          ToolType newType = (ToolType) SelectType.select((ToolType)getAttribute().getParent(), type);
          if (newType != null){
            attribute.setType(newType.getFullName());
          }
        }
      }
    });
    new Label(myComposite, SWT.NONE);
   
    btnCheckButton = new Button(myComposite, SWT.CHECK);
    btnCheckButton.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, false, false, 1, 1));
    btnCheckButton.setText("Public");
    new Label(myComposite, SWT.NONE);
   
    m_bindingContext = initDataBindings();
    return myComposite;
  }

  public ToolAttribute getAttribute() {
    return attribute;
  }

  public void setAttribute(ToolAttribute attribute) {
    this.attribute = attribute;
  }
 
  protected DataBindingContext initDataBindings() {
    DataBindingContext bindingContext = new DataBindingContext();
    //
    IObservableValue nameTextObserveTextObserveWidget = SWTObservables.observeText(nameText, SWT.Modify);
    IObservableValue attributeNameObserveValue = BeansObservables.observeValue(attribute, "toolName");
    UpdateValueStrategy strategy = new UpdateValueStrategy();
    if (this.attribute instanceof ToolWindowAttribute){
      strategy.setAfterConvertValidator(new ToolNameValidatorWithBrackets(nameText));
    } else {
      strategy.setAfterConvertValidator(new ToolNameValidator(nameText));
    }
    bindingContext.bindValue(nameTextObserveTextObserveWidget, attributeNameObserveValue, strategy, null);
    //
    IObservableValue typeTextObserveTextObserveWidget = SWTObservables.observeText(typeText, SWT.Modify);
    IObservableValue attributeTypeObserveValue = BeansObservables.observeValue(attribute, "type");
    bindingContext.bindValue(typeTextObserveTextObserveWidget, attributeTypeObserveValue, null, null);
    //
    IObservableValue btnCheckButtonObserveSelectionObserveWidget = SWTObservables.observeSelection(btnCheckButton);
    IObservableValue attributePublicObserveValue = BeansObservables.observeValue(attribute, "public");
    bindingContext.bindValue(btnCheckButtonObserveSelectionObserveWidget, attributePublicObserveValue, null, null);
    //
    IObservableValue nameTextObserveEditableObserveWidget = SWTObservables.observeEditable(nameText);
    IObservableValue attributeReadWriteObserveValue = BeansObservables.observeValue(attribute, "readWrite");
    bindingContext.bindValue(nameTextObserveEditableObserveWidget, attributeReadWriteObserveValue, null, null);
    //
    IObservableValue typeTextObserveEditableObserveWidget = SWTObservables.observeEditable(typeText);
    bindingContext.bindValue(typeTextObserveEditableObserveWidget, attributeReadWriteObserveValue, null, null);
    //
    IObservableValue btnCheckButtonObserveEnabledObserveWidget = SWTObservables.observeEnabled(btnCheckButton);
    bindingContext.bindValue(btnCheckButtonObserveEnabledObserveWidget, attributeReadWriteObserveValue, null, null);
    //
    return bindingContext;
  }
}
TOP

Related Classes of tool.editors.cdf.AttributeDialog

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.