Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Awesomeness

macrumors member
Original poster
Feb 12, 2009
73
0
Sorry, It's me and my compression program again :)

Ok, so all I have left to do is make it so that it works and decompresses when I double click on a .cmprzz file. So, I've registered the extension in the Info.plist and it works fine, but I can't seem to get my application to USE it. It's going to be hard to explain it to you, so I'll post the whole thing:


Code:
package compression;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.apple.eawt.Application;
import com.apple.eawt.ApplicationEvent;
import com.apple.eawt.ApplicationListener;



/*
 * Comprezzor.java uses these files:
 *   images/Open.gif
 *   images/Save.gif
 */

public class Comprezzor extends JPanel
			implements ActionListener, ApplicationListener {
	static private final String newline = "\n";
	JButton comprezzButton, decomprezzButton;
	JTextArea log;
	JFileChooser fc;
	ApplicationListener appListener;


	public Comprezzor() {
		super(new BorderLayout());

		//Create the log first, because the action listeners
		//need to refer to it.  We format the log here.
		log = new JTextArea(12,30);
		log.setLineWrap(true);
		log.setWrapStyleWord(true);
		log.setMargin(new Insets(5,5,5,5));
		log.setEditable(false);
		JScrollPane logScrollPane = new JScrollPane(log);
		logScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		logScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
		log.append("Welcome to Comprezzor v1.0.  This program sucks.  It might not even work anyway." + newline + newline);

		log.setCaretPosition(log.getDocument().getLength());

		//Create a file chooser
		fc = new JFileChooser();
		fc.setMultiSelectionEnabled(false);
		fc.setFileSelectionMode(JFileChooser.FILES_ONLY);



		//Create the comprezz button.
		comprezzButton = new JButton("Comprezz File...",
								 createImageIcon("images/Open.gif"));
		comprezzButton.addActionListener(this);

		//	  Create the decomprezz button.
		decomprezzButton = new JButton("Decomprezz File...",
								 createImageIcon("images/Save.gif"));
		decomprezzButton.addActionListener(this);

		//For layout purposes, put the buttons in a separate panel
		JPanel buttonPanel = new JPanel(); //use FlowLayout
		buttonPanel.add(comprezzButton);
		buttonPanel.add(decomprezzButton);

		//Add the buttons and the log to this panel.
		add(buttonPanel, BorderLayout.PAGE_START);
		add(logScrollPane, BorderLayout.CENTER);
	}
	String fileText = null;
	String filePath = null;
	String text = null;

	public void MacOSAppAdapter() {
		appListener = new MyAppEventHandler();
	}

	/** Method to register this handler with Apple's event manager, calling
	 * addApplicationListener in parent class.
	 */
	public void register() {
		addApplicationListener(appListener);
	}

	class MyAppEventHandler implements ApplicationListener {
		public void handleOpenApplication(ApplicationEvent event) {
		}

		public void handleAbout(ApplicationEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		public void handleOpenFile(ApplicationEvent event) {
			try {
				File file = event.//HELP!  How do I get the file path?!
				log.append("Now decomprezzing: " + file.getName() + "." + newline);
				filePath = file.getAbsolutePath();
				log.append("Opening file..." + newline);
				fileText = Opener.readFile(filePath);
				fileText = JDecompress.decompress(fileText);
				log.append("Decomprezzing..." + newline);
				log.append("Writing file..." + newline);
				Writer.Save(file, fileText);
				file.setName(file.getName().split(".cmprzz")[0]);
				log.append("Done!" + newline + newline);
			}
			catch (Throwable t) {
				log.append("Didn't work =(" + newline + newline);
			}
			
			
		}

		public void handlePreferences(ApplicationEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		public void handlePrintFile(ApplicationEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		public void handleQuit(ApplicationEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		public void handleReOpenApplication(ApplicationEvent arg0) {
			// TODO Auto-generated method stub
			
		}
	}


	public void actionPerformed(ActionEvent e) {
		//Handle open button action.
		if (e.getSource() == comprezzButton) {
			fc.setDialogTitle("Comprezz File");
			int returnVal = fc.showOpenDialog(Comprezzor.this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				//This is where a real application would open the file.
				try {
					File file = fc.getSelectedFile();
					log.append("Now comprezzing: " + file.getName() + "." + newline);
					fc.setCurrentDirectory(new File("."));
					fc.showOpenDialog(null);
					filePath = file.getAbsolutePath();
					fileText = Opener.readFile(filePath);
					log.append("Done!" + newline + newline);
				}
				catch (Throwable t) {
					log.append("Didn't work =(" + newline + newline);
				}
			} else {
				log.append("Comprezz command cancelled." + newline + newline);
			}
			log.setCaretPosition(log.getDocument().getLength());


		}

		//Handle save button action.
		else if (e.getSource() == decomprezzButton) {
			fc.setDialogTitle("Decomprezz File");
			int returnVal = fc.showOpenDialog(Comprezzor.this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				//This is where a real application would save the file.
				try {
					File file = fc.getSelectedFile();
					log.append("Now decomprezzing: " + file.getName() + "." + newline);
					fc.setCurrentDirectory(new File("."));
					filePath = file.getAbsolutePath();
					log.append("Opening file..." + newline);
					fileText = Opener.readFile(filePath);
					fileText = JDecompress.decompress(fileText);
					log.append("Decomprezzing..." + newline);
					log.append("Writing file..." + newline);
					Writer.Save(file, fileText);
					file.setName(file.getName().split(".cmprzz")[0]);
					log.append("Done!" + newline + newline);
				}
				catch (Throwable t) {
					log.append("Didn't work =(" + newline + newline);
				}
			} else {
				log.append("Decomprezz command cancelled." + newline + newline);
			}
			log.setCaretPosition(log.getDocument().getLength());
		}
	}

	/** Returns an ImageIcon, or null if the path was invalid. */
	protected static ImageIcon createImageIcon(String path) {
		java.net.URL imgURL = Comprezzor.class.getResource(path);
		if (imgURL != null) {
			return new ImageIcon(imgURL);
		} else {
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}

	/**
	 * Create the GUI and show it.  For thread safety,
	 * this method should be invoked from the
	 * event dispatch thread.
	 */
	private static void createAndShowGUI() {
		//Create and set up the window.
		JFrame frame = new JFrame("Comprezzor");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//Add content to the window.
		frame.add(new Comprezzor());

		//Display the window.
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(final String args[]) {
		//Schedule a job for the event dispatch thread:
		//creating and showing this application's GUI.
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				//Turn off metal's use of bold fonts
				UIManager.put("swing.boldMetal", Boolean.FALSE);
				createAndShowGUI();
			}
		});
	}

	public void handleAbout(ApplicationEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void handleOpenApplication(ApplicationEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void handleOpenFile(ApplicationEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void handlePreferences(ApplicationEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void handlePrintFile(ApplicationEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void handleQuit(ApplicationEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void handleReOpenApplication(ApplicationEvent arg0) {
		// TODO Auto-generated method stub

	}
}

Eclipse forced me to add those thingies with the TODO crap... I don't want them! I have a limited understanding of creating .app applications. This code has a few errors in it. setName doesn't work/exist either! I just want it to open double clicked .cmprzz files, decompress them, and remove the .cmprzz.

This is so hard :(
 

Burtonsnow9

macrumors regular
Feb 22, 2008
137
0
Before I look through the code to try to see what is happening, thought I would point out that the // TODO ... stubs are just put in there by Eclipse to remind you that those methods were auto-added and you may want to do stuff with them. It's more of a friendly reminder to you, but you can definitely just get rid of them.

But now I'll try to look through the code and see what is up.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.