示例#1
0
  private static Long getIdForSelection(Node selectedSourceNode, VpvVisualModel visualModel) {
    Long id = null;
    if (selectedSourceNode != null && visualModel != null) {
      Map<Node, Node> sourceVisuaMapping = visualModel.getSourceVisualMapping();

      Node visualNode = null;
      Node sourceNode = selectedSourceNode;
      do {
        visualNode = sourceVisuaMapping.get(sourceNode);
        sourceNode = DomUtil.getParentNode(sourceNode);
      } while (visualNode == null && sourceNode != null);

      if (!(visualNode instanceof Element)) { // text node, comment, etc
        visualNode = DomUtil.getParentNode(visualNode); // should be element now or null
      }

      String idString = null;
      if (visualNode instanceof Element) {
        Element elementNode = (Element) visualNode;
        idString = elementNode.getAttribute(VpvDomBuilder.ATTR_VPV_ID);
      }

      if (idString != null && !idString.isEmpty()) {
        try {
          id = Long.parseLong(idString);
        } catch (NumberFormatException e) {
          Activator.logError(e);
        }
      }
    }
    return id;
  }
示例#2
0
  public static void navigateToVisual(
      IEditorPart currentEditor, Browser browser, VpvVisualModel visualModel, int x, int y) {
    String stringToEvaluate = ""; // $NON-NLS-1$
    if (OS.LINUX.equals(PlatformUtil.getOs())) {
      /* outerHTML is not available with XulRunner we shipping, so <code>result</code> variable will be null
       * because we make it default browser on Linux this workaround is used
       * @see JBIDE-17454
       */
      stringToEvaluate =
          "var selected = document.elementFromPoint("
              + x
              + ", "
              + y
              + ");"
              + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
              "var temp = document.createElement('div');"
              + //$NON-NLS-1$
              "temp.appendChild(selected.cloneNode(true));"
              + //$NON-NLS-1$
              "return temp.innerHTML;"; //$NON-NLS-1$
    } else {
      stringToEvaluate =
          "return document.elementFromPoint("
              + x
              + ", "
              + y
              + ").outerHTML;"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    }
    String result = (String) browser.evaluate(stringToEvaluate);
    if (result != null) {
      String selectedElementId =
          TransformUtil.getSelectedElementId(result, "(?<=data-vpvid=\").*?(?=\")"); // $NON-NLS-1$

      Long id =
          (selectedElementId != null
                  && !selectedElementId.isEmpty()) // avoiding NumberFormatException
              ? Long.parseLong(selectedElementId)
              : null;
      NavigationUtil.outlineSelectedElement(browser, id);

      String fileExtension = EditorUtil.getFileExtensionFromEditor(currentEditor);

      if (SuitableFileExtensions.isHTML(fileExtension)) {
        try {
          Node visualNode = TransformUtil.getVisualNodeByVpvId(visualModel, selectedElementId);
          Node sourseNode = TransformUtil.getSourseNodeByVisualNode(visualModel, visualNode);

          if (sourseNode != null && sourseNode instanceof IDOMNode) {
            int startOffset = ((IDOMNode) sourseNode).getStartOffset();
            int endOffset = ((IDOMNode) sourseNode).getEndOffset();

            StructuredTextEditor editor =
                (StructuredTextEditor) currentEditor.getAdapter(StructuredTextEditor.class);
            editor.selectAndReveal(startOffset, endOffset - startOffset);
          }

        } catch (XPathExpressionException e) {
          Activator.logError(e);
        }
      }
    }
  }