From 8249e3a52f3299cffab74ad9fd089627fccb4555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=F8=EDk?= Date: Wed, 2 Jan 2013 18:42:05 +0100 Subject: [PATCH] AS3: Exporting selected scripts AS3: Search bar to the top --- trunk/src/com/jpexs/asdec/abc/ABC.java | 11 +--- .../jpexs/asdec/abc/gui/ClassesListTree.java | 37 +++++++++++ .../com/jpexs/asdec/abc/gui/MainFrame.java | 64 +++++++++++++++---- trunk/src/com/jpexs/asdec/abc/gui/Tree.java | 2 +- .../com/jpexs/asdec/abc/types/ScriptInfo.java | 18 ++++++ 5 files changed, 107 insertions(+), 25 deletions(-) diff --git a/trunk/src/com/jpexs/asdec/abc/ABC.java b/trunk/src/com/jpexs/asdec/abc/ABC.java index 7fd2697ed..feb672274 100644 --- a/trunk/src/com/jpexs/asdec/abc/ABC.java +++ b/trunk/src/com/jpexs/asdec/abc/ABC.java @@ -522,7 +522,6 @@ public class ABC { for (int i = 0; i < script_info.length; i++) { String path = script_info[i].getPath(this); String packageName = path.substring(0, path.lastIndexOf(".")); - String className = path.substring(path.lastIndexOf(".") + 1); if (packageName.equals("")) { path = path.substring(1); } @@ -532,15 +531,7 @@ public class ABC { } String exStr = "Exporting " + abcStr + cnt + path + " ..."; informListeners("export", exStr); - logger.info(exStr); - File outDir = new File(directory + File.separatorChar + packageName.replace('.', File.separatorChar)); - if (!outDir.exists()) { - outDir.mkdirs(); - } - String fileName = outDir.toString() + File.separator + className + ".as"; - FileOutputStream fos = new FileOutputStream(fileName); - fos.write(script_info[i].convert(abcList, this, pcode, false).getBytes()); - fos.close(); + script_info[i].export(this, abcList, directory, pcode); } } diff --git a/trunk/src/com/jpexs/asdec/abc/gui/ClassesListTree.java b/trunk/src/com/jpexs/asdec/abc/gui/ClassesListTree.java index 87ea3133b..45dbccbfe 100644 --- a/trunk/src/com/jpexs/asdec/abc/gui/ClassesListTree.java +++ b/trunk/src/com/jpexs/asdec/abc/gui/ClassesListTree.java @@ -20,6 +20,7 @@ import com.jpexs.asdec.Main; import com.jpexs.asdec.abc.types.traits.Trait; import com.jpexs.asdec.abc.types.traits.TraitClass; import com.jpexs.asdec.tags.DoABCTag; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import javax.swing.ImageIcon; @@ -28,6 +29,7 @@ import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.TreePath; +import javax.swing.tree.TreeSelectionModel; public class ClassesListTree extends JTree implements TreeSelectionListener { @@ -55,6 +57,41 @@ public class ClassesListTree extends JTree implements TreeSelectionListener { setCellRenderer(treeRenderer); } + public List getSelectedScripts() { + TreeSelectionModel tsm = getSelectionModel(); + final List selectedScripts = new ArrayList(); + TreePath tps[] = tsm.getSelectionPaths(); + if (tps == null) { + return selectedScripts; + } + for (TreePath tp : tps) { + TreeElement te = (TreeElement) tp.getLastPathComponent(); + if (te.isLeaf()) { + Object item = te.getItem(); + if (item instanceof TreeLeafScript) { + selectedScripts.add((TreeLeafScript) item); + } + } else { + TreeVisitor tvi = new TreeVisitor() { + @Override + public void onBranch(TreeElement branch) { + } + + @Override + public void onLeaf(TreeElement leaf) { + Object item = leaf.getItem(); + if (item instanceof TreeLeafScript) { + selectedScripts.add((TreeLeafScript) item); + } + } + }; + te.visitBranches(tvi); + te.visitLeafs(tvi); + } + } + return selectedScripts; + } + public HashMap getTreeList(List list) { HashMap ret = new HashMap(); for (DoABCTag tag : list) { diff --git a/trunk/src/com/jpexs/asdec/abc/gui/MainFrame.java b/trunk/src/com/jpexs/asdec/abc/gui/MainFrame.java index 54a016e4c..12d4a7799 100644 --- a/trunk/src/com/jpexs/asdec/abc/gui/MainFrame.java +++ b/trunk/src/com/jpexs/asdec/abc/gui/MainFrame.java @@ -37,6 +37,8 @@ import javax.swing.border.BevelBorder; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.table.*; +import javax.swing.tree.TreePath; +import javax.swing.tree.TreeSelectionModel; import jsyntaxpane.DefaultSyntaxKit; public class MainFrame extends JFrame implements ActionListener, ItemListener { @@ -254,7 +256,7 @@ public class MainFrame extends JFrame implements ActionListener, ItemListener { searchPanel.add(filterField, BorderLayout.CENTER); JLabel picLabel = new JLabel(new ImageIcon(View.loadImage("com/jpexs/asdec/gui/graphics/search.png"))); searchPanel.add(picLabel, BorderLayout.EAST); - treePanel.add(searchPanel, BorderLayout.SOUTH); + treePanel.add(searchPanel, BorderLayout.NORTH); splitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, treePanel, @@ -314,20 +316,41 @@ public class MainFrame extends JFrame implements ActionListener, ItemListener { miSaveAs.setIcon(new ImageIcon(View.loadImage("com/jpexs/asdec/gui/graphics/save16.png"))); miSaveAs.setActionCommand("SAVEAS"); miSaveAs.addActionListener(this); - JMenuItem miExport = new JMenuItem("Export as ActionScript..."); - miExport.setIcon(new ImageIcon(View.loadImage("com/jpexs/asdec/gui/graphics/exportas16.png"))); - miExport.setActionCommand("EXPORT"); - miExport.addActionListener(this); + JMenu menuExportAll = new JMenu("Export all"); + JMenuItem miExportAllAS = new JMenuItem("ActionScript..."); + miExportAllAS.setIcon(new ImageIcon(View.loadImage("com/jpexs/asdec/gui/graphics/exportas16.png"))); + miExportAllAS.setActionCommand("EXPORT"); + miExportAllAS.addActionListener(this); + + JMenuItem miExportAllPCode = new JMenuItem("PCode..."); + miExportAllPCode.setIcon(new ImageIcon(View.loadImage("com/jpexs/asdec/gui/graphics/exportpc16.png"))); + miExportAllPCode.setActionCommand("EXPORTPCODE"); + miExportAllPCode.addActionListener(this); + + menuExportAll.add(miExportAllAS); + menuExportAll.add(miExportAllPCode); + + + JMenu menuExportSel = new JMenu("Export selection"); + JMenuItem miExportSelAS = new JMenuItem("ActionScript..."); + miExportSelAS.setIcon(new ImageIcon(View.loadImage("com/jpexs/asdec/gui/graphics/exportas16.png"))); + miExportSelAS.setActionCommand("EXPORTSEL"); + miExportSelAS.addActionListener(this); + + JMenuItem miExportSelPCode = new JMenuItem("PCode..."); + miExportSelPCode.setIcon(new ImageIcon(View.loadImage("com/jpexs/asdec/gui/graphics/exportpc16.png"))); + miExportSelPCode.setActionCommand("EXPORTPCODESEL"); + miExportSelPCode.addActionListener(this); + + menuExportSel.add(miExportSelAS); + menuExportSel.add(miExportSelPCode); + - JMenuItem miExportPCode = new JMenuItem("Export as PCode..."); - miExportPCode.setIcon(new ImageIcon(View.loadImage("com/jpexs/asdec/gui/graphics/exportpc16.png"))); - miExportPCode.setActionCommand("EXPORTPCODE"); - miExportPCode.addActionListener(this); menuFile.add(miOpen); menuFile.add(miSave); menuFile.add(miSaveAs); - menuFile.add(miExport); - menuFile.add(miExportPCode); + menuFile.add(menuExportAll); + menuFile.add(menuExportSel); menuFile.addSeparator(); JMenuItem miClose = new JMenuItem("Exit"); miClose.setIcon(new ImageIcon(View.loadImage("com/jpexs/asdec/gui/graphics/exit16.png"))); @@ -470,7 +493,7 @@ public class MainFrame extends JFrame implements ActionListener, ItemListener { } - if (e.getActionCommand().equals("EXPORT") || e.getActionCommand().equals("EXPORTPCODE")) { + if (e.getActionCommand().startsWith("EXPORT")) { JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new java.io.File((String) Configuration.getConfig("lastExportDir", "."))); chooser.setDialogTitle("Select directory to export"); @@ -480,12 +503,25 @@ public class MainFrame extends JFrame implements ActionListener, ItemListener { Main.startWork("Exporting..."); final String selFile = chooser.getSelectedFile().getAbsolutePath(); Configuration.setConfig("lastExportDir", chooser.getSelectedFile().getParentFile().getAbsolutePath()); - final boolean isPcode = e.getActionCommand().equals("EXPORTPCODE"); + final boolean isPcode = e.getActionCommand().startsWith("EXPORTPCODE"); + final boolean onlySel = e.getActionCommand().endsWith("SEL"); (new Thread() { @Override public void run() { try { - Main.swf.exportActionScript(selFile, isPcode); + if (onlySel) { + List tlsList = classTree.getSelectedScripts(); + if(tlsList.isEmpty()){ + JOptionPane.showMessageDialog(null, "No script selected!"); + } + for (int i = 0; i < tlsList.size(); i++) { + TreeLeafScript tls = tlsList.get(i); + Main.startWork("Exporting " + (i + 1) + "/" + tlsList.size() + " " + tls.abc.script_info[tls.scriptIndex].getPath(tls.abc) + " ..."); + tls.abc.script_info[tls.scriptIndex].export(tls.abc, list, selFile, isPcode); + } + } else { + Main.swf.exportActionScript(selFile, isPcode); + } } catch (Exception ignored) { JOptionPane.showMessageDialog(null, "Cannot write to the file"); } diff --git a/trunk/src/com/jpexs/asdec/abc/gui/Tree.java b/trunk/src/com/jpexs/asdec/abc/gui/Tree.java index a17ff92cf..d09ae628c 100644 --- a/trunk/src/com/jpexs/asdec/abc/gui/Tree.java +++ b/trunk/src/com/jpexs/asdec/abc/gui/Tree.java @@ -20,7 +20,7 @@ import java.util.StringTokenizer; public class Tree { - private final TreeElement ROOT = new TreeElement("", "", -1, null); + private final TreeElement ROOT = new TreeElement("", "", null, null); public void add(String name, String path, Object item) { StringTokenizer st = new StringTokenizer(path, "."); diff --git a/trunk/src/com/jpexs/asdec/abc/types/ScriptInfo.java b/trunk/src/com/jpexs/asdec/abc/types/ScriptInfo.java index f40124060..aa2c4cf64 100644 --- a/trunk/src/com/jpexs/asdec/abc/types/ScriptInfo.java +++ b/trunk/src/com/jpexs/asdec/abc/types/ScriptInfo.java @@ -20,6 +20,9 @@ import com.jpexs.asdec.abc.ABC; import com.jpexs.asdec.abc.types.traits.Trait; import com.jpexs.asdec.abc.types.traits.Traits; import com.jpexs.asdec.tags.DoABCTag; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -51,4 +54,19 @@ public class ScriptInfo { public String convert(List abcTags, ABC abc, boolean pcode, boolean highlighting) { return traits.convert(abcTags, abc, false, pcode, true, -1, highlighting, new ArrayList()); } + + public void export(ABC abc, List abcList, String directory, boolean pcode) throws IOException { + String path = getPath(abc); + String packageName = path.substring(0, path.lastIndexOf(".")); + String className = path.substring(path.lastIndexOf(".") + 1); + File outDir = new File(directory + File.separatorChar + packageName.replace('.', File.separatorChar)); + if (!outDir.exists()) { + outDir.mkdirs(); + } + String fileName = outDir.toString() + File.separator + className + ".as"; + FileOutputStream fos = new FileOutputStream(fileName); + fos.write(convert(abcList, abc, pcode, false).getBytes()); + fos.close(); + + } }