001 package org.shiftone.jrat.desktop.action.inject; 002 003 import org.shiftone.jrat.desktop.DesktopFrame; 004 import org.shiftone.jrat.inject.Injector; 005 import org.shiftone.jrat.util.io.IOUtil; 006 import org.shiftone.jrat.util.log.Logger; 007 008 import javax.swing.*; 009 import java.awt.event.ActionEvent; 010 import java.io.File; 011 012 /** 013 * @author (jeff@shiftone.org) Jeff Drost 014 */ 015 public abstract class AbstractInjectAction extends AbstractAction { 016 017 private static final Logger LOG = Logger.getLogger(AbstractInjectAction.class); 018 private final DesktopFrame desktopFrame; 019 private final int fileSelectionMode; 020 private final String dialogTitle; 021 022 protected AbstractInjectAction(String string, DesktopFrame desktopFrame, int fileSelectionMode, String dialogTitle) { 023 super(string); 024 this.desktopFrame = desktopFrame; 025 this.fileSelectionMode = fileSelectionMode; 026 this.dialogTitle = dialogTitle; 027 } 028 029 protected abstract File getLastInjected(); 030 031 protected abstract void setLastInjected(File file); 032 033 034 public void actionPerformed(ActionEvent actionEvent) { 035 036 LOG.info("actionPerformed"); 037 038 JFileChooser chooser = new JFileChooser(); 039 File lastInjected = getLastInjected(); 040 041 chooser.setDialogTitle(dialogTitle); 042 chooser.setFileSelectionMode(fileSelectionMode); 043 chooser.setMultiSelectionEnabled(true); 044 045 if (lastInjected != null) { 046 chooser.setCurrentDirectory(IOUtil.getNearestExistingParent(lastInjected)); 047 chooser.setSelectedFile(lastInjected); 048 } 049 050 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(null)) { 051 052 File[] targets = chooser.getSelectedFiles(); 053 054 if (targets.length > 0) { 055 056 setLastInjected(targets[0]); 057 058 RunnableLogPanel logPanel = new RunnableLogPanel(); 059 060 desktopFrame.createView("Injecting", logPanel); 061 062 logPanel.run(new InjectRunnable(targets)); 063 064 } 065 } 066 } 067 068 private class InjectRunnable implements Runnable { 069 070 private final File[] targets; 071 private final Injector injector = new Injector(); 072 073 public InjectRunnable(File[] targets) { 074 this.targets = targets; 075 } 076 077 private void inject() { 078 for (int i = 0; i < targets.length; i++) { 079 injector.inject(targets[i]); 080 } 081 } 082 083 public void run() { 084 inject(); 085 } 086 } 087 }