mywork-web/src/main/java/ru/indvdum/mywork/vaadin/EditTaskDialog.java
changeset 16 2d6a668325f9
parent 14 1d1d4c94d251
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mywork-web/src/main/java/ru/indvdum/mywork/vaadin/EditTaskDialog.java	Wed Nov 23 17:52:25 2011 +0300
     1.3 @@ -0,0 +1,116 @@
     1.4 +package ru.indvdum.mywork.vaadin;
     1.5 +
     1.6 +import static ru.indvdum.mywork.MyWork.EM;
     1.7 +import ru.indvdum.mywork.openjpa.model.Task;
     1.8 +
     1.9 +import com.vaadin.ui.Alignment;
    1.10 +import com.vaadin.ui.Button;
    1.11 +import com.vaadin.ui.HorizontalLayout;
    1.12 +import com.vaadin.ui.Label;
    1.13 +import com.vaadin.ui.RichTextArea;
    1.14 +import com.vaadin.ui.TextField;
    1.15 +import com.vaadin.ui.VerticalLayout;
    1.16 +import com.vaadin.ui.Window;
    1.17 +import com.vaadin.ui.Button.ClickEvent;
    1.18 +import com.vaadin.ui.Button.ClickListener;
    1.19 +
    1.20 +/**
    1.21 + * @author indvdum
    1.22 + * 23.08.2011 19:11:50
    1.23 + *
    1.24 + */
    1.25 +public class EditTaskDialog extends Window implements ClickListener {
    1.26 +	
    1.27 +	private MyWorkApplication myApp = null;
    1.28 +	private EditState state = null;
    1.29 +	private Task task = null;
    1.30 +	private TextField taskName = null;
    1.31 +	private RichTextArea taskDescription = null;
    1.32 +
    1.33 +	public EditTaskDialog(MyWorkApplication myApp, Task task) throws Exception {
    1.34 +		super();
    1.35 +		this.myApp = myApp;
    1.36 +		if(task == null)
    1.37 +			state = EditState.CREATE;
    1.38 +		else 
    1.39 +			state = EditState.EDIT;
    1.40 +		switch(state){
    1.41 +		case CREATE:
    1.42 +			setCaption("Creating new task");
    1.43 +			task = new Task();
    1.44 +			task.setName("");
    1.45 +			task.setDescription("");
    1.46 +			this.task = task;
    1.47 +			break;
    1.48 +		case EDIT:
    1.49 +			setCaption("Editing task");
    1.50 +			this.task = task;
    1.51 +			break;
    1.52 +		}
    1.53 +		init();
    1.54 +	}
    1.55 +	
    1.56 +	private void init(){
    1.57 +		setModal(true);
    1.58 +		setWidth("640px");
    1.59 +		setHeight("480px");
    1.60 +		VerticalLayout vl = new VerticalLayout();
    1.61 +		vl.setSizeFull();
    1.62 +		vl.setSpacing(true);
    1.63 +		vl.setMargin(true);
    1.64 +		setContent(vl);
    1.65 +		
    1.66 +		HorizontalLayout hlName = new HorizontalLayout();
    1.67 +		hlName.setWidth("100%");
    1.68 +		Label lblName = new Label("Name:");
    1.69 +		lblName.setWidth("100%");
    1.70 +		hlName.addComponent(lblName);
    1.71 +		hlName.setExpandRatio(lblName, 1f);
    1.72 +		taskName = new TextField();
    1.73 +		taskName.setWidth("100%");
    1.74 +		taskName.setValue(task.getName());
    1.75 +		hlName.addComponent(taskName);
    1.76 +		hlName.setExpandRatio(taskName, 5f);
    1.77 +		vl.addComponent(hlName);
    1.78 +		
    1.79 +		taskDescription = new RichTextArea("Description");
    1.80 +		taskDescription.setValue(task.getDescription());
    1.81 +		taskDescription.setSizeFull();
    1.82 +		vl.addComponent(taskDescription);
    1.83 +		vl.setExpandRatio(taskDescription, 1f);
    1.84 +		
    1.85 +		HorizontalLayout hlOk = new HorizontalLayout();
    1.86 +		hlOk.setWidth("100%");
    1.87 +		Button okButton = new Button();
    1.88 +		switch(state){
    1.89 +		case CREATE:
    1.90 +			okButton.setCaption("Create");
    1.91 +			break;
    1.92 +		case EDIT:
    1.93 +			okButton.setCaption("Save");
    1.94 +			break;
    1.95 +		}
    1.96 +		okButton.addListener((ClickListener)this);
    1.97 +		hlOk.addComponent(okButton);
    1.98 +		hlOk.setComponentAlignment(okButton, Alignment.MIDDLE_RIGHT);
    1.99 +		vl.addComponent(hlOk);
   1.100 +	}
   1.101 +
   1.102 +	@Override
   1.103 +	public void buttonClick(ClickEvent event) {
   1.104 +		task.setName(taskName.getValue().toString());
   1.105 +		task.setDescription(taskDescription.getValue().toString());
   1.106 +		try{
   1.107 +			EM.getTransaction().begin();
   1.108 +			EM.persist(task);
   1.109 +			EM.getTransaction().commit();
   1.110 +			EM.clear();
   1.111 +		} finally {
   1.112 +			if(EM.getTransaction().isActive())
   1.113 +				EM.getTransaction().rollback();
   1.114 +		}
   1.115 +		getParent().removeWindow(this);
   1.116 +		myApp.getTaskTable().requestRepaint();
   1.117 +	}
   1.118 +
   1.119 +}