44 |
44 |
import org.txm.rcp.views.fileexplorer.Explorer;
|
45 |
45 |
import org.txm.rcp.views.fileexplorer.MacroExplorer;
|
46 |
46 |
import org.txm.utils.DeleteDir;
|
47 |
|
// TODO: Auto-generated Javadoc
|
|
47 |
|
|
48 |
|
48 |
49 |
/**
|
49 |
|
* Execute a groovy script file in the same thread a the RCP @ author mdecorde.
|
|
50 |
* Delete file command
|
|
51 |
*
|
|
52 |
* @author mdecorde.
|
50 |
53 |
*/
|
51 |
54 |
public class DeleteFile extends AbstractHandler {
|
52 |
|
|
53 |
|
/* (non-Javadoc)
|
|
55 |
|
|
56 |
/*
|
|
57 |
* (non-Javadoc)
|
54 |
58 |
* @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
|
55 |
59 |
*/
|
56 |
60 |
@Override
|
57 |
61 |
public Object execute(ExecutionEvent event) throws ExecutionException {
|
58 |
|
|
|
62 |
|
59 |
63 |
ISelection selection = HandlerUtil.getCurrentSelection(event);
|
60 |
64 |
if (selection instanceof IStructuredSelection) {
|
61 |
|
Object o = ((IStructuredSelection)selection).getFirstElement();
|
62 |
|
|
|
65 |
Object o = ((IStructuredSelection) selection).getFirstElement();
|
|
66 |
|
63 |
67 |
if (o instanceof File) {
|
64 |
|
File file = (File)o;
|
|
68 |
File file = (File) o;
|
65 |
69 |
delete(file);
|
66 |
70 |
}
|
67 |
71 |
}
|
68 |
72 |
return null;
|
69 |
73 |
}
|
70 |
|
|
|
74 |
|
|
75 |
/**
|
|
76 |
* open a dialog box to confirm and update views
|
|
77 |
*
|
|
78 |
* @param file the file or directory to delete
|
|
79 |
*/
|
71 |
80 |
public static void delete(File file) {
|
72 |
|
//ask new file name
|
73 |
|
|
|
81 |
// ask new file name
|
|
82 |
|
74 |
83 |
Shell shell = new Shell();
|
75 |
84 |
MessageBox dialog = new MessageBox(shell, SWT.YES | SWT.NO);
|
76 |
|
dialog.setMessage(TXMUIMessages.areYouSureYouWantToDeleteP0Colon+file+" ? "); //$NON-NLS-1$
|
77 |
|
|
|
85 |
dialog.setMessage(TXMUIMessages.areYouSureYouWantToDeleteP0Colon + file + " ? "); //$NON-NLS-1$
|
|
86 |
|
78 |
87 |
int buttonID = dialog.open();
|
79 |
|
switch(buttonID) {
|
80 |
|
case SWT.YES:
|
81 |
|
File parentFile = file.getParentFile();
|
82 |
|
if (file.isFile()) {
|
83 |
|
if (!file.delete()) System.out.println(NLS.bind(TXMUIMessages.failedToDeleteFileP0, file));
|
84 |
|
} else if (file.isDirectory()) {
|
85 |
|
if (!DeleteDir.deleteDirectory(file)) System.out.println(NLS.bind(TXMUIMessages.failedToDeleteFileP0, file));
|
86 |
|
}
|
87 |
|
Explorer.refresh(parentFile);
|
88 |
|
MacroExplorer.refresh(parentFile);
|
89 |
|
case SWT.NO:
|
90 |
|
// exits here ...
|
91 |
|
break;
|
92 |
|
case SWT.CANCEL:
|
93 |
|
// does nothing ...
|
|
88 |
switch (buttonID) {
|
|
89 |
case SWT.YES:
|
|
90 |
File parentFile = file.getParentFile();
|
|
91 |
if (file.isFile()) {
|
|
92 |
if (!file.delete()) System.out.println(NLS.bind(TXMUIMessages.failedToDeleteFileP0, file));
|
|
93 |
}
|
|
94 |
else if (file.isDirectory()) {
|
|
95 |
if (!DeleteDir.deleteDirectory(file)) System.out.println(NLS.bind(TXMUIMessages.failedToDeleteFileP0, file));
|
|
96 |
}
|
|
97 |
Explorer.refresh(parentFile);
|
|
98 |
MacroExplorer.refresh(parentFile);
|
|
99 |
case SWT.NO:
|
|
100 |
// exits here ...
|
|
101 |
break;
|
|
102 |
case SWT.CANCEL:
|
|
103 |
// does nothing ...
|
94 |
104 |
}
|
95 |
105 |
}
|
96 |
|
|
|
106 |
|
|
107 |
/**
|
|
108 |
* open a dialog box to confirm and update views
|
|
109 |
*
|
|
110 |
* @param files the files or directories to delete
|
|
111 |
*/
|
97 |
112 |
public static void delete(List files) {
|
98 |
113 |
Shell shell = new Shell();
|
99 |
114 |
MessageBox dialog = new MessageBox(shell, SWT.YES | SWT.NO);
|
100 |
|
dialog.setMessage(TXMUIMessages.areYouSureYouWantToDeleteP0Colon+files+" ? "); //$NON-NLS-1$
|
|
115 |
dialog.setMessage(TXMUIMessages.areYouSureYouWantToDeleteP0Colon + files + " ? "); //$NON-NLS-1$
|
101 |
116 |
|
102 |
117 |
int buttonID = dialog.open();
|
103 |
|
switch(buttonID) {
|
104 |
|
case SWT.YES:
|
105 |
|
for (Object o : files) {
|
106 |
|
if (o instanceof File) {
|
107 |
|
File file = (File) o;
|
108 |
|
if (file.exists())
|
109 |
|
if (file.isFile()) {
|
110 |
|
if (!file.delete()) System.out.println(NLS.bind(TXMUIMessages.failedToDeleteFileP0, file));
|
111 |
|
} else if (file.isDirectory()) {
|
112 |
|
if (!DeleteDir.deleteDirectory(file)) System.out.println(NLS.bind(TXMUIMessages.failedToDeleteFileP0, file));
|
|
118 |
switch (buttonID) {
|
|
119 |
case SWT.YES:
|
|
120 |
for (Object o : files) {
|
|
121 |
if (o instanceof File) {
|
|
122 |
File file = (File) o;
|
|
123 |
if (file.exists()) {
|
|
124 |
if (file.isFile()) {
|
|
125 |
if (!file.delete()) {
|
|
126 |
System.out.println(NLS.bind(TXMUIMessages.failedToDeleteFileP0, file));
|
|
127 |
}
|
|
128 |
}
|
|
129 |
else if (file.isDirectory()) {
|
|
130 |
if (!DeleteDir.deleteDirectory(file)) {
|
|
131 |
System.out.println(NLS.bind(TXMUIMessages.failedToDeleteFileP0, file));
|
|
132 |
}
|
|
133 |
}
|
|
134 |
}
|
113 |
135 |
}
|
114 |
136 |
}
|
115 |
|
}
|
116 |
|
Explorer.refresh();
|
117 |
|
MacroExplorer.refresh();
|
118 |
|
case SWT.NO:
|
119 |
|
// exits here ...
|
120 |
|
break;
|
121 |
|
case SWT.CANCEL:
|
122 |
|
// does nothing ...
|
|
137 |
Explorer.refresh();
|
|
138 |
MacroExplorer.refresh();
|
|
139 |
case SWT.NO:
|
|
140 |
// exits here ...
|
|
141 |
break;
|
|
142 |
case SWT.CANCEL:
|
|
143 |
// does nothing ...
|
123 |
144 |
}
|
124 |
|
|
125 |
|
|
126 |
145 |
}
|
127 |
|
}
|
|
146 |
}
|