Skip to content

Commit

Permalink
Added editor.
Browse files Browse the repository at this point in the history
  • Loading branch information
cereda committed Dec 1, 2014
1 parent bb5f1ee commit e530e9a
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 5 deletions.
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ for more details.
<modelVersion>4.0.0</modelVersion>
<groupId>br.usp.poli.lta.cereda</groupId>
<artifactId>macro</artifactId>
<version>1.0</version>
<version>2.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
Expand Down Expand Up @@ -76,6 +76,11 @@ for more details.
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.19.0</version>
</dependency>
<dependency>
<groupId>com.fifesoft</groupId>
<artifactId>rsyntaxtextarea</artifactId>
<version>2.5.4</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -103,4 +108,4 @@ for more details.
</plugins>
</build>

</project>
</project>
20 changes: 20 additions & 0 deletions src/main/java/br/usp/poli/lta/cereda/macro/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
package br.usp.poli.lta.cereda.macro;

import br.usp.poli.lta.cereda.macro.model.Pair;
import br.usp.poli.lta.cereda.macro.ui.Editor;
import br.usp.poli.lta.cereda.macro.util.CLIParser;
import br.usp.poli.lta.cereda.macro.util.DisplayUtils;
import java.io.File;
import java.nio.charset.Charset;
import javax.swing.SwingUtilities;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
Expand Down Expand Up @@ -93,6 +96,23 @@ public static void main(String[] args) {
}

}
else {

// verifica se a execução corresponde a uma chamada ao editor
// embutido de macros
if (parser.isEditor()) {

// cria o editor e exibe
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
DisplayUtils.init();
Editor editor = new Editor();
editor.setVisible(true);
}
});
}
}
}
catch (Exception exception) {

Expand Down
178 changes: 178 additions & 0 deletions src/main/java/br/usp/poli/lta/cereda/macro/ui/Editor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/**
* ------------------------------------------------------
* Laboratório de Linguagens e Técnicas Adaptativas
* Escola Politécnica, Universidade São Paulo
* ------------------------------------------------------
*
* This program is free software: you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
**/
package br.usp.poli.lta.cereda.macro.ui;

import br.usp.poli.lta.cereda.macro.MacroExpander;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.nio.charset.Charset;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileNameExtensionFilter;
import net.miginfocom.swing.MigLayout;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rtextarea.RTextScrollPane;

/**
* Editor embutido de macros.
* @author Paulo Roberto Massa Cereda
* @version 1.0
* @since 1.0
*/
public class Editor extends JFrame {

// atributos da classe
private final JButton open;
private final JButton save;
private final JButton run;
private final JButton clear;
private final JFileChooser chooser;
private final RSyntaxTextArea input;
private final RSyntaxTextArea output;

/**
* Construtor.
*/
public Editor() {

// define as configurações de exibição
super("Expansor de macros");
setPreferredSize(new Dimension(550, 550));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setResizable(false);
setLayout(new MigLayout());

// cria os botões e suas respectivas ações
open = new JButton("Abrir", new ImageIcon(getClass().
getResource("/br/usp/poli/lta/cereda/macro/images/open.png")));
save = new JButton("Salvar", new ImageIcon(getClass().
getResource("/br/usp/poli/lta/cereda/macro/images/save.png")));
run = new JButton("Executar", new ImageIcon(getClass().
getResource("/br/usp/poli/lta/cereda/macro/images/play.png")));
clear = new JButton("Limpar", new ImageIcon(getClass().
getResource("/br/usp/poli/lta/cereda/macro/images/clear.png")));

// cria uma janela de diálogo para abrir e salvar arquivos de texto
chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(false);
FileNameExtensionFilter filter =
new FileNameExtensionFilter("Arquivos de texto", "txt", "text");
chooser.setFileFilter(filter);

// ação de abertura de arquivo
open.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
int value = chooser.showOpenDialog(Editor.this);
if (value == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
String content = FileUtils.readFileToString(file);
input.setText(content);
output.setText("");
}
catch (Exception e) {}
}
}
});

