Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .settings/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<module name="Checker">
<property name="severity" value="warning"/>
<module name="TreeWalker">
<property name="severity" value="ignore"/>
<metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
<module name="JavadocStyle">
<property name="checkEmptyJavadoc" value="true"/>
<property name="tokens" value="CLASS_DEF,CTOR_DEF,INTERFACE_DEF,METHOD_DEF,VARIABLE_DEF"/>
Expand All @@ -27,8 +29,8 @@
<module name="RedundantImport"/>
<module name="LineLength">
<property name="ignorePattern" value="^import"/>
<property name="tabWidth" value="2"/>
<property name="max" value="90"/>
<property name="tabWidth" value="2"/>
</module>
<module name="EmptyForIteratorPad"/>
<module name="MethodParamPad"/>
Expand All @@ -53,7 +55,10 @@
<module name="AvoidNestedBlocks"/>
<module name="LeftCurly"/>
<module name="RightCurly"/>
<module name="DoubleCheckedLocking"/>
<module name="DoubleCheckedLocking">
<property name="severity" value="ignore"/>
<metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
</module>
<module name="EqualsHashCode"/>
<module name="IllegalInstantiation"/>
<module name="MagicNumber">
Expand Down
10 changes: 10 additions & 0 deletions src/de/woerteler/charty/ParseTree.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.woerteler.charty;

import de.woerteler.charty.ChartParser.Edge;
import de.woerteler.latex.LaTeXDisplay;

