Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bcafdf853337987236a8bc3da00c064328fa5b33 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*******************************************************************************
 * Copyright (c) 2000, 2017 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ui;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourceAttributes;
import org.eclipse.core.resources.team.FileModificationValidationContext;
import org.eclipse.core.runtime.*;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.internal.core.DefaultFileModificationValidator;
import org.eclipse.team.internal.ui.dialogs.DetailsDialog;

/**
 * Override the default file modification validator to prompt to
 * make read-only files writable
 */
public class DefaultUIFileModificationValidator extends DefaultFileModificationValidator {

    public static class FileListDialog extends DetailsDialog {

        private final IFile[] files;

        public static boolean openQuestion(Shell shell, IFile[] files) {
            FileListDialog dialog = new FileListDialog(shell, files);
            int code = dialog.open();
            return code == OK;
        }

        public FileListDialog(Shell parentShell, IFile[] files) {
            super(parentShell, TeamUIMessages.DefaultUIFileModificationValidator_0);
            this.files = files;
			setImageKey(DLG_IMG_WARNING);
        }

        @Override
		protected void createMainDialogArea(Composite parent) {
			createWrappingLabel(parent, TeamUIMessages.DefaultUIFileModificationValidator_1);
        }

        @Override
		protected Composite createDropDownDialogArea(Composite parent) {
			Composite composite = createComposite(parent);
			createWrappingLabel(composite, TeamUIMessages.DefaultUIFileModificationValidator_2);
			org.eclipse.swt.widgets.List fileList = new org.eclipse.swt.widgets.List(composite, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
			GridData data = new GridData ();
			data.heightHint = 75;
			data.horizontalAlignment = GridData.FILL;
			data.grabExcessHorizontalSpace = true;
			fileList.setLayoutData(data);
			fileList.setFont(parent.getFont());
			for (int i = 0; i < files.length; i++) {
				fileList.add(files[i].getFullPath().toString());
			}
			return composite;
        }

        @Override
		protected void updateEnablements() {
            // Nothing to do
        }

        @Override
		protected boolean includeCancelButton() {
            return false;
        }

        @Override
		protected boolean includeOkButton() {
            return false;
        }

        @Override
		protected void createButtonsForButtonBar(Composite parent) {
            createButton(parent, IDialogConstants.YES_ID, IDialogConstants.YES_LABEL, true);
            createButton(parent, IDialogConstants.NO_ID, IDialogConstants.NO_LABEL, true);
            super.createButtonsForButtonBar(parent);
        }

        @Override
		protected void buttonPressed(int id) {
            if (IDialogConstants.YES_ID == id) {
                okPressed();
            } else if (IDialogConstants.NO_ID == id) {
                cancelPressed();
            } else {
                super.buttonPressed(id);
            }
        }
    }

    @Override
	public IStatus validateEdit(final IFile[] allFiles, FileModificationValidationContext context) {
    	final IFile[] readOnlyFiles = getReadOnlyFiles(allFiles);
        if (readOnlyFiles.length > 0 && context != null) {
            final Shell shell = getShell(context);
            final boolean[] ok = new boolean[] { false };
            if (readOnlyFiles.length == 1) {
                shell.getDisplay().syncExec(() -> ok[0] = MessageDialog.openQuestion(shell, TeamUIMessages.DefaultUIFileModificationValidator_3, NLS.bind(TeamUIMessages.DefaultUIFileModificationValidator_4, new String[] { readOnlyFiles[0].getFullPath().toString() })));
            } else {
                shell.getDisplay().syncExec(() -> ok[0] = FileListDialog.openQuestion(shell, readOnlyFiles));
            }
            if (ok[0]) {
                setWritable(readOnlyFiles);
            };
        } else if (readOnlyFiles.length > 0 && context == null) {
        	if (isMakeWrittableWhenContextNotProvided()) {
        		setWritable(readOnlyFiles);
        	}
        }
        return getStatus(readOnlyFiles);
    }

    private Shell getShell(FileModificationValidationContext context) {
		if (context.getShell() != null)
			return (Shell)context.getShell();
		return Utils.getShell(null, true);
	}

	@Override
	public IStatus validateSave(IFile file) {
    	if (file.isReadOnly() && isMakeWrittableWhenContextNotProvided()) {
    		IFile[] readOnlyFiles = new IFile[] { file };
    		setWritable(readOnlyFiles);
    		return getStatus(readOnlyFiles);
    	} else {
    		return getDefaultStatus(file);
    	}
    }

	private boolean isMakeWrittableWhenContextNotProvided() {
		return TeamUIPlugin.getPlugin().getPreferenceStore().getBoolean(IPreferenceIds.MAKE_FILE_WRITTABLE_IF_CONTEXT_MISSING);
	}

	private IFile[] getReadOnlyFiles(IFile[] files) {
		List<IFile> result = new ArrayList<>();
		for (int i = 0; i < files.length; i++) {
			IFile file = files[i];
			if (file.isReadOnly()) {
				result.add(file);
			}
		}
		return result.toArray(new IFile[result.size()]);
	}

	protected IStatus setWritable(final IFile[] files) {
        for (int i = 0; i < files.length; i++) {
        	IFile file = files[i];
        	ResourceAttributes attributes = file.getResourceAttributes();
        	if (attributes != null) {
        		attributes.setReadOnly(false);
        	}
        	try {
        		file.setResourceAttributes(attributes);
        	} catch (CoreException e) {
        		return e.getStatus();
        	}
        }
        return Status.OK_STATUS;
    }
}

Back to the top