You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by al...@apache.org on 2007/09/17 20:21:41 UTC

svn commit: r576553 - in /jakarta/jmeter/trunk: src/core/org/apache/jmeter/gui/ src/core/org/apache/jmeter/gui/action/ src/core/org/apache/jmeter/gui/util/ xdocs/

Author: alf
Date: Mon Sep 17 11:21:41 2007
New Revision: 576553

URL: http://svn.apache.org/viewvc?rev=576553&view=rev
Log:
Add File->Open recent file functionality. Fix for bugzilla 31336.

Added:
    jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/action/LoadRecentProject.java
Modified:
    jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/GuiPackage.java
    jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/MainFrame.java
    jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/action/ActionNames.java
    jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/util/JMeterMenuBar.java
    jakarta/jmeter/trunk/xdocs/changes.xml

Modified: jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/GuiPackage.java
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/GuiPackage.java?rev=576553&r1=576552&r2=576553&view=diff
==============================================================================
--- jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/GuiPackage.java (original)
+++ jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/GuiPackage.java Mon Sep 17 11:21:41 2007
@@ -606,6 +606,7 @@
         getMainFrame().setTitle(JMeterUtils.getExtendedFrameTitle(testPlanFile));
         // Enable file revert action if a file is used
         getMainFrame().setFileRevertEnabled(f != null);
+        getMainFrame().setProjectFileLoaded(f);
 
 		try {
 			FileServer.getFileServer().setBasedir(testPlanFile);

Modified: jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/MainFrame.java
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/MainFrame.java?rev=576553&r1=576552&r2=576553&view=diff
==============================================================================
--- jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/MainFrame.java (original)
+++ jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/MainFrame.java Mon Sep 17 11:21:41 2007
@@ -185,6 +185,15 @@
 		menuBar.setFileRevertEnabled(enabled);
 	}
 