/**
* Class representing a parse tree generated by the {@link ChartParser}.
Expand Down Expand Up @@ -33,4 +34,13 @@ public Displayer getDisplayer(final DisplayMethod method)
return method.getDisplayer(edge);
}

/**
* Getter.
*
* @return Creates a LaTeX representation of the syntax tree.
*/
public String getLaTeXText() {
return LaTeXDisplay.toLaTeX(edge);
}

}
33 changes: 25 additions & 8 deletions src/de/woerteler/gui/ChartyGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ private ChartyGUI() {
displayMenu.addSeparator();
displayMenu.add(ctrl.getActionFor(VIEW_SAVE));
displayMenu.add(ctrl.getActionFor(SYNTAX_TREE_SAVE));
displayMenu.add(ctrl.getActionFor(LATEX_SAVE));
// Left side: edit grammar and phrase
editor = new GrammarEditor(ctrl);
model.setDocument(editor.getDocument());
Expand Down Expand Up @@ -274,11 +275,7 @@ private void addMenuItem(final ActionID id, final Class<?> lookup, final JMenu m

@Override
public void dispose() {
if(ctrl.closeGrammar()) {
// abort dispose
setVisible(true);
return;
}
if(ctrl.closeGrammar()) return; // abort dispose
ctrl.refreshIniValues();
refreshIniValues();
writeIniOnChange();
Expand Down Expand Up @@ -505,6 +502,7 @@ public File saveGrammarDialog(final File cur) {
dir = INI.getObject("last", "grammarDir", Converter.FILE_CONVERTER, HOME_STR);
}
final JFileChooser choose = new JFileChooser(dir);
choose.setFileFilter(new FileNameExtensionFilter("Grammar (*.gr)", "gr"));
choose.setMultiSelectionEnabled(false);
choose.setFileSelectionMode(JFileChooser.FILES_ONLY);
final boolean approved = choose.showSaveDialog(this) == JFileChooser.APPROVE_OPTION;
Expand All @@ -515,6 +513,26 @@ public File saveGrammarDialog(final File cur) {
return res;
}

/**
* Shows a LaTeX save dialog.
*
* @return The selected file.
*/
public File saveLaTeXDialog() {
final JFileChooser choose = new JFileChooser(INI.getObject("last", "latexDir",
Converter.FILE_CONVERTER, HOME_STR));
choose.setFileFilter(new FileNameExtensionFilter("TeX-Document (*.tex)", "tex"));
choose.setMultiSelectionEnabled(false);
choose.setFileSelectionMode(JFileChooser.FILES_ONLY);
final boolean approved = choose.showSaveDialog(this) == JFileChooser.APPROVE_OPTION;
final File res = approved ? choose.getSelectedFile() : null;
if(res == null) return null;
final File par = res.getParentFile();
INI.setObject("last", "latexDir", par);
final String name = res.getName();
return !name.contains(".") ? new File(par, name + ".tex") : res;
}

/**
* Shows a view save dialog to the user.
*
Expand All @@ -529,10 +547,9 @@ public File saveViewDialog() {
final File res = approved ? choose.getSelectedFile() : null;
if(res == null) return null;
final File par = res.getParentFile();
final String name = res.getName();
INI.setObject("last", "viewDir", par);
if(!name.contains(".")) return new File(par, name + ".png");
return res;
final String name = res.getName();
return !name.contains(".") ? new File(par, name + ".png") : res;
}

}
36 changes: 36 additions & 0 deletions src/de/woerteler/gui/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import static de.woerteler.gui.GUIActions.ActionID.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -323,6 +326,21 @@ public void showTree(final int pos) {
}
}

/**
* Getter.
*
* @return The LaTeX representation of the current tree.
*/
public String getLaTeXText() {
final ParseTree[] trees = model.getParseTrees();
final int pos = model.getParseTreePos();
if(trees == null || pos < 0 || pos >= trees.length) {
gui.showError("No tree to generate LaTeX for.");
return null;
}
return trees[pos].getLaTeXText();
}

/**
* Parses the given phrase.
*
Expand Down Expand Up @@ -384,6 +402,24 @@ public void saveView() {
gui.saveView(file);
}

/**
* Saves the current tree as LaTeX document.
*/
public void saveLaTeX() {
if(!gui.canSaveView()) return;
final String str = getLaTeXText();
if(str == null) return;
final File file = gui.saveLaTeXDialog();
if(file == null) return;
try {
final Writer out = new OutputStreamWriter(new FileOutputStream(file), IOUtils.UTF8);
out.append(str);
out.close();
} catch(final Exception e) {
gui.showError("Error saving LaTeX:\n" + e.getMessage());
}
}

/**
* Saves the current syntax tree.
*/
Expand Down
13 changes: 13 additions & 0 deletions src/de/woerteler/gui/GUIActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public static enum ActionID {
/** Saves the current syntax tree. */
SYNTAX_TREE_SAVE,

/** Saves the current syntax tree as LaTeX document. */
LATEX_SAVE,

/** Saves the current view. */
VIEW_SAVE,

Expand Down Expand Up @@ -186,6 +189,16 @@ public void actionPerformed(final ActionEvent e) {
ctrl.saveTree();
}

});
actionMap.put(ActionID.LATEX_SAVE, new AbstractAction("Save syntax tree as LaTeX") {

private static final long serialVersionUID = 2654022091067389989L;

@Override
public void actionPerformed(final ActionEvent e) {
ctrl.saveLaTeX();
}

});
actionMap.put(ActionID.VIEW_SAVE, new AbstractAction("Save current view...") {

Expand Down
14 changes: 14 additions & 0 deletions src/de/woerteler/gui/ParseTreeViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.geom.Rectangle2D;

import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;

import de.woerteler.charty.Displayer;
Expand Down Expand Up @@ -100,6 +104,7 @@ public void mousePressed(final MouseEvent e) {
origY = getOffsetY();
drag = true;
}
grabFocus();
}

@Override
Expand Down Expand Up @@ -153,6 +158,14 @@ public void mouseWheelMoved(final MouseWheelEvent e) {
right.setEnabled(false);
nav.add(right, BorderLayout.EAST);
add(nav, BorderLayout.SOUTH);

setFocusable(true);
final ActionMap action = getActionMap();
final InputMap input = getInputMap();
input.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), VIEW_PREV);
input.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), VIEW_NEXT);
action.put(VIEW_NEXT, ctrl.getActionFor(VIEW_NEXT));
action.put(VIEW_PREV, ctrl.getActionFor(VIEW_PREV));
}

/**
Expand All @@ -173,6 +186,7 @@ public void showParseTree(final Displayer disp, final int pos, final int num) {
right.setEnabled(pos < num);
}
setTree(disp);
grabFocus();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/de/woerteler/latex/LaTeXDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class LaTeXDisplay implements DisplayMethod {
* @param edge The root edge.
* @return LaTeX document
*/
private static String toLaTeX(final Edge edge) {
public static String toLaTeX(final Edge edge) {
return "\\documentclass{article}\n\\usepackage{qtree}\n"
+ "\\usepackage[utf8]{inputenc}\n\n"
+ "\\usepackage[landscape]{geometry}\n" + "\\usepackage{fullpage}\n"
Expand Down