KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > ui > action > LoadResourceAction


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2004-2005 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: LoadResourceAction.java,v 1.11 2005/06/20 20:18:14 davidms Exp $
16  */

17 package org.eclipse.emf.edit.ui.action;
18
19
20 import java.io.File JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import org.eclipse.core.resources.IResource;
27 import org.eclipse.core.resources.ResourcesPlugin;
28 import org.eclipse.core.runtime.Platform;
29 import org.eclipse.jface.action.Action;
30 import org.eclipse.jface.dialogs.Dialog;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.layout.FormAttachment;
35 import org.eclipse.swt.layout.FormData;
36 import org.eclipse.swt.layout.FormLayout;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.widgets.Button;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Control;
41 import org.eclipse.swt.widgets.FileDialog;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.Shell;
44 import org.eclipse.swt.widgets.Text;
45 import org.eclipse.ui.IEditorPart;
46 import org.eclipse.ui.IWorkbenchPart;
47 import org.eclipse.ui.PlatformUI;
48 import org.eclipse.ui.dialogs.ResourceSelectionDialog;
49
50 import org.eclipse.emf.common.util.URI;
51 import org.eclipse.emf.edit.domain.EditingDomain;
52 import org.eclipse.emf.edit.domain.IEditingDomainProvider;
53 import org.eclipse.emf.edit.ui.EMFEditUIPlugin;
54
55
56 /**
57  * An action to load a resource into an editing domain's resource set.
58  */

59 public class LoadResourceAction extends Action
60 {
61   protected EditingDomain domain;
62
63   public LoadResourceAction(EditingDomain domain)
64   {
65     this();
66     this.domain = domain;
67     update();
68   }
69
70   public LoadResourceAction()
71   {
72     super(EMFEditUIPlugin.INSTANCE.getString("_UI_LoadResource_menu_item"));
73     setDescription(EMFEditUIPlugin.INSTANCE.getString("_UI_LoadResource_menu_item_description"));
74   }
75
76   /**
77    * This returns the action's domain.
78    */

79   public EditingDomain getEditingDomain()
80   {
81     return domain;
82   }
83
84   /**
85    * This sets the action's domain.
86    */

87   public void setEditingDomain(EditingDomain domain)
88   {
89     this.domain = domain;
90   }
91
92   public void run()
93   {
94     LoadResourceDialog loadResourceDialog =
95       new LoadResourceDialog
96           (PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), domain);
97   
98     loadResourceDialog.open();
99   }
100
101   public void update()
102   {
103     setEnabled(domain != null);
104   }
105
106   /**
107    * @deprecated As of EMF 2.1.0, replaced by {@link #setActiveWorkbenchPart}.
108    */

109   public void setActiveEditor(IEditorPart editorPart)
110   {
111     setActiveWorkbenchPart(editorPart);
112   }
113
114   /**
115    * @since 2.1.0
116    */

