View Javadoc
1   package org.argeo.tracker.ui.dialogs;
2   
3   import static javax.jcr.PropertyType.STRING;
4   import static org.argeo.connect.ConnectNames.CONNECT_UID;
5   import static org.argeo.connect.util.ConnectJcrUtils.get;
6   import static org.argeo.eclipse.ui.EclipseUiUtils.isEmpty;
7   import static org.argeo.tracker.TrackerNames.TRACKER_PROJECT_UID;
8   
9   import javax.jcr.Node;
10  import javax.jcr.Property;
11  import javax.jcr.PropertyType;
12  import javax.jcr.RepositoryException;
13  
14  import org.argeo.connect.ui.ConnectUiUtils;
15  import org.argeo.connect.util.ConnectJcrUtils;
16  import org.argeo.eclipse.ui.EclipseUiUtils;
17  import org.argeo.jcr.JcrUtils;
18  import org.argeo.tracker.TrackerException;
19  import org.argeo.tracker.TrackerNames;
20  import org.argeo.tracker.TrackerService;
21  import org.argeo.tracker.core.TrackerUtils;
22  import org.argeo.tracker.ui.controls.ProjectDropDown;
23  import org.eclipse.jface.dialogs.MessageDialog;
24  import org.eclipse.jface.wizard.Wizard;
25  import org.eclipse.jface.wizard.WizardPage;
26  import org.eclipse.swt.SWT;
27  import org.eclipse.swt.events.FocusAdapter;
28  import org.eclipse.swt.events.FocusEvent;
29  import org.eclipse.swt.events.ModifyEvent;
30  import org.eclipse.swt.events.ModifyListener;
31  import org.eclipse.swt.layout.GridData;
32  import org.eclipse.swt.layout.GridLayout;
33  import org.eclipse.swt.widgets.Composite;
34  import org.eclipse.swt.widgets.Label;
35  import org.eclipse.swt.widgets.Text;
36  
37  /** Dialog to simply configure a component */
38  public class ConfigureComponentWizard extends Wizard implements ModifyListener {
39  	private static final long serialVersionUID = -8365425809976445458L;
40  
41  	// Context
42  	private final Node project;
43  	private final Node component;
44  
45  	private Node chosenProject;
46  
47  	// UI controls
48  	private Text idTxt;
49  	private ProjectDropDown projectDD;
50  	private Text descTxt;
51  
52  	public ConfigureComponentWizard(TrackerService trackerService, Node component) {
53  		// this.trackerService = trackerService;
54  		this.component = component;
55  		project = TrackerUtils.getRelatedProject(trackerService, component);
56  		chosenProject = project;
57  	}
58  
59  	@Override
60  	public void addPages() {
61  		setWindowTitle("Create a new component");
62  		addPage(new MainPage("Main page"));
63  	}
64  
65  	@Override
66  	public boolean performFinish() {
67  		String id = idTxt.getText();
68  		if (EclipseUiUtils.isEmpty(id)) {
69  			MessageDialog.openError(getShell(), "Compulsory ID", "Please define the component ID");
70  			return false;
71  			// } else if (TrackerUtils.getComponentById(project, getId()) !=
72  			// null) {
73  			// MessageDialog.openError(getShell(), "Already existing component",
74  			// "A component with ID " + getId() + " already exists, cannot
75  			// create");
76  			// return false;
77  		}
78  		try {
79  			ConnectJcrUtils.setJcrProperty(component, TRACKER_PROJECT_UID, STRING, get(chosenProject, CONNECT_UID));
80  			ConnectJcrUtils.setJcrProperty(component, TrackerNames.TRACKER_ID, PropertyType.STRING, id);
81  			ConnectJcrUtils.setJcrProperty(component, Property.JCR_TITLE, PropertyType.STRING, id);
82  			ConnectJcrUtils.setJcrProperty(component, Property.JCR_DESCRIPTION, PropertyType.STRING, descTxt.getText());
83  
84  			if (component.getSession().hasPendingChanges())
85  				JcrUtils.updateLastModified(component);
86  		} catch (RepositoryException e1) {
87  			throw new TrackerException("Unable to create component with ID " + getId() + " on " + project, e1);
88  		}
89  		return true;
90  	}
91  
92  	@Override
93  	public boolean canFinish() {
94  		if (EclipseUiUtils.isEmpty(getId()))
95  			return false;
96  		else
97  			return true;
98  	}
99  
100 	@Override
101 	public boolean performCancel() {
102 		return true;
103 	}
104 
105 	protected class MainPage extends WizardPage {
106 		private static final long serialVersionUID = 3061153468301727903L;
107 		private Text projectTxt;
108 
109 		public MainPage(String pageName) {
110 			super(pageName);
111 			setMessage("Please complete belowinformation.");
112 		}
113 
114 		public void createControl(Composite parent) {
115 			parent.setLayout(new GridLayout(2, false));
116 
117 			// Project
118 			ConnectUiUtils.createBoldLabel(parent, "Project");
119 			projectTxt = new Text(parent, SWT.BORDER);
120 			projectTxt.setMessage("Choose relevant project");
121 			projectTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
122 
123 			if (project == null) {
124 				projectDD = new ProjectDropDown(ConnectJcrUtils.getSession(component), projectTxt, false);
125 
126 				projectTxt.addFocusListener(new FocusAdapter() {
127 					private static final long serialVersionUID = 1L;
128 
129 					@Override
130 					public void focusLost(FocusEvent event) {
131 						Node project = projectDD.getChosenProject();
132 						if (project == null)
133 							setErrorMessage("Choose a valid project");
134 						else {
135 							setErrorMessage(null);
136 							chosenProject = project;
137 						}
138 					}
139 				});
140 			} else
141 				projectTxt.setEditable(false);
142 
143 			createLabel(parent, "Name", SWT.CENTER);
144 			idTxt = new Text(parent, SWT.BORDER);
145 			idTxt.setMessage("A short name for this component, that is also used as ID within this project");
146 			idTxt.setLayoutData(EclipseUiUtils.fillWidth());
147 			idTxt.addModifyListener(ConfigureComponentWizard.this);
148 
149 			createLabel(parent, "Description", SWT.TOP);
150 			descTxt = new Text(parent, SWT.BORDER | SWT.MULTI | SWT.WRAP);
151 			GridData gd = EclipseUiUtils.fillAll();
152 			gd.heightHint = 150;
153 			descTxt.setLayoutData(gd);
154 			descTxt.setMessage("An optional description for this component");
155 
156 			String id = ConnectJcrUtils.get(component, TrackerNames.TRACKER_ID);
157 			String desc = ConnectJcrUtils.get(component, Property.JCR_DESCRIPTION);
158 			if (project != null)
159 				projectTxt.setText(ConnectJcrUtils.get(project, Property.JCR_TITLE));
160 
161 			if (EclipseUiUtils.notEmpty(id)) {
162 				idTxt.setText(id);
163 				idTxt.setEditable(false);
164 			}
165 			if (EclipseUiUtils.notEmpty(desc))
166 				descTxt.setText(desc);
167 
168 			if (project == null) {
169 				setControl(projectTxt);
170 				projectTxt.setFocus();
171 			} else if (isEmpty(id)) {
172 				setControl(idTxt);
173 				idTxt.setFocus();
174 			} else
175 				setControl(idTxt);
176 
177 		}
178 	}
179 
180 	private String getId() {
181 		return idTxt.getText();
182 	}
183 
184 	private Label createLabel(Composite parent, String label, int verticalAlign) {
185 		Label lbl = new Label(parent, SWT.NONE);
186 		lbl.setText(label);
187 		lbl.setFont(EclipseUiUtils.getBoldFont(parent));
188 		lbl.setLayoutData(new GridData(SWT.RIGHT, verticalAlign, false, false));
189 		return lbl;
190 	}
191 
192 	@Override
193 	public void modifyText(ModifyEvent event) {
194 		getContainer().updateButtons();
195 	}
196 }