2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.client
.ui
.wizards
;
18 import javax
.jcr
.Node
;
19 import javax
.jcr
.RepositoryException
;
20 import javax
.jcr
.Session
;
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
;
43 public class ConfirmOverwriteWizard
extends Wizard
{
45 // Define widget here to simplify getters
46 private Button overwriteBtn
, renameBtn
;
47 private Text newNameTxt
;
48 private Label newNameLbl
;
51 private String sourceNodeName
;
52 private Node targetParentNode
;
54 private String newName
;
55 private String parentRelPath
;
56 private boolean overwrite
;
58 public ConfirmOverwriteWizard(String sourceNodeName
, Node targetParentNode
) {
59 setWindowTitle("Confirm overwrite or define a new name");
60 this.sourceNodeName
= sourceNodeName
;
61 this.targetParentNode
= targetParentNode
;
65 public void addPages() {
67 addPage(new MyPage());
68 } catch (Exception e
) {
69 throw new SlcException("Cannot add page to wizard ", e
);
72 ClientUiPlugin
.getDefault().getWorkbench().getSharedImages()
73 .getImageDescriptor(ISharedImages
.IMG_LCL_LINKTO_HELP
)
77 // Expose info to the calling view
78 public boolean overwrite() {
82 public String
newName() {
87 public boolean performFinish() {
88 boolean doFinish
= false;
91 if (overwriteBtn
.getSelection())
92 doFinish
= MessageDialog
.openConfirm(Display
.getDefault()
93 .getActiveShell(), "CAUTION", "All data contained in ["
94 + (parentRelPath
!= null ? parentRelPath
: "")
96 + "] are about to be definitively destroyed. \n "
97 + "Are you sure you want to proceed ?");
103 overwrite
= overwriteBtn
.getSelection();
104 newName
= newNameTxt
.getText();
109 class MyPage
extends WizardPage
implements ModifyListener
{
113 String msg
= "An object with same name (" + sourceNodeName
114 + ") already exists at chosen target path";
116 // Add target rel path to the message
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
129 } catch (RepositoryException e
) {
130 throw new SlcException("Unexpected error while defining "
131 + "target parent node rel path", e
);
133 msg
= msg
+ (parentRelPath
== null ?
"." : ": \n" + parentRelPath
);
139 public void createControl(Composite parent
) {
140 Composite composite
= new Composite(parent
, SWT
.NONE
);
141 composite
.setLayout(new GridLayout(2, false));
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);
152 renameBtn
= new Button(composite
, SWT
.RADIO
);
153 renameBtn
.setText("Rename");
154 renameBtn
.setSelection(false);
155 renameBtn
.setText("Rename");
157 gd
.horizontalIndent
= 30;
158 gd
.horizontalSpan
= 2;
159 renameBtn
.setLayoutData(gd
);
161 newNameLbl
= new Label(composite
, SWT
.LEAD
);
162 newNameLbl
.setText("New name");
163 newNameLbl
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false,
165 newNameLbl
.setEnabled(false);
167 newNameTxt
= new Text(composite
, SWT
.LEAD
| SWT
.BORDER
);
168 newNameTxt
.setText(sourceNodeName
);
169 newNameTxt
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true,
171 if (newNameTxt
!= null)
172 newNameTxt
.addModifyListener(this);
173 newNameTxt
.setEnabled(false);
175 SelectionAdapter sa
= new SelectionAdapter() {
176 public void widgetSelected(SelectionEvent e
) {
177 updateSelection(overwriteBtn
.getSelection());
180 overwriteBtn
.addSelectionListener(sa
);
181 renameBtn
.addSelectionListener(sa
);
184 setControl(composite
);
187 private void updateSelection(boolean overwrite
) {
188 newNameLbl
.setEnabled(!overwrite
);
189 newNameTxt
.setEnabled(!overwrite
);
191 setPageComplete(true);
196 protected String
getTechName() {
197 return newNameTxt
.getText();
200 public void modifyText(ModifyEvent event
) {
204 private void checkComplete() {
207 String newName
= newNameTxt
.getText();
208 if (newName
== null || "".equals(newName
.trim())) {
209 setMessage("Name cannot be blank or empty",
211 setPageComplete(false);
212 } else if (targetParentNode
.hasNode(newName
)) {
213 setMessage("An object with the same name already exists.",
215 setPageComplete(false);
217 setMessage("Complete", WizardPage
.INFORMATION
);
218 setPageComplete(true);
220 } catch (RepositoryException e
) {
221 throw new SlcException("Unexpected error while checking "
222 + "children node with same name", e
);