+    /**
+     * Specify the project file that was just loaded
+     * 
+     * @param file - the full path to the file that was loaded
+     */
+    public void setProjectFileLoaded(String file) {
+        menuBar.setProjectFileLoaded(file);
+    }
+    
 	/**
 	 * Set the menu that should be used for the Edit menu.
 	 * 

Modified: jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/action/ActionNames.java
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/action/ActionNames.java?rev=576553&r1=576552&r2=576553&view=diff
==============================================================================
--- jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/action/ActionNames.java (original)
+++ jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/action/ActionNames.java Mon Sep 17 11:21:41 2007
@@ -56,6 +56,7 @@
     public static final String LAF_PREFIX       = "laf:"; // Look and Feel prefix
     public static final String MERGE            = "merge"; // $NON-NLS-1$
     public static final String OPEN             = "open"; // $NON-NLS-1$
+    public static final String OPEN_RECENT      = "open_recent"; // $NON-NLS-1$
     public static final String PASTE            = "Paste"; // $NON-NLS-1$
     public static final String REMOTE_EXIT      = "remote_exit"; // $NON-NLS-1$
     public static final String REMOTE_EXIT_ALL  = "remote_exit_all"; // $NON-NLS-1$

Added: jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/action/LoadRecentProject.java
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/action/LoadRecentProject.java?rev=576553&view=auto
==============================================================================
--- jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/action/LoadRecentProject.java (added)
+++ jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/action/LoadRecentProject.java Mon Sep 17 11:21:41 2007
@@ -0,0 +1,247 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * 
+ */
+
+package org.apache.jmeter.gui.action;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.KeyEvent;
+import java.io.File;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+import java.util.prefs.Preferences;
+
+import javax.swing.JMenuItem;
+import javax.swing.JSeparator;
+import javax.swing.KeyStroke;
+
+/**
+ * Handles the loading of recent files, and also the content and
+ * visibility of menu items for loading the recent files
+ */
+public class LoadRecentProject extends Load {
+    /** Prefix for the user preference key */
+    private static String USER_PREFS_KEY = "recent_file_"; //$NON-NLS-1$
+    /** The number of menu items used for recent files */
+    private static final int NUMBER_OF_MENU_ITEMS = 4;
+    private static Set commands = new HashSet();
+    static {
+        commands.add(ActionNames.OPEN_RECENT);
+    }
+
+	public LoadRecentProject() {
+		super();
+	}
+
+    public Set getActionNames() {
+        return commands;
+    }
+
+	public void doAction(ActionEvent e) {
+        // We must ask the user if it is ok to close current project
+	    if (!Close.performAction(e)) return;
+        // Load the file for this recent file command
+	    loadProjectFile(e, getRecentFile(e), false);
+	}
+    
+    /**
+     * Get the recent file for the menu item
+     */
+    private File getRecentFile(ActionEvent e) {
+        JMenuItem menuItem = (JMenuItem)e.getSource();
+        // Get the preference for the recent files
+        Preferences prefs = Preferences.userNodeForPackage(LoadRecentProject.class);
+        return new File(getRecentFile(prefs, Integer.parseInt(menuItem.getName())));
+    }
+
+    /**
+     * Get the menu items to add to the menu bar, to get recent file functionality
+     * 
+     * @return a List of JMenuItem and a JSeparator, representing recent files
+     */
+    public static List getRecentFileMenuItems() {
+        LinkedList menuItems = new LinkedList();
+        // Get the preference for the recent files
+        Preferences prefs = Preferences.userNodeForPackage(LoadRecentProject.class);
+        for(int i = 0; i < NUMBER_OF_MENU_ITEMS; i++) {
+            // Create the menu item
+            JMenuItem recentFile = new JMenuItem();
+            // Use the index as the name, used when processing the action
+            recentFile.setName(Integer.toString(i));
+            recentFile.addActionListener(ActionRouter.getInstance());
+            recentFile.setActionCommand(ActionNames.OPEN_RECENT);
+            // Set the KeyStroke to use
+            int shortKey = getShortcutKey(i);
+            if(shortKey >= 0) {
+                recentFile.setAccelerator(KeyStroke.getKeyStroke(shortKey, 0));
+            }            
+            // Add the menu item
+            menuItems.add(recentFile);
+        }
+        // Add separator as the last item
+        JSeparator separator = new JSeparator();
+        separator.setVisible(false);
+        menuItems.add(separator);
+        
+        // Update menu items to reflect recent files
+        updateMenuItems(menuItems, prefs);
+        
+        return menuItems;
+    }
+    
+    /**
+     * Update the content and visibility of the menu items for recent files
+     * 
+     * @param menuItems the JMenuItem and JSeparator to update
+     * @param loadedFileName the file name of the project file that has just
+     * been loaded 
+     */
+    public static void updateRecentFileMenuItems(List menuItems, String loadedFileName) {
+        // Get the preference for the recent files
+        Preferences prefs = Preferences.userNodeForPackage(LoadRecentProject.class);
+        LinkedList newRecentFiles = new LinkedList();
+        // Check if the new file is already in the recent list
+        boolean alreadyExists = false;
+        for(int i = 0; i < NUMBER_OF_MENU_ITEMS; i++) {
+            String recentFilePath = getRecentFile(prefs, i);
+            if(!loadedFileName.equals(recentFilePath)) {
+                newRecentFiles.add(recentFilePath);
+            }
+            else {
+                alreadyExists = true;
+            }
+        }
+        // Add the new file at the start of the list
+        newRecentFiles.add(0, loadedFileName);
+        // Remove the last item from the list if it was a brand new file
+        if(!alreadyExists) {
+            newRecentFiles.removeLast();
+        }
+        // Store the recent files
+        for(int i = 0; i < NUMBER_OF_MENU_ITEMS; i++) {
+            String fileName = (String)newRecentFiles.get(i);
+            if(fileName != null) {
+                setRecentFile(prefs, i, fileName);
+            }
+        }
+        // Update menu items to reflect recent files
+        updateMenuItems(menuItems, prefs);
+    }
+
+    /**
+     * Set the content and visibility of menu items and menu separator,
+     * based on the recent file stored user preferences.
+     */
+    private static void updateMenuItems(List menuItems, Preferences prefs) {
+        // Assume no recent files
+        boolean someRecentFiles = false;
+        // Update the menu items
+        for(int i = 0; i < NUMBER_OF_MENU_ITEMS; i++) {
+            // Get the menu item
+            JMenuItem recentFile = (JMenuItem)menuItems.get(i);
+            
+            // Find and set the file for this recent file command
+            String recentFilePath = getRecentFile(prefs, i);
+            if(recentFilePath != null) {
+                File file = new File(recentFilePath);
+                recentFile.setText(getMenuItemDisplayName(file));
+                recentFile.setToolTipText(recentFilePath);
+                recentFile.setEnabled(true);
+                recentFile.setVisible(true);
+                // At least one recent file menu item is visible
+                someRecentFiles = true;
+            }
+            else {
+                recentFile.setEnabled(false);
+                recentFile.setVisible(false);
+            }
+        }
+        // If there are some recent files, we must make the separator visisble
+        // The separator is the last item in the list
+        JSeparator separator = (JSeparator)menuItems.get(menuItems.size() - 1);
+        separator.setVisible(someRecentFiles);
+    }
+
+    /**
+     * Get the name to display in the menu item, it will chop the file name
+     * if it is too long to display in the menu bar
+     */
+    private static String getMenuItemDisplayName(File file) {
+        // Limit the length of the menu text if needed
+        final int maxLength = 40;
+        String menuText = file.getName();
+        if(menuText.length() > maxLength) {
+            menuText = "..." + menuText.substring(menuText.length() - maxLength, menuText.length()); //$NON-NLS-1$
+        }
+        return menuText;        
+    }
+
+    /**
+     * Get the KeyEvent to use as shortcut key for menu item
+     */
+    private static int getShortcutKey(int index) {
+        int shortKey = -1;
+        switch(index+1) {
+            case 1:
+                shortKey = KeyEvent.VK_1;
+                break;
+            case 2:
+                shortKey = KeyEvent.VK_2;
+                break;
+            case 3:
+                shortKey = KeyEvent.VK_3;
+                break;
+            case 4:
+                shortKey = KeyEvent.VK_4;
+                break;
+            case 5:
+                shortKey = KeyEvent.VK_5;
+                break;
+            case 6:
+                shortKey = KeyEvent.VK_6;
+                break;
+            case 7:
+                shortKey = KeyEvent.VK_7;
+                break;
+            case 8:
+                shortKey = KeyEvent.VK_8;
+                break;
+            case 9:
+                shortKey = KeyEvent.VK_9;
+                break;
+            default:
+                break;
+        }
+        return shortKey;
+    }
+
+    /**
+     * Get the full path to the recent file where index 0 is the most recent
+     */
+    private static String getRecentFile(Preferences prefs, int index) {
+        return prefs.get(USER_PREFS_KEY + index, null);        
+    }
+
+    /**
+     * Set the full path to the recent file where index 0 is the most recent
+     */
+    private static void setRecentFile(Preferences prefs, int index, String fileName) {
+        prefs.put(USER_PREFS_KEY + index, fileName);
+    }
+}

