private static void comment(final JTextPane pane, final int startIdx, final int endIdx) { final Document doc = pane.getDocument(); try { final int startNo = ScriptEditPanel.getLineOfOffset(doc, startIdx); final int endNo = ScriptEditPanel.getLineOfOffset(doc, endIdx); boolean isComment = true; for (int i = startNo; i <= endNo; i++) { final int lineStartIdx = ScriptEditPanel.getLineStartOffset(doc, i); final int lineEndIdx = ScriptEditPanel.getLineEndOffset(doc, i); final String text = doc.getText(lineStartIdx, lineEndIdx - lineStartIdx); final char[] textChars = text.toCharArray(); int commentIdx = -1; int unSpaceIdx = -1; int commentEndIdx = -1; // 注意:*/ the star index final int textCharLen = textChars.length; for (int j = 0; j < textCharLen; j++) { final char currChar = textChars[j]; if (unSpaceIdx == -1 && (currChar == ' ' || currChar == '\t') == false) { unSpaceIdx = j; } if (commentIdx == -1 && j + 1 < textCharLen && currChar == '/' && textChars[j + 1] == '*') { commentIdx = j; if (i == startNo) { isComment = false; // 以首行来判断是否进行comment/uncomment } if (isComment) { break; } } if (isComment == false && j + 1 < textCharLen && currChar == '*' && textChars[j + 1] == '/') { commentEndIdx = j; break; } } if (isComment && commentIdx >= 0) { continue; // 当前行已注释 } else if (isComment == false && commentIdx == -1) { continue; // 当前行已无注释 } if (isComment) { doc.insertString(lineEndIdx - 1, "*/", null); doc.insertString(lineStartIdx + unSpaceIdx, "/*", null); } else { if (commentEndIdx > 0) { doc.remove(lineStartIdx + commentEndIdx, 2); } if (commentIdx >= 0) { doc.remove(lineStartIdx + commentIdx, 2); } } } } catch (final Throwable e) { e.printStackTrace(); } }
/** * @param cssScriptsWithRem * @param cssProjectClasses 如果只有一个,则存储为CSSClassIndex;多个,则为Vector<CSSClassIndex> * @param docMaybeNull * @return */ public static String updateCSSClass( final String cssScriptsWithRem, final Vector<Object> cssProjectClasses, final Document docMaybeNull) { String sameFullName = null; final Matcher m = cssClassPattern.matcher(commentPattern.matcher(cssScriptsWithRem).replaceAll("")); int stringLineNoIdx = 0; int stringLineNo = 0; while (m.find()) { final String html = m.group(2); final String cssClass = m.group(3); final String checkFullName = html.toUpperCase() + "." + cssClass; final String matchItem = m.group(0); int lineNo = 0; final int startIdx = cssScriptsWithRem.indexOf(matchItem, m.start(0)); // 注:须包括可能的/**/ if (docMaybeNull != null) { try { lineNo = ScriptEditPanel.getLineOfOffset(docMaybeNull, startIdx) + 1; } catch (final BadLocationException e) { e.printStackTrace(); } } else { lineNo = StringUtil.getLineNo(cssScriptsWithRem, stringLineNoIdx, stringLineNo, startIdx); stringLineNoIdx = startIdx; stringLineNo = lineNo; } final CSSClassIndex idx = new CSSClassIndex(checkFullName, cssClass, startIdx, lineNo); boolean isAdded = false; final int size = cssProjectClasses.size(); for (int i = 0; i < size; i++) { final Object item = cssProjectClasses.elementAt(i); if (item instanceof CSSClassIndex) { final CSSClassIndex compIdx = (CSSClassIndex) item; if (compIdx.className.equals(cssClass)) { final Vector<CSSClassIndex> v = new Vector<CSSClassIndex>(2); cssProjectClasses.remove(i); cssProjectClasses.add(i, v); v.add(compIdx); v.add(idx); isAdded = true; if (sameFullName == null && checkFullName.equals(compIdx.fullName)) { sameFullName = "<html>Error same CSS define [<strong>" + checkFullName + "</strong>]." + "<BR><BR>" + "It is defined at line : <strong>" + compIdx.lineNo + "</strong> and line : <strong>" + idx.lineNo + "</strong></html>"; } break; } } else { final Vector<CSSClassIndex> v = (Vector<CSSClassIndex>) item; // 检查fullName final int subSize = v.size(); for (int j = 0; j < subSize; j++) { final CSSClassIndex compIdx = v.elementAt(j); if (sameFullName == null && checkFullName.equals(compIdx.fullName)) { sameFullName = "<html>Error same CSS define [<strong>" + checkFullName + "</strong>]." + "<BR><BR>" + "It is defined at line : <strong>" + compIdx.lineNo + "</strong> and line : <strong>" + idx.lineNo + "</strong></html>"; break; } } if (v.elementAt(0).className.equals(cssClass)) { isAdded = true; v.add(idx); } } } if (isAdded == false) { cssProjectClasses.add(idx); } } final int cssSize = cssProjectClasses.size(); for (int i = 0; i < cssSize; i++) { final Object element = cssProjectClasses.elementAt(i); if (element instanceof Vector) { Collections.sort((Vector<CSSClassIndex>) element); } } return sameFullName; }
public static void initScriptPanel(final JTextPane sPanel, final ScriptEditPanel sep) { sep.rebuildASTNode(); scriptPanel = sPanel; scriptPanel.setCaretPosition(0); }