2 * Created by IntelliJ IDEA.
6 * To change template for new class use
7 * Code Style | Class Templates options (Tools | IDE Options).
9 package com
.intellij
.codeInspection
.actions
;
11 import com
.intellij
.codeInspection
.InspectionManager
;
12 import com
.intellij
.codeInspection
.ex
.GlobalInspectionContextImpl
;
13 import com
.intellij
.codeInspection
.ex
.InspectionManagerEx
;
14 import com
.intellij
.codeInspection
.offlineViewer
.OfflineView
;
15 import com
.intellij
.ide
.RecentProjectsManager
;
16 import com
.intellij
.ide
.util
.PropertiesComponent
;
17 import com
.intellij
.openapi
.actionSystem
.*;
18 import com
.intellij
.openapi
.fileTypes
.FileType
;
19 import com
.intellij
.openapi
.fileTypes
.FileTypeManager
;
20 import com
.intellij
.openapi
.project
.Project
;
21 import com
.intellij
.openapi
.project
.ex
.ProjectEx
;
22 import com
.intellij
.openapi
.ui
.Messages
;
23 import com
.intellij
.openapi
.util
.JDOMUtil
;
24 import com
.intellij
.openapi
.util
.SystemInfo
;
25 import com
.intellij
.openapi
.wm
.ToolWindowId
;
26 import com
.intellij
.openapi
.wm
.ToolWindowManager
;
27 import com
.intellij
.openapi
.wm
.WindowManager
;
28 import com
.intellij
.util
.Icons
;
29 import org
.jdom
.Document
;
30 import org
.jdom
.Element
;
31 import org
.jdom
.JDOMException
;
32 import org
.jetbrains
.annotations
.NonNls
;
35 import javax
.swing
.filechooser
.FileView
;
37 import java
.io
.IOException
;
38 import java
.util
.List
;
40 public class ViewOfflineResultsAction
extends AnAction
{
42 public void update(AnActionEvent event
) {
43 Presentation presentation
= event
.getPresentation();
44 DataContext dataContext
= event
.getDataContext();
45 Project project
= (Project
)dataContext
.getData(DataConstants
.PROJECT
);
46 presentation
.setEnabled(project
!= null);
47 presentation
.setVisible(ActionPlaces
.MAIN_MENU
.equals(event
.getPlace()));
50 public void actionPerformed(AnActionEvent event
) {
51 DataContext dataContext
= event
.getDataContext();
52 Project project
= (Project
)dataContext
.getData(DataConstants
.PROJECT
);
54 String lastFilePath
=getLastFilePath(project
);
55 String path
= lastFilePath
!= null ? lastFilePath
: RecentProjectsManager
.getInstance().getLastProjectPath();
56 JFileChooser fileChooser
= new JFileChooser(path
);
57 FileView fileView
= new FileView() {
58 public Icon
getIcon(File f
) {
59 if (f
.isDirectory()) return super.getIcon(f
);
60 @NonNls final String name
= f
.getName();
61 if (name
.endsWith(".ipr")) {
62 return Icons
.PROJECT_ICON
;
64 FileType fileType
= FileTypeManager
.getInstance().getFileTypeByFileName(name
);
65 return fileType
.getIcon();
69 fileChooser
.setFileView(fileView
);
70 fileChooser
.setAcceptAllFileFilterUsed(false);
71 fileChooser
.setDialogTitle("Open offline inspection results folder");
72 fileChooser
.setFileSelectionMode(JFileChooser
.DIRECTORIES_ONLY
);
74 if (fileChooser
.showOpenDialog(WindowManager
.getInstance().suggestParentWindow(project
)) != JFileChooser
.APPROVE_OPTION
) return;
75 File file
= fileChooser
.getSelectedFile();
76 if (file
== null) return;
77 setLastFilePath(project
, file
.getParent());
79 if (!file
.isDirectory()) return;
80 InspectionManagerEx manager
= (InspectionManagerEx
) InspectionManager
.getInstance(project
);
81 final GlobalInspectionContextImpl inspectionContext
= manager
.createNewGlobalContext(false);
82 OfflineView view
= OfflineView
.create(project
.getName(), project
, inspectionContext
);
83 final File
[] files
= file
.listFiles();
84 for (File inspectionFile
: files
) {
87 doc
= JDOMUtil
.loadDocument(inspectionFile
);
88 ((ProjectEx
) project
).getExpandMacroReplacements().substitute(doc
.getRootElement(), SystemInfo
.isFileSystemCaseSensitive
);
89 } catch (JDOMException e
) {
90 Messages
.showMessageDialog(project
, "Error parsing the results file", "Error", Messages
.getErrorIcon());
92 } catch (IOException e
) {
93 Messages
.showMessageDialog(project
, "Error loading the results file", "Error", Messages
.getErrorIcon());
96 Element root
= doc
.getRootElement();
97 List problems
= root
.getChildren("problem");
98 for (final Object problemElement
: problems
) {
99 Element problem
= (Element
)problemElement
;
100 view
.addProblem(problem
);
104 inspectionContext
.getContentManager().addContent(view
.getContent());
105 ToolWindowManager
.getInstance(project
).getToolWindow(ToolWindowId
.INSPECTION
).activate(null);
108 private static String
getLastFilePath(Project project
) {
109 return PropertiesComponent
.getInstance(project
).getValue("last_opened_file_path");
112 private static void setLastFilePath(Project project
,String path
) {
113 PropertiesComponent
.getInstance(project
).setValue("last_opened_file_path",path
);