package tools.vitruv.framework.vsum.ui;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
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.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

public class ModelSelectionPage extends WizardPage {

  private static final String PAGENAME = "Vitruvius Project";
  private static final String DESCRIPTION = "Create a new Vitruvius Project.";
  private int numOfChecked;

  private Text text1;

  private Composite container;

  protected ModelSelectionPage() {
    super(PAGENAME);
    setTitle(PAGENAME);
    setDescription(DESCRIPTION);
    numOfChecked = 0;
    // setControl(text1);

  }

  @Override
  public void createControl(Composite parent) {
    container = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout();
    container.setLayout(layout);
    layout.numColumns = 1;
    Label label1 = new Label(container, SWT.NONE);
    label1.setText("Say hello to Fred");
    // text1 = new Text(container, SWT.BORDER | SWT.SINGLE);

    IProject projects[] = getProjects();

    final Tree tree = new Tree(container, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    System.out.println(projects.length);
    tree.setItemCount(projects.length);

    GridData treeGridData = new GridData(GridData.FILL_HORIZONTAL);
    tree.setLayoutData(treeGridData);
    for (int i = 0; i < projects.length; i++) {
      System.out.println(projects[i] + " " + i);
    }
    System.out.println(projects);
    for (IProject project : projects) {
      System.out.println("Loop");
      TreeItem t = new TreeItem(tree, SWT.CHECK);
      t.setText(project.getName());
      for (int i = 0; i < 3; i++) {
        TreeItem childItem = new TreeItem(t, SWT.CHECK);
        childItem.setText("Model " + i);
      }
    }
    tree.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        if (event.detail == SWT.CHECK) {
          TreeItem item = (TreeItem) event.item;
          System.out.println(item.getClass());
          System.out.println(item);
          System.out.println(item.getParentItem());
          TreeItem parent = item.getParentItem();
          if (item.getChecked()) {
            // check a project automatically, when one of it's model is checked
            if (null != parent) {
              parent.setChecked(true);
              //TODO schlechte idee. zählt auch, wen schon geckecht
              numOfChecked++;
            }
            numOfChecked++;
          } else {
            // all models get deselected, when project is deselected.
            if (null == parent) {
              for (TreeItem child : item.getItems()) {
                child.setChecked(false);
                numOfChecked--;
              }
            }
            numOfChecked--;
          }
          if(numOfChecked > 0){
            setPageComplete(true);
          } else{
            setPageComplete(false);
          }
        }
        System.out.println(numOfChecked);
      }
    });

    // text1.setText("");
    // text1.addKeyListener(new KeyListener() {
    //
    // @Override
    // public void keyPressed(KeyEvent e) {
    // // TODO Auto-generated method stub
    // }
    //
    // @Override
    // public void keyReleased(KeyEvent e) {
    // if (!text1.getText().isEmpty()) {
    // setPageComplete(true);
    // }
    // }
    //
    // });
    GridData gd = new GridData(GridData.FILL_HORIZONTAL);
    // text1.setLayoutData(gd);
    Label labelCheck = new Label(container, NONE);
    labelCheck.setText("This is a check");
    Button b = new Button(container, SWT.CHECK);
    b.setSelection(true);

    setControl(container);
    setPageComplete(false);
  }

  public Text getText1() {
    return text1;
  }

  private IProject[] getProjects() {
    return ResourcesPlugin.getWorkspace().getRoot().getProjects();
  }

}

@@ -129,16 +129,162 @@
 Plugin;%0A
+import org.eclipse.core.runtime.IConfigurationElement;%0Aimport org.eclipse.core.runtime.Platform;%0Aimport org.eclipse.core.runtime.RegistryFactory;%0A
 import o
@@ -941,36 +941,8 @@
 t.%22;
-%0A  private int numOfChecked;
 %0A%0A  
@@ -1111,30 +1111,8 @@
 N);%0A
-    numOfChecked = 0;%0A
     
@@ -1968,16 +1968,274 @@
 jects);%0A
+    %0A    //Versuch mit den Extensionpoints%0A    IConfigurationElement%5B%5D ice = Platform.getExtensionRegistry().getConfigurationElementsFor(%22tools.vitruv.framework.vsum.domain%22);%0A    System.out.println(ice.length);%0A//    RegistryFactory.getRegistry().get;%0A    %0A
     for 
@@ -2726,143 +2726,8 @@
 em;%0A
-          System.out.println(item.getClass());%0A          System.out.println(item);%0A          System.out.println(item.getParentItem());%0A
     
@@ -2875,16 +2875,17 @@
 's model
+s
  is chec
@@ -2977,135 +2977,9 @@
     
-  //TODO schlechte idee. z%C3%A4hlt auch, wen schon geckecht%0A              numOfChecked++;%0A            %7D%0A            numOfChecked++;
+%7D
 %0A   
@@ -3208,36 +3208,18 @@
         
-  numOfChecked--;%0A  
+%7D%0A
         
@@ -3226,34 +3226,32 @@
     %7D%0A          
-  
 %7D%0A            nu
@@ -3250,157 +3250,81 @@
     
-  numOfChecked--;%0A          %7D%0A          if(numOfChecked %3E 0)%7B%0A            setPageComplete(true);%0A          %7D else%7B%0A            setPageComplete(
+//only finish if something is checked.%0A          boolean finished = 
 false
-)
 ;%0A  
@@ -3335,448 +3335,184 @@
     
-%7D%0A        %7D%0A        System.out.println(numOfChecked);%0A      %7D%0A    %7D);%0A%0A    // text1.setText(%22%22);%0A    // text1.addKeyListener(new KeyListener() 
+for(TreeItem treeItem : tree.getItems())
 %7B%0A    
-//%0A
     
-// @Override%0A    // public void keyPressed(KeyEvent e) %7B%0A    // // TODO Auto-generated method stub%0A    // %7D%0A    //%0A    // @Override%0A    // public void keyReleased(KeyEvent e) %7B%0A    // if (!text1.getText().isEmpty()) %7B%0A    // setPageComplete(true);%0A    // %7D%0A    // %7D%0A
+    finished = finished %7C%7C treeItem.getChecked();%0A          %7D%0A          setPageComplete(finished);%0A        %7D%0A  
     
-//
+%7D
 %0A   
- //
  %7D);%0A
+%0A
     

Add finish mechanism.