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