View Javadoc
1   package org.argeo.activities.ui;
2   
3   import static org.argeo.eclipse.ui.EclipseUiUtils.isEmpty;
4   
5   import java.util.GregorianCalendar;
6   import java.util.List;
7   
8   import javax.jcr.Node;
9   import javax.jcr.RepositoryException;
10  
11  import org.argeo.activities.ActivitiesException;
12  import org.argeo.activities.ActivitiesService;
13  import org.argeo.activities.ActivitiesTypes;
14  import org.argeo.connect.UserAdminService;
15  import org.argeo.connect.core.OfficeRole;
16  import org.argeo.connect.ui.ConnectUiUtils;
17  import org.argeo.connect.ui.widgets.DateText;
18  import org.argeo.connect.ui.widgets.GroupDropDown;
19  import org.argeo.eclipse.ui.EclipseUiUtils;
20  import org.eclipse.jface.dialogs.MessageDialog;
21  import org.eclipse.jface.viewers.TableViewer;
22  import org.eclipse.jface.wizard.Wizard;
23  import org.eclipse.jface.wizard.WizardPage;
24  import org.eclipse.swt.SWT;
25  import org.eclipse.swt.layout.GridData;
26  import org.eclipse.swt.layout.GridLayout;
27  import org.eclipse.swt.widgets.Composite;
28  import org.eclipse.swt.widgets.Label;
29  import org.eclipse.swt.widgets.Text;
30  
31  /**
32   * Generic wizard to created a new task. Caller might use setRelatedTo method to
33   * add some related entities after creation and before opening the wizard
34   * dialog.
35   * 
36   * The newly created task might be retrieved after termination if Dialog.open()
37   * return Window.OK.
38   * 
39   * Warning: the passed session is not saved: the task stays in a transient mode
40   * until the caller save the session to enable rollback.
41   */
42  
43  public class NewSimpleTaskWizard extends Wizard {
44  	// private final static Log log = LogFactory
45  	// .getLog(CreateSimpleTaskWizard.class);
46  
47  	// Set upon instantiation
48  	private final UserAdminService userAdminService;
49  	private final ActivitiesService activityService;
50  	private final Node draftTask;
51  
52  	// Business objects
53  	private List<Node> relatedTo;
54  	// private String assignedToGroupId;
55  
56  	// This page widgets
57  	protected Text titleTxt;
58  	protected Text descTxt;
59  	private GroupDropDown assignedToDD;
60  
61  	private DateText dueDateCmp;
62  	private DateText wakeUpDateCmp;
63  
64  	protected TableViewer itemsViewer;
65  
66  	public NewSimpleTaskWizard(UserAdminService userAdminService, ActivitiesService activityService, Node draftTask) {
67  		this.userAdminService = userAdminService;
68  		this.activityService = activityService;
69  		this.draftTask = draftTask;
70  	}
71  
72  	public void setRelatedTo(List<Node> relatedTo) {
73  		this.relatedTo = relatedTo;
74  	}
75  
76  	// // Exposes to extending classes
77  	// protected Session getSession() {
78  	// return currSession;
79  	// }
80  
81  	@Override
82  	public void addPages() {
83  		setWindowTitle("Create a task");
84  		SelectChildrenPage page = new SelectChildrenPage("Main page");
85  		addPage(page);
86  	}
87  
88  	/**
89  	 * Called when the user click on 'Finish' in the wizard. The task is then
90  	 * created and the corresponding session saved.
91  	 */
92  	@Override
93  	public boolean performFinish() {
94  		// Sanity check
95  		String msg = null;
96  		String title = titleTxt.getText();
97  		if (isEmpty(title))
98  			msg = "Please give at least a title";
99  
100 		// if (isEmpty(assignedToGroupId))
101 		// msg = "Please assign this task to a group.";
102 		if (msg != null) {
103 			MessageDialog.openError(getShell(), "Uncomplete information", msg);
104 			return false;
105 		}
106 
107 		try {
108 			activityService.configureTask(draftTask, ActivitiesTypes.ACTIVITIES_TASK,
109 					draftTask.getSession().getUserID(), titleTxt.getText(), descTxt.getText(), assignedToDD.getText(),
110 					relatedTo, GregorianCalendar.getInstance(), dueDateCmp.getCalendar(), wakeUpDateCmp.getCalendar());
111 		} catch (RepositoryException e) {
112 			throw new ActivitiesException("Unable to create simple task with title " + titleTxt.getText(), e);
113 		}
114 
115 		return true;
116 	}
117 
118 	@Override
119 	public boolean performCancel() {
120 		return true;
121 	}
122 
123 	@Override
124 	public boolean canFinish() {
125 		return true;
126 	}
127 
128 	protected class SelectChildrenPage extends WizardPage {
129 		private static final long serialVersionUID = 1L;
130 
131 		public SelectChildrenPage(String pageName) {
132 			super(pageName);
133 			setTitle("Create a new simple task");
134 			setMessage("Please fill out following information.");
135 		}
136 
137 		public void createControl(Composite parent) {
138 			parent.setLayout(new GridLayout(4, false));
139 
140 			// TITLE
141 			ConnectUiUtils.createBoldLabel(parent, "Title");
142 			titleTxt = new Text(parent, SWT.BORDER);
143 			titleTxt.setMessage("A title for the new task");
144 			GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
145 			gd.horizontalSpan = 3;
146 			titleTxt.setLayoutData(gd);
147 
148 			// ASSIGNED TO
149 			ConnectUiUtils.createBoldLabel(parent, "Assigned to");
150 			Text assignedToTxt = new Text(parent, SWT.BORDER);
151 			assignedToTxt.setMessage("Pick up a group");
152 			assignedToTxt.setToolTipText("Choose a group or person to manage this issue");
153 			assignedToTxt.setLayoutData(EclipseUiUtils.fillWidth(3));
154 			// assignedToDD = new ExistingGroupsDropDown(assignedToTxt, userAdminService,
155 			// true, false);
156 			assignedToDD = new GroupDropDown(assignedToTxt, userAdminService, OfficeRole.coworker.dn());
157 
158 			// ConnectWorkbenchUtils.createBoldLabel(parent, "Assigned to");
159 			// Composite assignedToCmp = new Composite(parent, SWT.NO_FOCUS);
160 			// gd = new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1);
161 			// assignedToCmp.setLayoutData(gd);
162 			// GridLayout gl = EclipseUiUtils.noSpaceGridLayout(new
163 			// GridLayout(2, false));
164 			// gl.horizontalSpacing = 5;
165 			// assignedToCmp.setLayout(gl);
166 			// final Text assignedToTxt = new Text(assignedToCmp, SWT.BORDER |
167 			// SWT.NO_FOCUS);
168 			// assignedToTxt.setMessage("Assign a group to manage this task");
169 			// CmsUtils.style(assignedToTxt, ConnectUiStyles.FORCE_BORDER);
170 			// assignedToTxt.setLayoutData(EclipseUiUtils.fillWidth());
171 			// assignedToTxt.setEnabled(false);
172 			//
173 			// Link assignedToLk = new Link(assignedToCmp, SWT.NONE);
174 			// assignedToLk.setText("<a>Pick up</a>");
175 			// assignedToLk.addSelectionListener(new SelectionAdapter() {
176 			// private static final long serialVersionUID = 1L;
177 			//
178 			// @Override
179 			// public void widgetSelected(final SelectionEvent event) {
180 			// PickUpGroupDialog diag = new
181 			// PickUpGroupDialog(assignedToTxt.getShell(), "Choose a group",
182 			// userAdminService);
183 			// if (diag.open() == Window.OK) {
184 			// assignedToGroupId = diag.getSelected().getName();
185 			// if (EclipseUiUtils.notEmpty(assignedToGroupId))
186 			// assignedToTxt.setText(userAdminService.getUserDisplayName(assignedToGroupId));
187 			// }
188 			// }
189 			// });
190 
191 			// DUE DATE
192 			ConnectUiUtils.createBoldLabel(parent, "Due date");
193 			dueDateCmp = new DateText(parent, SWT.NO_FOCUS);
194 
195 			// WAKE UP DATE
196 			Label lbl = ConnectUiUtils.createBoldLabel(parent, "Wake up date");
197 			gd = new GridData();
198 			gd.horizontalIndent = 15;
199 			lbl.setLayoutData(gd);
200 			wakeUpDateCmp = new DateText(parent, SWT.NO_FOCUS);
201 
202 			// DESCRIPTION
203 			Label label = new Label(parent, SWT.LEAD | SWT.TOP);
204 			label.setText("Description");
205 			label.setFont(EclipseUiUtils.getBoldFont(parent));
206 			gd = new GridData(SWT.LEAD, SWT.TOP, false, false);
207 			label.setLayoutData(gd);
208 
209 			descTxt = new Text(parent, SWT.BORDER | SWT.MULTI | SWT.WRAP);
210 			descTxt.setMessage("A description");
211 			gd = EclipseUiUtils.fillAll();
212 			gd.horizontalSpan = 3;
213 			gd.heightHint = 150;
214 			descTxt.setLayoutData(gd);
215 			// Don't forget this.
216 			setControl(titleTxt);
217 			titleTxt.setFocus();
218 		}
219 	}
220 }