Show TextObserver.java syntax highlighted
package fi.hip.gb.client.ui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.StringReader;
import java.rmi.RemoteException;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.text.EditorKit;
import javax.swing.text.StyledEditorKit;
import fi.hip.gb.core.WorkResult;
import fi.hip.gb.utils.Base64;
/**
* Builds user interface for showing plain text and html results to the user.
*
* @author Juho Karppinen
*/
public class TextObserver extends JComponent {
/** textarea for text */
private JTextPane text;
/** result to be shown */
private String data;
private static final long serialVersionUID = 7349551307526265945L;
public TextObserver(String data) {
this.data = data;
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(1, 2, 1, 2);
c.gridx = 0; c.gridwidth = 1;
c.gridy = 0; c.gridheight = 10;
c.weightx = 1.0f; c.weighty = 1.0f; c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.NORTH;
this.text = new JTextPane();
Utils.addPopupMenu(this.text);
//setRaw();
JScrollPane scrollPane = new JScrollPane(this.text);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
this.add(scrollPane, c);
c.gridy = 11; c.gridheight = 1;
c.weightx = 0.0f; c.weighty = 0.0f; c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.SOUTH;
this.add(getButtons(), c);
setRaw();
}
/**
* Saves the content of text area.
*
* @see fi.hip.gb.mobile.Observer#saveResult(WorkResult)
*/
public String saveResult(WorkResult result) throws RemoteException {
return this.text.getText();
}
/**
* Sets output to raw text
*/
private void setRaw() {
StringBuffer raw = new StringBuffer();
raw.append(this.data);
this.text.setText("");
JTextPane jtp = new JTextPane();
EditorKit editorKit = new StyledEditorKit();
jtp.setEditorKit(editorKit);
try {
editorKit.read(new StringReader(raw.toString()), jtp.getStyledDocument(),0);
} catch (Exception e) { System.out.println("Error reading file as plain text: " + e); }
this.text.setFont(new JTextArea().getFont());
this.text.replaceSelection(jtp.getText());
}
/**
* Sets output to html text
*/
private void setHtml() {
StringBuffer html = new StringBuffer();
html.append("<html>");
html.append(this.data);
html.append("</html>");
this.text.setText("");
this.text.setContentType("text/html");
this.text.setText(html.toString());
}
private JComponent getButtons() {
JComponent pane = new JPanel();
pane.setLayout(new GridBagLayout());
pane.setBorder(
BorderFactory.createTitledBorder("Visualization options"));
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(1, 2, 1, 2);
c.gridy = 0; c.weightx = 1.0f; c.weighty = 1.0f;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.CENTER;
JButton de64 = new JButton("Base64 Decode");
de64.setMnemonic(KeyEvent.VK_F);
de64.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
TextObserver.this.data = new String(Base64.decode(TextObserver.this.data.toCharArray()));
if(TextObserver.this.text.getContentType().equals("text/html")) {
setHtml();
} else {
setRaw();
}
}});
c.gridx = 0;
pane.add(de64, c);
JRadioButton btnHtml = new JRadioButton("Show as HTML");
btnHtml.setMnemonic(KeyEvent.VK_H);
btnHtml.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
setHtml();
}});
c.gridx++;
pane.add(btnHtml, c);
JRadioButton btnRaw = new JRadioButton("Show as plain text");
btnRaw.setMnemonic(KeyEvent.VK_P);
btnRaw.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
setRaw();
}});
c.gridx++;
pane.add(btnRaw, c);
//Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(btnHtml);
group.add(btnRaw);
btnRaw.setSelected(true);
return pane;
}
}
See more files for this project here