1 11 package org.eclipse.ui.actions; 12 13 import com.ibm.icu.text.MessageFormat; 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.core.resources.IContainer; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.resources.ResourceAttributes; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.jface.dialogs.ErrorDialog; 22 import org.eclipse.jface.dialogs.IDialogConstants; 23 import org.eclipse.jface.dialogs.MessageDialog; 24 import org.eclipse.swt.widgets.Shell; 25 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 26 27 32 public class ReadOnlyStateChecker { 33 private Shell shell; 34 35 private String titleMessage; 36 37 private String mainMessage; 38 39 private boolean yesToAllSelected = false; 40 41 private boolean cancelSelected = false; 42 43 private boolean ignoreLinkedResources = false; 44 45 private String READ_ONLY_EXCEPTION_MESSAGE = IDEWorkbenchMessages.ReadOnlyCheck_problems; 46 47 54 public ReadOnlyStateChecker(Shell parent, String title, String message) { 55 this.shell = parent; 56 this.titleMessage = title; 57 this.mainMessage = message; 58 } 59 60 65 private boolean checkAcceptedResource(IResource resourceToCheck, 66 List selectedChildren) throws CoreException { 67 68 if (resourceToCheck.getType() == IResource.FILE) { 69 selectedChildren.add(resourceToCheck); 70 } else if (getIgnoreLinkedResources() && resourceToCheck.isLinked()) { 71 selectedChildren.add(resourceToCheck); 72 } 73 else { 74 IContainer container = (IContainer) resourceToCheck; 75 if (container.isAccessible()) { 78 int childCheck = checkReadOnlyResources(container.members(), 80 selectedChildren); 81 if (childCheck == IDialogConstants.YES_TO_ALL_ID) { 83 selectedChildren.add(resourceToCheck); 84 } else { 85 return false; 87 } 88 } else { 89 selectedChildren.add(resourceToCheck); 90 } 91 } 92 return true; 93 94 } 95 96 104 public IResource[] checkReadOnlyResources(IResource[] itemsToCheck) { 105 106 List selections = new ArrayList (); 107 int result = IDialogConstants.CANCEL_ID; 108 try { 109 result = checkReadOnlyResources(itemsToCheck, selections); 110 } catch (final CoreException exception) { 111 shell.getDisplay().syncExec(new Runnable () { 112 public void run() { 113 ErrorDialog.openError(shell, READ_ONLY_EXCEPTION_MESSAGE, 114 null, exception.getStatus()); 115 } 116 }); 117 } 118 119 if (result == IDialogConstants.CANCEL_ID) { 120 return new IResource[0]; 121 } 122 123 if (result == IDialogConstants.YES_TO_ALL_ID) { 125 return itemsToCheck; 126 } 127 128 IResource[] returnValue = new IResource[selections.size()]; 129 selections.toArray(returnValue); 130 return returnValue; 131 } 132 133 143 private int checkReadOnlyResources(IResource[] itemsToCheck, 144 List allSelected) throws CoreException { 145 146 if (yesToAllSelected) { 148 return IDialogConstants.YES_TO_ALL_ID; 149 } 150 151 boolean noneSkipped = true; 152 List selectedChildren = new ArrayList (); 153 154 for (int i = 0; i < itemsToCheck.length; i++) { 155 IResource resourceToCheck = itemsToCheck[i]; 156 ResourceAttributes checkAttributes = resourceToCheck.getResourceAttributes(); 157 if (!yesToAllSelected && shouldCheck(resourceToCheck) 158 && checkAttributes!=null 159 && checkAttributes.isReadOnly()) { 160 int action = queryYesToAllNoCancel(resourceToCheck); 161 if (action == IDialogConstants.YES_ID) { 162 boolean childResult = checkAcceptedResource( 163 resourceToCheck, selectedChildren); 164 if (!childResult) { 165 noneSkipped = false; 166 } 167 } 168 if (action == IDialogConstants.NO_ID) { 169 noneSkipped = false; 170 } 171 if (action == IDialogConstants.CANCEL_ID) { 172 cancelSelected = true; 173 return IDialogConstants.CANCEL_ID; 174 } 175 if (action == IDialogConstants.YES_TO_ALL_ID) { 176 yesToAllSelected = true; 177 selectedChildren.add(resourceToCheck); 178 } 179 } else { 180 boolean childResult = checkAcceptedResource(resourceToCheck, 181 selectedChildren); 182 if (cancelSelected) { 183 return IDialogConstants.CANCEL_ID; 184 } 185 if (!childResult) { 186 noneSkipped = false; 187 } 188 } 189 190 } 191 192 if (noneSkipped) { 193 return IDialogConstants.YES_TO_ALL_ID; 194 } 195 allSelected.addAll(selectedChildren); 196 return IDialogConstants.NO_ID; 197 198 } 199 200 206 private boolean shouldCheck(IResource resourceToCheck) { 207 if (ignoreLinkedResources) { 208 if (resourceToCheck.isLinked()) { 209 return false; 210 } 211 } 212 return true; 213 } 214 215 227 private int queryYesToAllNoCancel(IResource resource) { 228 229 final MessageDialog dialog = new MessageDialog(this.shell, 230 this.titleMessage, null, MessageFormat.format(this.mainMessage, 231 new Object [] { resource.getName() }), 232 MessageDialog.QUESTION, new String [] { 233 IDialogConstants.YES_LABEL, 234 IDialogConstants.YES_TO_ALL_LABEL, 235 IDialogConstants.NO_LABEL, 236 IDialogConstants.CANCEL_LABEL }, 0); 237 shell.getDisplay().syncExec(new Runnable () { 238 public void run() { 239 dialog.open(); 240 } 241 }); 242 int result = dialog.getReturnCode(); 243 if (result == 0) { 244 return IDialogConstants.YES_ID; 245 } 246 if (result == 1) { 247 return IDialogConstants.YES_TO_ALL_ID; 248 } 249 if (result == 2) { 250 return IDialogConstants.NO_ID; 251 } 252 return IDialogConstants.CANCEL_ID; 253 } 254 255 261 public boolean getIgnoreLinkedResources() { 262 return ignoreLinkedResources; 263 } 264 265 272 public void setIgnoreLinkedResources(boolean ignore) { 273 ignoreLinkedResources = ignore; 274 } 275 } 276 | Popular Tags |