notifications for unknown macros
[fedora-idea.git] / platform / platform-impl / src / com / intellij / openapi / project / impl / ProjectMacrosUtil.java
blobd487438889c0c0afe1baf3c4915b5cb8e1559dbf
1 /*
2 * User: anna
3 * Date: 25-Sep-2007
4 */
5 package com.intellij.openapi.project.impl;
7 import com.intellij.openapi.application.Application;
8 import com.intellij.openapi.application.ApplicationManager;
9 import com.intellij.openapi.application.PathMacros;
10 import com.intellij.openapi.diagnostic.Logger;
11 import com.intellij.openapi.options.ex.SingleConfigurableEditor;
12 import com.intellij.openapi.project.Project;
13 import com.intellij.openapi.project.ProjectBundle;
14 import com.intellij.openapi.ui.Messages;
15 import com.intellij.openapi.util.text.StringUtil;
16 import org.jetbrains.annotations.NonNls;
18 import javax.swing.*;
19 import java.lang.reflect.InvocationTargetException;
20 import java.util.*;
22 public class ProjectMacrosUtil {
23 private static final Logger LOG = Logger.getInstance("#" + ProjectMacrosUtil.class.getName());
25 private ProjectMacrosUtil() {
28 public static boolean showMacrosConfigurationDialog(Project project, final Collection<String> undefinedMacros) {
29 final String text = ProjectBundle.message("project.load.undefined.path.variables.message");
30 final Application application = ApplicationManager.getApplication();
31 if (application.isHeadlessEnvironment() || application.isUnitTestMode()) {
32 throw new RuntimeException(text + ": " + StringUtil.join(undefinedMacros, ", "));
34 final UndefinedMacrosConfigurable configurable =
35 new UndefinedMacrosConfigurable(text, undefinedMacros);
36 final SingleConfigurableEditor editor = new SingleConfigurableEditor(project, configurable) {
37 protected void doOKAction() {
38 if (!getConfigurable().isModified()) {
39 Messages.showErrorDialog(getContentPane(), ProjectBundle.message("project.load.undefined.path.variables.all.needed"),
40 ProjectBundle.message("project.load.undefined.path.variables.title"));
41 return;
43 super.doOKAction();
46 editor.show();
47 return editor.isOK();
50 public static boolean checkMacros(final Project project, final Set<String> usedMacros) {
51 final Set<String> defined = getDefinedMacros();
52 usedMacros.removeAll(defined);
54 // try to lookup values in System properties
55 @NonNls final String pathMacroSystemPrefix = "path.macro.";
56 for (Iterator it = usedMacros.iterator(); it.hasNext();) {
57 final String macro = (String)it.next();
58 final String value = System.getProperty(pathMacroSystemPrefix + macro, null);
59 if (value != null) {
60 ApplicationManager.getApplication().runWriteAction(new Runnable() {
61 public void run() {
62 PathMacros.getInstance().setMacro(macro, value);
64 });
65 it.remove();
69 if (usedMacros.isEmpty()) {
70 return true; // all macros in configuration files are defined
73 // there are undefined macros, need to define them before loading components
74 final boolean[] result = new boolean[1];
76 try {
77 final Runnable r = new Runnable() {
78 public void run() {
79 result[0] = showMacrosConfigurationDialog(project, usedMacros);
83 if (!ApplicationManager.getApplication().isDispatchThread()) {
84 SwingUtilities.invokeAndWait(r);
86 else {
87 r.run();
90 catch (InterruptedException e) {
91 LOG.error(e);
93 catch (InvocationTargetException e) {
94 LOG.error(e);
96 return result[0];
99 public static Set<String> getDefinedMacros() {
100 final PathMacros pathMacros = PathMacros.getInstance();
102 Set<String> definedMacros = new HashSet<String>(pathMacros.getUserMacroNames());
103 definedMacros.addAll(pathMacros.getSystemMacroNames());
104 definedMacros = Collections.unmodifiableSet(definedMacros);
105 return definedMacros;