Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2782c92b9de840839b23aa1a8c27049949320411 (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
178
179
180
181
182
183
/*******************************************************************************
 * Copyright (c) 2000, 2012 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal.editors.text;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

import org.eclipse.core.runtime.IPath;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;

import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.filebuffers.manipulation.ConvertLineDelimitersOperation;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.window.Window;

import org.eclipse.jface.text.ITextSelection;

import org.eclipse.ui.internal.editors.text.SelectResourcesDialog.IFilter;

import org.eclipse.ui.editors.text.FileBufferOperationAction;


/**
 * A file buffer operation action that changes the line delimiters to a specified
 * line delimiter.
 *
 * @since 3.1
 */
public class ConvertLineDelimitersAction extends FileBufferOperationAction {

	private String fLabel;
	private boolean fStrictCheckIfTextLocation;

	protected ConvertLineDelimitersAction(String lineDelimiter, String label) {
		super(new ConvertLineDelimitersOperation(lineDelimiter));
		setText(constructLabel(label, lineDelimiter, System.getProperty("line.separator"))); //$NON-NLS-1$
		fLabel= Action.removeMnemonics(label);
	}

	private static String constructLabel(String label, String lineDelimiter, String platformLineDelimiter) {
		if (lineDelimiter.equals(platformLineDelimiter))
			return label + TextEditorMessages.ConvertLineDelimitersAction_default_label;
		return label;
	}

	@Override
	protected boolean isAcceptableLocation(IPath location) {
		ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
		return location != null && manager.isTextFileLocation(location, fStrictCheckIfTextLocation);
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		super.selectionChanged(action, selection);
		fStrictCheckIfTextLocation= !(selection instanceof ITextSelection);
	}

	@Override
	protected IFile[] collectFiles(final IResource[] resources) {
		fStrictCheckIfTextLocation= fStrictCheckIfTextLocation || resources.length != 1 || resources[0].getType() != IResource.FILE;
		if (containsOnlyFiles(resources)) {
			IFile[] files= super.collectFiles(resources);
			return filterUnacceptableFiles(files);
		}

    	final IFilter filter= new IFilter() {
			@Override
			public boolean accept(IResource resource) {
				return resource != null && isAcceptableLocation(resource.getFullPath());
			}
		};

		SelectResourcesDialog dialog= new SelectResourcesDialog(getShell(), getDialogTitle(), TextEditorMessages.ConvertLineDelimitersAction_dialog_description, filter) {
			@Override
			protected Composite createSelectionButtonGroup(Composite parent) {
				Composite buttonGroup= super.createSelectionButtonGroup(parent);
				
				final Button button = new Button(buttonGroup, SWT.CHECK);
				((GridLayout) buttonGroup.getLayout()).numColumns++;
				button.setText(TextEditorMessages.ConvertLineDelimitersAction_show_only_text_files);
				button.setFont(JFaceResources.getDialogFont());
				button.setSelection(fStrictCheckIfTextLocation);
				button.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent event) {
						fStrictCheckIfTextLocation= button.getSelection();
						refresh();
					}
				});
				
				return buttonGroup;
			}
		};
		dialog.setInput(resources);
		int result= dialog.open();
		if (Window.OK == result) {
			IResource[] selectedResources= dialog.getSelectedResources();
			return super.collectFiles(selectedResources);
		}
		return null;
	}
	
	private String getDialogTitle() {
		return NLSUtility.format(TextEditorMessages.ConvertLineDelimitersAction_dialog_title, fLabel);
	}

	/**
	 * Checks whether the given resources array contains
	 * only files.
	 *
	 * @param resources the array with the resources
	 * @return <code>true</code> if there array only contains <code>IFiles</code>s
	 * @since 3.2
	 */
	private boolean containsOnlyFiles(IResource[] resources) {
		for (int i= 0; i < resources.length; i++) {
			IResource resource= resources[i];
			if ((IResource.FILE & resource.getType()) == 0)
				return false;
		}
		return true;
	}

	/**
	 * Filters the unacceptable files.
	 *
	 * @param files the files to filter
	 * @return an array of files
	 * @since 3.2
	 */
	private IFile[] filterUnacceptableFiles(IFile[] files) {
		boolean askForBinary= true;
		Set<IFile> filtered= new HashSet<>();
		for (int i= 0; i < files.length; i++) {
			IFile file= files[i];
			if (isAcceptableLocation(file.getFullPath())) {
				filtered.add(file);
			} else if (askForBinary) {
				int result= new MessageDialog(getShell(), getDialogTitle(), null,
						TextEditorMessages.ConvertLineDelimitersAction_nontext_selection,
						MessageDialog.WARNING,
						new String[] { TextEditorMessages.ConvertLineDelimitersAction_convert_all , TextEditorMessages.ConvertLineDelimitersAction_convert_text, IDialogConstants.CANCEL_LABEL}, 1).open();
				if (result == 0) {
					fStrictCheckIfTextLocation= false;
					filtered.add(file);
				} else if (result == 1) {
					askForBinary= false;
				} else {
					return null;
				}
			}
		}
		return filtered.toArray(new IFile[filtered.size()]);
	}

}

Back to the top