[Mc4j-cvs] mc4j/src/org/mc4j/console/dashboard/components/html DashboardHtmlTransformer.java,1.1,1.2
Brought to you by:
ghinkl
Update of /cvsroot/mc4j/mc4j/src/org/mc4j/console/dashboard/components/html In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20162/src/org/mc4j/console/dashboard/components/html Modified Files: HtmlDashboardComponent.java DashboardHtmlEditorKit.java MappedObjectView.java Added Files: DashboardHtmlTransformer.java Log Message: Merging EMS into head for the 2.0 release work Index: MappedObjectView.java =================================================================== RCS file: /cvsroot/mc4j/mc4j/src/org/mc4j/console/dashboard/components/html/MappedObjectView.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** MappedObjectView.java 14 Mar 2005 16:05:35 -0000 1.2 --- MappedObjectView.java 12 Apr 2006 19:14:01 -0000 1.3 *************** *** 17,34 **** package org.mc4j.console.dashboard.components.html; ! import org.mc4j.console.bean.MBeanNode; ! import org.mc4j.console.bean.attribute.AttributeNode; ! import org.mc4j.console.dashboard.components.AttributeComponent; import javax.swing.*; import javax.swing.text.Element; import javax.swing.text.html.HTML; - import javax.swing.text.html.ObjectView; - import javax.swing.text.html.CSS; import javax.swing.text.html.HTMLDocument; import java.awt.*; import java.util.Map; import java.util.NoSuchElementException; import java.util.StringTokenizer; /** --- 17,34 ---- package org.mc4j.console.dashboard.components.html; ! import org.mc4j.console.Refreshable; ! import org.mc4j.ems.connection.bean.attribute.EmsAttribute; import javax.swing.*; import javax.swing.text.Element; + import javax.swing.text.BadLocationException; import javax.swing.text.html.HTML; import javax.swing.text.html.HTMLDocument; + import javax.swing.text.html.ObjectView; import java.awt.*; import java.util.Map; import java.util.NoSuchElementException; import java.util.StringTokenizer; + import java.io.IOException; /** *************** *** 41,45 **** private Map contextMap; ! public MappedObjectView(Element elem,Map contextMap) { super(elem); this.contextMap = contextMap; --- 41,45 ---- private Map contextMap; ! public MappedObjectView(Element elem, Map contextMap) { super(elem); this.contextMap = contextMap; *************** *** 52,61 **** try { width = Integer.parseInt((String) getElement().getAttributes().getAttribute(HTML.Attribute.WIDTH)); ! } catch(Exception e) {} try { height = Integer.parseInt((String) getElement().getAttributes().getAttribute(HTML.Attribute.HEIGHT)); ! } catch(Exception e) {} ! classId = (String)getElement().getAttributes().getAttribute(HTML.Attribute.CLASSID); // String text = (String) getElement().getAttributes().getAttribute("message"); --- 52,63 ---- try { width = Integer.parseInt((String) getElement().getAttributes().getAttribute(HTML.Attribute.WIDTH)); ! } catch (Exception e) { ! } try { height = Integer.parseInt((String) getElement().getAttributes().getAttribute(HTML.Attribute.HEIGHT)); ! } catch (Exception e) { ! } ! classId = (String) getElement().getAttributes().getAttribute(HTML.Attribute.CLASSID); // String text = (String) getElement().getAttributes().getAttribute("message"); *************** *** 67,90 **** try { // create an attribute pointer? ! StringTokenizer tok = new StringTokenizer(classId,"."); String beanName = (String) tok.nextElement(); String attributeName = (String) tok.nextElement(); ! if (beanName != null && attributeName != null) { ! MBeanNode bean = (MBeanNode) contextMap.get(beanName); if (bean != null) { ! AttributeNode attributeNode = (AttributeNode) bean.getAttributeNodeMap().get(attributeName); ! if (attributeNode != null) { ! AttributeComponent attributeComponent = new AttributeComponent(); ! attributeComponent.setBeanNode(bean); ! attributeComponent.setAttributeName(attributeName); ! attributeComponent.setContext(contextMap); ! attributeComponent.setToolTipText(attributeNode.getShortDescription()); ! contextMap.put(classId,attributeComponent); ! comp = attributeComponent; } } ! } ! } catch (NoSuchElementException nsee) {} } --- 69,96 ---- try { // create an attribute pointer? ! StringTokenizer tok = new StringTokenizer(classId, "."); String beanName = (String) tok.nextElement(); String attributeName = (String) tok.nextElement(); ! /*if (beanName != null && attributeName != null) { ! EmsBean bean = (EmsBean) co ! ntextMap.get(beanName); if (bean != null) { ! EmsAttribute emsAttribute = bean.getAttribute(attributeName); ! if (emsAttribute != null) { ! // AttributeComponent attributeComponent = new AttributeComponent(); ! // attributeComponent.setBean(bean); ! // attributeComponent.setAttributeName(attributeName); ! // attributeComponent.setContext(contextMap); ! // attributeComponent.setToolTipText(emsAttribute.getDescription()); ! // contextMap.put(classId, attributeComponent); ! // comp = attributeComponent; ! comp = new InfoComponent(getElement(),emsAttribute); } } ! }*/ ! // comp = new InfoComponent(getElement(), null); ! } catch (NoSuchElementException nsee) { ! } } *************** *** 96,105 **** if (comp instanceof JLabel) { // This seems to set the label text a the inline text baseline ! ((JLabel)comp).setAlignmentY(0.80f);//SwingConstants.TOP); Font inlineFont = ! ((HTMLDocument)getDocument()).getStyleSheet().getFont( ! ((HTMLDocument)getDocument()).getStyleSheet().getViewAttributes(this)); ! ((JLabel)comp).setFont(inlineFont); } --- 102,111 ---- if (comp instanceof JLabel) { // This seems to set the label text a the inline text baseline ! ((JLabel) comp).setAlignmentY(0.80f);//SwingConstants.TOP); Font inlineFont = ! ((HTMLDocument) getDocument()).getStyleSheet().getFont( ! ((HTMLDocument) getDocument()).getStyleSheet().getViewAttributes(this)); ! ((JLabel) comp).setFont(inlineFont); } *************** *** 109,111 **** --- 115,190 ---- return comp; } + + /* + public static class InfoComponent extends JComponent implements Refreshable { + static long l; + + EmsAttribute attribute; + int existingInfoLength; + Element element; + String key; + + boolean inited = false; + + public InfoComponent(Element element, EmsAttribute attribute) { + if (element.getDocument() instanceof Tester.InfoBackedHtmlDocument) { + Tester.InfoBackedHtmlDocument doc = (Tester.InfoBackedHtmlDocument) element.getDocument(); + doc.register(this); + } + + setSize(1, 1); + setPreferredSize(new Dimension(1,1)); + setMaximumSize(new Dimension(1,1)); + this.attribute = attribute; + this.element = element; + + HTMLDocument doc = (HTMLDocument) element.getDocument(); + + } + + public void refresh() { + Tester.InfoBackedHtmlDocument doc = (Tester.InfoBackedHtmlDocument) element.getDocument(); + + if (!inited) { + try { + long n = l++; + this.key = "_key" + n; + + doc.dump(System.out); + doc.insertBeforeStart(element,"<span id=\"" + key + "\">Loading...</span>"); + doc.dump(System.out); + Thread.sleep(1000); + doc.dump(System.out); + + this.element = doc.getElement("_key" + String.valueOf(n)); + assert(element != null); + } catch (BadLocationException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + inited = true; + } + + try { + // getDocument().remove(start, oldLength); + Object val = "hi";//attribute.refresh(); + String txt = val == null ? "null" : val.toString(); + + + doc.setOuterHTML(element,"<span id=\"" + key + "\">" + "just a test" + " </span>"); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + public Element getElement() { + return element; + } + */ + + } \ No newline at end of file Index: HtmlDashboardComponent.java =================================================================== RCS file: /cvsroot/mc4j/mc4j/src/org/mc4j/console/dashboard/components/html/HtmlDashboardComponent.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** HtmlDashboardComponent.java 13 Oct 2004 04:59:36 -0000 1.1 --- HtmlDashboardComponent.java 12 Apr 2006 19:14:01 -0000 1.2 *************** *** 17,26 **** package org.mc4j.console.dashboard.components.html; import org.mc4j.console.dashboard.DashboardComponent; ! import org.openide.modules.InstalledFileLocator; import javax.swing.*; import javax.swing.text.html.HTMLDocument; ! import java.awt.*; import java.io.File; import java.io.IOException; --- 17,36 ---- package org.mc4j.console.dashboard.components.html; + import ognl.OgnlException; + import org.jdesktop.swingx.JXEditorPane; + import org.mc4j.console.dashboard.Dashboard; import org.mc4j.console.dashboard.DashboardComponent; ! import org.mc4j.console.dashboard.context.OgnlHelper; import javax.swing.*; + import javax.swing.text.BadLocationException; + import javax.swing.text.Element; import javax.swing.text.html.HTMLDocument; ! import java.awt.BorderLayout; ! import java.awt.Component; ! import java.awt.Dimension; ! import java.awt.Graphics; ! import java.awt.Graphics2D; ! import java.awt.RenderingHints; import java.io.File; import java.io.IOException; *************** *** 28,31 **** --- 38,42 ---- import java.net.URL; import java.net.URLClassLoader; + import java.util.HashMap; import java.util.Map; *************** *** 40,44 **** --- 51,59 ---- private DashboardHtmlEditorKit editorKit; private String htmlDocumentName; + private String html; + private boolean refreshAttributes; private Map contextMap; + private DashboardHtmlTransformer transformer; + protected void addImpl(Component comp, Object constraints, int index) { *************** *** 52,57 **** public static class AliasedEditorPane extends JEditorPane { protected void paintComponent(Graphics g) { ! ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, ! RenderingHints.VALUE_ANTIALIAS_ON); super.paintComponent(g); } --- 67,72 ---- public static class AliasedEditorPane extends JEditorPane { protected void paintComponent(Graphics g) { ! ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, ! RenderingHints.VALUE_ANTIALIAS_ON); super.paintComponent(g); } *************** *** 59,82 **** private void initComponents() { ! editorPane = new AliasedEditorPane(); editorKit = new DashboardHtmlEditorKit(contextMap); editorPane.setEditorKit(editorKit); editorPane.setEditable(false); ! File dashboardDir = ! InstalledFileLocator.getDefault().locate("dashboards","org.mc4j.console",false); try { ! URLClassLoader cl = new URLClassLoader(new URL[] { dashboardDir.toURL() }); ! InputStream documentStream = cl.getResourceAsStream(htmlDocumentName); ! editorPane.read(documentStream,(HTMLDocument) editorKit.createDefaultDocument()); } catch (IOException e) { e.printStackTrace(); } setLayout(new BorderLayout()); --- 74,109 ---- private void initComponents() { + setMaximumSize(new Dimension(Short.MAX_VALUE, + Short.MAX_VALUE)); ! ! editorPane = new JXEditorPane(); //new AliasedEditorPane(); editorKit = new DashboardHtmlEditorKit(contextMap); editorPane.setEditorKit(editorKit); editorPane.setEditable(false); ! // File dashboardDir = new File("dashboards"); ! //InstalledFileLocator.getDefault().locate("dashboards","org.mc4j.console",false); + File dashboardFile = (File) contextMap.get(Dashboard.CONTEXT_DASHBOARD_FILE); + File dashboardDir = dashboardFile.getParentFile(); try { ! if (htmlDocumentName != null) { ! URLClassLoader cl = new URLClassLoader(new URL[]{dashboardDir.toURL()}); ! InputStream documentStream = cl.getResourceAsStream(htmlDocumentName); ! editorPane.read(documentStream, editorKit.createDefaultDocument()); ! } else if (html != null) { ! transformer = new DashboardHtmlTransformer(); ! html = transformer.transform(html, contextMap); ! editorPane.setText(html); + } } catch (IOException e) { e.printStackTrace(); } + editorPane.setEditable(false); setLayout(new BorderLayout()); *************** *** 86,104 **** } - public String getHtmlDocumentName() { - return htmlDocumentName; - } - public void setHtmlDocumentName(String htmlDocumentName) { this.htmlDocumentName = htmlDocumentName; } public void setContext(Map context) { ! this.contextMap = context; initComponents(); } ! public void refresh() { ! initComponents(); } } --- 113,186 ---- } public void setHtmlDocumentName(String htmlDocumentName) { this.htmlDocumentName = htmlDocumentName; } + public void setHtml(String html) { + this.html = html; + } + public void setContext(Map context) { ! this.contextMap = new HashMap(context); initComponents(); } ! ! ! private Map<String, Element> elements = new HashMap<String, Element>(); ! ! public synchronized void refresh() { ! //initComponents(); ! ! // try { ! // HTMLDocument doc = (HTMLDocument) editorPane.getDocument(); ! // doc. ! // editorPane.getDocument().insertString(editorPane.getDocument().getEndPosition().getOffset(),"\n<P>Add some more lines</P>",null); ! // } catch (BadLocationException e) { ! // e.printStackTrace(); ! // } ! ! if (transformer != null) { ! final HTMLDocument doc = (HTMLDocument) editorPane.getDocument(); ! ! ! ! for (Map.Entry<String, String> entry : transformer.getKeys().entrySet()) { ! ! String displayValue; ! try { ! Object val = OgnlHelper.getValue(entry.getValue(),contextMap, String.class); ! //Ognl.getValue(entry.getValue(), contextMap); ! displayValue = val != null ? val.toString() : "null"; ! } catch (OgnlException e) { ! e.printStackTrace(); ! displayValue = "~unreadable: " + e.getMessage() + "~"; ! } ! try { ! final Element e = doc.getElement(entry.getKey()); ! ! final int p0 = e.getStartOffset(); ! final int p1 = e.getEndOffset(); ! ! final String displayValue1 = displayValue; ! SwingUtilities.invokeAndWait(new Runnable() { ! public void run() { ! try { ! doc.replace(p0, p1 - p0, ! displayValue1, ! e.getAttributes()); ! } catch (BadLocationException e1) { ! e1.printStackTrace(); ! } ! ! } ! }); ! // doc.setOuterHTML(e, "<span id=\"" + entry.getKey() + "\">" + displayValue + "</span>"); ! } catch (Exception e1) { ! e1.printStackTrace(); ! } ! } ! ! } } } Index: DashboardHtmlEditorKit.java =================================================================== RCS file: /cvsroot/mc4j/mc4j/src/org/mc4j/console/dashboard/components/html/DashboardHtmlEditorKit.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** DashboardHtmlEditorKit.java 13 Oct 2004 04:59:36 -0000 1.1 --- DashboardHtmlEditorKit.java 12 Apr 2006 19:14:01 -0000 1.2 *************** *** 19,22 **** --- 19,23 ---- import org.mc4j.console.swing.html.ClassPathImageView; + import javax.swing.*; import javax.swing.text.Element; import javax.swing.text.StyleConstants; *************** *** 25,28 **** --- 26,30 ---- import javax.swing.text.html.HTML; import javax.swing.text.html.HTMLEditorKit; + import javax.swing.text.html.ParagraphView; import java.util.Map; *************** *** 53,68 **** public View create(Element elem) { Object o = ! elem.getAttributes().getAttribute(StyleConstants.NameAttribute); if (o instanceof HTML.Tag) { HTML.Tag kind = (HTML.Tag) o; ! if (kind == HTML.Tag.IMG) { ! return new ClassPathImageView(elem); ! } else if (kind == HTML.Tag.OBJECT) { return new MappedObjectView(elem, contextMap); } } ! return super.create( elem ); } } } --- 55,96 ---- public View create(Element elem) { Object o = ! elem.getAttributes().getAttribute(StyleConstants.NameAttribute); ! if (o instanceof HTML.Tag) { HTML.Tag kind = (HTML.Tag) o; ! if (kind == HTML.Tag.IMG) { ! return new ClassPathImageView(elem); ! } else if (kind == HTML.Tag.OBJECT) { return new MappedObjectView(elem, contextMap); + } else if (kind == HTML.Tag.P || kind == HTML.Tag.IMPLIED) { + return new WordSplittingParagraphView(elem); } } ! return super.create(elem); ! } ! } ! ! ! /** ! * This just allows us to split long words onto multiple lines instead of ! * making the component bigger. ! */ ! private static class WordSplittingParagraphView extends ParagraphView { ! public WordSplittingParagraphView(Element elem) { ! super(elem); ! } ! ! protected SizeRequirements calculateMinorAxisRequirements(int axis, ! SizeRequirements r) { ! SizeRequirements sup = super.calculateMinorAxisRequirements(axis, r); ! // TODO: This is the minumum rendering size (sizes smaller than a displayable character) ! // will cause the renderer to go nuts. Should never size smaler than the largest character in a ! // paragraph ! sup.minimum = 30; ! return sup; } } + + } --- NEW FILE: DashboardHtmlTransformer.java --- /* * Copyright 2002-2005 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.dashboard.components.html; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Arrays; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author Greg Hinkle (gh...@us...), Dec 21, 2005 * @version $Revision: 1.2 $($Author: ghinkl $ / $Date: 2006/04/12 19:14:01 $) */ public class DashboardHtmlTransformer { static int n; private Map<String, String> keys = new HashMap<String, String>(); public String transform(String data, Map context) { // Searches for cases of ${foo.bar.baz} as they are property references Pattern pattern = Pattern.compile("\\$\\{([^\\}]*)\\}"); String result = transformForEach(data,context); Matcher m = pattern.matcher(result); while (m.find()) { String identifier = m.group(); identifier = identifier.substring(2,identifier.length()-1); String key = "key" + ++n; result = m.replaceFirst("<span id=\"" + key +"\">loading...</span>"); keys.put(key,identifier); m = pattern.matcher(result); //System.out.println(key + " = " + identifier); } return result; } private String transformForEach(String data, Map context) { Pattern pattern = Pattern.compile("\\#foreach\\(([^\\)]*)\\)(.*)\\#endfor",Pattern.MULTILINE|Pattern.DOTALL); // Pattern pattern = Pattern.compile("\\#foreach\\(([^\\)]*)\\)(.*)\\#endfor", Pattern.MULTILINE|Pattern.DOTALL); Matcher m = pattern.matcher(data); while (m.find()) { StringBuffer allRows = new StringBuffer(); String variableAssignment = m.group(1); String content = m.group(2); String[] vars =variableAssignment.split("\\:"); String key = vars[0].trim(); String contextKey = vars[1].trim(); Collection c; try { contextKey = Pattern.compile("\\$\\{(.*)\\}").matcher(contextKey).replaceAll("$1"); Object val = ognl.Ognl.getValue(contextKey,context); if (val.getClass().isArray()) { c = Arrays.asList((Object[]) val); } else { c = (Collection) val; } } catch (Exception e) { c = Collections.EMPTY_LIST; } key = key.replace("$","\\$"); Pattern p = Pattern.compile(key); //("\\$\\{(.*)(" + key + ")(.*)\\}"); for (Object row : c) { allRows.append(p.matcher(content).replaceAll(row.toString())); //("\\${$1"+row.toString()+"$3}")); } data = data.substring(0,m.start()) + allRows.toString() + data.substring(m.end(),data.length()); m = pattern.matcher(data); } return data; } public Map<String, String> getKeys() { return Collections.unmodifiableMap(keys); } public static void main3(String[] args) { String[] foo = new String[] { "a","b"}; Object bar = foo; System.out.println(Arrays.asList((Object[]) bar).size()); } public static void main(String[] args) { String test = "<html> hello, world <p> \n <b>${foo.bar}</b> ${baz} - [${biteme}] $100.50 {cool}\n" + " #foreach($foo : ${bar[\"woa\"]}) \n" + " <b> woa neat</b>\n" + " Cool: ${$foo.bar.baz a.b[\"$foo\"]}\n" + " #endfor\n" + " bah. ${last.one}"; System.out.println("Start:\n"+test); Map ctx = new HashMap(); ctx.put("bar", Arrays.asList(new String[] { "alpha","beta","gamma","delta" })); System.out.println("Transform:\n"+ new DashboardHtmlTransformer().transform(test,ctx)); } public static void main2(String[] args) { Pattern p = Pattern.compile("\\#foreach\\((.*)\\)(.*)\\#endfor",Pattern.MULTILINE|Pattern.DOTALL); Matcher m = p.matcher("fdgh #foreach($a : ${b.c.d}) foo\n bar\n baz\n #endfor dfg "); System.out.println(m.find()); System.out.println(m.group(1)); } } |