Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7c24d2966f90680f5056acb9e852e79798de740c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*******************************************************************************
 * Copyright (c) 2012 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.ui.skynet.render;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.core.commands.Command;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.filesystem.IFileSystem;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.operation.IOperation;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.jdk.core.type.OseeArgumentException;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Collections;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.types.IArtifact;
import org.eclipse.osee.framework.ui.plugin.util.AWorkbench;
import org.eclipse.osee.framework.ui.skynet.FindInWorkspaceOperation;
import org.eclipse.osee.framework.ui.skynet.FindInWorkspaceOperation.FindInWorkspaceCollector;
import org.eclipse.osee.framework.ui.skynet.FrameworkImage;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.osee.framework.ui.swt.ImageManager;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

/**
 * @author John R. Misinco
 */
public class JavaRenderer extends FileSystemRenderer {

   private static final String COMMAND_ID = "org.eclipse.osee.framework.ui.skynet.javaeditor.command";

   @Override
   public String getName() {
      return "Java Editor";
   }

   @Override
   public DefaultArtifactRenderer newInstance() {
      return new JavaRenderer();
   }

   @Override
   public int getApplicabilityRating(PresentationType presentationType, IArtifact artifact) throws OseeCoreException {
      int toReturn = NO_MATCH;
      Artifact aArtifact = artifact.getFullArtifact();
      if (aArtifact.isOfType(CoreArtifactTypes.TestCase)) {
         if (presentationType.matches(PresentationType.SPECIALIZED_EDIT, PresentationType.DEFAULT_OPEN)) {
            toReturn = PRESENTATION_SUBTYPE_MATCH;
         }
      }
      return toReturn;
   }

   @Override
   public List<String> getCommandIds(CommandGroup commandGroup) {
      ArrayList<String> commandIds = new ArrayList<String>(1);
      if (commandGroup.isEdit()) {
         commandIds.add(COMMAND_ID);
      }
      return commandIds;
   }

   @Override
   public boolean supportsCompare() {
      return false;
   }

   @Override
   public void open(final List<Artifact> artifacts, PresentationType presentationType) {
      final List<Artifact> notMatched = new LinkedList<Artifact>();
      final StringBuffer findErrorMessage = new StringBuffer();

      FindInWorkspaceCollector collector = new FindInWorkspaceCollector() {

         @Override
         public void onResource(final IResource resource) {
            Displays.ensureInDisplayThread(new Runnable() {
               @Override
               public void run() {
                  IFileSystem fs = EFS.getLocalFileSystem();

                  IPath fullPath = resource.getLocation();
                  final File fileToOpen = fullPath.toFile();
                  if (fileToOpen != null && fileToOpen.exists() && fileToOpen.isFile()) {
                     try {
                        IFileStore fileStore = fs.getStore(fileToOpen.toURI());
                        IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
                        IDE.openEditorOnFileStore(page, fileStore);
                     } catch (PartInitException e) {
                        findErrorMessage.append(e.toString());
                     }
                  }
               }
            });
         }

         @Override
         public void onNotFound(Artifact artifact) {
            notMatched.add(artifact);
         }
      };
      IOperation op = new FindInWorkspaceOperation(artifacts, collector);
      Operations.executeAsJob(op, true, Job.LONG, new JobChangeAdapter() {
         @Override
         public void done(IJobChangeEvent event) {
            if (event.getResult().isOK()) {
               Displays.ensureInDisplayThread(new Runnable() {
                  @Override
                  public void run() {
                     StringBuilder builder = new StringBuilder();
                     builder.append(findErrorMessage);
                     if (!notMatched.isEmpty()) {
                        builder.append(String.format("Item(s) not found in the workspace: [%s]\n",
                           Collections.toString(",", notMatched)));
                     }

                     if (builder.length() > 0) {
                        Shell shell = AWorkbench.getActiveShell();
                        MessageDialog.openError(shell, getName(), builder.toString());
                     }
                  }
               });
            }
         }
      });
   }

   @Override
   public String getAssociatedExtension(Artifact artifact) {
      return "java";
   }

   @Override
   public Program getAssociatedProgram(Artifact artifact) throws OseeCoreException {
      Program program = Program.findProgram("java");
      if (program == null) {
         throw new OseeArgumentException("No program associated with the extension [%s] found on your local machine.",
            "java");
      }
      return program;
   }

   @Override
   public InputStream getRenderInputStream(PresentationType presentationType, List<Artifact> artifacts) throws OseeCoreException {
      final List<IResource> matches = new LinkedList<IResource>();
      final List<Artifact> notMatched = new LinkedList<Artifact>();

      FindInWorkspaceCollector collector = new FindInWorkspaceCollector() {

         @Override
         public void onResource(IResource resource) {
            matches.add(resource);
         }

         @Override
         public void onNotFound(Artifact artifact) {
            notMatched.add(artifact);
         }
      };
      IOperation op = new FindInWorkspaceOperation(artifacts.subList(0, 1), collector);
      Operations.executeWorkAndCheckStatus(op);
      for (IResource resource : matches) {
         IPath fullPath = resource.getLocation();
         File fileToOpen = fullPath.toFile();
         try {
            return new FileInputStream(fileToOpen);
         } catch (FileNotFoundException ex) {
            OseeExceptions.wrapAndThrow(ex);
         }
      }
      return null;
   }

   @Override
   public ImageDescriptor getCommandImageDescriptor(Command command, Artifact artifact) {
      return ImageManager.getImageDescriptor(FrameworkImage.JAVA_COMPILATION_UNIT);
   }

   @SuppressWarnings("unused")
   @Override
   protected IOperation getUpdateOperation(File file, List<Artifact> artifacts, IOseeBranch branch, PresentationType presentationType) throws OseeCoreException {
      throw new UnsupportedOperationException();
   }

}

Back to the top