001: package com.vividsolutions.jump.workbench.ui;
002:
003: import java.awt.*;
004: import java.util.ArrayList;
005:
006: import javax.swing.*;
007:
008: public class HTMLPanel extends JPanel implements RecordPanelModel {
009: private ArrayList history = new ArrayList();
010: protected JButton okButton = new JButton();
011: private RecordPanel recordPanel = new RecordPanel(this );
012: private JPanel southPanel = new JPanel();
013: private JScrollPane scrollPane = new JScrollPane();
014: private JEditorPane editorPane = new JEditorPane();
015: private int currentIndex = -1;
016:
017: public HTMLPanel() {
018: try {
019: jbInit();
020: } catch (Exception e) {
021: e.printStackTrace();
022: }
023:
024: okButton.setVisible(false);
025: }
026:
027: public RecordPanel getRecordPanel() {
028: return recordPanel;
029: }
030:
031: public int getCurrentIndex() {
032: return currentIndex;
033: }
034:
035: public void addField(String label, String value) {
036: addField(label, value, "");
037: }
038:
039: private void setLatestDocument(String document) {
040: history.set(history.size() - 1, document);
041: }
042:
043: private String getLatestDocument() {
044: return (String) history.get(history.size() - 1);
045: }
046:
047: protected void setEditorPaneText() {
048: final String document = (String) history.get(currentIndex);
049:
050: SwingUtilities.invokeLater(new Runnable() {
051: public void run() {
052: editorPane.setText("<HTML>" + document + "</HTML>");
053: }
054: });
055: scrollToTop();
056: }
057:
058: /**
059: * Appends HTML text to the frame.
060: * @param html the HTML to append
061: */
062: public void append(final String html) {
063: setLatestDocument(getLatestDocument() + html);
064: goToLatestDocument();
065: }
066:
067: /**
068: * Appends non-HTML text to the frame. Text is assumed to be non-HTML, and is
069: * HTML-escaped to avoid control-char conflict.
070: * @param text
071: */
072: public void addText(String text) {
073: append(GUIUtil.escapeHTML(text, false, true) + " <BR>\n");
074: }
075:
076: public void addField(String label, String value, String units) {
077: append("<B> " + label + " </B>" + value + " " + units
078: + " <BR>\n");
079: }
080:
081: /**
082: *@param level 1, 2, 3, ...
083: */
084: public void addHeader(int level, String text) {
085: append("<H" + level + "> "
086: + GUIUtil.escapeHTML(text, false, false) + " </H"
087: + level + ">\n");
088: }
089:
090: private JPanel fillerPanel = new JPanel();
091:
092: public JButton getOKButton() {
093: return okButton;
094: }
095:
096: private GridBagLayout gridBagLayout1 = new GridBagLayout();
097:
098: private void jbInit() throws Exception {
099: setLayout(new BorderLayout());
100: editorPane.setEditable(false);
101: editorPane.setContentType("text/html");
102: southPanel.setLayout(gridBagLayout1);
103: scrollPane
104: .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
105: okButton.setText("OK");
106:
107: add(scrollPane, BorderLayout.CENTER);
108: add(southPanel, BorderLayout.SOUTH);
109: southPanel.add(fillerPanel, new GridBagConstraints(1, 1, 1, 1,
110: 1.0, 0.0, GridBagConstraints.CENTER,
111: GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0),
112: 0, 0));
113: southPanel.add(recordPanel, new GridBagConstraints(0, 1, 1, 1,
114: 0.0, 0.0, GridBagConstraints.CENTER,
115: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
116: southPanel.add(okButton, new GridBagConstraints(2, 1, 1, 1,
117: 0.0, 0.0, GridBagConstraints.CENTER,
118: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
119: scrollPane.getViewport().add(editorPane, null);
120: }
121:
122: public void setRecordNavigationControlVisible(boolean visible) {
123: southPanel.setVisible(visible);
124: }
125:
126: public void scrollToTop() {
127: //The text is set using #invokeLater, so the scroll-to-top must
128: //also be done using #invokeLater. [Jon Aquino]
129: SwingUtilities.invokeLater(new Runnable() {
130: public void run() {
131: editorPane.setCaretPosition(0);
132: }
133: });
134: }
135:
136: private void goToLatestDocument() {
137: setCurrentIndex(history.size() - 1);
138: }
139:
140: public void createNewDocument() {
141: history.add("");
142: goToLatestDocument();
143: recordPanel.updateAppearance();
144: }
145:
146: public void setCurrentIndex(int index) {
147: this .currentIndex = index;
148: setEditorPaneText();
149: }
150:
151: public int getRecordCount() {
152: return history.size();
153: }
154:
155: public Color getBackgroundColor() {
156: return editorPane.getBackground();
157: }
158:
159: public void setBackgroundColor(Color color) {
160: editorPane.setBackground(color);
161: }
162: }
|