Modified: jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/util/JMeterMenuBar.java
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/util/JMeterMenuBar.java?rev=576553&r1=576552&r2=576553&view=diff
==============================================================================
--- jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/util/JMeterMenuBar.java (original)
+++ jakarta/jmeter/trunk/src/core/org/apache/jmeter/gui/util/JMeterMenuBar.java Mon Sep 17 11:21:41 2007
@@ -22,8 +22,10 @@
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.Locale;
 
+import javax.swing.JComponent;
 import javax.swing.JMenu;
 import javax.swing.JMenuBar;
 import javax.swing.JMenuItem;
@@ -34,6 +36,7 @@
 import org.apache.jmeter.gui.action.ActionNames;
 import org.apache.jmeter.gui.action.ActionRouter;
 import org.apache.jmeter.gui.action.KeyStrokes;
+import org.apache.jmeter.gui.action.LoadRecentProject;
 import org.apache.jmeter.util.JMeterUtils;
 import org.apache.jmeter.util.LocaleChangeEvent;
 import org.apache.jmeter.util.LocaleChangeListener;
@@ -53,6 +56,8 @@
 
     private JMenuItem file_load;
 
+    private List file_load_recent_files;
+
     private JMenuItem file_merge;
 
     private JMenuItem file_exit;
@@ -111,6 +116,9 @@
 	private Collection remote_engine_exit;
 
 	public JMeterMenuBar() {
+		// List for recent files menu items
+        file_load_recent_files = new LinkedList();
+		// Lists for remote engines menu items
 		remote_engine_start = new LinkedList();
 		remote_engine_stop = new LinkedList();
 		remote_engine_exit = new LinkedList();
@@ -142,6 +150,12 @@
             file_revert.setEnabled(enabled);
         }
     }
+    
+    public void setProjectFileLoaded(String file) {
+        if(file_load_recent_files != null && file != null) {
+            LoadRecentProject.updateRecentFileMenuItems(file_load_recent_files, file);
+        }
+    }
 
 	public void setEditEnabled(boolean enabled) {
 		if (editMenu != null) {
@@ -489,6 +503,12 @@
 		fileMenu.add(file_save_as);
         fileMenu.add(file_revert);
         fileMenu.addSeparator();
+        // Add the recent files, which will also add a separator that is
+        // visible when needed
+        file_load_recent_files = LoadRecentProject.getRecentFileMenuItems();
+        for(Iterator i = file_load_recent_files.iterator(); i.hasNext();) {
+            fileMenu.add((JComponent)i.next());
+        }
         fileMenu.add(file_exit);
 	}
 

Modified: jakarta/jmeter/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/changes.xml?rev=576553&r1=576552&r2=576553&view=diff
==============================================================================
--- jakarta/jmeter/trunk/xdocs/changes.xml (original)
+++ jakarta/jmeter/trunk/xdocs/changes.xml Mon Sep 17 11:21:41 2007
@@ -38,6 +38,7 @@
 <ul>
 <li>Add run_gui Ant target, to package and then start the JMeter GUI from Ant</li>
 <li>Add File->Revert to easily drop the current changes and reload the project file currently loaded</li>
+<li>Bug 31366 - Remember recently opened file(s)</li>
 </ul>
 
 <!--  ===================  -->



---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org