KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > ReadOnlyStateChecker


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.actions;
12
13 import com.ibm.icu.text.MessageFormat;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
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 /**
28  * The ReadOnlyStateChecker is a helper class that takes a set of resource
29  * some of which may be read only and queries the user as to whether or
30  * not they wish to continue the operation on it.
31  */

32 public class ReadOnlyStateChecker {
33     private Shell shell;
34
35     private String JavaDoc titleMessage;
36
37     private String JavaDoc mainMessage;
38
39     private boolean yesToAllSelected = false;
40
41     private boolean cancelSelected = false;
42
43     private boolean ignoreLinkedResources = false;
44     
45     private String JavaDoc READ_ONLY_EXCEPTION_MESSAGE = IDEWorkbenchMessages.ReadOnlyCheck_problems;
46
47     /**
48      * Create a new checker that parents the dialog off of parent using the supplied
49      * title and message.
50      * @param parent the shell used for dialogs
51      * @param title the title for dialogs
52      * @param message the message for a dialog - this will be prefaced with the name of the resource.
53      */

54     public ReadOnlyStateChecker(Shell parent, String JavaDoc title, String JavaDoc message) {
55         this.shell = parent;
56         this.titleMessage = title;
57         this.mainMessage = message;
58     }
59
60     /**
61      * Check an individual resource to see if it passed the read only query. If it is a file
62      * just add it, otherwise it is a container and the children need to be checked too.
63      * Return true if all items are selected and false if any are skipped.
64      */

65     private boolean checkAcceptedResource(IResource resourceToCheck,
66             List JavaDoc 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 the project is closed, there's no point in checking
76
// it's children. bug 99858
77
if (container.isAccessible()) {
78                 // Now check below
79
int childCheck = checkReadOnlyResources(container.members(),
80                         selectedChildren);
81                 // Add in the resource only if nothing was left out
82
if (childCheck == IDialogConstants.YES_TO_ALL_ID) {
83                     selectedChildren.add(resourceToCheck);
84                 } else {
85                     // Something was left out - return false
86
return false;
87                 }
88             } else {
89                 selectedChildren.add(resourceToCheck);
90             }
91         }
92         return true;
93
94     }
95
96     /**
97      * Check the supplied resources to see if they are read only. If so then
98      * prompt the user to see if they can be deleted.Return those that were
99      * accepted.
100      *
101      * @param itemsToCheck
102      * @return the resulting selected resources
103      */

104     public IResource[] checkReadOnlyResources(IResource[] itemsToCheck) {
105
106         List JavaDoc selections = new ArrayList JavaDoc();
107         int result = IDialogConstants.CANCEL_ID;
108         try {
109             result = checkReadOnlyResources(itemsToCheck, selections);
110         } catch (final CoreException exception) {
111             shell.getDisplay().syncExec(new Runnable JavaDoc() {
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         //All were selected so return the original items
124
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     /**
134      * Check the children of the container to see if they are read only.
135      * @return int
136      * one of
137      * YES_TO_ALL_ID - all elements were selected
138      * NO_ID - No was hit at some point
139      * CANCEL_ID - cancel was hit
140      * @param itemsToCheck IResource[]
141      * @param allSelected the List of currently selected resources to add to.
142      */

143     private int checkReadOnlyResources(IResource[] itemsToCheck,
144             List JavaDoc allSelected) throws CoreException {
145
146         //Shortcut. If the user has already selected yes to all then just return it
147
if (yesToAllSelected) {
148             return IDialogConstants.YES_TO_ALL_ID;
149         }
150
151         boolean noneSkipped = true;
152         List JavaDoc selectedChildren = new ArrayList JavaDoc();
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     /**
201      * Returns whether the given resource should be checked for read-only state.
202      *
203      * @param resourceToCheck the resource to check
204      * @return <code>true</code> to check it, <code>false</code> to skip it
205      */

206     private boolean shouldCheck(IResource resourceToCheck) {
207         if (ignoreLinkedResources) {
208             if (resourceToCheck.isLinked()) {
209                 return false;
210             }
211         }
212         return true;
213     }
214
215     /**
216      * Open a message dialog with Yes No, Yes To All and Cancel buttons. Return the
217      * code that indicates the selection.
218      * @return int
219      * one of
220      * YES_TO_ALL_ID
221      * YES_ID
222      * NO_ID
223      * CANCEL_ID
224      *
225      * @param resource - the resource being queried.
226      */

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 JavaDoc[] { resource.getName() }),
232                 MessageDialog.QUESTION, new String JavaDoc[] {
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 JavaDoc() {
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     /**
256      * Returns whether to ignore linked resources.
257      *
258      * @return <code>true</code> to ignore linked resources, <code>false</code> to consider them
259      * @since 3.1
260      */

261     public boolean getIgnoreLinkedResources() {
262         return ignoreLinkedResources;
263     }
264
265     /**
266      * Sets whether to ignore linked resources.
267      * The default is <code>false</code>.
268      *
269      * @param ignore <code>true</code> to ignore linked resources, <code>false</code> to consider them
270      * @since 3.1
271      */

272     public void setIgnoreLinkedResources(boolean ignore) {
273         ignoreLinkedResources = ignore;
274     }
275 }
276
Popular Tags