예제 #1
0
  public void wrap(final String selection, @NotNull final CustomTemplateCallback callback) {
    InputValidatorEx validator =
        new InputValidatorEx() {
          public String getErrorText(String inputString) {
            if (!checkTemplateKey(inputString, callback)) {
              return XmlBundle.message("zen.coding.incorrect.abbreviation.error");
            }
            return null;
          }

          public boolean checkInput(String inputString) {
            return getErrorText(inputString) == null;
          }

          public boolean canClose(String inputString) {
            return checkInput(inputString);
          }
        };
    final String abbreviation =
        Messages.showInputDialog(
            callback.getProject(),
            XmlBundle.message("zen.coding.enter.abbreviation.dialog.label"),
            XmlBundle.message("zen.coding.title"),
            Messages.getQuestionIcon(),
            "",
            validator);
    if (abbreviation != null) {
      doWrap(selection, abbreviation, callback);
    }
  }
 protected boolean resultIsValid(
     final Project project,
     ProgressIndicator indicator,
     final String resourceUrl,
     FetchResult result) {
   if (myForceResultIsValid) {
     return true;
   }
   if (!ApplicationManager.getApplication().isUnitTestMode()
       && result.contentType != null
       && result.contentType.contains(HTML_MIME)
       && new String(result.bytes).contains("<html")) {
     ApplicationManager.getApplication()
         .invokeLater(
             () ->
                 Messages.showMessageDialog(
                     project,
                     XmlBundle.message("invalid.url.no.xml.file.at.location", resourceUrl),
                     XmlBundle.message("invalid.url.title"),
                     Messages.getErrorIcon()),
             indicator.getModalityState());
     return false;
   }
   return true;
 }
  @Nullable
  private static FetchResult fetchData(
      final Project project, final String dtdUrl, final ProgressIndicator indicator)
      throws IOException {
    try {
      return HttpRequests.request(dtdUrl)
          .accept("text/xml,application/xml,text/html,*/*")
          .connect(
              new HttpRequests.RequestProcessor<FetchResult>() {
                @Override
                public FetchResult process(@NotNull HttpRequests.Request request)
                    throws IOException {
                  FetchResult result = new FetchResult();
                  result.bytes = request.readBytes(indicator);
                  result.contentType = request.getConnection().getContentType();
                  return result;
                }
              });
    } catch (MalformedURLException e) {
      if (!ApplicationManager.getApplication().isUnitTestMode()) {
        ApplicationManager.getApplication()
            .invokeLater(
                () ->
                    Messages.showMessageDialog(
                        project,
                        XmlBundle.message("invalid.url.message", dtdUrl),
                        XmlBundle.message("invalid.url.title"),
                        Messages.getErrorIcon()),
                indicator.getModalityState());
      }
    }

    return null;
  }
 public EditLocationDialog(Project project, boolean showPath) {
   super(project, true);
   myProject = project;
   myShowPath = showPath;
   myTitle = XmlBundle.message("dialog.title.external.resource");
   myName = XmlBundle.message("label.edit.external.resource.uri");
   myLocation = XmlBundle.message("label.edit.external.resource.path");
   init();
 }
 {
   List<String> names = new ArrayList<String>();
   names.add(XmlBundle.message("column.name.edit.external.resource.uri"));
   names.add(XmlBundle.message("column.name.edit.external.resource.location"));
   if (myProject != null) {
     names.add("Project");
   }
   myNames = ArrayUtil.toStringArray(names);
 }
  public static void addColorPreviewAndCodeToLookup(final Color color, final StringBuilder buf) {
    if (color == null) return;
    final String code = '#' + toHex(color);
    final String colorName = getColorNameForHexCode(code);
    if (colorName != null) {
      buf.append(XmlBundle.message("color.name", colorName)).append(BR);
    }

    String colorBox =
        "<div style=\"border: 1px solid #000000; width: 50px; height: 20px; background-color:"
            + code
            + "\"></div>";
    buf.append(XmlBundle.message("color.preview", colorBox)).append(BR);
  }
  private static class IgnoredUrlsModel extends AddEditRemovePanel.TableModel<String> {
    private final String[] myNames = {XmlBundle.message("column.name.edit.external.resource.uri")};

    @Override
    public int getColumnCount() {
      return myNames.length;
    }

    @Override
    public Object getField(String o, int columnIndex) {
      return o;
    }

    @Override
    public Class getColumnClass(int columnIndex) {
      return String.class;
    }

    @Override
    public boolean isEditable(int column) {
      return false;
    }

    @Override
    public void setValue(Object aValue, String data, int columnIndex) {}

    @Override
    public String getColumnName(int column) {
      return myNames[column];
    }
  }
  @Override
  public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    if (!(file instanceof XmlFile)) return false;

    int offset = editor.getCaretModel().getOffset();
    String uri = findUri(file, offset);
    if (uri == null || !isAcceptableUri(uri)) return false;

    setText(XmlBundle.message(getQuickFixKeyId()));
    return true;
  }
  @Nullable
  private String fetchOneFile(
      final ProgressIndicator indicator,
      final String resourceUrl,
      final Project project,
      String extResourcesPath,
      @Nullable String refname)
      throws IOException {
    SwingUtilities.invokeLater(
        () -> indicator.setText(XmlBundle.message("fetching.progress.indicator", resourceUrl)));

    FetchResult result = fetchData(project, resourceUrl, indicator);
    if (result == null) return null;

    if (!resultIsValid(project, indicator, resourceUrl, result)) {
      return null;
    }

    int slashIndex = resourceUrl.lastIndexOf('/');
    String resPath = extResourcesPath + File.separatorChar;

    if (refname != null) { // resource is known under ref.name so need to save it
      resPath += refname;
      int refNameSlashIndex = resPath.lastIndexOf('/');
      if (refNameSlashIndex != -1) {
        final File parent = new File(resPath.substring(0, refNameSlashIndex));
        if (!parent.mkdirs() || !parent.exists()) {
          LOG.warn("Unable to create: " + parent);
        }
      }
    } else {
      resPath +=
          Integer.toHexString(resourceUrl.hashCode()) + "_" + resourceUrl.substring(slashIndex + 1);
    }

    final int lastDoPosInResourceUrl = resourceUrl.lastIndexOf('.', slashIndex);
    if (lastDoPosInResourceUrl == -1
        || FileTypeManager.getInstance()
                .getFileTypeByExtension(resourceUrl.substring(lastDoPosInResourceUrl + 1))
            == FileTypes.UNKNOWN) {
      // remote url does not contain file with extension
      final String extension =
          result.contentType != null && result.contentType.contains(HTML_MIME)
              ? StdFileTypes.HTML.getDefaultExtension()
              : StdFileTypes.XML.getDefaultExtension();
      resPath += "." + extension;
    }

    File res = new File(resPath);

    FileUtil.writeToFile(res, result.bytes);
    return resPath;
  }
 @Override
 public void selectIn(SelectInContext context, boolean requestFocus) {
   PsiElement psiElement = (PsiElement) context.getSelectorInFile();
   LOG.assertTrue(psiElement != null);
   PsiFile psiFile = psiElement.getContainingFile();
   LOG.assertTrue(psiFile != null);
   try {
     Url url = WebBrowserService.getInstance().getUrlToOpen(psiFile, false);
     if (url != null) {
       ApplicationManager.getApplication().saveAll();
       BrowserUtil.launchBrowser(url.toExternalForm());
     }
   } catch (WebBrowserUrlProvider.BrowserException e1) {
     Messages.showErrorDialog(e1.getMessage(), XmlBundle.message("browser.error"));
   } catch (Exception e1) {
     LOG.error(e1);
   }
 }
  @Override
  protected void doInvoke(
      @NotNull final PsiFile file, final int offset, @NotNull final String uri, final Editor editor)
      throws IncorrectOperationException {
    final String url = findUrl(file, offset, uri);
    final Project project = file.getProject();

    ProgressManager.getInstance()
        .run(
            new Task.Backgroundable(project, XmlBundle.message("fetching.resource.title")) {
              @Override
              public void run(@NotNull ProgressIndicator indicator) {
                while (true) {
                  try {
                    HttpConfigurable.getInstance().prepareURL(url);
                    fetchDtd(project, uri, url, indicator);
                    ApplicationManager.getApplication()
                        .invokeLater(() -> DaemonCodeAnalyzer.getInstance(project).restart(file));
                    return;
                  } catch (IOException ex) {
                    LOG.info(ex);
                    @SuppressWarnings("InstanceofCatchParameter")
                    String problemUrl =
                        ex instanceof FetchingResourceIOException
                            ? ((FetchingResourceIOException) ex).url
                            : url;
                    String message = XmlBundle.message("error.fetching.title");

                    if (!url.equals(problemUrl)) {
                      message = XmlBundle.message("error.fetching.dependent.resource.title");
                    }

                    if (!IOExceptionDialog.showErrorDialog(
                        message, XmlBundle.message("error.fetching.resource", problemUrl))) {
                      break; // cancel fetching
                    }
                  }
                }
              }
            });
  }
  @Override
  public boolean canSelect(SelectInContext context) {
    Object selectorInFile = context.getSelectorInFile();
    if (!(selectorInFile instanceof PsiElement)) {
      return false;
    }

    PsiFile file = ((PsiElement) selectorInFile).getContainingFile();
    if (file == null || file.getVirtualFile() == null) {
      return false;
    }

    Pair<WebBrowserUrlProvider, Url> browserUrlProvider = WebBrowserServiceImpl.getProvider(file);
    currentName = XmlBundle.message("browser.select.in.default.name");
    if (browserUrlProvider == null) {
      return HtmlUtil.isHtmlFile(file) && !(file.getVirtualFile() instanceof LightVirtualFile);
    } else {
      String customText = browserUrlProvider.first.getOpenInBrowserActionText(file);
      if (customText != null) {
        currentName = customText;
      }
    }
    return true;
  }
  @Override
  public JComponent createComponent() {
    myPanel =
        new JPanel(new GridBagLayout()) {
          @Override
          public Dimension getPreferredSize() {
            return new Dimension(-1, 400);
          }
        };

    myExtPanel =
        new AddEditRemovePanel<NameLocationPair>(
            new ExtUrlsTableModel(),
            myPairs,
            XmlBundle.message("label.edit.external.resource.configure.external.resources")) {
          @Override
          protected NameLocationPair addItem() {
            return addExtLocation();
          }

          @Override
          protected boolean removeItem(NameLocationPair o) {
            setModified(true);
            return true;
          }

          @Override
          protected NameLocationPair editItem(NameLocationPair o) {
            return editExtLocation(o);
          }
        };
    myExtPanel.getTable().setShowColumns(true);

    myExtPanel.setRenderer(1, new PathRenderer());

    JTable table = myExtPanel.getTable();
    if (myProject != null) {
      TableColumn column = table.getColumn(table.getColumnName(2));
      column.setMaxWidth(50);
      column.setCellEditor(JBTable.createBooleanEditor());
    }

    table
        .getModel()
        .addTableModelListener(
            new TableModelListener() {
              @Override
              public void tableChanged(TableModelEvent e) {
                setModified(true);
              }
            });
    myIgnorePanel =
        new AddEditRemovePanel<String>(
            new IgnoredUrlsModel(),
            myIgnoredUrls,
            XmlBundle.message("label.edit.external.resource.configure.ignored.resources")) {
          @Override
          protected String addItem() {
            return addIgnoreLocation();
          }

          @Override
          protected boolean removeItem(String o) {
            setModified(true);
            return true;
          }

          @Override
          protected String editItem(String o) {
            return editIgnoreLocation(o);
          }
        };

    myPanel.add(
        myExtPanel,
        new GridBagConstraints(
            0,
            0,
            1,
            1,
            1,
            1,
            GridBagConstraints.NORTH,
            GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0),
            0,
            0));
    myPanel.add(
        myIgnorePanel,
        new GridBagConstraints(
            0,
            1,
            1,
            1,
            1,
            1,
            GridBagConstraints.NORTH,
            GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0),
            0,
            0));

    myExtPanel.setData(myPairs);
    myIgnorePanel.setData(myIgnoredUrls);

    myExtPanel.getEmptyText().setText(XmlBundle.message("no.external.resources"));
    myIgnorePanel.getEmptyText().setText(XmlBundle.message("no.ignored.resources"));

    return myPanel;
  }
 @Override
 public String getDisplayName() {
   return XmlBundle.message("display.name.edit.external.resource");
 }
 public String getDisplayName() {
   return XmlBundle.message("web.editor.configuration.title");
 }
 @Override
 @Nls
 @NotNull
 public String getDisplayName() {
   return XmlBundle.message("html.inspections.unknown.tag");
 }
 protected String getPanelTitle() {
   return XmlBundle.message("html.inspections.unknown.tag.title");
 }
 protected String getCheckboxTitle() {
   return XmlBundle.message("html.inspections.unknown.tag.checkbox.title");
 }
  private static void doAction(
      final Project project, final GenerateSchemaFromInstanceDocumentDialog dialog) {
    FileDocumentManager.getInstance().saveAllDocuments();

    final String url = dialog.getUrl().getText();
    final VirtualFile relativeFile =
        VfsUtilCore.findRelativeFile(
            ExternalResourceManager.getInstance().getResourceLocation(url), null);
    VirtualFile relativeFileDir;
    if (relativeFile == null) {
      Messages.showErrorDialog(
          project, XmlBundle.message("file.doesnt.exist", url), XmlBundle.message("error"));
      return;
    } else {
      relativeFileDir = relativeFile.getParent();
    }
    if (relativeFileDir == null) {
      Messages.showErrorDialog(
          project, XmlBundle.message("file.doesnt.exist", url), XmlBundle.message("error"));
      return;
    }

    @NonNls List<String> parameters = new LinkedList<String>();
    parameters.add("-design");
    parameters.add(DESIGN_TYPES.get(dialog.getDesignType()));

    parameters.add("-simple-content-types");
    parameters.add(CONTENT_TYPES.get(dialog.getSimpleContentType()));

    parameters.add("-enumerations");
    String enumLimit = dialog.getEnumerationsLimit();
    parameters.add("0".equals(enumLimit) ? "never" : enumLimit);

    parameters.add("-outDir");
    final String dirPath = relativeFileDir.getPath();
    parameters.add(dirPath);

    final File expectedSchemaFile =
        new File(dirPath + File.separator + relativeFile.getName() + "0.xsd");
    if (expectedSchemaFile.exists()) {
      if (!expectedSchemaFile.delete()) {
        Messages.showErrorDialog(
            project,
            XmlBundle.message("cant.delete.file", expectedSchemaFile.getPath()),
            XmlBundle.message("error"));
        return;
      }
    }

    parameters.add("-outPrefix");
    parameters.add(relativeFile.getName());

    parameters.add(url);
    File xsd = new File(dirPath + File.separator + dialog.getTargetSchemaName());
    final VirtualFile xsdFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(xsd);
    if (xsdFile != null) {
      ApplicationManager.getApplication()
          .runWriteAction(
              () -> {
                try {
                  xsdFile.delete(null);
                } catch (IOException e) { //
                }
              });
    }

    Inst2Xsd.main(ArrayUtil.toStringArray(parameters));
    if (expectedSchemaFile.exists()) {
      final boolean renamed = expectedSchemaFile.renameTo(xsd);
      if (!renamed) {
        Messages.showErrorDialog(
            project,
            XmlBundle.message("cant.rename.file", expectedSchemaFile.getPath(), xsd.getPath()),
            XmlBundle.message("error"));
      }
    }

    VirtualFile xsdVFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(xsd);
    if (xsdVFile != null) {
      FileEditorManager.getInstance(project).openFile(xsdVFile, true);
    } else {
      Messages.showErrorDialog(
          project,
          XmlBundle.message("xml2xsd.generator.error.message"),
          XmlBundle.message("xml2xsd.generator.error"));
    }
  }
예제 #20
0
 @NotNull
 public String getTitle() {
   return XmlBundle.message("zen.coding.title");
 }
 @Nls
 @NotNull
 public String getDisplayName() {
   return XmlBundle.message("html.inspections.unknown.attribute");
 }
 @NotNull
 public ActionPresentation getPresentation() {
   return new ActionPresentationData(
       XmlBundle.message("html5.outline.mode"), null, AllIcons.Xml.Html5);
 }
 @Override
 @NotNull
 public String getFamilyName() {
   return XmlBundle.message(getQuickFixKeyId());
 }
 public String getCheckBoxText() {
   return XmlBundle.message("html5.outline.mode");
 }