// ação de salvamento de arquivo
save.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
int value = chooser.showSaveDialog(Editor.this);
if (value == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
FileUtils.writeStringToFile(
file,
input.getText(),
Charset.forName("UTF-8")
);
output.setText("");
}
catch (Exception e) {}
}
}
});

// ação de limpeza da janela de saída
clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
output.setText("");
}
});

// ação de execução do expansor de macros
run.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
try {
output.setText(MacroExpander.parse(input.getText()));
}
catch (Exception exception) {
String out = StringUtils.
rightPad("ERRO: ", 50, "-").concat("\n");
out = out.concat(WordUtils.
wrap(exception.getMessage(), 50)).concat("\n");
out = out.concat(StringUtils.repeat(".", 50)).concat("\n");
output.setText(out);
}
}
});

// tela de entrada do texto
input = new RSyntaxTextArea(14, 60);
input.setCodeFoldingEnabled(true);
input.setWrapStyleWord(true);
input.setLineWrap(true);
RTextScrollPane iinput = new RTextScrollPane(input);
add(iinput, "span 4, wrap");

// adiciona os botões
add(open);
add(save);
add(run);
add(clear, "wrap");

// tela de saída da expansão
output = new RSyntaxTextArea(14, 60);
output.setEditable(false);
output.setCodeFoldingEnabled(true);
output.setWrapStyleWord(true);
output.setLineWrap(true);
RTextScrollPane ioutput = new RTextScrollPane(output);
add(ioutput, "span 4");

// ajustes finais
pack();
setLocationRelativeTo(null);

}

}
34 changes: 31 additions & 3 deletions src/main/java/br/usp/poli/lta/cereda/macro/util/CLIParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class CLIParser {

// vetor de argumentos de linha de comando
private final String[] arguments;

private boolean editor = false;

/**
* Construtor.
Expand All @@ -68,24 +70,41 @@ public Pair<String, File> parse() throws IOException {
// opção de entrada
Option input = OptionBuilder.withLongOpt("input").
hasArgs().withArgName("lista de arquivos").
withDescription("arquivos de entrada").isRequired().
withDescription("arquivos de entrada").
create("i");

// opção de saída
Option output = OptionBuilder.withLongOpt("output").
hasArg().withArgName("arquivo").
withDescription("arquivo de saída").create("o");

// opção do editor embutido
Option ui = OptionBuilder.withLongOpt("editor").
withDescription("editor gráfico").create("e");

Options options = new Options();
options.addOption(input);
options.addOption(output);
options.addOption(ui);

try {

// parsing dos argumentos
Parser parser = new BasicParser();
CommandLine line = parser.parse(options, arguments);

// verifica se é uma chamada ao editor e retorna em caso positivo
if (line.hasOption("e")) {
editor = true;
return null;
}

// se não é uma chamada ao editor de macros, é necessário verificar
// se existe um arquivo de entrada
if (!line.hasOption("i")) {
throw new ParseException("");
}

// existem argumentos restantes, o que representa situação de erro
if (!line.getArgList().isEmpty()) {
throw new ParseException("");
Expand Down Expand Up @@ -133,7 +152,8 @@ public Pair<String, File> parse() throws IOException {
// imprime a ajuda
HelpFormatter help = new HelpFormatter();
help.printHelp(
"expander --input <lista de arquivos> [ --output <arquivo> ]",
"expander ( --editor | --input <lista de arquivos>"
+ " [ --output <arquivo> ] )",
options
);
}
Expand All @@ -143,5 +163,13 @@ public Pair<String, File> parse() throws IOException {
return null;

}


/**
* Verifica se é uma chamada ao editor embutido.
* @return Valor lógico que indica se é uma chamada ao editor de macros.
*/
public boolean isEditor() {
return editor;
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e530e9a

Please sign in to comment.