📄 javaviewfactory.java
字号:
}
}
return x;
}
// private boolean isClass(char indexedChar, int wordIndex) {
// Element element = getDocument().getDefaultRootElement();
// //the
// document
// int rowNumber = element.getElementIndex(wordIndex);
// Element row = element.getElement(rowNumber);
// int firstColumnInRow = row.getStartOffset();
// String rowContent = null;
// String className = null;
// try {
// rowContent = (getDocument()).getText(
// firstColumnInRow, wordIndex - firstColumnInRow);
// className = getLastToken(rowContent);
// } catch (BadLocationException e1) {
// e1.printStackTrace();
// }
// if (className != null) {
// try {
// Class.forName(className);
// return true;
// } catch (ClassNotFoundException e) {
// System.out.println("Class name: " + className
// + " not found, verifying imports");
// try {
// className = verifyImport(className, wordIndex);
// return true;
// /* } catch (FileNotFoundException exc) {
// System.out.println(className
// + " is not an imported Class");
// */} catch (Exception exc) {
// System.out.println("Exception?!?!");
// e.printStackTrace();
// }
// }
// }
// return false;
// }
//
// private String getLastToken(String rowSegment)
// throws BadLocationException {
// StringTokenizer st = new StringTokenizer(rowSegment,
// "; \n\"()[]$|!,+-/=?^:<>\f\'\t\r%&~ ", false);
// String lastToken = null;
// while (st.hasMoreTokens()) {
// lastToken = st.nextToken();
// }
// return lastToken;
// }
//
// private String verifyImport(String classToFind, int
// wordIndex)
// throws FileNotFoundException, IOException,
// BadLocationException {
//
// Element element = getDocument().getDefaultRootElement();
// int rowNumber = element.getElementIndex(wordIndex);
// Element row = element.getElement(rowNumber);
//
// String classInPackage = null;
// /* if ((classInPackage = verifyPackage(classToFind)) !=
// null) {
// return classInPackage;
// }
// */ System.out.println("Verifying imports...");
// Pattern p = Pattern.compile("\\bimport\\b");
// String content = getDocument().getText(0, getDocument()
// .getLength());
// Matcher m = p.matcher(content);
// //java.lang.* is always imported
// try {
// String langClass = "java.lang." + classToFind;
// Class.forName(langClass);
// return langClass;
// } catch (ClassNotFoundException e) {
// System.out.println(classToFind + "is not a java.lang
// class");
// }
// return null;
// /* while (m.find()) {
// int end = m.end();
// System.out.println("Keyword import found at line " + end);
// String importLine[] = getImportLine(end);
// String packageName = importLine[0];
// String className = importLine[1];
// if (className.equals(classToFind))
// return packageName + "." + className;
// else if (className.equals("*")) {
// try {
// Class.forName(packageName + "." + classToFind);
// return packageName + "." + classToFind;
// } catch (ClassNotFoundException e) {
// System.out.println(packageName + "." + classToFind
// + "is not a true class!");
// }
// }
// }
// throw new FileNotFoundException("Class not found: " +
// classToFind);*/
// }
//
// /*
// private String verifyPackage(String className)
// throws FileNotFoundException, IOException,
// BadLocationException {
// System.out.println("Verifying if class is in package...");
// Pattern p = Pattern.compile("\\bpackage\\b");
// String content = ((AbstractDocument) getDocument()
// .getDefaultRootElement()).getText();
// Matcher m = p.matcher(content);
// if (m.find()) {
// int end = m.end();
// System.out.println("Keyword package found at line " + end);
// //String packageName = importLine[0];
// Element row = getRowAt(end);
// int lastColumnInRow = row.getEndOffset();
// String packageName =
// getLastTokenNoDot(EJEArea.this.getText(
// end, lastColumnInRow - end));
// try {
// Class.forName(packageName + "." + className);
// return packageName + "." + className;
// } catch (ClassNotFoundException e) {
// System.out.println(className + " is not in this package");
// }
// }
// return null;
// }
//
// private String[] getImportLine(int importEndOffset)
// throws BadLocationException {
// Element row = getRowAt(importEndOffset);
// int lastColumnInRow = row.getEndOffset();
// String[] importLine = new String[2];
// importLine[1] =
// getLastToken(EJEArea.this.getText(importEndOffset,
// lastColumnInRow - importEndOffset));
// int packageEndOffset = lastColumnInRow
// - (importLine[1].length() + 3);
// importLine[0] = EJEArea.this.getText(importEndOffset,
// packageEndOffset - importEndOffset).trim();
// return importLine;
// }
// */
private Color getColorForTokenType(String tokenType) {
JTextComponent host = (JTextComponent) getContainer();
if (tokenType == KEYWORD)
return (Color) host.getClientProperty(JavaTextEditor.KEYWORD_COLOR);
// else if (tokenType == CLASS)
// return (Color) host
// .getClientProperty(EJEArea.COMMON_WORD_COLOR);
else
return host.getForeground();
}
private boolean isSingleLineCommentStart(SegmentExt string, int index) {
if (string.charAt(index) == '/' && index + 1 < string.length()
&& string.charAt(index + 1) == '/')
return true;
return false;
}
private int scanSingleLineComment(SegmentExt string, int index) {
int commentLength = 0;
for (commentLength = 0; commentLength < (string.length() - index); commentLength++) {
char commentChar = string.charAt(index + commentLength);
if (commentChar == '\n')
break;
}
return commentLength;
}
private boolean isMultiLineCommentStart(SegmentExt string, int index) {
if (string.charAt(index) == '/' && index + 1 < string.length()
&& string.charAt(index + 1) == '*')
return true;
return false;
}
private boolean isMultiLineCommentEnd(SegmentExt string, int index) {
if (string.charAt(index) == '*' && index + 1 < string.length()
&& string.charAt(index + 1) == '/')
return true;
return false;
}
private Element getRowAt(int offset) {
Element element = getDocument().getDefaultRootElement();
int rowNumber = element.getElementIndex(offset);
return element.getElement(rowNumber);
}
private int scanMultiLineComment(SegmentExt string, int index) {
int commentLength = 0;
boolean starFound = false;
for (commentLength = 0; commentLength < (string.length() - index); commentLength++) {
char commentChar = string.charAt(index + commentLength);
if (starFound && commentChar == '/') {
commentLength++;
break;
}
starFound = false;
if (commentChar == '\n')
break;
else if (commentChar == '*')
starFound = true;
}
return commentLength;
}
private int scanStringLiteral(SegmentExt string, int index) {
int stringLength = 1;
boolean backslash = false;
for (stringLength = 1; stringLength < (string.length() - index); stringLength++) {
char stringChar = string.charAt(index + stringLength);
if (stringChar == '\\')
backslash = true;
else if (stringChar == '\n')
break;
else if (stringChar == '"' && !backslash) {
stringLength++;
break;
} else if (backslash)
backslash = false;
}
return stringLength;
}
private int scanCharLiteral(SegmentExt string, int index) {
int charLength = 1;
boolean backslash = false;
for (charLength = 1; charLength < (string.length() - index); charLength++) {
char charChar = string.charAt(index + charLength);
if (charChar == '\\')
backslash = true;
else if (charChar == '\n')
break;
else if (charChar == '\'' && !backslash) {
charLength++;
break;
} else if (backslash)
backslash = false;
}
return charLength;
}
private String scanIdentifier(SegmentExt stringSegment, int index) {
String string = new String(stringSegment.array, stringSegment.offset,
stringSegment.count);
int identifierLength = 0;
if (!Character.isJavaIdentifierStart(string.charAt(index)))
return "";
else
for (identifierLength = 1; identifierLength < (string.length() - index); identifierLength++) {
char identifierChar = string.charAt(index + identifierLength);
if (!Character.isJavaIdentifierPart(string.charAt(index + identifierLength)))
break;
}
return string.substring(index, index + identifierLength);
}
private String scanNumericLiteral(SegmentExt stringSegment, int index) {
String string = new String(stringSegment.array, stringSegment.offset,
stringSegment.count);
int identifierLength = 0;
for (identifierLength = 0; identifierLength < (string.length() - index); identifierLength++) {
char identifierChar = string.charAt(index + identifierLength);
if (!Character.isDigit(string.charAt(index + identifierLength)))
break;
}
return string.substring(index, index + identifierLength);
}
private boolean isOperator(char c) {
return (c == '-' || c == '+' || c == '*' || c == '/' || c == '<' || c == '>'
|| c == '!' || c == '~' || c == '%' || c == '^' || c == '&' || c == '|'
|| c == '=' || c == '.');
}
}
}
/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -