Skip to content

Commit

Permalink
Cut/Copy/Paste Blocks
Browse files Browse the repository at this point in the history
+updated MSDlite
  • Loading branch information
apocist committed Aug 9, 2014
1 parent 8e93100 commit ceb5321
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 27 deletions.
Binary file modified lib/MSDlite.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.PrintWriter;

import javax.swing.BorderFactory;
Expand All @@ -25,6 +26,7 @@
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
Expand All @@ -36,6 +38,7 @@ public class BlockWindow {

public DynamicTree blocks;
private JFrame blockFrame;
private BuildingBlock blockClipboard;

public BlockWindow(final Main Main){
this.Main = Main;
Expand All @@ -52,7 +55,8 @@ public void run(){
//Menu
JMenuBar menuBar = new JMenuBar();//Create the menu bar.

JMenu menu = new JMenu("Edit");//Build the first menu.
//File menu
JMenu menu = new JMenu("File");//Build the first menu.

//a group of JMenuItems
JMenuItem menuItem = new JMenuItem("Generate Script");
Expand Down Expand Up @@ -96,6 +100,54 @@ public void actionPerformed(ActionEvent e){

menuBar.add(menu);

//Edit menu
menu = new JMenu("Edit");//Build the first menu.

//a group of JMenuItems
menuItem = new JMenuItem("Cut");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,KeyEvent.ALT_DOWN_MASK));
menuItem.setMnemonic(KeyEvent.VK_X);
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(blocks.getCurrentNode() != null){
if(blocks.getCurrentNode() != blocks.getRootNode()){
blockClipboard = blocks.cloneNode(blocks.getCurrentNode());
blocks.removeCurrentNode();
Main.ImageWindow.update();
}else{blockClipboard = null;}
}else{blockClipboard = null;}
}
});
menu.add(menuItem);
menuItem = new JMenuItem("Copy");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,KeyEvent.ALT_DOWN_MASK));
menuItem.setMnemonic(KeyEvent.VK_C);
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(blocks.getCurrentNode() != null){
if(blocks.getCurrentNode() != blocks.getRootNode()){
blockClipboard = blocks.cloneNode(blocks.getCurrentNode());
}else{blockClipboard = null;}
}else{blockClipboard = null;}
}
});
menu.add(menuItem);
menuItem = new JMenuItem("Paste");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,KeyEvent.ALT_DOWN_MASK));
menuItem.setMnemonic(KeyEvent.VK_V);
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(blockClipboard != null){
blocks.addObject(blocks.cloneNode(blockClipboard));
Main.ImageWindow.update();
}
}
});
menu.add(menuItem);

menuBar.add(menu);

//divides item to sides
menuBar.add(Box.createGlue());//divides item to sides

JButton menuBut = new JButton(new ImageIcon(System.getProperty("user.dir") + "/system/addBut.png"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,29 @@ public class BuildingBlock extends DefaultMutableTreeNode{
public final boolean ISPARENTABLE = true;
protected int x = 0;
protected int y = 0;
private BufferedImage image;
private String name;

//public Signature sig;
public final Main Main;
public transient Main Main;

public BuildingBlock(String name, Main Main){
setName(name);
//this.sig = sig;
this.Main = Main;
saveObject();
}
public boolean isFilter(){
public void reinit(Main Main){
this.Main = Main;
if(getChildCount() > 0){
for(int i = 0; i< getChildCount(); i++){
BuildingBlock block = (BuildingBlock) ((DefaultMutableTreeNode) getChildAt(i)).getUserObject();
if(block != null){
block.reinit(Main);
}
}
}
}
public boolean isFilter(){
return false;
}
/**
Expand All @@ -54,24 +64,12 @@ public void setX(int x) {
public void setY(int y) {
this.y = y;
}
/**
* @return the image
*/
public BufferedImage getImage() {
return image;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param image the image to set
*/
public void setImage(BufferedImage image) {
this.image = image;
}
public void saveObject(){
setUserObject(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import java.awt.GridLayout;
import java.awt.Toolkit;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
Expand Down Expand Up @@ -190,24 +195,50 @@ public void reload(){
}

private List<TreePath> getCurrExpandedPaths(TreePath currPath){
List<TreePath> paths = new ArrayList<TreePath>();
Enumeration<TreePath> expandEnum = tree.getExpandedDescendants(currPath);
if (expandEnum == null)
return null;
List<TreePath> paths = new ArrayList<TreePath>();
Enumeration<TreePath> expandEnum = tree.getExpandedDescendants(currPath);
if (expandEnum == null)
return null;

while (expandEnum.hasMoreElements())
paths.add(expandEnum.nextElement());
while (expandEnum.hasMoreElements())
paths.add(expandEnum.nextElement());

return paths;
return paths;
}

private void reExpandPaths(List<TreePath> expPaths){
if(expPaths == null)
return;
for(TreePath tp : expPaths)
tree.expandPath(tp);
if(expPaths == null)
return;
for(TreePath tp : expPaths)
tree.expandPath(tp);
}

public BuildingBlock cloneNode(BuildingBlock node){
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BuildingBlock theReturn = null;
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(bos);
oos.writeObject(node);
oos.flush();
}
catch(Exception e){System.out.println("ObjectOutputStream error");}
finally {
try {
oos.close();
} catch (IOException e) {}
}

byte[] bytes = bos.toByteArray();
ObjectInputStream ois;
try {
ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
theReturn = (BuildingBlock)ois.readObject();
theReturn.reinit(Main);
}
catch (Exception e) {System.out.println("ObjectInputStream error");}
return theReturn;
}

class MyTreeModelListener implements TreeModelListener {
public void treeNodesChanged(TreeModelEvent e) {
Expand Down

0 comments on commit ceb5321

Please sign in to comment.