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