import javax.swing.*; //This is the final package name. import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; public class view_plots extends JApplet implements ActionListener, ListSelectionListener { JList menuJList; DefaultListModel dynamicMenuList; static List all_images = new List(20, true); String[] dirs; int lastSelectedIndex = -1; Container contentPane; String currentDir = null; JLabel theImage; String[] movieFiles; int numSelected = 0; Timer timer; int delay = 400; int frameNumber = 0; public void init() { dirs = getDirsAtURL(""); for( int index = 0 ; index < dirs.length ; index++) { String dirName = dirs[index]; String[] imageFiles = getImagesAtURL(dirName); for( int file_index = 0 ; file_index < imageFiles.length ; file_index++) { String imagePath = dirName + imageFiles[file_index]; all_images.add(imagePath); } } dynamicMenuList = new DefaultListModel(); currentDir = getDateParam(); // List ALL files in the current directory if (currentDir.equals("")) { buildMenu(getDirsAtURL(currentDir)); } else { // Want empty file list at init //buildMenu(getImagesAtURL(currentDir)); String[] emptyList = {""}; buildMenu(emptyList); } // String[] tmps = {currentDir}; // buildMenu(tmps); createLayout(); //Set up a timer that calls this object's action handler. timer = new Timer(delay, this); timer.setInitialDelay(delay); timer.setCoalesce(true); } //######################################################################### // implement the button listener //######################################################################### class ButtonListener implements ActionListener { public void actionPerformed (ActionEvent acevt) { String button_action = acevt.getActionCommand(); // frameNumber = 0; if (button_action.equalsIgnoreCase("play")) { if (!timer.isRunning()) { if (currentDir != null) { timer.start(); } } } else if (button_action.equalsIgnoreCase("stop")) { timer.stop(); } else if (button_action.equalsIgnoreCase("faster")) { if (delay > 50) { delay -= 50; timer.setDelay(delay); } } else if (button_action.equalsIgnoreCase("slower")) { if (delay < 1500) { delay += 50; timer.setDelay(delay); } } else if (button_action.equalsIgnoreCase("backOne")) { timer.stop(); showPreviousImage(); } else if (button_action.equalsIgnoreCase("forwardOne")) { timer.stop(); showNextImage(); } else if (button_action.equalsIgnoreCase("reset")) { delay = 400; timer.setDelay(delay); } } } public int[] getIndicesToDisplay() { int[] indicesToDisplay; int[] selected_indices = menuJList.getSelectedIndices(); int imagesSelected = selected_indices.length; if (imagesSelected > 1) { indicesToDisplay = selected_indices; } else { DefaultListModel tempList = new DefaultListModel(); int itemsInList = dynamicMenuList.size(); for (int i = 0 ; i < itemsInList ; i++) { String menuItem = (String)dynamicMenuList.getElementAt(i); String i_string = "" + i; if (isImage(menuItem)) { tempList.addElement(i_string); } } indicesToDisplay = new int[tempList.size()]; for (int i = 0 ; i < tempList.size() ; i++) { int temp_int = stringToInt((String)tempList.getElementAt(i)); indicesToDisplay[i] = temp_int; } } return indicesToDisplay; } public void showNextImage() { frameNumber++; int[] selected_indices = getIndicesToDisplay(); numSelected = selected_indices.length; if (frameNumber >= numSelected) { frameNumber = 0; } int fileIndex = selected_indices[frameNumber]; String file_name = (String)dynamicMenuList.get(fileIndex); String file_path = currentDir + file_name; ImageIcon temp_icon = createImageIcon(file_path); theImage.setIcon(temp_icon); } public void showPreviousImage() { frameNumber--; int[] selected_indices = getIndicesToDisplay(); numSelected = selected_indices.length; if (frameNumber < 0) { frameNumber = numSelected; } int fileIndex = selected_indices[frameNumber]; String file_name = (String)dynamicMenuList.get(fileIndex); String file_path = currentDir + file_name; ImageIcon temp_icon = createImageIcon(file_path); theImage.setIcon(temp_icon); } //###################################################################### //###################################################################### public void actionPerformed(ActionEvent e) { //Advance the animation frame. showNextImage(); } //###################################################################### //###################################################################### public void pause (int time) { try { Thread.sleep(time); } catch (InterruptedException e) {} } //############################################################# //############################################################# public Object[] getSelectedMenuItems() { int[] selected_indexes = menuJList.getSelectedIndices(); int maxIndex = selected_indexes.length - 1; if(maxIndex < 0) { Object[] temp = {""}; return temp; } Object[] selected_items = new String[maxIndex]; // String[] selected_items = new String[4]; // String[] selected_items = {"one", "two", "three"}; for (int i = 0 ; i <= maxIndex ; i++) { // Object tempObj = dynamicMenuList.getElementAt(selected_indexes[i]); selected_items[i] = dynamicMenuList.getElementAt(selected_indexes[i]); } return selected_items; } //########################################################################### // takes an array from the menu and returs an array that conatins only files //########################################################################### public String[] filterFiles(String[] allItems) { List fileList = new List(20, true); for (int i = 0 ; i < allItems.length ; i++) { String item = allItems[i]; if (isFile(item)) { fileList.add(item); } } String[] allFiles = fileList.getItems(); return allFiles; } //######################################################################### // implement valueChanged for the list item listener //######################################################################### public void removeAll(int indx) { // int first = menuJList.getMinSelectionIndex(); // int last = menuJList.getMaxSelectionIndex(); menuJList.removeSelectionInterval(indx, indx); } public void valueChanged(ListSelectionEvent e) { /* if (e.getValueIsAdjusting()) { return; } */ int selectedIndex = menuJList.getSelectedIndex(); String selected = (String)menuJList.getSelectedValue(); if (isImage(selected)) { if (timer.isRunning()) { } else { String imagePath = currentDir + selected; theImage.setIcon(createImageIcon(imagePath)); /* int[] temp_indices = menuJList.getSelectedIndices(); int temp_num_selected = temp_indices.length; if(temp_num_selected ==1) { lastSelectedIndex = selectedIndex; if (selectedIndex == lastSelectedIndex) { // dynamicMenuList.addElement("ADDED"+temp_num_selected); removeAll(selectedIndex); } } */ } } else if (isDir(selected)) { currentDir = selected; movieFiles = getImagesAtURL(selected); movieFiles = filterFiles(movieFiles); buildMenu(movieFiles); // } else if (selected.equalsIgnoreCase("view a different date") ) { // currentDir = null; // timer.stop(); // buildMenu(dirs); } else if (selected.equalsIgnoreCase("average & max") || selected.equalsIgnoreCase("average & sdev") || selected.equalsIgnoreCase("select an applet:") || selected.equalsIgnoreCase("skew & num") ) { String[] filteredImages = imagesByType(selected); movieFiles = filterFiles(filteredImages); buildMenu(movieFiles); // Clear Image Area theImage.setIcon(createImageIcon("")); } } //###################################################################### // given the selection field, returns an array of all files that // are of the correct plot type //###################################################################### public String[] imagesByType (String plotSelection) { String filterType = ""; String fname = ""; if (plotSelection.equalsIgnoreCase("average & max")) { filterType = "REFL_PARAM"; } else if (plotSelection.equalsIgnoreCase("average & sdev")) { filterType = "Sdev"; } else if (plotSelection.equalsIgnoreCase("skew & num")) { filterType = "Skew_N"; } else if (plotSelection.equalsIgnoreCase("select an applet:")) { String[] filteredImages = {""}; return filteredImages; } // check the list for the specified plotType and display appropriately List filteredList = new List(20, true); String[] allImages = all_images.getItems(); for (int currImage = 0; currImage < allImages.length; currImage++) { if ( (allImages[currImage].indexOf(filterType) != -1) && (allImages[currImage].indexOf(currentDir) != -1) ) { fname = allImages[currImage]; fname = fname.substring(fname.lastIndexOf("/") + 1); filteredList.add(fname); } } String[] filteredImages = filteredList.getItems(); return filteredImages; } //############################################################ //############################################################ public void createLayout() { //##### // Define the window //##### contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); //unnecessary //##### // Create the movie buttons //##### JButton playButton = new JButton("Play"); JButton stopButton = new JButton("Stop"); JButton fasterButton = new JButton("Faster"); JButton slowerButton = new JButton("Slower"); JButton backOneButton = new JButton("Back One"); JButton forwardOneButton = new JButton("Forward One"); JButton resetButton = new JButton("Reset"); playButton.setActionCommand("play"); playButton.addActionListener(new ButtonListener()); stopButton.setActionCommand("stop"); stopButton.addActionListener(new ButtonListener()); fasterButton.setActionCommand("faster"); fasterButton.addActionListener(new ButtonListener()); slowerButton.setActionCommand("slower"); slowerButton.addActionListener(new ButtonListener()); backOneButton.setActionCommand("backOne"); backOneButton.addActionListener(new ButtonListener()); forwardOneButton.setActionCommand("forwardOne"); forwardOneButton.addActionListener(new ButtonListener()); resetButton.setActionCommand("reset"); resetButton.addActionListener(new ButtonListener()); JPanel movieButtons = new JPanel(); movieButtons.setLayout(new FlowLayout()); movieButtons.setBackground(Color.blue); movieButtons.add(playButton); movieButtons.add(stopButton); movieButtons.add(fasterButton); movieButtons.add(slowerButton); movieButtons.add(backOneButton); movieButtons.add(forwardOneButton); movieButtons.add(resetButton); contentPane.add(movieButtons, BorderLayout.NORTH); //##### // Create the menu panel to the right //##### menuJList = new JList(dynamicMenuList); menuJList.setSelectedIndex(0); menuJList.setFixedCellWidth(130); menuJList.setVisibleRowCount(25); // menuJList.addListSelectionListener(new SharedListSelectionHandler()); menuJList.addListSelectionListener(this); menuJList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); JPanel menuPanel = new JPanel(); menuPanel.setBackground(Color.blue); JScrollPane menuScrollPane = new JScrollPane(menuJList); menuPanel.add(menuScrollPane); contentPane.add(menuPanel, BorderLayout.EAST); //##### // Create the picture area //##### theImage = new JLabel(); JPanel imagePanel = new JPanel(); imagePanel.setBackground(Color.white); imagePanel.add(theImage); contentPane.add(imagePanel, BorderLayout.CENTER); } //############################################################################ // rewrites the menu //############################################################################ public void buildMenu(String[] menuItems) { dynamicMenuList.clear(); dynamicMenuList.addElement("Select an Applet:"); dynamicMenuList.addElement("---------------------------"); dynamicMenuList.addElement("Average & Max"); dynamicMenuList.addElement("Average & SDev"); dynamicMenuList.addElement("Skew & Num"); dynamicMenuList.addElement("---------------------------"); // dynamicMenuList.addElement("View A Different Date"); // dynamicMenuList.addElement("---------------------------"); //##### //append the string we passed in //##### for (int file_index = 0 ; file_index < menuItems.length ; file_index++) { dynamicMenuList.addElement(menuItems[file_index]); } /* // add the items to the display list int numMenuItems = menu_lst.getItemCount(); for (int file_index = 0 ; file_index < numMenuItems ; file_index++) { String temp_item = menu_lst.getItem(file_index); int max_width = 25; if (temp_item.length() > max_width) { int start_index = 0; int end_index = max_width; temp_item = temp_item.substring(start_index, end_index); } display_lst.add(temp_item); } */ } //########################################################################### // given a url, retruns a string array of all image files at the url //########################################################################### public String[] getImagesAtURL (String url) { String[] allFiles = readURLDir(url); List imageList = new List(20, true); for (int i = 0 ; i < allFiles.length ; i++) { String file = allFiles[i]; if (isImage(file)) { imageList.add(file); } } String[] imageFiles = imageList.getItems(); return imageFiles; } //############################################################################ // given a url, retruns a string array of all directories at the url //############################################################################ public String[] getDirsAtURL (String url) { String[] allFiles = readURLDir(url); List dirList = new List(20, true); for (int i = 0 ; i < allFiles.length ; i++) { String file = allFiles[i]; if (isDir(file)) { dirList.add(file); } } String[] dirFiles = dirList.getItems(); return dirFiles; } //########################################################################### // given a url, check to see if it's a directorry and return a String[] // containing it's listsed files //########################################################################### public String[] readURLDir (String dir_string) { List dirFiles = new List(20, true); BufferedReader contents; try { URL dirURL = getURL(dir_string); InputStream in = dirURL.openStream(); contents = new BufferedReader(new InputStreamReader(in)); String line; int i = 0; while ((line = contents.readLine()) != null) { line = findLink(line); if (! line.equals("") ) { dirFiles.add(line); } i++; } } catch (IOException exc) {} String[] all_files = dirFiles.getItems(); return all_files; } //###################################################################### // create a new ImageIcon //###################################################################### public ImageIcon createImageIcon(String file_path) { URL imageURL = getURL(file_path); ImageIcon icon = new ImageIcon(imageURL); return icon; } //###################################################################### // Create a new URL object //###################################################################### public URL getURL(String filename) { URL codeBase = getCodeBase(); URL url = null; try { url = new URL(codeBase, filename); } catch (java.net.MalformedURLException e) { System.err.println("Couldn't create URL: " + "badly specified URL" + url); return null; } return url; } public int getHeight () { String height = getParameter("crop_height"); int h = stringToInt(height); return h; } public int getWidth () { String width = getParameter("crop_width"); int w = stringToInt(width); return w; } public int stringToInt (String str) { int i = 0; try { i = Integer.parseInt(str); } catch (NumberFormatException e) { } return i; } //############################################################# // parse out the link from an HTML HREF tag //############################################################# public String findLink(String line) { String file_name = ""; String startTag = "HREF=\""; String endTag = "\""; int index = line.indexOf(startTag); if (index < 1) { startTag = "href=\""; index = line.indexOf(startTag); } if (index < 1) { // file_name = "NO LINK" + index; file_name = ""; } else { int start_index = line.indexOf(startTag); start_index += 6; file_name = line.substring(start_index, line.length()); int end_index = file_name.indexOf(endTag);; file_name = file_name.substring(0, end_index); } return file_name; } //############################################################# // determine if a string passed is a file //############################################################# public boolean isFile(String line) { boolean isFile = false; if (isImage(line) ){ isFile = true; } return isFile; } //############################################################# // given a files name, return boolean if it looks like an image //############################################################# public boolean isImage (String fileName) { boolean isImage = false; if ((fileName.indexOf(".jpg") != -1) || (fileName.indexOf(".JPG") != -1) || (fileName.indexOf(".jpeg") != -1) || (fileName.indexOf(".JPEG") != -1) || (fileName.indexOf(".gif") != -1) || (fileName.indexOf(".GIF") != -1)) { isImage = true; } if ((fileName.indexOf("back.gif") != -1) ) { isImage = false; } return(isImage); } //############################################################# // detemines if a string looks like a directory //############################################################# public boolean isDir(String fileName) { boolean isDir = false; int end_index = fileName.length(); String lastChar = fileName.substring(end_index-1, end_index); if (lastChar.equals("/") ){ if (fileName.indexOf("0") != -1) { isDir = true; } } return isDir; } //###################################################################### // This is a hack to avoid an ugly error message in 1.1. //###################################################################### public view_plots() { getRootPane().putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE); } //######################################################################### // over-riding the update method prevents flickering //######################################################################### public void update(Graphics g) { paint(g); } //###################################################################### // //###################################################################### public String getDateParam () { String date = getParameter("start_dir"); return date; } private final static char base64Array [] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; public String base64Encode (String string) { String encodedString = ""; byte bytes [] = string.getBytes (); int i = 0; int pad = 0; while (i < bytes.length) { byte b1 = bytes [i++]; byte b2; byte b3; if (i >= bytes.length) { b2 = 0; b3 = 0; pad = 2; } else { b2 = bytes [i++]; if (i >= bytes.length) { b3 = 0; pad = 1; } else b3 = bytes [i++]; } byte c1 = (byte)(b1 >> 2); byte c2 = (byte)(((b1 & 0x3) << 4) | (b2 >> 4)); byte c3 = (byte)(((b2 & 0xf) << 2) | (b3 >> 6)); byte c4 = (byte)(b3 & 0x3f); encodedString += base64Array [c1]; encodedString += base64Array [c2]; switch (pad) { case 0: encodedString += base64Array [c3]; encodedString += base64Array [c4]; break; case 1: encodedString += base64Array [c3]; encodedString += "="; break; case 2: encodedString += "=="; break; } } return encodedString; } public String userNamePasswordBase64(String username, String password) { return "Basic " + base64Encode (username + ":" + password); } }