package markdowneditor;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.Document;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
import org.markdown4j.Markdown4jProcessor;
public final class MainForm extends javax.swing.JFrame {
final UndoManager undo = new UndoManager();
FileHandler fileHandler = new FileHandler(this);
CheatSheet cheatSheet = new CheatSheet();
HtmlView htmlView = new HtmlView();
void UpdateTitle() {
setTitle(fileHandler.GetFilenameAlias());
}
void ClearFields() {
jTextArea1.setText("");
RefreshPreviewField();
UpdateTitle();
}
void InitializeUndo() {
// Taken from
// http://www.java2s.com/Tutorial/Java/0240__Swing/AddingUndoandRedotoaTextComponent.htm
Document doc = jTextArea1.getDocument();
doc.addUndoableEditListener(new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent evt) {
undo.addEdit(evt.getEdit());
}
});
jTextArea1.getActionMap().put("Undo",
new AbstractAction("Undo") {
@Override
public void actionPerformed(ActionEvent evt) {
try {
if (undo.canUndo()) {
undo.undo();
}
}
catch (CannotUndoException e) {
}
}
});
jTextArea1.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");
jTextArea1.getActionMap().put("Redo",
new AbstractAction("Redo") {
@Override
public void actionPerformed(ActionEvent evt) {
try {
if (undo.canRedo()) {
undo.redo();
}
}
catch (CannotRedoException e) {
}
}
});
jTextArea1.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");
}
public MainForm() {
initComponents();
markdownPane1.Init();
InitializeUndo();
RefreshPreviewField();
UpdateTitle();
}
void UpdateScolling() {
double relPos = ScrollUtil.GetRelativeXScrollPosition(jTextArea1);
ScrollUtil.SetRelativeXScrollPosition(markdownPane1, relPos);
}
public void RefreshPreviewField() {
String mdText = jTextArea1.getText();
try {
String body = new Markdown4jProcessor().process(mdText);
markdownPane1.setText(body);
htmlView.SetHtmlContent(markdownPane1.getText());
}
catch (IOException ex) {
Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
markdownPane1.setText("Error: Conversion failed");
}
UpdateScolling();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jSplitPane1 = new javax.swing.JSplitPane();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jScrollPane3 = new javax.swing.JScrollPane();
markdownPane1 = new markdowneditor.MarkdownPane();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItemNew = new javax.swing.JMenuItem();
jMenuItemOpen = new javax.swing.JMenuItem();
jMenuItemSave = new javax.swing.JMenuItem();
jMenuItemSaveAs = new javax.swing.JMenuItem();
jMenuItemExit = new javax.swing.JMenuItem();
jMenuTools = new javax.swing.JMenu();
jMenuItemCheatSheet = new javax.swing.JMenuItem();
jMenuItemShowHtml = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
jSplitPane1.setDividerLocation(350);
jSplitPane1.setResizeWeight(0.5);
jTextArea1.setColumns(20);
jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
jTextArea1.setRows(5);
jTextArea1.addCaretListener(new javax.swing.event.CaretListener() {
public void caretUpdate(javax.swing.event.CaretEvent evt) {
jTextArea1CaretUpdate(evt);
}
});
jTextArea1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jTextArea1KeyReleased(evt);
}
});
jScrollPane2.setViewportView(jTextArea1);
jSplitPane1.setLeftComponent(jScrollPane2);
markdownPane1.setEditable(false);
jScrollPane3.setViewportView(markdownPane1);
jSplitPane1.setRightComponent(jScrollPane3);
jMenu1.setText("File");
jMenuItemNew.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
jMenuItemNew.setText("New");
jMenuItemNew.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemNewActionPerformed(evt);
}
});
jMenu1.add(jMenuItemNew);
jMenuItemOpen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
jMenuItemOpen.setText("Open");
jMenuItemOpen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemOpenActionPerformed(evt);
}
});
jMenu1.add(jMenuItemOpen);
jMenuItemSave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
jMenuItemSave.setText("Save");
jMenuItemSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemSaveActionPerformed(evt);
}
});
jMenu1.add(jMenuItemSave);
jMenuItemSaveAs.setText("SaveAs");
jMenuItemSaveAs.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemSaveAsActionPerformed(evt);
}
});
jMenu1.add(jMenuItemSaveAs);
jMenuItemExit.setText("Exit");
jMenuItemExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemExitActionPerformed(evt);
}
});
jMenu1.add(jMenuItemExit);
jMenuBar1.add(jMenu1);
jMenuTools.setText("Tools");
jMenuItemCheatSheet.setText("Cheat sheet");
jMenuItemCheatSheet.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemCheatSheetActionPerformed(evt);
}
});
jMenuTools.add(jMenuItemCheatSheet);
jMenuItemShowHtml.setText("Show html");
jMenuItemShowHtml.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemShowHtmlActionPerformed(evt);
}
});
jMenuTools.add(jMenuItemShowHtml);
jMenuBar1.add(jMenuTools);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 712, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jTextArea1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextArea1KeyReleased
fileHandler.SetModified();
UpdateTitle();
RefreshPreviewField();
}//GEN-LAST:event_jTextArea1KeyReleased
private void jMenuItemNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemNewActionPerformed
String currentText = jTextArea1.getText();
if (fileHandler.NewFile(currentText)) {
ClearFields();
}
UpdateTitle();
}//GEN-LAST:event_jMenuItemNewActionPerformed
private void jMenuItemOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemOpenActionPerformed
String currentText = jTextArea1.getText();
if (fileHandler.FileOpen(currentText)) {
fileHandler.GetFileContent(jTextArea1);
RefreshPreviewField();
}
UpdateTitle();
}//GEN-LAST:event_jMenuItemOpenActionPerformed
private void jMenuItemSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSaveActionPerformed
fileHandler.FileSave(jTextArea1.getText());
UpdateTitle();
}//GEN-LAST:event_jMenuItemSaveActionPerformed
private void jMenuItemSaveAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSaveAsActionPerformed
fileHandler.FileSaveAs(jTextArea1.getText());
UpdateTitle();
}//GEN-LAST:event_jMenuItemSaveAsActionPerformed
void HandleExit() {
String currentText = jTextArea1.getText();
if (fileHandler.PrepareExit(currentText)) {
cheatSheet.dispose();
htmlView.dispose();
dispose();
}
}
private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExitActionPerformed
HandleExit();
}//GEN-LAST:event_jMenuItemExitActionPerformed
private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
HandleExit();
}//GEN-LAST:event_formWindowClosing
private void jTextArea1CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jTextArea1CaretUpdate
UpdateScolling();
}//GEN-LAST:event_jTextArea1CaretUpdate
private void jMenuItemCheatSheetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemCheatSheetActionPerformed
cheatSheet.setVisible(true);
}//GEN-LAST:event_jMenuItemCheatSheetActionPerformed
private void jMenuItemShowHtmlActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemShowHtmlActionPerformed
htmlView.setVisible(true);
}//GEN-LAST:event_jMenuItemShowHtmlActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
String osVersion = System.getProperty("os.name");
if (osVersion.contains("Linux")) {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
}
else {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new MainForm().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItemCheatSheet;
private javax.swing.JMenuItem jMenuItemExit;
private javax.swing.JMenuItem jMenuItemNew;
private javax.swing.JMenuItem jMenuItemOpen;
private javax.swing.JMenuItem jMenuItemSave;
private javax.swing.JMenuItem jMenuItemSaveAs;
private javax.swing.JMenuItem jMenuItemShowHtml;
private javax.swing.JMenu jMenuTools;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JTextArea jTextArea1;
private markdowneditor.MarkdownPane markdownPane1;
// End of variables declaration//GEN-END:variables
}