Debian Codegraph @

plugins/org.eclipse.emf.edit.ui/src/org/eclipse/emf/edit/ui/action/LoadResourceAction.java

raw
1 /**
2 * Copyright (c) 2004-2012 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v2.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v20.html
7 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 */
11 package org.eclipse.emf.edit.ui.action;
12
13
14 import java.util.List;
15
16 import org.eclipse.jface.action.Action;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.ui.IEditorPart;
20 import org.eclipse.ui.IWorkbenchPart;
21 import org.eclipse.ui.PlatformUI;
22
23 import org.eclipse.emf.common.ui.dialogs.ResourceDialog;
24 import org.eclipse.emf.common.util.URI;
25 import org.eclipse.emf.ecore.resource.Resource;
26 import org.eclipse.emf.edit.domain.EditingDomain;
27 import org.eclipse.emf.edit.domain.IEditingDomainProvider;
28 import org.eclipse.emf.edit.ui.EMFEditUIPlugin;
29
30
31 /**
32 * An action to load a resource into an editing domain's resource set.
33 */
34 public class LoadResourceAction extends Action
35 {
36 protected EditingDomain domain;
37
38 public LoadResourceAction(EditingDomain domain)
39 {
40 this();
41 this.domain = domain;
42 update();
43 }
44
45 public LoadResourceAction()
46 {
47 super(EMFEditUIPlugin.INSTANCE.getString("_UI_LoadResource_menu_item"));
48 setDescription(EMFEditUIPlugin.INSTANCE.getString("_UI_LoadResource_menu_item_description"));
49 }
50
51 /**
52 * This returns the action's domain.
53 */
54 public EditingDomain getEditingDomain()
55 {
56 return domain;
57 }
58
59 /**
60 * This sets the action's domain.
61 */
62 public void setEditingDomain(EditingDomain domain)
63 {
64 this.domain = domain;
65 }
66
67 @Override
68 public void run()
69 {
70 LoadResourceDialog loadResourceDialog =
71 new LoadResourceDialog
72 (PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), domain);
73
74 loadResourceDialog.open();
75 }
76
77 public void update()
78 {
79 setEnabled(domain != null);
80 }
81
82 /**
83 * @deprecated As of EMF 2.1.0, replaced by {@link #setActiveWorkbenchPart}.
84 */
85 @Deprecated
86 public void setActiveEditor(IEditorPart editorPart)
87 {
88 setActiveWorkbenchPart(editorPart);
89 }
90
91 /**
92 * @since 2.1.0
93 */
94 public void setActiveWorkbenchPart(IWorkbenchPart workbenchPart)
95 {
96 setEditingDomain(workbenchPart instanceof IEditingDomainProvider ? ((IEditingDomainProvider)workbenchPart).getEditingDomain() : null);
97 }
98
99 public static class LoadResourceDialog extends ResourceDialog
100 {
101 protected EditingDomain domain;
102
103 public LoadResourceDialog(Shell parent)
104 {
105 this(parent, null);
106 }
107
108 public LoadResourceDialog(Shell parent, EditingDomain domain)
109 {
110 super(parent, EMFEditUIPlugin.INSTANCE.getString("_UI_LoadResourceDialog_title"), SWT.OPEN | SWT.MULTI, getContextURI(domain));
111 this.domain = domain;
112 }
113
114 private static URI getContextURI(EditingDomain domain)
115 {
116 if (domain != null)
117 {
118 List<Resource> resources = domain.getResourceSet().getResources();
119 if (!resources.isEmpty())
120 {
121 return resources.get(0).getURI();
122 }
123 }
124 return null;
125 }
126
127 @Override
128 protected boolean processResources()
129 {
130 if (domain != null)
131 {
132 for (URI uri : getURIs())
133 {
134 try
135 {
136 if (!processResource(domain.getResourceSet().getResource(uri, true)))
137 {
138 return false;
139 }
140 }
141 catch (RuntimeException exception)
142 {
143 EMFEditUIPlugin.INSTANCE.log(exception);
144 }
145 }
146 }
147 return true;
148 }
149
150 protected boolean processResource(Resource resource)
151 {
152 return true;
153 }
154 }
155 }