-
Notifications
You must be signed in to change notification settings - Fork 8
/
GUIWindow.java
168 lines (144 loc) · 6.81 KB
/
GUIWindow.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package de.uni_bielefeld.cebitec.mzurowie.pretty_formula.main;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter.DefaultHighlightPainter;
import org.scilab.forge.jlatexmath.ParseException;
/**
*
* @author Martin Zurowietz
*/
public class GUIWindow extends javax.swing.JFrame {
private final DefaultHighlightPainter errorHighlighter;
final JFileChooser fileChooser;
/**
* Creates new form GUIWindow
*/
public GUIWindow() {
initComponents();
this.errorHighlighter = new DefaultHighlightPainter(Color.red);
this.fileChooser = new JFileChooser();
this.fileChooser.setFileFilter(new FileNameExtensionFilter("SVG file", "svg", "SVG"));
}
/**
* 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() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextPane1 = new javax.swing.JTextPane();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setForeground(new java.awt.Color(176, 1, 1));
jTextPane1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jTextPane1KeyReleased(evt);
}
public void keyTyped(java.awt.event.KeyEvent evt) {
jTextPane1KeyTyped(evt);
}
});
jScrollPane1.setViewportView(jTextPane1);
jButton1.setText("export");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jTextPane1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextPane1KeyTyped
// disallow multiple lines
String formula = this.jTextPane1.getText();
if (formula.contains("\n")) {
this.jTextPane1.setText(formula.replace("\n", ""));
}
}//GEN-LAST:event_jTextPane1KeyTyped
private void jTextPane1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextPane1KeyReleased
this.jLabel1.setText("");
this.jLabel2.getGraphics().clearRect(0, 0, this.jLabel2.getWidth(), this.jLabel2.getHeight());
this.jTextPane1.getHighlighter().removeAllHighlights();
try {
BufferedImage image = FormulaParser.parseToImage(this.jTextPane1.getText());
this.jLabel2.getGraphics().drawImage(image, 0, 0, null);
} catch (DetailedParseCancellationException e) {
this.handleDetailedParseCancellationException(e);
} catch (ParseException e) {
this.jLabel1.setText(e.getMessage());
}
}//GEN-LAST:event_jTextPane1KeyReleased
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
int returnVal = this.fileChooser.showSaveDialog(jTextPane1);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
FormulaParser.saveToSVG(this.jTextPane1.getText(),
this.fileChooser.getSelectedFile());
} catch (IOException | ParseException e) {
this.jLabel1.setText(e.getMessage());
} catch (DetailedParseCancellationException e) {
this.handleDetailedParseCancellationException(e);
}
}
this.jLabel1.setText("saved");
}//GEN-LAST:event_jButton1MouseClicked
private void handleDetailedParseCancellationException(
DetailedParseCancellationException e) {
try {
// highlight the position at which the error occurred
this.jTextPane1.getHighlighter().addHighlight(
e.getCharPositionInLine(), e.getEndCharPositionInLine(),
this.errorHighlighter);
} catch (BadLocationException ex) {
// simply don't highlight
}
this.jLabel1.setText(e.getMessage());
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextPane jTextPane1;
// End of variables declaration//GEN-END:variables
}