2 |
2 |
|
3 |
3 |
import org.eclipse.core.commands.AbstractHandler;
|
4 |
4 |
import org.eclipse.core.commands.ExecutionEvent;
|
|
5 |
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
|
6 |
import org.eclipse.e4.ui.workbench.modeling.EModelService;
|
|
7 |
import org.eclipse.jface.dialogs.MessageDialog;
|
5 |
8 |
import org.eclipse.ui.IEditorReference;
|
6 |
|
import org.eclipse.ui.IViewReference;
|
|
9 |
import org.eclipse.ui.IPerspectiveDescriptor;
|
7 |
10 |
import org.eclipse.ui.IWorkbenchPage;
|
8 |
11 |
import org.eclipse.ui.IWorkbenchPart;
|
|
12 |
import org.eclipse.ui.IWorkbenchWindow;
|
9 |
13 |
import org.eclipse.ui.PlatformUI;
|
10 |
14 |
import org.eclipse.ui.handlers.HandlerUtil;
|
11 |
|
import org.eclipse.ui.handlers.IHandlerService;
|
12 |
|
import org.txm.core.messages.TXMCoreMessages;
|
13 |
|
import org.txm.rcp.messages.TXMUIMessages;
|
14 |
|
import org.txm.utils.logger.Log;
|
|
15 |
import org.eclipse.ui.internal.Workbench;
|
|
16 |
import org.eclipse.ui.internal.registry.PerspectiveDescriptor;
|
|
17 |
import org.eclipse.ui.internal.registry.PerspectiveRegistry;
|
15 |
18 |
|
16 |
19 |
public class CleanPerspective extends AbstractHandler {
|
|
20 |
|
17 |
21 |
/* (non-Javadoc)
|
18 |
22 |
* @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
|
19 |
23 |
*/
|
20 |
24 |
@Override
|
21 |
25 |
public Object execute(ExecutionEvent event) {
|
|
26 |
if (MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Reset perspective", "This will will restore the views and close the additional views of the current perspective. Continue?" )) {
|
|
27 |
reset(event, true, false);
|
|
28 |
}
|
|
29 |
return null;
|
|
30 |
}
|
|
31 |
|
|
32 |
public static void reset(ExecutionEvent event, boolean perspective, boolean parts) {
|
22 |
33 |
//System.out.println("clean perspective");
|
23 |
|
IWorkbenchPart part = HandlerUtil.getActivePart(event);
|
24 |
|
//System.out.println("part: "+part.getTitle());
|
25 |
|
IWorkbenchPage page = part.getSite().getPage();
|
26 |
|
//System.out.println("page: "+page.getLabel());
|
27 |
|
if (page.isEditorAreaVisible()) {
|
28 |
|
//System.out.println(" page is visible");
|
|
34 |
|
|
35 |
IWorkbenchPage page;
|
|
36 |
if (event == null) {
|
|
37 |
IWorkbenchPart part = HandlerUtil.getActivePart(event);
|
|
38 |
page = part.getSite().getPage();
|
|
39 |
} else {
|
|
40 |
page = Workbench.getInstance().getActiveWorkbenchWindow().getActivePage();
|
|
41 |
}
|
|
42 |
|
|
43 |
if (page == null) return;
|
|
44 |
|
|
45 |
if (parts) {
|
29 |
46 |
IEditorReference[] editors = page.getEditorReferences();
|
30 |
|
IViewReference[] views = page.getViewReferences();
|
31 |
|
if (editors.length == 0 && views.length == 0) {
|
32 |
|
//System.out.println(" no editor");
|
33 |
|
page.setEditorAreaVisible(false);
|
34 |
|
} else {
|
35 |
|
//System.out.println(" editors: "+editors.length);
|
|
47 |
for (IEditorReference er : editors) {
|
|
48 |
page.closeEditor(er.getEditor(false), false);
|
36 |
49 |
}
|
37 |
|
} else {
|
38 |
|
//System.out.println(" page is not visible");
|
39 |
50 |
}
|
40 |
51 |
|
41 |
|
IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
|
42 |
|
if (handlerService != null) {
|
43 |
|
try {
|
44 |
|
handlerService.executeCommand("org.eclipse.ui.window.resetPerspective", null); //$NON-NLS-1$
|
45 |
|
} catch (Exception e) {
|
46 |
|
System.out.println(TXMCoreMessages.bind(TXMUIMessages.errorWhileCallingOrgeclipseuiwindowresetPerspectiveColon, e));
|
47 |
|
Log.printStackTrace(e);
|
|
52 |
if (perspective) {
|
|
53 |
IPerspectiveDescriptor descriptor = page.getPerspective();
|
|
54 |
if (descriptor != null) {
|
|
55 |
boolean offerRevertToBase = false;
|
|
56 |
if (descriptor instanceof PerspectiveDescriptor) {
|
|
57 |
PerspectiveDescriptor desc = (PerspectiveDescriptor) descriptor;
|
|
58 |
offerRevertToBase = desc.isPredefined() && desc.hasCustomDefinition();
|
|
59 |
}
|
|
60 |
|
|
61 |
if (offerRevertToBase) {
|
|
62 |
|
|
63 |
PerspectiveRegistry reg = (PerspectiveRegistry) PlatformUI.getWorkbench().getPerspectiveRegistry();
|
|
64 |
reg.revertPerspective(descriptor);
|
|
65 |
}
|
|
66 |
page.resetPerspective();
|
48 |
67 |
}
|
49 |
|
} else {
|
50 |
|
System.out.println(TXMUIMessages.errorWhileCleaningPerspectiveColonNoHandlerServiceFound);
|
51 |
68 |
}
|
52 |
|
|
53 |
|
return null;
|
|
69 |
return;
|
54 |
70 |
}
|
55 |
71 |
}
|