Java - Eclipse-RCP

Java - Eclipse-RCP - Wizards, mit Forms

Basis dieses Beispiels ist wieder die Beispiel-Applikation.

Der Vorteil von Wizards mit Forms ist der, dass die selben GUI-Elemente für Editor und Wizard verwendet werden können.



Beginnen wir mit der Klasse MyWizard, in welcher die Methode addPages() wie folgt geändert wird:

public class MyWizard extends Wizard implements INewWizard{
   ...
 
   @Override
   public void addPages() {
      addPage(new MyWizardPage("Page 1", "Page 1", null));
   }
 
   ...
}


Die verwendete Klasse MyWizardPage kann wie folgt aussehen:

package com.sowas.javawiki.rcpbaseapplication.wizard;
 
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.ManagedForm;
import org.eclipse.ui.forms.widgets.ScrolledForm;
 
import com.sowas.javawiki.rcpbaseapplication.MyForm;
 
/**
 * Eine WizardPage, welche Forms verwendet.
 */
public class MyWizardPage extends WizardPage{
   private ManagedForm m_managedForm;
 
   protected MyWizardPage(final String pageName, final String title, final ImageDescriptor titleImage){
      super(pageName, title, titleImage);
   }
 
   @Override
   public void createControl(final Composite parent){
      m_managedForm = createManagedForm(parent);
      m_managedForm.getForm().getBody().setLayout(new FillLayout());
 
      parent.setBackground(m_managedForm.getToolkit().getColors().getBackground());
      ScrolledForm scrolledForm = m_managedForm.getForm();
      setControl(scrolledForm);
 
      initialize(m_managedForm);
   }
 
   public void initialize(ManagedForm managedForm) {
      MyForm myForm = new MyForm(managedForm.getForm().getBody(), managedForm.getToolkit());
      myForm.initialize(managedForm);
   }
 
   protected ManagedForm createManagedForm(final Composite parent){
      ManagedForm managedForm = new ManagedForm(parent);
      managedForm.setContainer(this);
      GridData gridData = new GridData();
      gridData.grabExcessHorizontalSpace = true;
      gridData.grabExcessVerticalSpace = true;
      gridData.verticalAlignment = SWT.FILL;
      managedForm.getForm().setLayoutData(gridData);
      return managedForm;
   }
 
   @Override
   public void dispose(){
      if (m_managedForm != null){
         m_managedForm.dispose();
         m_managedForm = null;
      }
      super.dispose();
   }
 
   public boolean setFocus(){
      return m_managedForm.getForm().setFocus();
   }
 
   /**
    * @return Returns the managedForm.
    */
   protected ManagedForm getManagedForm(){
      return m_managedForm;
   }
}


Die Klasse MyForm repäsentiert eine Section:

package com.sowas.javawiki.rcpbaseapplication;
 
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
 
public class MyForm extends SectionPart{
   private FormToolkit m_formToolkit;
 
   public MyForm(Composite parent, FormToolkit toolkit){
      super(parent, toolkit, Section.TWISTIE | Section.TITLE_BAR | Section.EXPANDED);
      m_formToolkit = toolkit;
   }
 
   public FormToolkit getFormToolkit() {
      return m_formToolkit;
  }
 
   @Override
   public void initialize(IManagedForm managedForm) {
      super.initialize(managedForm);
      MyFormAssambly myFormAssambly = new MyFormAssambly(getSection(), m_formToolkit);
      myFormAssambly.initialize();
   }
}


MyFormAssembly erstellt die Controls (hier ein Label und ein Text-Feld) und definiert das Layout:

package com.sowas.javawiki.rcpbaseapplication;
 
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
 
/**
 * Hier werden für MyForm die Controls erstellt und das Layout festgelegt.
 */
public class MyFormAssembly{
   private FormToolkit m_formToolkit;
   private Section     m_section;
   private Composite   m_sectionClient;
 
   public MyFormAssembly(final Section section, final FormToolkit toolkit) {
      m_section = section;
      m_formToolkit = toolkit;
   }
 
   public void initialize() {
      m_sectionClient = m_formToolkit.createComposite(m_section);
      m_section.setClient(m_sectionClient);
      m_section.setText("Beispiel");
 
      createContent(m_sectionClient, m_formToolkit);
  }
 
   private void createContent(Composite client, FormToolkit toolkit){
      client.setLayout(new GridLayout(2, false));
 
      Label label = toolkit.createLabel(client, "Ort:");
      GridData gdl = new GridData();
      label.setLayoutData(gdl);
 
      Text text = toolkit.createText(client, "", SWT.SINGLE|SWT.BORDER);
      GridData gdt = new GridData();
      text.setLayoutData(gdt);
   }
}



Der Editor kann nun ebenfalls MyForm verwenden:

package com.sowas.javawiki.rcpbaseapplication;
 
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.editor.IFormPage;
 
public class MyEditorPage extends FormPage implements IFormPage{
 
   public MyEditorPage(FormEditor editor, String pageId, String pageTitle){
      super(editor, pageId, pageTitle);
   }
 
   @Override
   protected void createFormContent(final IManagedForm managedForm) {
       super.createFormContent(managedForm);
 
       managedForm.getForm().getBody().setLayout(new GridLayout());
 
       MyForm myForm = new MyForm(managedForm.getForm().getBody(), managedForm.getToolkit());
       managedForm.addPart(myForm);
   }
}

Eigene Werkzeuge
Werkzeuge

gratis Counter by GOWEB
seit 9.10.2007