[Mc4j-cvs] mc4j/src/org/mc4j/console/swing/editor/xml JEditorTest.java,1.1,1.2 EditorDocument.java,1
Brought to you by:
ghinkl
Update of /cvsroot/mc4j/mc4j/src/org/mc4j/console/swing/editor/xml In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20162/src/org/mc4j/console/swing/editor/xml Added Files: JEditorTest.java EditorDocument.java XMLStyleTokens.java RETokenizer.java BasicMBean.xml StyleTokens.java Log Message: Merging EMS into head for the 2.0 release work --- NEW FILE: RETokenizer.java --- /* * Copyright 2002-2004 Greg Hinkle * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.mc4j.console.swing.editor.xml; import java.util.regex.Matcher; /** * @author Greg Hinkle (gh...@us...), Nov 16, 2004 * @version $Revision: 1.2 $($Author: ghinkl $ / $Date: 2006/04/12 19:14:04 $) */ public class RETokenizer { protected StyleTokens types; protected Matcher matcher; public RETokenizer(StyleTokens types, String text) { this.types = types; matcher = types.getMatcher(text); } /* protected Token getToken(int position) { for (int i = 0; i < types.getTokens().length; i++) { StyleTokens.StyleToken styleToken = types.getTokens()[i]; String tokenMatch = matcher.group(i); if (tokenMatch != null) { String type = styleToken.name; return new Token(tokenMatch, type, position); } } return null; }*/ protected Token getToken(int pos) { int count = types.getTokens().length; for (int i = 0; i < types.getTokens().length; i++) { StyleTokens.StyleToken styleToken = types.getTokens()[i]; String token = matcher.group(i+1); if (token != null) { String type = styleToken.name; return new Token(token, type, pos); } } return null; } public Token nextToken() { if (matcher.find()) { return getToken(matcher.start()); } return null; } public static class Token { public String token; public String type; protected int position; public Token(String token, String type, int getPosition) { this.token = token; this.type = type; this.position = getPosition; } public String getText() { return token; } public String getType() { return type; } public int getPosition() { return position; } public String toString() { return type + "(" + token + ", " + position + ')'; } } } --- NEW FILE: BasicMBean.xml --- <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE Dashboard PUBLIC "-//MC4J//DTD Dashboard 1.0//EN" "http://mc4j.sourceforge.net/Dashboard_1_0.dtd"> <!-- Document : BasicMBean.xml Created on : October 6, 2002, 10:01 PM Author : ghinkl Description: Displays the basic information of an MBean --> <Dashboard version="1.0" name="Basic MBean View"> <Description>Displays general information about any MBean in the system.</Description> <DashboardMatch type="Bean"> <BeanMatch id="BeanNode" type="Single"> <Condition type="BeanObjectNameCondition" filter=".*"/> </BeanMatch> </DashboardMatch> <LayoutManager type="java.awt.BorderLayout"/> <Content> <!-- *** The header section displays title and a refresh control *** --> <Component type="javax.swing.JPanel"> <Constraint type="BorderConstraints" direction="NORTH"/> <LayoutManager type="java.awt.BorderLayout"/> <Border type="javax.swing.border.EmptyBorder" top="(Literal)2" left="(Literal)5" bottom="(Literal)2" right="(Literal)5"/> <Content> <Component type="javax.swing.JPanel"> <Constraint type="BorderConstraints" direction="CENTER"/> <LayoutManager type="java.awt.BorderLayout"/> <Content> <Component type="javax.swing.JLabel" id="titleLable"> <Constraint type="BorderConstraints" direction="CENTER"/> <Attribute name="text" value="DashboardName"/> <Attribute name="font" value="(Literal)SansSerif bold 20"/> <Attribute name="foreground" value="(Literal)0x666666"/> </Component> <Component type="javax.swing.JTextArea"> <Constraint type="BorderConstraints" direction="SOUTH"/> <Attribute name="text" value="(Message)General information about {ObjectName}"/> <Attribute name="font" value="(Literal)DialogInput bold 14"/> <Attribute name="opaque" value="(Literal)false"/> <Attribute name="editable" value="(Literal)false"/> </Component> </Content> </Component> <Component type="org.mc4j.console.dashboard.components.RefreshControlComponent"> <Constraint type="BorderConstraints" direction="EAST"/> <Attribute name="refreshDelay" value="(Literal)30000"/> </Component> </Content> </Component> <!-- *** The main content is displayed within a scrollpane for easy viewing *** --> <Component type="javax.swing.JScrollPane"> <Constraint type="BorderConstraints" direction="CENTER"/> <Attribute name="background" value="(Literal)0xFFFFFF"/> <Content> <Component type="javax.swing.JPanel"> <Attribute name="background" value="(Literal)0xFFFFFF"/> <LayoutManager type="java.awt.BoxLayout" axis="Y_AXIS"/> <Content> <!-- *** A Section holder is an included component for providing hide-able parts *** --> <Component type="org.mc4j.console.swing.SectionHolder"> <Attribute name="title" value="(Literal)MBean Attributes"/> <Attribute name="background" value="(Literal)0xFFFFFF"/> <LayoutManager type="java.awt.BorderLayout"/> <Content> <!-- *** This component displays the attributes of an MBean *** --> <Component type="org.mc4j.console.dashboard.components.AttributeTreeTableExplorer"> <!-- *** Notice were setting the node that we matched above by name here *** --> <Attribute name="beanNode" value="BeanNode"/> <Constraint type="BorderConstraints" direction="CENTER"/> <Border type="javax.swing.border.LineBorder" color="(Literal)0x444444" thickness="(Literal)1"/> <Attribute name="background" value="(Literal)0xFFFFFF"/> <Attribute name="background" value="(Literal)0xFFFFFF"/> </Component> </Content> </Component> <Component type="org.mc4j.console.swing.SectionHolder"> <Attribute name="title" value="(Literal)MBean Operations"/> <Attribute name="background" value="(Literal)0xFFFFFF"/> <Content> <Component type="org.mc4j.console.dashboard.components.OperationListComponent"> <Attribute name="beanNode" value="BeanNode"/> <Attribute name="format" value="(Literal)FULL_FORMAT"/> </Component> </Content> </Component> <!-- <Component type="org.mc4j.console.dashboard.components.FillerComponent"> <Attribute name="type" value="(Literal)VERTICAL_GLUE_SHAPE"/> </Component> --> </Content> </Component> </Content> </Component> </Content> </Dashboard> --- NEW FILE: StyleTokens.java --- /* * Copyright 2002-2004 Greg Hinkle * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.mc4j.console.swing.editor.xml; import javax.swing.text.Style; import javax.swing.text.StyleConstants; import javax.swing.text.StyledDocument; import java.awt.*; import java.util.LinkedList; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author Greg Hinkle (gh...@us...), Nov 16, 2004 * @version $Revision: 1.2 $($Author: ghinkl $ / $Date: 2006/04/12 19:14:04 $) */ public class StyleTokens { protected LinkedList tokenList; public StyleTokens() { tokenList = new LinkedList(); } public void addTokenType(String name, String expression, Color color, boolean bold) { tokenList.add(new StyleToken(name, expression, color, bold)); } public StyleToken[] getTokens() { return (StyleToken[]) tokenList.toArray(new StyleToken[tokenList.size()]); } public void setStyles(StyledDocument doc) { for (int i = 0; i < tokenList.size(); i++) { StyleToken styleToken = (StyleToken) tokenList.get(i); Style style = doc.addStyle(styleToken.name,null); StyleConstants.setForeground(style,styleToken.color); StyleConstants.setBold(style, styleToken.bold); } // Add a special background colored, yellow // since I'm not sure how to do squiglies under text Style style = doc.addStyle("error",null); StyleConstants.setBackground(style, Color.yellow); } public String getExpression() { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < tokenList.size(); i++) { StyleToken styleToken = (StyleToken) tokenList.get(i); if (i > 0) buffer.append('|'); buffer.append('('); buffer.append(styleToken.expression); buffer.append(')'); } return buffer.toString(); } public Pattern getPattern() { String expression = getExpression(); return Pattern.compile(expression, Pattern.MULTILINE); //Pattern.DOTALL); } public Matcher getMatcher(String text) { return getPattern().matcher(text); } public static class StyleToken { protected String name; protected String expression; protected Color color; protected boolean bold; public StyleToken(String name, String expr, Color color, boolean bold) { this.name = name; this.expression = expr; this.color = color; this.bold = bold; } } } --- NEW FILE: XMLStyleTokens.java --- /* * Copyright 2002-2004 Greg Hinkle * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.mc4j.console.swing.editor.xml; import javax.swing.text.Document; import javax.swing.text.StyledEditorKit; import java.awt.*; /** * @author Greg Hinkle (gh...@us...), Nov 16, 2004 * @version $Revision: 1.2 $($Author: ghinkl $ / $Date: 2006/04/12 19:14:04 $) */ public class XMLStyleTokens extends StyleTokens { public static final String TAG = "tag"; public static final String TEXT = "text"; public static final String COMMENT = "comment"; private static final String ATTRIBUTE = "attribute"; public XMLStyleTokens() { //"([^ >]*)=\"" // "<[^ >]* ([^ >])?" // "(\\w)+=\"?:[^\"]*\" addTokenType(COMMENT, "<!--[^--]*-->", Color.gray, false); addTokenType(TAG, "<[^ >]*|[\\?]?>", new Color(0,0,132), true); addTokenType(TEXT, "\"(?:\\\\.|[^\"\\\\])*\"", new Color(72,125,55), true); addTokenType(ATTRIBUTE, "\\w+(?<=)", Color.red, false); } public static class Kit extends StyledEditorKit { public Document createDefaultDocument() { return new EditorDocument(new XMLStyleTokens()); } } } --- NEW FILE: JEditorTest.java --- /* * Copyright 2002-2004 Greg Hinkle * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.mc4j.console.swing.editor.xml; /** * @author Greg Hinkle (gh...@us...), Nov 16, 2004 * @version $Revision: 1.2 $($Author: ghinkl $ / $Date: 2006/04/12 19:14:02 $) */ import javax.swing.*; import javax.swing.text.StyledEditorKit; import java.awt.*; import java.io.File; import java.io.FileReader; import java.io.IOException; public class JEditorTest extends JPanel { public JEditorTest(File file) throws IOException { //KeywordManager.loadKeywordFiles(); StyledEditorKit kit = null; if (file.getName().toLowerCase().endsWith(".xml")) { kit = new XMLStyleTokens.Kit(); } JEditorPane editor = new JEditorPane(); editor.setEditorKit(kit); editor.read(new FileReader(file), null); ((EditorDocument)editor.getDocument()).setValidating(true); setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); setLayout(new GridLayout()); add(new JScrollPane(editor)); } public static void main(String[] args) throws IOException { File file = new File("org/mc4j/console/swing/editor/xml/BasicMBean.xml"); JFrame frame = new JFrame("JEditor Test"); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(new JEditorTest(file),BorderLayout.CENTER); frame.setSize(700, 550); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } --- NEW FILE: EditorDocument.java --- /* * Copyright 2002-2004 Greg Hinkle * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.mc4j.console.swing.editor.xml; import org.w3c.dom.Document; import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.DefaultStyledDocument; import javax.swing.text.Style; import javax.swing.text.StyleConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import java.io.IOException; import java.io.InputStream; import java.io.StringBufferInputStream; /** * @author Greg Hinkle (gh...@us...), Nov 16, 2004 * @version $Revision: 1.2 $($Author: ghinkl $ / $Date: 2006/04/12 19:14:04 $) */ public class EditorDocument extends DefaultStyledDocument implements ErrorHandler { protected StyleTokens types; protected boolean validating = false; public EditorDocument(StyleTokens types) { Style defaultStyle = getStyle("default"); StyleConstants.setFontSize(defaultStyle, 12); StyleConstants.setFontFamily(defaultStyle, "Courier New"); Style tagStyle = addStyle("tag", defaultStyle); StyleConstants.setBold(tagStyle,true); this.types = types; types.setStyles(this); } public void insertString(int offset, String text, AttributeSet style) throws BadLocationException { super.insertString(offset, text, style); fireUpdate(); } public void remove(int offset, int length) throws BadLocationException { super.remove(offset, length); fireUpdate(); } private Thread updateThread = new Thread(new Runnable() { public void run() { while (true) { if (validating && needsUpdate) { needsUpdate = false; highlightSyntax(); validateSyntax(); } try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } }); private boolean needsUpdate = false; public void fireUpdate() { needsUpdate = true; } public void warning(SAXParseException exception) throws SAXException { System.out.println(exception); highlightError(exception); } public void error(SAXParseException exception) throws SAXException { System.out.println(exception); highlightError(exception); } public void fatalError(SAXParseException exception) throws SAXException { System.out.println(exception); highlightError(exception); } public void validateSyntax() { long start = System.currentTimeMillis(); try { System.out.println("VALIDATING"); String text = getText(0, getLength()); InputStream s = new StringBufferInputStream(text); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); // documentBuilderFactory.setValidating(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); documentBuilder.setErrorHandler(this); Document doc = documentBuilder.parse(s); // System.out.println("VER: " + doc.getXmlVersion()); } catch (BadLocationException e) { // e.printStackTrace(); } catch (IOException e) { // e.printStackTrace(); } catch (ParserConfigurationException e) { // e.printStackTrace(); } catch (SAXException e) { // e.printStackTrace(); } finally { System.out.println("Validation took: " + (System.currentTimeMillis() - start) + "ms"); } } public void highlightError(SAXException se) { if (se instanceof SAXParseException) { try { String text = getText(0, getLength()); SAXParseException spe = (SAXParseException) se; int line = spe.getLineNumber(); int column = spe.getColumnNumber(); if (line < 1 || column < 1) return; String[] lines = text.split("\n"); int l = 0; for (int i=0;i<line-1;i++) { l += lines[i].length()+1; } int position = l + column; setCharacterAttributes(position,1,getStyle("error"),false); } catch (BadLocationException e) { e.printStackTrace(); } } } public void highlightSyntax() { try { String text = getText(0, getLength()); // Clear the existing styling setCharacterAttributes(0, getLength(), getStyle("default"), true); RETokenizer.Token token; RETokenizer tokenizer = new RETokenizer(types, text); while ((token = tokenizer.nextToken()) != null) { int pos = token.getPosition(); String type = token.getType(); String word = token.getText(); int len = word.length(); for (int i = 0; i < types.getTokens().length; i++) { StyleTokens.StyleToken styleToken = types.getTokens()[i]; String name = styleToken.name; if (type.equals(name)) { setCharacterAttributes(pos, len, getStyle(name), false); } } } } catch (Exception e) { e.printStackTrace(); } } public boolean isValidating() { return validating; } public void setValidating(boolean validating) { this.validating = validating; if (validating) { fireUpdate(); updateThread.start(); } } } |