Package: ImportCompleteOCLResourceDialog$1

ImportCompleteOCLResourceDialog$1

nameinstructionbranchcomplexitylinemethod
widgetSelected(SelectionEvent)
M: 42 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
{...}
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2014 Obeo.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * Obeo - Initial API and implementation
10: *******************************************************************************/
11: package org.eclipse.acceleo.ui.interpreter.completeocl.internal.action;
12:
13: import java.io.BufferedReader;
14: import java.io.File;
15: import java.io.IOException;
16: import java.io.InputStream;
17: import java.io.InputStreamReader;
18: import java.util.Arrays;
19: import java.util.HashMap;
20: import java.util.List;
21:
22: import org.eclipse.acceleo.ui.interpreter.InterpreterPlugin;
23: import org.eclipse.acceleo.ui.interpreter.completeocl.internal.CompleteOCLInterpreterActivator;
24: import org.eclipse.core.resources.IFile;
25: import org.eclipse.core.resources.IResource;
26: import org.eclipse.core.resources.IWorkspaceRoot;
27: import org.eclipse.core.resources.ResourcesPlugin;
28: import org.eclipse.core.runtime.IStatus;
29: import org.eclipse.core.runtime.Path;
30: import org.eclipse.core.runtime.Status;
31: import org.eclipse.emf.common.ui.dialogs.WorkspaceResourceDialog;
32: import org.eclipse.emf.common.util.URI;
33: import org.eclipse.emf.ecore.resource.URIConverter;
34: import org.eclipse.emf.ecore.resource.URIHandler;
35: import org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl;
36: import org.eclipse.emf.edit.ui.action.LoadResourceAction.LoadResourceDialog;
37: import org.eclipse.jface.text.TextViewer;
38: import org.eclipse.jface.viewers.Viewer;
39: import org.eclipse.jface.viewers.ViewerFilter;
40: import org.eclipse.swt.SWT;
41: import org.eclipse.swt.events.SelectionAdapter;
42: import org.eclipse.swt.events.SelectionEvent;
43: import org.eclipse.swt.widgets.Button;
44: import org.eclipse.swt.widgets.FileDialog;
45: import org.eclipse.swt.widgets.Shell;
46:
47: /**
48: * This dialog can be used to import a Complete OCL resource's content within the expression section of the
49: * interpreter.
50: *
51: * @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
52: */
53: public class ImportCompleteOCLResourceDialog extends LoadResourceDialog {
54:         /** Extensions of the files we allow users to import. */
55:         protected static final String[] FILE_EXTENSIONS = new String[] {"ocl", }; //$NON-NLS-1$
56:
57:         /** The viewer into which we'll import the content of the selected resource. */
58:         private TextViewer viewer;
59:
60:         /**
61:          * Constructor.
62:          *
63:          * @param parent
64:          * The parent shell.
65:          * @param viewer
66:          * The viewer into which we need to import a Complete OCL resource content.
67:          */
68:         public ImportCompleteOCLResourceDialog(Shell parent, TextViewer viewer) {
69:                 super(parent);
70:                 this.viewer = viewer;
71:                 int shellStyle = getShellStyle();
72:                 int newShellStyle = shellStyle & ~(SWT.MULTI);
73:                 setShellStyle(newShellStyle);
74:         }
75:
76:         /**
77:          * Overridden in order to filter the proposed files according to {@link #FILE_EXTENSIONS}.
78:          *
79:          * @see org.eclipse.emf.common.ui.dialogs.ResourceDialog.prepareBrowseFileSystemButton(Button)
80:          */
81:         @Override
82:         protected void prepareBrowseFileSystemButton(Button browseFileSystemButton) {
83:                 // Prepend all extensions with "*." to respect format.
84:                 final String[] extensionsFilter = new String[FILE_EXTENSIONS.length];
85:                 for (int i = 0; i < FILE_EXTENSIONS.length; i++) {
86:                         extensionsFilter[i] = "*." + FILE_EXTENSIONS[i]; //$NON-NLS-1$
87:                 }
88:                 browseFileSystemButton.addSelectionListener(new SelectionAdapter() {
89:                         @SuppressWarnings("synthetic-access")
90:                         @Override
91:                         public void widgetSelected(SelectionEvent event) {
92:                                 FileDialog fileDialog = new FileDialog(getShell(), style);
93:                                 fileDialog.setFilterExtensions(extensionsFilter);
94:                                 fileDialog.open();
95:
96:                                 String filterPath = fileDialog.getFilterPath();
97:                                 String fileName = fileDialog.getFileName();
98:•                                if (fileName != null) {
99:                                         uriField.setText(URI.createFileURI(filterPath + File.separator + fileName).toString());
100:                                 }
101:                         }
102:                 });
103:         }
104:
105:         /**
106:          * Overridden in order to filter the proposed files according to {@link #FILE_EXTENSIONS}.
107:          *
108:          * @see org.eclipse.emf.common.ui.dialogs.ResourceDialog.prepareBrowseWorkspaceButton(Button)
109:          */
110:         @Override
111:         protected void prepareBrowseWorkspaceButton(Button browseWorkspaceButton) {
112:                 browseWorkspaceButton.addSelectionListener(new BrowseWorkspaceSelectionListener());
113:         }
114:
115:         /**
116:          * {@inheritDoc}
117:          *
118:          * @see org.eclipse.emf.common.ui.dialogs.ResourceDialog.processResources()
119:          */
120:         @Override
121:         protected boolean processResources() {
122:                 final List<URI> uris = getURIs();
123:                 if (!uris.isEmpty()) {
124:                         // We only accept single resource selection
125:                         copyContentToViewer(uris.get(0));
126:                 }
127:
128:                 return true;
129:         }
130:
131:         /**
132:          * Load the content of the file denoted by the given URI and copy it into the {@link #viewer}'s document.
133:          * <p>
134:          * <b>Note</b> that this will not check if the file is a valid Complete OCL resource. We made sure that it
135:          * had the "ocl" extension, but we won't do any more verification than that. This will simply make a
136:          * verbatim copy of the file content into the target viewer.
137:          * </p>
138:          *
139:          * @param uri
140:          * The selected uri.
141:          */
142:         private void copyContentToViewer(URI uri) {
143:                 BufferedReader reader = null;
144:                 try {
145:                         InputStream fileContent = getFileContent(uri);
146:                         reader = new BufferedReader(new InputStreamReader(fileContent));
147:
148:                         StringBuffer stringContent = new StringBuffer();
149:                         String line = reader.readLine();
150:                         while (line != null) {
151:                                 stringContent.append(line);
152:                                 stringContent.append('\n');
153:                                 line = reader.readLine();
154:                         }
155:
156:                         viewer.getDocument().set(stringContent.toString());
157:                 } catch (IOException e) {
158:                         final IStatus status = new Status(IStatus.ERROR, CompleteOCLInterpreterActivator.PLUGIN_ID,
159:                                         e.getMessage(), e);
160:                         InterpreterPlugin.getDefault().getLog().log(status);
161:                 } finally {
162:                         if (reader != null) {
163:                                 try {
164:                                         reader.close();
165:                                 } catch (IOException e) {
166:                                         // Should have been caught by the outer try
167:                                 }
168:                         }
169:                 }
170:         }
171:
172:         /**
173:          * Tries and retrieve the content of the file denoted by the given URI.
174:          *
175:          * @param uri
176:          * The uri which target file we need to load.
177:          * @return An open input stream towards the file at the given URI. The caller will need to close that
178:          * input stream when it's done with it.
179:          * @throws IOException
180:          * Thrown if we cannot resolve the URI or load the target file.
181:          */
182:         private InputStream getFileContent(URI uri) throws IOException {
183:                 final URIConverter converter = new ExtensibleURIConverterImpl();
184:                 final URIHandler handler = converter.getURIHandler(uri);
185:                 if (handler != null) {
186:                         return handler.createInputStream(uri, new HashMap<String, Object>());
187:                 }
188:                 throw new IOException("Could not load " + uri); //$NON-NLS-1$
189:         }
190:
191:         /**
192:          * Mostly copied from the anonymous implementation in
193:          * org.eclipse.emf.common.ui.dialogs.ResourceDialog.prepareBrowseWorkspaceButton(Button). This only adds a
194:          * viewer filter to the opened dialog.
195:          */
196:         // Copied class. Suppress all warning since we remain as close as possible to original.
197:         @SuppressWarnings("all")
198:         protected class BrowseWorkspaceSelectionListener extends SelectionAdapter {
199:                 @Override
200:                 public void widgetSelected(SelectionEvent event) {
201:                         final ViewerFilter extensionFilter = new ViewerFilter() {
202:                                 @Override
203:                                 public boolean select(Viewer filteredViewer, Object parentElement, Object element) {
204:                                         if (element instanceof IFile) {
205:                                                 return Arrays.asList(FILE_EXTENSIONS).contains(((IFile)element).getFileExtension());
206:                                         }
207:                                         return true;
208:                                 }
209:                         };
210:                         IFile file = null;
211:
212:                         if (isSave()) {
213:                                 String path = getContextPath();
214:                                 file = WorkspaceResourceDialog.openNewFile(getShell(), null, null, path != null ? new Path(
215:                                                 path) : null, Arrays.asList(extensionFilter));
216:                         } else {
217:                                 IFile[] files = WorkspaceResourceDialog.openFileSelection(getShell(), null, null, false,
218:                                                 getContextSelection(), Arrays.asList(extensionFilter));
219:                                 if (files.length != 0) {
220:                                         file = files[0];
221:                                 }
222:                         }
223:
224:                         if (file != null) {
225:                                 uriField.setText(URI.createPlatformResourceURI(file.getFullPath().toString(), true)
226:                                                 .toString());
227:                         }
228:                 }
229:
230:                 private String getContextPath() {
231:                         return context != null && context.isPlatformResource() ? URI.createURI(".").resolve(context) //$NON-NLS-1$
232:                                         .path().substring(9) : null;
233:                 }
234:
235:                 private Object[] getContextSelection() {
236:                         String path = getContextPath();
237:                         if (path != null) {
238:                                 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
239:                                 IResource resource = root.findMember(path);
240:                                 if (resource != null && resource.isAccessible()) {
241:                                         return new Object[] {resource, };
242:                                 }
243:                         }
244:                         return null;
245:                 }
246:         }
247: }