View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.slc.client.ui.wizards;
17  
18  import javax.jcr.Node;
19  import javax.jcr.RepositoryException;
20  import javax.jcr.Session;
21  
22  import org.argeo.slc.SlcException;
23  import org.argeo.slc.client.ui.ClientUiPlugin;
24  import org.argeo.slc.client.ui.SlcUiConstants;
25  import org.argeo.slc.jcr.SlcJcrResultUtils;
26  import org.eclipse.jface.dialogs.MessageDialog;
27  import org.eclipse.jface.wizard.Wizard;
28  import org.eclipse.jface.wizard.WizardPage;
29  import org.eclipse.swt.SWT;
30  import org.eclipse.swt.events.ModifyEvent;
31  import org.eclipse.swt.events.ModifyListener;
32  import org.eclipse.swt.events.SelectionAdapter;
33  import org.eclipse.swt.events.SelectionEvent;
34  import org.eclipse.swt.layout.GridData;
35  import org.eclipse.swt.layout.GridLayout;
36  import org.eclipse.swt.widgets.Button;
37  import org.eclipse.swt.widgets.Composite;
38  import org.eclipse.swt.widgets.Display;
39  import org.eclipse.swt.widgets.Label;
40  import org.eclipse.swt.widgets.Text;
41  import org.eclipse.ui.ISharedImages;
42  
43  public class ConfirmOverwriteWizard extends Wizard {
44  
45  	// Define widget here to simplify getters
46  	private Button overwriteBtn, renameBtn;
47  	private Text newNameTxt;
48  	private Label newNameLbl;
49  
50  	// business object
51  	private String sourceNodeName;
52  	private Node targetParentNode;
53  
54  	private String newName;
55  	private String parentRelPath;
56  	private boolean overwrite;
57  
58  	public ConfirmOverwriteWizard(String sourceNodeName, Node targetParentNode) {
59  		setWindowTitle("Confirm overwrite or define a new name");
60  		this.sourceNodeName = sourceNodeName;
61  		this.targetParentNode = targetParentNode;
62  	}
63  
64  	@Override
65  	public void addPages() {
66  		try {
67  			addPage(new MyPage());
68  		} catch (Exception e) {
69  			throw new SlcException("Cannot add page to wizard ", e);
70  		}
71  		getShell().setImage(
72  				ClientUiPlugin.getDefault().getWorkbench().getSharedImages()
73  						.getImageDescriptor(ISharedImages.IMG_LCL_LINKTO_HELP)
74  						.createImage());
75  	}
76  
77  	// Expose info to the calling view
78  	public boolean overwrite() {
79  		return overwrite;
80  	}
81  
82  	public String newName() {
83  		return newName;
84  	}
85  
86  	@Override
87  	public boolean performFinish() {
88  		boolean doFinish = false;
89  
90  		if (canFinish()) {
91  			if (overwriteBtn.getSelection())
92  				doFinish = MessageDialog.openConfirm(Display.getDefault()
93  						.getActiveShell(), "CAUTION", "All data contained in ["
94  						+ (parentRelPath != null ? parentRelPath : "")
95  						+ "/"+ sourceNodeName
96  						+ "] are about to be definitively destroyed. \n "
97  						+ "Are you sure you want to proceed ?");
98  			else
99  				doFinish = true;
100 			// cache values
101 		}
102 		if (doFinish) {
103 			overwrite = overwriteBtn.getSelection();
104 			newName = newNameTxt.getText();
105 		}
106 		return doFinish;
107 	}
108 
109 	class MyPage extends WizardPage implements ModifyListener {
110 
111 		public MyPage() {
112 			super("");
113 			String msg = "An object with same name (" + sourceNodeName
114 					+ ") already exists at chosen target path";
115 
116 			// Add target rel path to the message
117 			Session session;
118 			String relPath;
119 			try {
120 				session = targetParentNode.getSession();
121 				relPath = targetParentNode.getPath();
122 				String basePath = SlcJcrResultUtils
123 						.getMyResultsBasePath(session);
124 				if (relPath.startsWith(basePath))
125 					relPath = relPath.substring(basePath.length());
126 				// FIXME currently add the default base label
127 				parentRelPath = SlcUiConstants.DEFAULT_MY_RESULTS_FOLDER_LABEL
128 						+ relPath;
129 			} catch (RepositoryException e) {
130 				throw new SlcException("Unexpected error while defining "
131 						+ "target parent node rel path", e);
132 			}
133 			msg = msg + (parentRelPath == null ? "." : ": \n" + parentRelPath);
134 
135 			// Set Title
136 			setTitle(msg);
137 		}
138 
139 		public void createControl(Composite parent) {
140 			Composite composite = new Composite(parent, SWT.NONE);
141 			composite.setLayout(new GridLayout(2, false));
142 
143 			// choose between overwrite and rename
144 			overwriteBtn = new Button(composite, SWT.RADIO);
145 			overwriteBtn.setText("Overwrite");
146 			GridData gd = new GridData();
147 			gd.horizontalIndent = 30;
148 			gd.horizontalSpan = 2;
149 			overwriteBtn.setLayoutData(gd);
150 			overwriteBtn.setSelection(true);
151 
152 			renameBtn = new Button(composite, SWT.RADIO);
153 			renameBtn.setText("Rename");
154 			renameBtn.setSelection(false);
155 			renameBtn.setText("Rename");
156 			gd = new GridData();
157 			gd.horizontalIndent = 30;
158 			gd.horizontalSpan = 2;
159 			renameBtn.setLayoutData(gd);
160 
161 			newNameLbl = new Label(composite, SWT.LEAD);
162 			newNameLbl.setText("New name");
163 			newNameLbl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
164 					false));
165 			newNameLbl.setEnabled(false);
166 
167 			newNameTxt = new Text(composite, SWT.LEAD | SWT.BORDER);
168 			newNameTxt.setText(sourceNodeName);
169 			newNameTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
170 					false));
171 			if (newNameTxt != null)
172 				newNameTxt.addModifyListener(this);
173 			newNameTxt.setEnabled(false);
174 
175 			SelectionAdapter sa = new SelectionAdapter() {
176 				public void widgetSelected(SelectionEvent e) {
177 					updateSelection(overwriteBtn.getSelection());
178 				}
179 			};
180 			overwriteBtn.addSelectionListener(sa);
181 			renameBtn.addSelectionListener(sa);
182 
183 			// Compulsory
184 			setControl(composite);
185 		}
186 
187 		private void updateSelection(boolean overwrite) {
188 			newNameLbl.setEnabled(!overwrite);
189 			newNameTxt.setEnabled(!overwrite);
190 			if (overwrite)
191 				setPageComplete(true);
192 			else
193 				checkComplete();
194 		}
195 
196 		protected String getTechName() {
197 			return newNameTxt.getText();
198 		}
199 
200 		public void modifyText(ModifyEvent event) {
201 			checkComplete();
202 		}
203 
204 		private void checkComplete() {
205 			try {
206 
207 				String newName = newNameTxt.getText();
208 				if (newName == null || "".equals(newName.trim())) {
209 					setMessage("Name cannot be blank or empty",
210 							WizardPage.ERROR);
211 					setPageComplete(false);
212 				} else if (targetParentNode.hasNode(newName)) {
213 					setMessage("An object with the same name already exists.",
214 							WizardPage.ERROR);
215 					setPageComplete(false);
216 				} else {
217 					setMessage("Complete", WizardPage.INFORMATION);
218 					setPageComplete(true);
219 				}
220 			} catch (RepositoryException e) {
221 				throw new SlcException("Unexpected error while checking "
222 						+ "children node with same name", e);
223 			}
224 		}
225 	}
226 }