KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > DefaultUIFileModificationValidator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.team.internal.ui;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.ResourceAttributes;
18 import org.eclipse.core.resources.team.FileModificationValidationContext;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.jface.dialogs.IDialogConstants;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.osgi.util.NLS;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.team.internal.core.DefaultFileModificationValidator;
28 import org.eclipse.team.internal.ui.dialogs.DetailsDialog;
29
30 /**
31  * Override the default file modification validator to prompt to
32  * make read-only files writable
33  */

34 public class DefaultUIFileModificationValidator extends DefaultFileModificationValidator {
35
36     public static class FileListDialog extends DetailsDialog {
37
38         private final IFile[] files;
39
40         public static boolean openQuestion(Shell shell, IFile[] files) {
41             FileListDialog dialog = new FileListDialog(shell, files);
42             int code = dialog.open();
43             return code == OK;
44         }
45         
46         public FileListDialog(Shell parentShell, IFile[] files) {
47             super(parentShell, TeamUIMessages.DefaultUIFileModificationValidator_0);
48             this.files = files;
49             setImageKey(DLG_IMG_WARNING);
50         }
51
52         /* (non-Javadoc)
53          * @see org.eclipse.team.internal.ui.dialogs.DetailsDialog#createMainDialogArea(org.eclipse.swt.widgets.Composite)
54          */

55         protected void createMainDialogArea(Composite parent) {
56             createWrappingLabel(parent, TeamUIMessages.DefaultUIFileModificationValidator_1);
57         }
58
59         /* (non-Javadoc)
60          * @see org.eclipse.team.internal.ui.dialogs.DetailsDialog#createDropDownDialogArea(org.eclipse.swt.widgets.Composite)
61          */

62         protected Composite createDropDownDialogArea(Composite parent) {
63             Composite composite = createComposite(parent);
64             createWrappingLabel(composite, TeamUIMessages.DefaultUIFileModificationValidator_2);
65             org.eclipse.swt.widgets.List fileList = new org.eclipse.swt.widgets.List(composite, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
66             GridData data = new GridData ();
67             data.heightHint = 75;
68             data.horizontalAlignment = GridData.FILL;
69             data.grabExcessHorizontalSpace = true;
70             fileList.setLayoutData(data);
71             fileList.setFont(parent.getFont());
72             for (int i = 0; i < files.length; i++) {
73                 fileList.add(files[i].getFullPath().toString());
74             }
75             return composite;
76         }
77
78         /* (non-Javadoc)
79          * @see org.eclipse.team.internal.ui.dialogs.DetailsDialog#updateEnablements()
80          */

81         protected void updateEnablements() {
82             // Nothing to do
83
}
84         
85         /* (non-Javadoc)
86          * @see org.eclipse.team.internal.ui.dialogs.DetailsDialog#includeCancelButton()
87          */

88         protected boolean includeCancelButton() {
89             return false;
90         }
91         
92         /* (non-Javadoc)
93          * @see org.eclipse.team.internal.ui.dialogs.DetailsDialog#includeOkButton()
94          */

95         protected boolean includeOkButton() {
96             return false;
97         }
98         
99         /* (non-Javadoc)
100          * @see org.eclipse.team.internal.ui.dialogs.DetailsDialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
101          */

102         protected void createButtonsForButtonBar(Composite parent) {
103             createButton(parent, IDialogConstants.YES_ID, IDialogConstants.YES_LABEL, true);
104             createButton(parent, IDialogConstants.NO_ID, IDialogConstants.NO_LABEL, true);
105             super.createButtonsForButtonBar(parent);
106         }
107         
108         /* (non-Javadoc)
109          * @see org.eclipse.team.internal.ui.dialogs.DetailsDialog#buttonPressed(int)
110          */

111         protected void buttonPressed(int id) {
112             if (IDialogConstants.YES_ID == id)
113                 okPressed();
114             else if (IDialogConstants.NO_ID == id)
115                 cancelPressed();
116             else
117                 super.buttonPressed(id);
118         }
119     }
120     
121     /* (non-Javadoc)
122      * @see org.eclipse.team.internal.core.DefaultFileModificationValidator#validateEdit(org.eclipse.core.resources.IFile[], org.eclipse.core.resources.team.FileModificationValidationContext)
123      */

124     public IStatus validateEdit(final IFile[] allFiles, FileModificationValidationContext context) {
125         final IFile[] readOnlyFiles = getReadOnlyFiles(allFiles);
126         if (readOnlyFiles.length > 0 && context != null) {
127             final Shell shell = getShell(context);
128             final boolean[] ok = new boolean[] { false };
129             if (readOnlyFiles.length == 1) {
130                 shell.getDisplay().syncExec(new Runnable JavaDoc() {
131                     public void run() {
132                         ok[0] = MessageDialog.openQuestion(shell, TeamUIMessages.DefaultUIFileModificationValidator_3, NLS.bind(TeamUIMessages.DefaultUIFileModificationValidator_4, new String JavaDoc[] { readOnlyFiles[0].getFullPath().toString() })); //
133
}
134                 });
135             } else {
136                 shell.getDisplay().syncExec(new Runnable JavaDoc() {
137                     public void run() {
138                         ok[0] = FileListDialog.openQuestion(shell, readOnlyFiles);
139                     }
140                 });
141             }
142             if (ok[0]) {
143                 setWritable(readOnlyFiles);
144             };
145         } else if (readOnlyFiles.length > 0 && context == null) {
146             if (isMakeWrittableWhenContextNotProvided()) {
147                 setWritable(readOnlyFiles);
148             }
149         }
150         return getStatus(readOnlyFiles);
151     }
152     
153     private Shell getShell(FileModificationValidationContext context) {
154         if (context.getShell() != null)
155             return (Shell)context.getShell();
156         return Utils.getShell(null, true);
157     }
158
159     public IStatus validateSave(IFile file) {
160         if (file.isReadOnly() && isMakeWrittableWhenContextNotProvided()) {
161             IFile[] readOnlyFiles = new IFile[] { file };
162             setWritable(readOnlyFiles);
163             return getStatus(readOnlyFiles);
164         } else {
165             return getDefaultStatus(file);
166         }
167     }
168     
169     private boolean isMakeWrittableWhenContextNotProvided() {
170         return TeamUIPlugin.getPlugin().getPreferenceStore().getBoolean(IPreferenceIds.MAKE_FILE_WRITTABLE_IF_CONTEXT_MISSING);
171     }
172
173     private IFile[] getReadOnlyFiles(IFile[] files) {
174         List JavaDoc result = new ArrayList JavaDoc();
175         for (int i = 0; i < files.length; i++) {
176             IFile file = files[i];
177             if (file.isReadOnly()) {
178                 result.add(file);
179             }
180         }
181         return (IFile[]) result.toArray(new IFile[result.size()]);
182     }
183
184     protected IStatus setWritable(final IFile[] files) {
185         for (int i = 0; i < files.length; i++) {
186             IFile file = files[i];
187             ResourceAttributes attributes = file.getResourceAttributes();
188             if (attributes != null) {
189                 attributes.setReadOnly(false);
190             }
191             try {
192                 file.setResourceAttributes(attributes);
193             } catch (CoreException e) {
194                 return e.getStatus();
195             }
196         }
197         return Status.OK_STATUS;
198     }
199 }
200
Popular Tags