001 /*
002 * Copyright 2002-2004 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package org.springframework.richclient.form.builder;
017
018 import java.awt.BorderLayout;
019 import java.awt.event.MouseEvent;
020 import java.util.HashMap;
021 import java.util.Iterator;
022 import java.util.Map;
023
024 import javax.swing.JCheckBox;
025 import javax.swing.JComponent;
026 import javax.swing.JPanel;
027 import javax.swing.JTextPane;
028 import javax.swing.text.DefaultCaret;
029 import javax.swing.text.Element;
030 import javax.swing.text.View;
031 import javax.swing.text.ViewFactory;
032 import javax.swing.text.html.FormView;
033 import javax.swing.text.html.HTML;
034
035 import org.springframework.richclient.form.binding.BindingFactory;
036 import org.springframework.richclient.text.HtmlPane;
037 import org.springframework.richclient.text.SynchronousHTMLEditorKit;
038
039 /**
040 * @author Oliver Hutchison
041 */
042 public class HtmlFormBuilder extends AbstractFormBuilder {
043
044 private JPanel panel;
045
046 private JTextPane htmlPane;
047
048 private Map formViewMap;
049
050 protected boolean inLink;
051
052 public HtmlFormBuilder(BindingFactory bindingFactory, String html) {
053 super(bindingFactory);
054 formViewMap = new HashMap();
055 panel = new JPanel(new BorderLayout());
056 htmlPane = new HtmlPane();
057 panel.add(htmlPane);
058 htmlPane.setEditorKit(new InternalHTMLEditorKit());
059 htmlPane.setFocusCycleRoot(false);
060 htmlPane.setCaret(new DefaultCaret() {
061 public void mouseDragged(MouseEvent e) {
062 }
063
064 public void mouseMoved(MouseEvent e) {
065 }
066
067 public void mouseClicked(MouseEvent e) {
068
069 }
070 });
071 setHtml(html);
072 }
073
074 private void setHtml(String html) {
075 htmlPane.setText(html);
076 htmlPane.setEditable(false);
077
078 for (Iterator i = formViewMap.entrySet().iterator(); i.hasNext();) {
079 Map.Entry entry = (Map.Entry)i.next();
080 Element element = (Element)entry.getKey();
081 FormView view = (FormView)entry.getValue();
082
083 String propertyName = (String)element.getAttributes().getAttribute(HTML.getAttributeKey("id"));
084 if (propertyName != null) {
085 JComponent comp = (JComponent)view.getComponent();
086 getBindingFactory().bindControl(comp, propertyName);
087 if (comp instanceof JCheckBox)
088 ((JCheckBox)comp).setOpaque(false);
089 }
090 }
091 }
092
093 public JComponent getForm() {
094 return panel;
095 }
096
097 private class InternalHTMLEditorKit extends SynchronousHTMLEditorKit {
098
099 public ViewFactory getViewFactory() {
100 return new HTMLFactory() {
101 public View create(Element elem) {
102 View view = super.create(elem);
103 if (view instanceof FormView) {
104 formViewMap.put(elem, view);
105 }
106 return view;
107 }
108 };
109 }
110 }
111 }