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
|
/*******************************************************************************
* Copyright (c) 2003, 2006 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.jst.j2ee.internal.plugin;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.EditorManager;
/**
* @author cbridgha
*
*/
public class CommonEditorUtility {
public static final Class IRESOURCE_CLASS = IResource.class;
private CommonEditorUtility() {
super();
}
/**
* Returns an array of all editors that have an unsaved content. If the identical content is
* presented in more than one editor, only one of those editor parts is part of the result.
*
* @return an array of all dirty editor parts.
*/
public static IEditorPart[] getDirtyEditors() {
return getDirtyEditors(null);
}
/**
* Returns an array of all editors that have an unsaved content, where the content is include in
* one of the projects in the List parameter. If the identical content is presented in more than
* one editor, only one of those editor parts is part of the result.
*
* @return an array of all dirty editor parts.
*/
public static IEditorPart[] getDirtyEditors(List projects) {
Set inputs = new HashSet();
List result = new ArrayList(0);
IWorkbench workbench = J2EEUIPlugin.getPluginWorkbench();
IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();
for (int i = 0; i < windows.length; i++) {
IWorkbenchPage[] pages = windows[i].getPages();
for (int x = 0; x < pages.length; x++) {
IEditorPart[] editors = pages[x].getDirtyEditors();
for (int z = 0; z < editors.length; z++) {
IEditorPart ep = editors[z];
IEditorInput input = ep.getEditorInput();
if (!inputs.contains(input) && (projects == null || inputInProjects(input, projects))) {
inputs.add(input);
result.add(ep);
}
}
}
}
return (IEditorPart[]) result.toArray(new IEditorPart[result.size()]);
}
public static boolean inputInProjects(IEditorInput input, List projects) {
IResource res = (IResource) input.getAdapter(IRESOURCE_CLASS);
IProject project = res == null ? null : res.getProject();
if (project == null)
return false;
for (int i = 0; i < projects.size(); i++) {
if (project.equals(projects.get(i)))
return true;
}
return false;
}
public static boolean promptToSaveAllDirtyEditors() {
return promptToSaveDirtyEditors(Arrays.asList(getDirtyEditors()));
}
public static boolean promptToSaveDirtyEditors(List dirtyEditors) {
if (dirtyEditors.isEmpty())
return true;
return EditorManager.saveAll(dirtyEditors, true, true,false, J2EEUIPlugin.getActiveWorkbenchWindow());
}
/**
* This will close all editors without prompting for save.
* @param dirtyEditors
* @return true is succeeded, false if not
*/
public static void closeAllEditors() {
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
page.closeAllEditors(false);
}
public static boolean promptToSaveDirtyEditorsInProjects(List projects) {
return promptToSaveDirtyEditors(Arrays.asList(getDirtyEditors(projects)));
}
}
|