[Top ページ] [他のサンプル]

TestBoxAttributeSampleApplet.java


import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
 
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.text.Element;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLWriter;
 
 
public class HTMLEditorSample extends JApplet {
	private JEditorPane editorPane_ = new JEditorPane("text/html", "");
	private JTextArea sourceTextArea_ = new JTextArea();
	
	public HTMLEditorSample() {
		setLayout(new GridLayout(2, 1));
		
		add(createHtmlEditorPanel());
		add(createSourcePanel());
	}
	
	public void init() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			SwingUtilities.updateComponentTreeUI(this);
		} catch(Exception e) {}
	}
	
	// 上部の html エディタ部分
	private JPanel createHtmlEditorPanel() {
		JPanel panel = new JPanel(new BorderLayout());
		// ---------------------------------------------------------------------
		// ボタンを配置するためのパネル
		// ---------------------------------------------------------------------
		JPanel toolPanel = new JPanel();
		panel.add(toolPanel, BorderLayout.NORTH);
		// [Bold]ボタン
		JButton boldButton = new JButton("Bold");
		toolPanel.add(boldButton);
		boldButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				StyledDocument doc = (StyledDocument) editorPane_.getDocument();
				SimpleAttributeSet a = new SimpleAttributeSet();
				a.addAttribute(StyleConstants.Bold, true);
				int selStart = editorPane_.getSelectionStart();
				int selEnd   = editorPane_.getSelectionEnd();
				doc.setCharacterAttributes(selStart, selEnd - selStart, a, false);
			}
		});
		// [H1]ボタン
		JButton h1Button = new JButton("H1");
		toolPanel.add(h1Button);
		h1Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				StyledDocument doc = (StyledDocument) editorPane_.getDocument();
				Element pElem = doc.getParagraphElement(editorPane_.getCaretPosition());
				SimpleAttributeSet attribSet = new SimpleAttributeSet();
				attribSet.addAttribute(StyleConstants.NameAttribute, HTML.Tag.H1);
				doc.setParagraphAttributes(pElem.getStartOffset(), pElem.getEndOffset() - pElem.getStartOffset(), attribSet, true);
			}
		});
		// ---------------------------------------------------------------------
		// html エディタ
		// ---------------------------------------------------------------------
		panel.add(new JScrollPane(editorPane_), BorderLayout.CENTER);
		
		return panel;
	}
	
	// 下部の htmlソース部分
	private JPanel createSourcePanel() {
		JPanel panel = new JPanel(new BorderLayout());
		// ---------------------------------------------------------------------
		// ボタンを配置するためのパネル
		// ---------------------------------------------------------------------
		JPanel toolPanel = new JPanel();
		panel.add(toolPanel,BorderLayout.NORTH);
		// [ソースを出力]ボタン
		JButton toSourceButton = new JButton("↓ ソースを出力");
		toolPanel.add(toSourceButton);
		toSourceButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					StringWriter writer = new StringWriter();
					MultiByteHtmlWriter htmlWriter = new MultiByteHtmlWriter(writer, (HTMLDocument)editorPane_.getDocument());
					htmlWriter.write();
					writer.close();
					sourceTextArea_.setText(writer.toString());
					//outputDoc(editorPane_.getDocument().getDefaultRootElement(), "");
				} catch(Exception ex) {
					ex.printStackTrace();
				}
			}
		});
		// [ソースからHTMLに]ボタン
		JButton toHtmlButton = new JButton("↑ ソースからHTMLに");
		toolPanel.add(toHtmlButton);
		toHtmlButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				editorPane_.setText(sourceTextArea_.getText());
			}
		});
		// ---------------------------------------------------------------------
		// ソースのテキストボックス
		// ---------------------------------------------------------------------
		panel.add(new JScrollPane(sourceTextArea_), BorderLayout.CENTER);
		
		return panel;
	}
	
	// ==============================================================================================
	// HTML 出力用クラス (HTMLWriter だと、日本語などの文字が &#xxx; にエンコードされるので)
	// ==============================================================================================
	private static class MultiByteHtmlWriter extends HTMLWriter {
		public MultiByteHtmlWriter(Writer w, HTMLDocument doc) {
			super(w, doc);
		}
		
		protected void output(char[] chars, int start, int length) throws IOException {
			int startPos = start;
			boolean isMultiByte = false;
			for (int i = start; i < start + length; i++) {
				char c = chars[i];
				boolean isCurrentMultiByte = (c >= 128);
				if (isMultiByte != isCurrentMultiByte && startPos != i) {
					int len = i - startPos;
					output(chars, startPos, len, isMultiByte);
					startPos = i;
				}
				isMultiByte = isCurrentMultiByte;
			}
			int len = start + length - startPos;
			if (len > 0) { output(chars, startPos, len, isMultiByte); }
		}
		
		private void output(char[] chars, int start, int length, boolean isMultiByte) throws IOException {
			if (isMultiByte) {
				getWriter().write(chars, start, length);
				setCurrentLineLength(getCurrentLineLength() + length);
			} else {
				super.output(chars, start, length);
			}
		}
	}
}