Update of /cvsroot/bitcollider/jbitcollider/plugins/org.bitpedia.collider.ui/src/org/bitpedia/collider/ui In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20753/plugins/org.bitpedia.collider.ui/src/org/bitpedia/collider/ui Added Files: MainDialog.java ColliderUtils.java BrowserControl.java WidthSpring.java AppPlugin.java Log Message: initial commit from code drop of april 12 --- NEW FILE: MainDialog.java --- package org.bitpedia.collider.ui; import java.awt.BorderLayout; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.dnd.DnDConstants; import java.awt.dnd.DropTarget; import java.awt.dnd.DropTargetDragEvent; import java.awt.dnd.DropTargetDropEvent; import java.awt.dnd.DropTargetEvent; import java.awt.dnd.DropTargetListener; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.io.Reader; import java.net.URL; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedList; import java.util.List; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JProgressBar; import javax.swing.JTextArea; import javax.swing.Spring; import javax.swing.SpringLayout; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import org.bitpedia.collider.core.Bitcollider; import org.bitpedia.collider.core.Submission; import edu.stanford.ejalbert.BrowserLauncher; import edu.stanford.ejalbert.exception.BrowserLaunchingExecutionException; import edu.stanford.ejalbert.exception.BrowserLaunchingInitializingException; import edu.stanford.ejalbert.exception.UnsupportedOperatingSystemException; public class MainDialog extends JPanel implements ActionListener, DropTargetListener, Bitcollider.Progress { public static final int GAP = 6; private Collection fmtHandlers; private JFrame frame; private SpringLayout layout = new SpringLayout(); private JLabel lblCurFile = new JLabel("Current File:"); private JProgressBar pbCurFile = new JProgressBar(0, 100); private JLabel lblOverallProgress = new JLabel("Overall progress:"); private JProgressBar pbOverallProgress = new JProgressBar(0, 100); private JLabel lblFilesToProcess = new JLabel("0 files to process", SwingConstants.CENTER); private JLabel lblFilesProcessed = new JLabel("0 files processed", SwingConstants.CENTER); private JLabel lblFilesSkipped = new JLabel("0 files skipped", SwingConstants.CENTER); private JTextArea taHint = new JTextArea( " (1) Drag files onto this window\n" + " (2) The Bitcollider extracts identifiers and metadata\n" + " (3) Your default web browser will launch to submit the info"); private JCheckBox chbCloseWindow = new JCheckBox( "Close window after processing", false); private JCheckBox chbDontShowForSmall = new JCheckBox( "Don't show window for small files", true); private JButton btnAbout = new JButton("About"); private JButton btnBrowse = new JButton("Browse"); private JButton btnClose = new JButton("Close"); private Spring sprPnlWidth = new WidthSpring(this); private JFileChooser fileChooser = new JFileChooser(); private int filesSkipped = 0; private int filesProcessed = 0; private int filesToProcess = 0; private boolean noSubmitting = false; private String alternateUrl; private boolean md5 = false; private boolean crc32 = false; public MainDialog(JFrame frame) { this.frame = frame; fileChooser.setMultiSelectionEnabled(true); setLayout(layout); add(lblCurFile); layout.putConstraint(SpringLayout.NORTH, lblCurFile, GAP, SpringLayout.NORTH, this); layout.putConstraint(SpringLayout.WEST, lblCurFile, GAP, SpringLayout.WEST, this); add(pbCurFile); layout.putConstraint(SpringLayout.EAST, pbCurFile, -GAP, SpringLayout.EAST, this); layout.putConstraint(SpringLayout.WEST, pbCurFile, GAP, SpringLayout.WEST, this); layout.putConstraint(SpringLayout.NORTH, pbCurFile, GAP, SpringLayout.SOUTH, lblCurFile); add(lblOverallProgress); layout.putConstraint(SpringLayout.NORTH, lblOverallProgress, GAP, SpringLayout.SOUTH, pbCurFile); layout.putConstraint(SpringLayout.WEST, lblOverallProgress, GAP, SpringLayout.WEST, this); add(pbOverallProgress); layout.putConstraint(SpringLayout.EAST, pbOverallProgress, -GAP, SpringLayout.EAST, this); layout.putConstraint(SpringLayout.WEST, pbOverallProgress, GAP, SpringLayout.WEST, this); layout.putConstraint(SpringLayout.NORTH, pbOverallProgress, GAP, SpringLayout.SOUTH, lblOverallProgress); Spring lblWidth = Spring.scale(Spring.sum(sprPnlWidth, Spring .constant(-4 * GAP)), 1f / 3f); add(lblFilesToProcess); layout.putConstraint(SpringLayout.NORTH, lblFilesToProcess, GAP, SpringLayout.SOUTH, pbOverallProgress); layout.putConstraint(SpringLayout.WEST, lblFilesToProcess, GAP, SpringLayout.WEST, this); layout.getConstraints(lblFilesToProcess).setWidth(lblWidth); add(lblFilesProcessed); layout.putConstraint(SpringLayout.NORTH, lblFilesProcessed, GAP, SpringLayout.SOUTH, pbOverallProgress); layout.putConstraint(SpringLayout.WEST, lblFilesProcessed, GAP, SpringLayout.EAST, lblFilesToProcess); layout.getConstraints(lblFilesProcessed).setWidth(lblWidth); add(lblFilesSkipped); layout.putConstraint(SpringLayout.EAST, lblFilesSkipped, -GAP, SpringLayout.EAST, this); layout.putConstraint(SpringLayout.WEST, lblFilesSkipped, GAP, SpringLayout.EAST, lblFilesProcessed); layout.putConstraint(SpringLayout.NORTH, lblFilesSkipped, GAP, SpringLayout.SOUTH, pbOverallProgress); layout.getConstraints(lblFilesSkipped).setWidth(lblWidth); add(taHint); taHint.setEditable(false); taHint.setFocusable(false); taHint.setBackground(this.getBackground()); layout.putConstraint(SpringLayout.EAST, taHint, -GAP, SpringLayout.EAST, this); layout.putConstraint(SpringLayout.WEST, taHint, GAP, SpringLayout.WEST, this); layout.putConstraint(SpringLayout.NORTH, taHint, GAP, SpringLayout.SOUTH, lblFilesToProcess); add(chbCloseWindow); layout.putConstraint(SpringLayout.NORTH, chbCloseWindow, GAP, SpringLayout.SOUTH, taHint); layout.putConstraint(SpringLayout.WEST, chbCloseWindow, GAP, SpringLayout.WEST, this); add(chbDontShowForSmall); layout.putConstraint(SpringLayout.NORTH, chbDontShowForSmall, GAP, SpringLayout.SOUTH, chbCloseWindow); layout.putConstraint(SpringLayout.WEST, chbDontShowForSmall, GAP, SpringLayout.WEST, this); Spring btnWidth = Spring.scale(Spring.sum(sprPnlWidth, Spring .constant(-4 * GAP)), 1f / 3f); add(btnAbout); btnAbout.addActionListener(this); layout.putConstraint(SpringLayout.NORTH, btnAbout, GAP, SpringLayout.SOUTH, chbCloseWindow); layout.putConstraint(SpringLayout.WEST, btnAbout, GAP, SpringLayout.WEST, this); layout.putConstraint(SpringLayout.SOUTH, btnAbout, -GAP, SpringLayout.SOUTH, this); layout.getConstraints(btnAbout).setWidth(btnWidth); add(btnBrowse); btnBrowse.addActionListener(this); layout.putConstraint(SpringLayout.NORTH, btnBrowse, GAP, SpringLayout.SOUTH, chbCloseWindow); layout.putConstraint(SpringLayout.WEST, btnBrowse, GAP, SpringLayout.EAST, btnAbout); layout.putConstraint(SpringLayout.SOUTH, btnBrowse, -GAP, SpringLayout.SOUTH, this); layout.getConstraints(btnBrowse).setWidth(btnWidth); add(btnClose); btnClose.addActionListener(this); layout.putConstraint(SpringLayout.EAST, btnClose, -GAP, SpringLayout.EAST, this); layout.putConstraint(SpringLayout.NORTH, btnClose, GAP, SpringLayout.SOUTH, chbCloseWindow); layout.putConstraint(SpringLayout.WEST, btnClose, GAP, SpringLayout.EAST, btnBrowse); layout.putConstraint(SpringLayout.SOUTH, btnClose, -GAP, SpringLayout.SOUTH, this); layout.getConstraints(btnClose).setWidth(btnWidth); new DropTarget(this, this); new DropTarget(taHint, this); } public void configure(boolean noSubmitting, String alternateUrl, boolean md5, boolean crc32) { this.noSubmitting = noSubmitting; this.alternateUrl = alternateUrl; this.md5 = md5; this.crc32 = crc32; } public static void main(String[] args) { JFrame frame = new JFrame("jBitcollider"); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add("Center", new MainDialog(frame)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(450, 300); frame.setResizable(false); frame.setLocationRelativeTo(null); frame.setVisible(true); } public void actionPerformed(ActionEvent event) { if (event.getSource() == btnAbout) { JOptionPane .showMessageDialog( null, "jBitcollider 0.1.0 \n" + "(PD) 2006 The Bitzi Corporation\n\n" + "For more information about the jBitcollider and Bitzi's Free\n" + "Universal Media Catalog , please visit http://bitzi.com", "About", JOptionPane.INFORMATION_MESSAGE); } else if (event.getSource() == btnBrowse) { int ret = fileChooser.showOpenDialog(frame); if(ret == JFileChooser.APPROVE_OPTION) { File[] selFiles = fileChooser.getSelectedFiles(); List files = new LinkedList(); for (int i = 0; i < selFiles.length; i++) { files.add(selFiles[i].getPath()); } executeBitcollider(files); } } else if (event.getSource() == btnClose) { System.exit(0); } } public void setSize(int width, int height) { super.setSize(width, height); System.out.println("setSize: " + width + ", " + height); } private void processDtde(DropTargetDragEvent dtde) { DataFlavor[] flavors = dtde.getTransferable().getTransferDataFlavors(); for (int i = 0; i < flavors.length; i++) { if (flavors[i].isFlavorJavaFileListType()) { dtde.acceptDrag(DnDConstants.ACTION_MOVE); return; } else if (flavors[i].getMimeType().startsWith("text/uri-list") && flavors[i].getRepresentationClass() == Reader.class) { dtde.acceptDrag(DnDConstants.ACTION_MOVE); return; } } dtde.rejectDrag(); } public void dragEnter(DropTargetDragEvent dtde) { processDtde(dtde); } public void dragOver(DropTargetDragEvent dtde) { processDtde(dtde); } public void dropActionChanged(DropTargetDragEvent dtde) { processDtde(dtde); } public void dragExit(DropTargetEvent dtde) { } private void executeBitcollider(final List files) { final Bitcollider bc = new Bitcollider(fmtHandlers); bc.setCalcCrc32(crc32); bc.setCalcMd5(md5); bc.setPreview(true); Submission prevSub = bc.generateSubmission(files, null, true); filesToProcess = prevSub.getNumBitprints(); lblFilesToProcess.setText(""+filesToProcess+" files to process"); Thread bt = new Thread(new Runnable() { public void run() { try { bc.setPreview(false); bc.setProgress(MainDialog.this); Submission submission = bc.generateSubmission(files, null, true); String tmpdir = System.getProperty("java.io.tmpdir"); String sep = System.getProperty("file.separator"); if ((null != tmpdir) && (!"".equals(tmpdir)) && (!tmpdir.endsWith(sep))) { tmpdir = tmpdir + sep; } String htmlFileName = tmpdir + "bitprint.htm"; PrintWriter pw = new PrintWriter(htmlFileName); try { submission.setAutoSubmit(!noSubmitting); submission.makeHtml(pw, alternateUrl); } finally { pw.close(); } // BrowserControl.displayURL("file://localhost/" // + htmlFileName); BrowserLauncher bl = new BrowserLauncher(null); bl.openURLinBrowser("file://localhost/"+htmlFileName); SwingUtilities.invokeLater(new Runnable() { public void run() { pbCurFile.setValue(0); pbOverallProgress.setValue(0); lblFilesToProcess.setText("0 files to process"); } }); } catch (FileNotFoundException e) { } catch (BrowserLaunchingInitializingException e) { } catch (UnsupportedOperatingSystemException e) { } catch (BrowserLaunchingExecutionException e) { } } }); bt.start(); } public String filenameFromUrl(URL url) { String filename = url.getFile(); int lastSlash = url.getPath().lastIndexOf("/"); if(lastSlash==filename.length()) { filename = filename.substring(0,filename.length()-1); lastSlash = url.getPath().lastIndexOf("/"); } if(lastSlash>0) { filename = filename.substring(lastSlash+1); } return filename; } public void drop(DropTargetDropEvent dtde) { DataFlavor[] flavors = dtde.getTransferable().getTransferDataFlavors(); Transferable tr = dtde.getTransferable(); for (int i = 0; i < flavors.length; i++) { if (flavors[i].isFlavorJavaFileListType()) { dtde.acceptDrop(DnDConstants.ACTION_MOVE); try { List files = (List) tr.getTransferData(flavors[i]); executeBitcollider(files); } catch (UnsupportedFlavorException ufe) { } catch (IOException e) { } return; } else if (flavors[i].getMimeType().startsWith("text/uri-list") && flavors[i].getRepresentationClass() == Reader.class) { dtde.acceptDrop(DnDConstants.ACTION_MOVE); try { BufferedReader reader = new BufferedReader((Reader)tr.getTransferData(flavors[i])); String line; List files = new ArrayList(); while((line = reader.readLine()) != null) { files.add((new URL(line.trim())).getFile()); } executeBitcollider(files); } catch (IOException e) { } catch (UnsupportedFlavorException e) { // TODO Auto-generated catch block e.printStackTrace(); } return; } } dtde.rejectDrop(); } public void setFmtHandlers(Collection fmtHandlers) { this.fmtHandlers = fmtHandlers; } private void updateProgress(int percent, String fileName, String message) { if (0 == percent) { if (null != message) { pbCurFile.setValue(0); filesSkipped++; lblFilesSkipped.setText(""+filesSkipped+" files skipped"); } else { pbCurFile.setValue(0); } } else if (percent <= 100) { pbCurFile.setValue(percent); if (null != message) { filesProcessed++; lblFilesProcessed.setText(""+filesProcessed+" files processed"); pbOverallProgress.setValue(100 * filesProcessed / filesToProcess); } } } public void progress(final int percent, final String fileName, final String message) { if (SwingUtilities.isEventDispatchThread()) { updateProgress(percent, fileName, message); } else { try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { updateProgress(percent, fileName, message); } }); } catch (Exception e) { } } } } --- NEW FILE: ColliderUtils.java --- package org.bitpedia.collider.ui; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.logging.ConsoleHandler; import java.util.logging.FileHandler; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; import java.util.logging.SimpleFormatter; import java.util.logging.XMLFormatter; public class ColliderUtils { public static final Logger colliderLogger = Logger .getLogger("org.bitpedia.collider"); static { colliderLogger.setLevel(Level.OFF); Handler[] handlers = colliderLogger.getHandlers(); for (int i = 0; i < handlers.length; i++) { colliderLogger.removeHandler(handlers[i]); } } public static final Logger debugLogger = Logger .getLogger("org.bitpedia.collider.debugLogger"); static { InputStream stm = ClassLoader.getSystemClassLoader() .getResourceAsStream("logging.properties"); if (null != stm) { Properties props = new Properties(); try { props.load(stm); configureLogger(debugLogger, props); } catch (Throwable e) { System.err.println("Error loading logging properties."); e.printStackTrace(System.err); } } } private static void configureLogger(Logger logger, Properties props) throws IOException { String propBaseName = logger.getName().substring( colliderLogger.getName().length() + 1); String level = props.getProperty(propBaseName + ".level"); if (null != level) { Level lev = Level.parse(level); logger.setLevel(lev); if (Level.OFF.equals(lev)) { return; } } String handlerName = props.getProperty(propBaseName + ".handler"); Handler handler = null; if ("console".equals(handlerName)) { handler = new ConsoleHandler(); } else if (null != handlerName) { handler = new FileHandler(handlerName); } if (null != handler) { logger.addHandler(handler); String formatterName = props.getProperty(propBaseName + ".formatter"); if ("xml".equals(formatterName)) { handler.setFormatter(new XMLFormatter()); } else if ("simple".equals(formatterName)) { handler.setFormatter(new SimpleFormatter()); } } } } --- NEW FILE: BrowserControl.java --- package org.bitpedia.collider.ui; import java.io.IOException; /** * A simple, static class to display a URL in the system browser. * * Under Unix, the system browser is hard-coded to be 'netscape'. Netscape must * be in your PATH for this to work. This has been tested with the following * platforms: AIX, HP-UX and Solaris. * * Under Windows, this will bring up the default browser under windows, usually * either Netscape or Microsoft IE. The default browser is determined by the OS. * This has been tested under Windows 95/98/NT. * * Examples: * * BrowserControl.displayURL("http://www.javaworld.com") * * BrowserControl.displayURL("file://c:\\docs\\index.html") * * BrowserContorl.displayURL("file:///user/joe/index.html"); * * Note - you must include the url type -- either "http://" or "file://". */ public class BrowserControl { /** * Display a file in the system browser. If you want to display a file, you * must include the absolute path name. * * @param url * the file's url (the url must start with either "http://" or * "file://"). */ public static void displayURL(String url) { boolean windows = isWindowsPlatform(); String cmd = null; try { if (windows) { // cmd = 'rundll32 url.dll,FileProtocolHandler http://...' cmd = WIN_PATH + " " + WIN_FLAG + " " + url; Process p = Runtime.getRuntime().exec(cmd); } else { // Under Unix, Netscape has to be running for the "-remote" // command to work. So, we try sending the command and // check for an exit value. If the exit command is 0, // it worked, otherwise we need to start the browser. // cmd = 'netscape -remote openURL(http://www.javaworld.com)' cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")"; Process p = Runtime.getRuntime().exec(cmd); try { // wait for exit code -- if it's 0, command worked, // otherwise we need to start the browser up. int exitCode = p.waitFor(); if (exitCode != 0) { // Command failed, start up the browser // cmd = 'netscape http://www.javaworld.com' cmd = UNIX_PATH + " " + url; p = Runtime.getRuntime().exec(cmd); } } catch (InterruptedException x) { System.err.println("Error bringing up browser, cmd='" + cmd + "'"); System.err.println("Caught: " + x); } } } catch (IOException x) { // couldn't exec browser System.err.println("Could not invoke browser, command=" + cmd); System.err.println("Caught: " + x); } } /** * Try to determine whether this application is running under Windows or * some other platform by examing the "os.name" property. * * @return true if this application is running under a Windows OS */ public static boolean isWindowsPlatform() { String os = System.getProperty("os.name"); if (os != null && os.startsWith(WIN_ID)) return true; else return false; } /** * Simple example. */ public static void main(String[] args) { displayURL("http://www.javaworld.com"); } // Used to identify the windows platform. private static final String WIN_ID = "Windows"; // The default system browser under windows. private static final String WIN_PATH = "rundll32"; // The flag to display a url. private static final String WIN_FLAG = "url.dll,FileProtocolHandler"; // The default browser under unix. private static final String UNIX_PATH = "netscape"; // The flag to display a url. private static final String UNIX_FLAG = "-remote openURL"; } --- NEW FILE: WidthSpring.java --- package org.bitpedia.collider.ui; import javax.swing.JComponent; import javax.swing.Spring; public class WidthSpring extends Spring { private JComponent comp; public WidthSpring(JComponent comp) { this.comp = comp; } public int getMinimumValue() { return comp.getWidth(); } public int getPreferredValue() { return comp.getWidth(); } public int getMaximumValue() { return comp.getWidth(); } public int getValue() { return comp.getWidth(); } public void setValue(int value) { if (0 <= value) { comp.setSize(value, comp.getHeight()); } } } --- NEW FILE: AppPlugin.java --- package org.bitpedia.collider.ui; import java.awt.BorderLayout; import java.util.Iterator; import java.util.LinkedList; import java.util.Collection; import javax.swing.JFrame; import org.java.plugin.boot.Application; import org.java.plugin.boot.ApplicationPlugin; import org.java.plugin.registry.Extension; import org.java.plugin.registry.ExtensionPoint; import org.java.plugin.registry.Extension.Parameter; import org.java.plugin.util.ExtendedProperties; public class AppPlugin extends ApplicationPlugin implements Application { private JFrame frame; private MainDialog mainDlg; private Collection fmtHandlers; public AppPlugin() { frame = new JFrame("jBitcollider"); frame.getContentPane().setLayout(new BorderLayout()); mainDlg = new MainDialog(frame); frame.getContentPane().add("Center", mainDlg); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(450, 300); frame.setResizable(false); frame.setLocationRelativeTo(null); } protected Application initApplication(ExtendedProperties props, String[] args) throws Exception { boolean noSubmitting = false; String alternateUrl = null; boolean md5 = false; boolean crc32 = false; int i = 0; while (i < args.length) { if ("-n".equals(args[i])) { noSubmitting = true; } else if ("-u".equals(args[i])) { if (i+1 < args.length) { alternateUrl = args[i+1]; i++; } } else if ("-md5".equals(args[i])) { md5 = true; } else if ("-crc32".equals(args[i])) { crc32 = true; } i++; } mainDlg.configure(noSubmitting, alternateUrl, md5, crc32); return this; } protected void doStart() throws Exception { } protected void doStop() throws Exception { } public void startApplication() throws Exception { fmtHandlers = loadFormatHandlers(); mainDlg.setFmtHandlers(fmtHandlers); frame.setVisible(true); } public Collection loadFormatHandlers() { Collection handlers = new LinkedList(); try { String id = getManager().getPlugin("org.bitpedia.collider.core") .getDescriptor().getId(); ExtensionPoint toolExtPoint = getManager().getRegistry() .getExtensionPoint(id, "FormatHandler"); for (Iterator it = toolExtPoint.getConnectedExtensions().iterator(); it .hasNext();) { Extension ext = (Extension) it.next(); Parameter classParam = ext.getParameter("class"); ClassLoader classLoader = getManager().getPluginClassLoader(ext.getDeclaringPluginDescriptor()); Class handlerClass = classLoader.loadClass(classParam.valueAsString()); handlers.add(handlerClass.newInstance()); } } catch (Exception e) { System.out.println("Error loading plugins: "+e.getMessage()); e.printStackTrace(); } return handlers; } public Collection getFmtHandlers() { return fmtHandlers; } } |