Serpent editor to support save/load files

This commit is contained in:
romanman 2014-05-29 16:24:37 +03:00
parent 0be061fddf
commit 0e62e5b58f
6 changed files with 131 additions and 10 deletions

View File

@ -37,6 +37,11 @@
<copy file="./target/classes/system.properties" todir="./target/package/ethereumj/config"/> <copy file="./target/classes/system.properties" todir="./target/package/ethereumj/config"/>
<copy file="./target/classes/GeoLiteCity.dat" todir="./target/package/ethereumj/config"/> <copy file="./target/classes/GeoLiteCity.dat" todir="./target/package/ethereumj/config"/>
<copy todir="./target/package/ethereumj/samples">
<fileset dir="./samples"/>
</copy>
<zip destfile="ethereumj-${DSTAMP}-${TSTAMP}.zip" basedir="./target/package"> <zip destfile="ethereumj-${DSTAMP}-${TSTAMP}.zip" basedir="./target/package">
</zip> </zip>

View File

@ -92,6 +92,12 @@ public class SystemProperties {
return Integer.parseInt(prop.getProperty("peer.discovery.port")); return Integer.parseInt(prop.getProperty("peer.discovery.port"));
} }
public String samplesDir(){
if(prop.isEmpty()) return "samples";
return prop.getProperty("samples.dir");
}
public String toString() { public String toString() {
Enumeration<?> e = prop.propertyNames(); Enumeration<?> e = prop.propertyNames();
while (e.hasMoreElements()) { while (e.hasMoreElements()) {

View File

@ -10,9 +10,13 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.InputEvent; import java.awt.event.InputEvent;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.io.*;
import java.util.Scanner;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static org.ethereum.config.SystemProperties.CONFIG;
/** /**
* www.ethereumJ.com * www.ethereumJ.com
* User: Roman Mandeleil * User: Roman Mandeleil
@ -46,7 +50,7 @@ public class SerpentEditor extends JFrame {
"\n" + "\n" +
"return(0)\n"; "return(0)\n";
private String codeSample3 = "\n" + private String defaultCode = "\n" +
"\n" + "\n" +
"init: \n" + "init: \n" +
"\n" + "\n" +
@ -69,6 +73,7 @@ public class SerpentEditor extends JFrame {
final JSplitPane splitPanel; final JSplitPane splitPanel;
final JTextArea result; final JTextArea result;
final JPanel contentPane; final JPanel contentPane;
JFileChooser fileChooser = null;
public SerpentEditor() { public SerpentEditor() {
@ -88,7 +93,7 @@ public class SerpentEditor extends JFrame {
codeArea.setSyntaxEditingStyle("text/serpent"); codeArea.setSyntaxEditingStyle("text/serpent");
codeArea.setCodeFoldingEnabled(true); codeArea.setCodeFoldingEnabled(true);
codeArea.setAntiAliasingEnabled(true); codeArea.setAntiAliasingEnabled(true);
codeArea.setText(codeSample3); codeArea.setText(defaultCode);
changeStyleProgrammatically(); changeStyleProgrammatically();
@ -103,6 +108,7 @@ public class SerpentEditor extends JFrame {
splitPanel.setContinuousLayout(true); splitPanel.setContinuousLayout(true);
contentPane.add(splitPanel, BorderLayout.CENTER); contentPane.add(splitPanel, BorderLayout.CENTER);
splitPanel.add(sp); splitPanel.add(sp);
@ -123,6 +129,7 @@ public class SerpentEditor extends JFrame {
contentPane.add(controlsPanel, BorderLayout.SOUTH); contentPane.add(controlsPanel, BorderLayout.SOUTH);
createToolBar(); createToolBar();
setContentPane(contentPane); setContentPane(contentPane);
@ -130,6 +137,8 @@ public class SerpentEditor extends JFrame {
pack(); pack();
this.revalidate();
this.repaint();
} }
@ -187,19 +196,20 @@ public class SerpentEditor extends JFrame {
} catch (Throwable th) { } catch (Throwable th) {
th.printStackTrace(); th.printStackTrace();
splitPanel.setDividerLocation(0.7); splitPanel.setDividerLocation(0.8);
result.setVisible(true); result.setVisible(true);
result.setText(th.getMessage()); result.setText(th.getMessage());
result.setForeground(Color.RED); result.setForeground(Color.RED);
return ; return ;
} }
result.setForeground(Color.BLACK.brighter()); result.setForeground(Color.BLACK.brighter());
result.setVisible(true); result.setVisible(true);
result.setText(asmResult); result.setText(asmResult);
splitPanel.setDividerLocation(0.7); splitPanel.setDividerLocation(
1 - result.getPreferredSize().getHeight() / codeArea.getPreferredSize().getHeight());
this.repaint(); this.repaint();
} }
@ -245,10 +255,7 @@ public class SerpentEditor extends JFrame {
toolbar.setFloatable(false); toolbar.setFloatable(false);
final JPanel mainContentPane = SerpentEditor.this.contentPane; final JPanel mainContentPane = SerpentEditor.this.contentPane;
{ {
java.net.URL url = ClassLoader.getSystemResource("buttons/open-file.png"); java.net.URL url = ClassLoader.getSystemResource("buttons/open-file.png");
Toolkit kit = Toolkit.getDefaultToolkit(); Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.createImage(url); Image img = kit.createImage(url);
@ -273,7 +280,21 @@ public class SerpentEditor extends JFrame {
new ActionListener() { new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
System.out.println("open file");
File file = callFileChooser();
try {
if (file == null) return;
String content = new Scanner(file).useDelimiter("\\Z").next();
codeArea.setText(content);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (java.util.NoSuchElementException e2){
// don't worry it's just the file is empty
codeArea.setText("");
}
} }
} }
); );
@ -306,7 +327,26 @@ public class SerpentEditor extends JFrame {
new ActionListener() { new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
System.out.println("save file");
File file = null;
if (fileChooser == null) file = callFileChooser();
else{
if (fileChooser.getSelectedFile() == null){
file = callFileChooser();
}else{
file = fileChooser.getSelectedFile();
}
}
try {
BufferedWriter out = new BufferedWriter(new FileWriter(file), 32768);
out.write(codeArea.getText());
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
} }
} }
); );
@ -431,6 +471,27 @@ public class SerpentEditor extends JFrame {
this.contentPane.add(toolbar, BorderLayout.EAST); this.contentPane.add(toolbar, BorderLayout.EAST);
} }
protected File callFileChooser(){
File file = null;
if (fileChooser == null) {
fileChooser = new JFileChooser(CONFIG.samplesDir());
fileChooser.setMultiSelectionEnabled(false);
}
switch (fileChooser.showOpenDialog(SerpentEditor.this))
{
case JFileChooser.APPROVE_OPTION:
file = fileChooser.getSelectedFile();
break;
}
return file;
}
public static void main(String[] args) { public static void main(String[] args) {
// Start all Swing applications on the EDT. // Start all Swing applications on the EDT.
SwingUtilities.invokeLater(new Runnable() { SwingUtilities.invokeLater(new Runnable() {

View File

@ -36,3 +36,8 @@ peer.discovery.timeout = 2
# retrieved from the peer [seconds] # retrieved from the peer [seconds]
transaction.approve.timeout = 5 transaction.approve.timeout = 5
# default directory where we keep
# basic Serpent samples relative
# to home.dir
samples.dir = samples

34
samples/my-currency.se Normal file
View File

@ -0,0 +1,34 @@
# the sample is good example how one can
# save program that manage his own currency
init:
# this part run only on init stage
# we are about to set the maxim
# amount of currency
contract.storage[0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826] = 1000000
code:
# the currency manager
# support two functions
if msg.datasize == 1:
# 1. balance check
addr = msg.data[0]
return(contract.storage[addr])
else:
# 2. balance manipulation
from = msg.sender
fromvalue = contract.storage[from]
to = msg.data[0]
value = msg.data[1]
if fromvalue >= value:
contract.storage[from] = fromvalue - value
contract.storage[to] = contract.storage[to] + value
return(1)
else:
return(0)

10
samples/namecoin.se Normal file
View File

@ -0,0 +1,10 @@
# The sample demonstrates how you
# can simply create a program running
# on top of chain and saving values for
# futre use
if !(contract.storage[msg.data[0]]):
contract.storage[msg.data[0]] = msg.data[1]
return(1)
else:
return(0)