00001 package edu.stanford.hci.r3.pen.handwriting;
00002
00003 import java.awt.BorderLayout;
00004 import java.awt.Color;
00005 import java.awt.Component;
00006 import java.awt.Dimension;
00007 import java.awt.Font;
00008 import java.awt.event.ActionEvent;
00009 import java.awt.event.ActionListener;
00010 import java.util.List;
00011
00012 import javax.swing.BorderFactory;
00013 import javax.swing.JButton;
00014 import javax.swing.JFrame;
00015 import javax.swing.JLabel;
00016 import javax.swing.JPanel;
00017 import javax.swing.JTextArea;
00018
00019 import edu.stanford.hci.r3.PaperToolkit;
00020 import edu.stanford.hci.r3.components.InkPanel;
00021 import edu.stanford.hci.r3.util.DebugUtils;
00022 import edu.stanford.hci.r3.util.WindowUtils;
00023
00035 public class HandwritingCaptureDebugger extends JFrame {
00036
00037 private static final String NO_TEXT = "No Text Yet";
00038
00039 public static void main(String[] args) {
00040 new HandwritingCaptureDebugger();
00041 }
00042
00043 private JButton alternativesButton;
00044
00045 private CaptureApplication app;
00046
00047 private JPanel buttonPanel;
00048
00049 private JButton calibrateButton;
00050
00051 private JButton clearButton;
00052
00056 private InkPanel mainPanel;
00057
00058 private JPanel outputPanel;
00059
00060 private JButton saveButton;
00061
00062 private JPanel statusBarPanel;
00063
00064 private JLabel statusMessageLabel;
00065
00066 private JTextArea textOutput;
00067
00068 public HandwritingCaptureDebugger() {
00069 PaperToolkit.initializeLookAndFeel();
00070 initGUI();
00071 startApp();
00072 }
00073
00074 private void displayAlternatives() {
00075 app.retrieveAlternatives();
00076 }
00077
00078 private void doClear() {
00079 getInkPanel().clear();
00080 app.clearInk();
00081 getOutputTextArea().setText(NO_TEXT);
00082 }
00083
00087 private JPanel getButtonPanel() {
00088 if (buttonPanel == null) {
00089 buttonPanel = new JPanel();
00090 buttonPanel.add(getCalibrateButton());
00091 buttonPanel.add(getSaveButton());
00092 buttonPanel.add(getClearButton());
00093 }
00094 return buttonPanel;
00095 }
00096
00100 private JButton getCalibrateButton() {
00101 if (calibrateButton == null) {
00102 calibrateButton = new JButton();
00103 calibrateButton.setText(" Calibrate ");
00104 calibrateButton.addActionListener(new ActionListener() {
00105 public void actionPerformed(ActionEvent ae) {
00106 doClear();
00107 DebugUtils.println("Calibrate: Choose Top Left and Bottom Right Corners...");
00108 app.addCalibrationHandler();
00109 }
00110 });
00111 }
00112 return calibrateButton;
00113 }
00114
00120 private Component getClearButton() {
00121 if (clearButton == null) {
00122 clearButton = new JButton("Clear");
00123 clearButton.addActionListener(new ActionListener() {
00124 public void actionPerformed(ActionEvent e) {
00125 DebugUtils.println("Clear!");
00126 doClear();
00127 }
00128
00129 });
00130 }
00131 return clearButton;
00132 }
00133
00137 InkPanel getInkPanel() {
00138 if (mainPanel == null) {
00139 mainPanel = new InkPanel();
00140 mainPanel.setPreferredSize(new Dimension(800, 600));
00141 mainPanel.setBackground(Color.WHITE);
00142 }
00143 return mainPanel;
00144 }
00145
00151 private JLabel getMessageLabel() {
00152 if (statusMessageLabel == null) {
00153 statusMessageLabel = new JLabel();
00154 statusMessageLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
00155 statusMessageLabel.setText("After clicking Calibrate, tap once on the upper left "
00156 + "corner of your page, and then once on the lower right corner of your page.");
00157 }
00158 return statusMessageLabel;
00159 }
00160
00161 private JPanel getOutputPanel() {
00162 if (outputPanel == null) {
00163 outputPanel = new JPanel();
00164 outputPanel.add(getOutputTextArea(), BorderLayout.CENTER);
00165 outputPanel.add(getRetrieveAlternativesButton(), BorderLayout.EAST);
00166 }
00167 return outputPanel;
00168 }
00169
00175 private JTextArea getOutputTextArea() {
00176 if (textOutput == null) {
00177 textOutput = new JTextArea(1, 50);
00178 textOutput.setFont(new Font("Trebuchet MS", Font.PLAIN, 18));
00179 textOutput.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
00180 textOutput.setText(NO_TEXT);
00181 textOutput.setForeground(Color.WHITE);
00182 textOutput.setEditable(false);
00183 }
00184 return textOutput;
00185 }
00186
00187 private Component getRetrieveAlternativesButton() {
00188 if (alternativesButton == null) {
00189 alternativesButton = new JButton("Get Alternatives");
00190 alternativesButton.addActionListener(new ActionListener() {
00191 public void actionPerformed(ActionEvent e) {
00192 displayAlternatives();
00193 }
00194
00195 });
00196 }
00197 return alternativesButton;
00198 }
00199
00203 private JButton getSaveButton() {
00204 if (saveButton == null) {
00205 saveButton = new JButton("Save");
00206 saveButton.addActionListener(new ActionListener() {
00207 public void actionPerformed(ActionEvent e) {
00208 DebugUtils.println("Save");
00209 app.saveInkToDisk();
00210 }
00211 });
00212 }
00213 return saveButton;
00214 }
00215
00219 private JPanel getToolBarPanel() {
00220 if (statusBarPanel == null) {
00221 statusBarPanel = new JPanel();
00222 BorderLayout statusBarPanelLayout = new BorderLayout();
00223 statusBarPanel.setLayout(statusBarPanelLayout);
00224 statusBarPanel.add(getMessageLabel(), BorderLayout.CENTER);
00225 statusBarPanel.add(getButtonPanel(), BorderLayout.EAST);
00226 }
00227 return statusBarPanel;
00228 }
00229
00233 private void initGUI() {
00234 setTitle("Handwriting Recognition Debugger");
00235 getContentPane().add(getToolBarPanel(), BorderLayout.NORTH);
00236 getContentPane().add(getInkPanel(), BorderLayout.CENTER);
00237 getContentPane().add(getOutputPanel(), BorderLayout.SOUTH);
00238 pack();
00239 setLocation(WindowUtils.getWindowOrigin(this, WindowUtils.DESKTOP_CENTER));
00240 setExtendedState(JFrame.MAXIMIZED_BOTH);
00241 setVisible(true);
00242 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00243 }
00244
00250 public void setAlternatives(List<String> topTen) {
00251 DebugUtils.println(topTen);
00252 }
00253
00257 public void setInfoText(String text) {
00258 getOutputTextArea().setText(text);
00259 }
00260
00264 public void showBottomRightPointConfirmation() {
00265 getOutputTextArea().setText("Bottom-Right Point has been set.");
00266 }
00267
00271 public void showTopLeftPointConfirmation() {
00272 getOutputTextArea().setText("Top-Left Point has been set.");
00273 }
00274
00278 private void startApp() {
00279 app = new CaptureApplication(this);
00280 PaperToolkit p = new PaperToolkit(true, true , true);
00281 p.startApplication(app);
00282 }
00283
00284 }