View Javadoc
1   package org.argeo.people.ui.dialogs;
2   
3   import static org.argeo.eclipse.ui.EclipseUiUtils.isEmpty;
4   
5   import javax.jcr.Node;
6   import javax.jcr.PropertyType;
7   
8   import org.argeo.connect.ui.ConnectUiUtils;
9   import org.argeo.connect.util.ConnectJcrUtils;
10  import org.argeo.eclipse.ui.EclipseUiUtils;
11  import org.argeo.people.PeopleException;
12  import org.argeo.people.PeopleNames;
13  import org.argeo.people.ui.PeopleMsg;
14  import org.eclipse.jface.dialogs.MessageDialog;
15  import org.eclipse.jface.wizard.Wizard;
16  import org.eclipse.jface.wizard.WizardPage;
17  import org.eclipse.swt.SWT;
18  import org.eclipse.swt.events.ModifyEvent;
19  import org.eclipse.swt.events.ModifyListener;
20  import org.eclipse.swt.layout.GridData;
21  import org.eclipse.swt.layout.GridLayout;
22  import org.eclipse.swt.widgets.Composite;
23  import org.eclipse.swt.widgets.Text;
24  
25  /** Ask first & last name. Update the passed node on finish */
26  public class NewPersonWizard extends Wizard implements PeopleNames {
27  	// private final static Log log = LogFactory.getLog(NewPersonWizard.class);
28  
29  	// Context
30  	private Node person;
31  
32  	// This page widgets
33  	protected Text lastNameTxt;
34  	protected Text firstNameTxt;
35  	// private Button useDistinctDisplayNameBtn;
36  	// private Text displayNameTxt;
37  
38  	public NewPersonWizard(Node person) {
39  		this.person = person;
40  	}
41  
42  	@Override
43  	public void addPages() {
44  		try {
45  			MainInfoPage page = new MainInfoPage("Main page");
46  			addPage(page);
47  		} catch (Exception e) {
48  			throw new PeopleException("Cannot add page to wizard", e);
49  		}
50  		setWindowTitle(PeopleMsg.personWizardWindowTitle.lead());
51  	}
52  
53  	/**
54  	 * Called when the user click on 'Finish' in the wizard. The task is then
55  	 * created and the corresponding session saved.
56  	 */
57  	@Override
58  	public boolean performFinish() {
59  		String lastName = lastNameTxt.getText();
60  		String firstName = firstNameTxt.getText();
61  		// String displayName = displayNameTxt.getText();
62  		// boolean useDistinct = useDistinctDisplayNameBtn.getSelection();
63  		if (EclipseUiUtils.isEmpty(lastName) && EclipseUiUtils.isEmpty(firstName)) {
64  			MessageDialog.openError(getShell(), "Non-valid information",
65  					"Please enter at least a name that is not empty.");
66  			return false;
67  		} else {
68  			ConnectJcrUtils.setJcrProperty(person, PEOPLE_LAST_NAME, PropertyType.STRING, lastName);
69  			ConnectJcrUtils.setJcrProperty(person, PEOPLE_FIRST_NAME, PropertyType.STRING, firstName);
70  			String fullName = firstName + " " + lastName;
71  			//ConnectJcrUtils.setJcrProperty(person, PEOPLE_LEGAL_NAME, PropertyType.STRING, fullName);
72  			ConnectJcrUtils.setJcrProperty(person, PEOPLE_DISPLAY_NAME, PropertyType.STRING, fullName);
73  			// if (useDistinct && EclipseUiUtils.notEmpty(displayName))
74  			// ConnectJcrUtils.setJcrProperty(person, PEOPLE_DISPLAY_NAME,
75  			// PropertyType.STRING, displayName);
76  			return true;
77  		}
78  	}
79  
80  	@Override
81  	public boolean performCancel() {
82  		return true;
83  	}
84  
85  	@Override
86  	public boolean canFinish() {
87  		String lastName = lastNameTxt.getText();
88  		String firstName = firstNameTxt.getText();
89  		if (isEmpty(lastName) && isEmpty(firstName)) {
90  			return false;
91  		} else
92  			return true;
93  	}
94  
95  	protected class MainInfoPage extends WizardPage {
96  		private static final long serialVersionUID = 1L;
97  
98  		public MainInfoPage(String pageName) {
99  			super(pageName);
100 			setTitle(PeopleMsg.personWizardPageTitle.lead());
101 			// setMessage("Please enter a last name and/or a first name.");
102 		}
103 
104 		public void createControl(Composite parent) {
105 			parent.setLayout(new GridLayout(2, false));
106 
107 			// FirstName
108 			ConnectUiUtils.createBoldLabel(parent, PeopleMsg.firstName.lead());
109 			firstNameTxt = new Text(parent, SWT.BORDER);
110 			// firstNameTxt.setMessage("a first name");
111 			firstNameTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
112 
113 			// LastName
114 			ConnectUiUtils.createBoldLabel(parent, PeopleMsg.lastName.lead());
115 			lastNameTxt = new Text(parent, SWT.BORDER);
116 			// lastNameTxt.setMessage("a last name");
117 			lastNameTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
118 
119 			// Display Name
120 			// useDistinctDisplayNameBtn = new Button(parent, SWT.CHECK);
121 			// useDistinctDisplayNameBtn.setText("Define a disting display name");
122 			// useDistinctDisplayNameBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
123 			// true, false, 2, 1));
124 			//
125 			// ConnectWorkbenchUtils.createBoldLabel(parent, "Display Name");
126 			// displayNameTxt = new Text(parent, SWT.BORDER);
127 			// displayNameTxt.setMessage("an optional display name");
128 			// displayNameTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
129 			// false));
130 			// displayNameTxt.setEnabled(false);
131 			//
132 			// useDistinctDisplayNameBtn.addSelectionListener(new SelectionAdapter() {
133 			// private static final long serialVersionUID = 1L;
134 			//
135 			// @Override
136 			// public void widgetSelected(SelectionEvent e) {
137 			// displayNameTxt.setEnabled(useDistinctDisplayNameBtn.getSelection());
138 			// }
139 			// });
140 
141 			ModifyListener ml = new ModifyListener() {
142 				private static final long serialVersionUID = -1628130380128946886L;
143 
144 				@Override
145 				public void modifyText(ModifyEvent event) {
146 					getContainer().updateButtons();
147 				}
148 			};
149 
150 			firstNameTxt.addModifyListener(ml);
151 			lastNameTxt.addModifyListener(ml);
152 			// displayNameTxt.addModifyListener(ml);
153 
154 			// Don't forget this.
155 			setControl(firstNameTxt);
156 			firstNameTxt.setFocus();
157 		}
158 	}
159 }