SWT开发的学习

复习以前使用的技术

package tcprcp;


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.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.ui.IWorkbenchPropertyPage;
import org.eclipse.ui.dialogs.PropertyPage;
import org.eclipse.ui.internal.dialogs.WorkbenchPreferenceDialog;

/**
 *
 * @author longgangbai
 *
 */
public abstract class PreferenceAndPropertyPage extends PropertyPage implements
 IWorkbenchPropertyPage {
    private Button btnEnableProjectSettings;

    /**
     * (non-Javadoc)
     *
     * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected final Control createContents(final Composite parent) {
 Composite container = new Composite(parent, SWT.NONE);
 container.setLayoutData(new GridData(GridData.FILL_BOTH));
 GridLayout layout = new GridLayout();
 layout.numColumns = 2;
 container.setLayout(layout);

 btnEnableProjectSettings = new Button(container, SWT.CHECK);
 btnEnableProjectSettings
  .setText(StingIDEMessages.PreferenceAndPropertyPage_enableProjectSettingsLabel);
 btnEnableProjectSettings.setLayoutData(new GridData(GridData.BEGINNING,
  GridData.CENTER, true, false, 1, 1));
 Link link = new Link(container, SWT.NONE);
 link
  .setText("<a>"
   + StingIDEMessages.PreferenceAndPropertyPage_configWorkspaceSettingsLink
   + "</a>");
 link.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER,
  false, false, 1, 1));

 Label label = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
 label.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true,
  false, 2, 1));

 final Composite comp = new Composite(container, SWT.NONE);
 comp.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true,
  true, 2, 1));
 comp.setLayout(new GridLayout());
 createCustomContents(comp);
 setEnabled(comp, false);

 btnEnableProjectSettings.addSelectionListener(new SelectionAdapter() {
     /**
      * (non-Javadoc)
      *
      * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
      */
     @Override
     public void widgetSelected(SelectionEvent e) {
  boolean selected = btnEnableProjectSettings.getSelection();
  setEnabled(comp, selected);
     }
 });
 link.addSelectionListener(new SelectionAdapter() {
     /**
      * (non-Javadoc)
      *
      * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
      */
     @Override
     public void widgetSelected(SelectionEvent e) {
  String id = getRelatedPreferencePageId();
  WorkbenchPreferenceDialog dialog = WorkbenchPreferenceDialog
   .createDialogOn(parent.getShell(), id);
  dialog.showOnly(new String[] { id });
  dialog.open();
     }
 });
 return container;
    }

    /**
     * 创建组件
     *
     * @param parent
     */
    protected abstract void createCustomContents(Composite parent);

    /**
     * 获取相关的首选项页面ID
     *
     * @return
     */
    protected abstract String getRelatedPreferencePageId();

    /**
     * 获取项目设置是否已经被启用
     *
     * @return
     */
    protected boolean isProjectSettingsEnabled() {
 return btnEnableProjectSettings.getSelection();
    }

    /**
     * 设置可用性
     *
     * @param comp
     * @param selected
     */
    private void setEnabled(final Composite comp, boolean selected) {
 comp.setEnabled(selected);
 Control[] controls = comp.getChildren();
 if (controls != null) {
     for (Control control : controls) {
  control.setEnabled(selected);
  if (control instanceof Composite) {
      setEnabled((Composite) control, selected);
  }
     }
 }
    }

    /**
     * 设置是否启用项目设置
     *
     * @param enabled
     */
    protected final void setEnableProjectSettings(boolean enabled) {
 btnEnableProjectSettings.setSelection(enabled);
    }
}

你可能感兴趣的:(eclipse,UI)