117   public void setActiveWorkbenchPart(IWorkbenchPart workbenchPart)
118   {
119     setEditingDomain(workbenchPart instanceof IEditingDomainProvider ? ((IEditingDomainProvider)workbenchPart).getEditingDomain() : null);
120   }
121
122   public static class LoadResourceDialog extends Dialog
123   {
124     public static int CONTROL_OFFSET = 10;
125     protected EditingDomain domain;
126     protected Text resourceURIField;
127     protected String JavaDoc resourceURIs;
128
129     public LoadResourceDialog
130     (Shell parent)
131   {
132     this(parent, null);
133   }
134
135     public LoadResourceDialog
136       (Shell parent, EditingDomain domain)
137     {
138       super(parent);
139       setShellStyle(getShellStyle() | SWT.MAX | SWT.RESIZE);
140       this.domain = domain;
141     }
142
143     protected void configureShell(Shell shell)
144     {
145       super.configureShell(shell);
146       shell.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_LoadResourceDialog_title"));
147     }
148
149     protected Control createDialogArea(Composite parent)
150     {
151       boolean resourcesBundleIsAvailable = (Platform.getBundle("org.eclipse.core.resources") != null);
152       
153       Composite composite = (Composite)super.createDialogArea(parent);
154       {
155           FormLayout layout = new FormLayout();
156           composite.setLayout(layout);
157           
158           GridData data = new GridData();
159           data.verticalAlignment = GridData.FILL;
160           data.grabExcessVerticalSpace = true;
161           data.horizontalAlignment = GridData.FILL;
162           data.grabExcessHorizontalSpace = true;
163           if(!resourcesBundleIsAvailable)
164           {
165             data.widthHint = 330;
166           }
167           composite.setLayoutData(data);
168       }
169
170       Composite buttonComposite = new Composite(composite, SWT.NONE);
171       {
172         FormData data = new FormData();
173         data.top = new FormAttachment(0, CONTROL_OFFSET);
174         data.left = new FormAttachment(30, 0);
175         data.right = new FormAttachment(100, -CONTROL_OFFSET);
176         buttonComposite.setLayoutData(data);
177
178         buttonComposite.setLayout(new FormLayout());
179       }
180
181       Label resourceURILabel = new Label(composite, SWT.LEFT);
182       {
183         resourceURILabel.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_ResourceURI_label"));
184         FormData data = new FormData();
185         data.top = new FormAttachment(buttonComposite, CONTROL_OFFSET, SWT.CENTER);
186         data.left = new FormAttachment(0, CONTROL_OFFSET);
187         resourceURILabel.setLayoutData(data);
188       }
189
190       resourceURIField = new Text(composite, SWT.BORDER);
191       {
192         FormData data = new FormData();
193         data.top = new FormAttachment(buttonComposite, CONTROL_OFFSET);
194         data.left = new FormAttachment(0, CONTROL_OFFSET);
195         data.right = new FormAttachment(100, -CONTROL_OFFSET);
196         resourceURIField.setLayoutData(data);
197       }
198       
199       Button resourceURIBrowseFileSystemButton = new Button(buttonComposite, SWT.PUSH);
200       resourceURIBrowseFileSystemButton.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_BrowseFileSystem_label"));
201       resourceURIBrowseFileSystemButton.addSelectionListener
202         (new SelectionAdapter()
203          {
204            public void widgetSelected(SelectionEvent event)
205            {
206              FileDialog fileDialog = new FileDialog(getShell());
207              fileDialog.open();
208              if (fileDialog.getFileName() != null && fileDialog.getFileName().length() > 0)
209              {
210                String JavaDoc filePath = fileDialog.getFilterPath() + File.separator + fileDialog.getFileName();
211                resourceURIField.setText((resourceURIField.getText() + " " + URI.createFileURI(filePath).toString()).trim());
212              }
213            }
214          });
215       
216       if(resourcesBundleIsAvailable)
217       {
218           Button resourceURIBrowseWorkspaceButton = new Button(buttonComposite, SWT.PUSH);
219         {
220           FormData data = new FormData();
221           data.right = new FormAttachment(100);
222           resourceURIBrowseWorkspaceButton.setLayoutData(data);
223         }
224         {
225           FormData data = new FormData();
226           data.right = new FormAttachment(resourceURIBrowseWorkspaceButton, -CONTROL_OFFSET);
227           resourceURIBrowseFileSystemButton.setLayoutData(data);
228         }
229           resourceURIBrowseWorkspaceButton.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_BrowseWorkspace_label"));
230           resourceURIBrowseWorkspaceButton.addSelectionListener
231             (new SelectionAdapter()
232              {
233                public void widgetSelected(SelectionEvent event)
234                {
235                  ResourceSelectionDialog resourceSelectionDialog =
236                    new ResourceSelectionDialog
237                      (getShell(),
238                       ResourcesPlugin.getWorkspace().getRoot(),
239                       EMFEditUIPlugin.INSTANCE.getString("_UI_SelectTheResource_label"));
240     
241                  resourceSelectionDialog.open();
242                  Object JavaDoc [] result = resourceSelectionDialog.getResult();
243                  if (result != null)
244                  {
245                    StringBuffer JavaDoc text = new StringBuffer JavaDoc();
246                    for (int i = 0; i < result.length; ++i)
247                    {
248                      IResource resource = (IResource)result[i];
249                      if (resource.getType() == IResource.FILE)
250                      {
251                        text.append(URI.createPlatformResourceURI(resource.getFullPath().toString(), true));
252                        text.append(" ");
253                      }
254                    }
255                    resourceURIField.setText((resourceURIField.getText() + " " + text.toString()).trim());
256                  }
257                }
258              });
259       }
260       else
261       {
262         FormData data = new FormData();
263         data.right = new FormAttachment(100);
264         resourceURIBrowseFileSystemButton.setLayoutData(data);
265       }
266       
267       Label separatorLabel = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
268       {
269         FormData data = new FormData();
270         data.top = new FormAttachment(resourceURIField, (int)(1.5*CONTROL_OFFSET));
271         data.left = new FormAttachment(0, -CONTROL_OFFSET);
272         data.right = new FormAttachment(100, CONTROL_OFFSET);
273         separatorLabel.setLayoutData(data);
274       }
275             
276       composite.setTabList(new Control[]{resourceURIField, buttonComposite});
277       return composite;
278     }
279
280     protected void okPressed()
281     {
282       resourceURIs = getResourceURIs();
283       if (domain != null)
284       {
285         for (Iterator JavaDoc i = getURIs().iterator(); i.hasNext();)
286         {
287             try
288             {
289               domain.getResourceSet().getResource((URI)i.next(), true);
290             }
291             catch (RuntimeException JavaDoc exception)
292             {
293               EMFEditUIPlugin.INSTANCE.log(exception);
294             }
295         }
296       }
297       super.okPressed();
298     }
299
300     public boolean close()
301     {
302       return super.close();
303     }
304     
305     public String JavaDoc getResourceURIs()
306     {
307       return resourceURIField != null && !resourceURIField.isDisposed() ? resourceURIField.getText() : resourceURIs;
308     }
309     
310     public List JavaDoc getURIs()
311     {
312       List JavaDoc uris = new ArrayList JavaDoc();
313       for (StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(getResourceURIs()); stringTokenizer.hasMoreTokens(); )
314       {
315         String JavaDoc resourceURI = stringTokenizer.nextToken();
316         uris.add(URI.createURI(resourceURI));
317       }
318       
319       return uris;
320     }
321   }
322 }
323
Popular Tags