Skip to content

Commit

Permalink
saveSelectedTextArea plus global ctrl+s support.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhulha committed Nov 16, 2017
1 parent cd59ca2 commit bbe7231
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/net/raysforge/rayslang/FileUtils.java
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileUtils {
Expand All @@ -19,4 +20,15 @@ public static char[] readCompleteFile(File f) {
throw new RuntimeException(e);
}
}

public static void writeCompleteFile(File file, String text) {
try {
FileWriter fw = new FileWriter(file);
fw.write(text);
fw.close();
} catch (IOException e) {
throw new RuntimeException(e);
}

}
}
54 changes: 47 additions & 7 deletions src/net/raysforge/rayslang/ide/EasyIDE.java
Expand Up @@ -7,7 +7,6 @@
import java.io.IOException;
import java.util.ResourceBundle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JOptionPane;
Expand All @@ -30,11 +29,12 @@ public class EasyIDE {

private static final String NEW_PROJECT = "newProject";
private static final String NEW_FILE = "newFile";
private static final String SAVE = "save";
private static final String SAVE_ALL = "saveAll";
private static final String RUN = "run";

private EasySwing es;
private EasyTextArea console;
private JButton addToolBarItem;
private RayLang rayLang;
private EasyTree easyTree;
private final File projectsHome;
Expand All @@ -50,6 +50,7 @@ public EasyIDE(File projectsHome) {

es = new EasySwing("RayLang IDE", 800, 600, JFrame.DO_NOTHING_ON_CLOSE);
es.addWindowListener(delegator);
es.addGlobalKeyEventListener(delegator);

EasySplitPane codeAndConsole = new EasySplitPane(false, 400);
EasySplitPane treeAndSplitPane = new EasySplitPane(true, 200);
Expand Down Expand Up @@ -82,11 +83,14 @@ public EasyIDE(File projectsHome) {
JMenu fileMenuItem = es.addMenuItem(rb.getString("MenuItemFile"));
es.addMenuItem(fileMenuItem, rb.getString("MenuItemNewProject"), NEW_PROJECT, delegator);
es.addMenuItem(fileMenuItem, rb.getString("MenuItemNewFile"), NEW_FILE, delegator);
es.addMenuItem(fileMenuItem, rb.getString("MenuItemSave"), SAVE, delegator);
es.addMenuItem(fileMenuItem, rb.getString("MenuItemSaveAll"), SAVE_ALL, delegator);

addToolBarItem = es.addToolBarItem("LOS");
addToolBarItem.addActionListener(delegator);
addToolBarItem.setActionCommand("run");
es.addToolBarItem(rb.getString("MenuItemNewProject"), NEW_PROJECT, delegator);
es.addToolBarItem(rb.getString("MenuItemNewFile"), NEW_FILE, delegator);
es.addToolBarItem(rb.getString("MenuItemSave"), SAVE, delegator);
es.addToolBarItem(rb.getString("MenuItemSaveAll"), SAVE_ALL, delegator);
es.addToolBarItem(rb.getString("MenuItemRun"), RUN, delegator);

}

Expand Down Expand Up @@ -161,9 +165,9 @@ public void actionPerformed(ActionEvent e) {

//System.out.println(e.getActionCommand());

if (e.getActionCommand().equals("run")) {
if (e.getActionCommand().equals(RUN)) {
RayLang.instance.unregisterClasses("test");
JTextArea textArea = (JTextArea) tabbedPane.getSelectedComponent();
JTextArea textArea = getSelectedTextArea();
RayClass.parse("test", RayUtils.convertSourceToTokenList(new RaySource(textArea.getText().toCharArray())));
RayLang.runClass(rayLang.getClass("test"));
} else if (e.getActionCommand().equals(NEW_PROJECT)) {
Expand All @@ -173,11 +177,16 @@ public void actionPerformed(ActionEvent e) {
else
JOptionPane.showMessageDialog(null, "'New Project' folder could not be created in: " + projectsHome);

} else if (e.getActionCommand().equals(SAVE)) {
saveSelectedTextArea();
} else if (e.getActionCommand().equals(SAVE_ALL)) {
for (int i = 0; i < tabbedPane.getTabCount(); i++) {
String title = tabbedPane.getTitleAt(i);
if( title.startsWith("*"))
{
FileUtils.writeCompleteFile( getFile(i), getTextArea(i).getText());
tabbedPane.setTitleAt(i, title.substring(1));
}
}
} else if (e.getActionCommand().equals(NEW_FILE)) {
if (easyTree.isSelected(1)) {
Expand All @@ -193,6 +202,26 @@ public void actionPerformed(ActionEvent e) {
}
}
}

private JTextArea getSelectedTextArea()
{
return (JTextArea) tabbedPane.getSelectedComponent();
}

private JTextArea getTextArea(int index)
{
return (JTextArea) tabbedPane.getTabComponentAt(index);
}

private File getFile(int index)
{
return new File(tabbedPane.getToolTipTextAt(index));
}

private File getSelectedFile()
{
return new File(tabbedPane.getToolTipTextAt(tabbedPane.getSelectedIndex()));
}

public static void main(String[] args) {
File userHome = new File(System.getProperty("user.home"));
Expand Down Expand Up @@ -225,9 +254,20 @@ protected void setSelectedTabToModified() {
tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(), "*" + title);
}

protected void setSelectedTabToSaved() {
String title = tabbedPane.getTitleAt(tabbedPane.getSelectedIndex());
if (title.startsWith("*"))
tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(), title.substring(1));
}

public JFrame getJFrame() {
return es.getFrame();
}

public void saveSelectedTextArea() {
FileUtils.writeCompleteFile(getSelectedFile(), getSelectedTextArea().getText());
setSelectedTabToSaved();
}


}
1 change: 1 addition & 0 deletions src/net/raysforge/rayslang/ide/EasyIDE.properties
Expand Up @@ -2,4 +2,5 @@ MenuItemFile=File
MenuItemNewProject=New Project
MenuItemNewFile=New File
MenuItemSaveAll=Save All
MenuItemSave=Save
MenuItemRun=Run
15 changes: 14 additions & 1 deletion src/net/raysforge/rayslang/ide/EventDelegator.java
@@ -1,7 +1,10 @@
package net.raysforge.rayslang.ide;

import java.awt.AWTEvent;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
Expand All @@ -15,7 +18,7 @@
import net.raysforge.easyswing.ValueForPathChangedListener;
import net.raysforge.rayslang.Output;

public class EventDelegator implements ActionListener, Output, ValueForPathChangedListener, MouseListener, DocumentListener, WindowListener {
public class EventDelegator implements ActionListener, Output, ValueForPathChangedListener, MouseListener, DocumentListener, WindowListener, AWTEventListener {

private final EasyIDE easyIDE;

Expand Down Expand Up @@ -127,4 +130,14 @@ public void actionPerformed(ActionEvent ae) {

}

@Override
public void eventDispatched(AWTEvent arg0) {
if (arg0 instanceof KeyEvent) {
KeyEvent ke = (KeyEvent) arg0;
if( ke.getKeyCode() == 'S' && (ke.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK) && ke.getID() == KeyEvent.KEY_RELEASED)
easyIDE.saveSelectedTextArea();
}

}

}

0 comments on commit bbe7231

Please sign in to comment.