]> gerrit.simantics Code Review - simantics/platform.git/blob - actions/e4/PageSettingsHandler.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / actions / e4 / PageSettingsHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.actions.e4;
13
14 import javax.inject.Named;
15
16 import org.eclipse.e4.core.di.annotations.CanExecute;
17 import org.eclipse.e4.core.di.annotations.Execute;
18 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
19 import org.eclipse.e4.ui.services.IServiceConstants;
20 import org.eclipse.ui.IEditorPart;
21 import org.eclipse.ui.IWorkbenchPart;
22 import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;
23 import org.simantics.db.Resource;
24 import org.simantics.modeling.ui.diagram.PageSettingsDialog;
25 import org.simantics.modeling.ui.diagramEditor.DiagramEditor;
26 import org.simantics.ui.workbench.IResourceEditorInput;
27 import org.simantics.utils.ui.workbench.WorkbenchUtils;
28
29
30 /**
31  * A Handler that shows PageSettings Dialog for DiagramViewer
32  * 
33  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
34  */
35 public class PageSettingsHandler {
36
37     @CanExecute
38     public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditor) {
39         // TODO: Fix this when we get rid of CompatibilityEditors
40         IEditorPart activeEditor = null;
41         if (mActiveEditor != null && mActiveEditor.getObject() instanceof CompatibilityEditor) {
42             CompatibilityEditor compatEditor = (CompatibilityEditor) mActiveEditor.getObject();
43             activeEditor = compatEditor.getEditor();
44         } else {
45             // TODO: This is not good practice with E4 but an OK fallback for now
46 //            activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
47         }
48         if (activeEditor == null)
49             return false;
50         
51         if (activeEditor instanceof DiagramEditor) {
52             DiagramEditor editor = (DiagramEditor) activeEditor;
53             IResourceEditorInput input = (IResourceEditorInput)editor.getEditorInput();
54             Resource diagramResource = input.getResource();
55             if (diagramResource != null)
56                 return true;
57         }
58         return false;
59     }
60     
61     @Execute
62     public void execute(){
63         IWorkbenchPart ap = WorkbenchUtils.getActiveEditor();
64         if (ap instanceof DiagramEditor) {
65             DiagramEditor editor = (DiagramEditor) ap;
66             IResourceEditorInput input = (IResourceEditorInput)editor.getEditorInput();
67             Resource diagramResource = input.getResource();
68             if (diagramResource == null)
69                 return;
70
71             PageSettingsDialog dialog = new PageSettingsDialog(diagramResource, ap.getSite().getShell());
72             if (dialog.open() == PageSettingsDialog.OK) {
73                 dialog.applySettings();
74             }
75         }
76     }
77
78 }