Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to the project to allow for Filter directory selection #50

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified imagelab.jar
Binary file not shown.
13 changes: 11 additions & 2 deletions imagelab/DisplayImage.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package imagelab;

import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import java.awt.Image;
import java.awt.Color;
import java.awt.image.MemoryImageSource;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FileDialog;

/**
* Graphics frame used to display an image.
Expand Down Expand Up @@ -45,6 +49,10 @@ public class DisplayImage extends ILFrame {
/** y-coordinate for next window. */
private static int yspot = YMAX;

/** Accessible menu bar for DisplayImage. */
private static JMenuBar myMenuBar;


/**
* This constructor takes the image object to display
* and a string to use as the title of the window.
Expand Down Expand Up @@ -104,11 +112,12 @@ public DisplayImage(
setBounds(xspot, yspot, width, height + EXTRA_HEIGHT);
WindowCloser wc = new WindowCloser(this);
this.addWindowListener(wc);
JMenuBar myMenuBar = new JMenuBar();

myMenuBar = new JMenuBar();
myMenuBar.add(ImageLab.newFileMenu(this));
myMenuBar.add(ImageLab.newFilterMenu());

setJMenuBar(myMenuBar);
setVisible(true);
}

} //class
132 changes: 82 additions & 50 deletions imagelab/ImageLab.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
import java.awt.FileDialog;
import java.awt.Container;
Expand Down Expand Up @@ -41,7 +42,7 @@ public class ImageLab {
/** Holds all open images (ImgProvider objects). */
private static List<ImgProvider> images = new ArrayList<ImgProvider>();

/** The directory that holds filter classes. (TODO) */
/** The directory that holds filter classes. */
private static String filterDir = FILTER_DIR;

/** Holds the actual filter objects. */
Expand Down Expand Up @@ -149,6 +150,11 @@ private JMenuBar buildMenus() {
JMenuItem save = new JMenuItem("Save", 'S');
file.add(save);
save.addActionListener(makeSaveListener());

JMenuItem findFilter = new JMenuItem("Configure Filter Directory", 'F');
file.add(findFilter);
findFilter.addActionListener(makeOpenFilterDirListener());

JMenuItem quit = new JMenuItem("Quit", 'Q');
file.add(quit);
quit.addActionListener(new ActionListener() {
Expand All @@ -158,17 +164,62 @@ public void actionPerformed(
}
}
);
JMenu filter = new JMenu("Filter");
mbar.add(filter);

mbar.add(newFilterMenu());

return mbar;
} //buildMenus

/**
* Return a File menu for a single image.
* Provides "Play", "Save" and "Close".
*
* @param who Display Image used for Action Event
* @return the new File menu
*/
public static JMenu newFileMenu(final DisplayImage who) {
JMenu fileMenu = new JMenu("File");
// JMenuItem open = new JMenuItem("Open",'O');
// fileMenu.add(open);
// open.addActionListener(makeOpenListener());
JMenuItem play = new JMenuItem("Play", 'P');
fileMenu.add(play);
play.addActionListener(makePlayListener());
JMenuItem save = new JMenuItem("Save", 'S');
fileMenu.add(save);
save.addActionListener(makeSaveListener());
JMenuItem close = new JMenuItem("Close", 'C');
fileMenu.add(close);
close.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent actev) {
who.setVisible(false);
who.byebye();
}
});
return fileMenu;
}

/**
* Return a Filter menu with the filters found in the location selected.
* Provides access to all filters.
*
* @return the new Filter menu
*/
public static JMenu newFilterMenu() {

//Find filters and build corresponding menu items.
//Look at each .class file in filterDir.
//Do the classForName stuff and enter into filter menu.
//System.out.println("**********finding filters***********");
FileSystemView fsv = FileSystemView.getFileSystemView();
//File [] fil = fsv.getFiles(new File("."),true);
File[] fil = fsv.getFiles(new File(filterDir), true);

//System.out.println("Found " + fil.length + " possible filters");
String clName = " "; //holds name of class

JMenu filter = new JMenu("Filter");

for (int k = 0; k < fil.length; k++) {
if (fil[k].getName().endsWith("class")) {
Class<?> cl;
Expand All @@ -180,7 +231,7 @@ public void actionPerformed(
clName = "filters." + clName.substring(0, spot);
//System.out.println("Trying: " + clName);
cl = Class.forName(clName);
//System.out.println("Class for name is: " + cl);
System.out.println("Class for name is: " + cl);
Class<?>[] interfaces = cl.getInterfaces();
boolean isFilter = false;
for (int j = 0; j < interfaces.length; j++) {
Expand All @@ -203,52 +254,7 @@ public void actionPerformed(

} //for k

return mbar;
} //buildMenus

/**
* Return a File menu for a single image.
* Provides "Play", "Save" and "Close".
*
* @param who Display Image used for Action Event
* @return the new File menu
*/
public static JMenu newFileMenu(final DisplayImage who) {
JMenu fileMenu = new JMenu("File");
// JMenuItem open = new JMenuItem("Open",'O');
// fileMenu.add(open);
// open.addActionListener(makeOpenListener());
JMenuItem play = new JMenuItem("Play", 'P');
fileMenu.add(play);
play.addActionListener(makePlayListener());
JMenuItem save = new JMenuItem("Save", 'S');
fileMenu.add(save);
save.addActionListener(makeSaveListener());
JMenuItem close = new JMenuItem("Close", 'C');
fileMenu.add(close);
close.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent actev) {
who.setVisible(false);
who.byebye();
}
});
return fileMenu;
}

/**
* Return a Filter menu for a single image.
* Provides access to all filters.
*
* @return the new Filter menu
*/
public static JMenu newFilterMenu() {
JMenu filterMenu = new JMenu("Filter");
for (int i = 0; i < filters.size(); i++) {
JMenuItem jmi = new JMenuItem(filters.get(i).getMenuLabel());
filterMenu.add(jmi);
jmi.addActionListener(makeActionListener(filters.get(i)));
}
return filterMenu;
return filter;
}

/**
Expand All @@ -275,6 +281,32 @@ public void actionPerformed(final ActionEvent ev) {
};
} //makeActionListener

/**
* Create an ActionListener for selecting the filter directory.
*
* @return the ActionListener used to perform opening the filter directory
*/
public static ActionListener makeOpenFilterDirListener() {
return new ActionListener() {
public void actionPerformed(final ActionEvent e) {
JFileChooser fd;
fd = new JFileChooser();
fd.setDialogTitle("Pick Filter Folder");
fd.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

if (fd.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
filterDir = fd.getSelectedFile().getAbsolutePath();
}

System.out.println("Listener - The directory name is " + filterDir);
menubar.remove(1);
menubar.add(newFilterMenu());
frame.setJMenuBar(menubar);

} //actionPerformed
};
} // makeOpenFilterDirListener

/**
* Create an ActionListener for opening an image file.
*
Expand Down