We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
My Bugfix Patch for Bug 26 (Read 1379 times)
My Bugfix Patch for Bug 26
Mar 24th, 2009, 9:53pm
 
The FAQ says that "We love to see bug fixes, new libraries, etc. If you're interested, contact us via the discourse section of the site."  I'd like to become involved with Processing development as my first open source project.  So I submit here my patch to fix Bug 26(http://dev.processing.org/bugs/show_bug.cgi?id=26).  If there's anywhere else I can submit it or any other instructions to adhere to, I'd love some guidance.

I built and compiled this from source downloaded from SVN within the last two weeks.

My fix consists of 1.) adding a method to processing.app.Editor, and 2.) changing a method of that same class.  

1.) Add the following method to processing.app.Editor:
/******************************************************
  * Sets a mnemonic for each menu and top level menu item by these rules: 1.)
  * if possible, the first letter is used as the mnemonic, 2.) if first letter
  * is taken, then the next widest character in the menu item text is used.
  * that's why you have to provide the font; character widths are different in
  * different fonts. 3.) if that doesn't work, it doesn't assign a mnemonic.
  *
  * @author Myer Nore
  * @param menu
  *          array of menus to set mnemonics for
  * @param font
  *          A font for rendering character widths
  */
 protected void setMenuMnemonics(JMenuItem[] menu) {
   final FontMetrics fm = menu[0].getFontMetrics(menu[0].getFont());
   Comparator<Character> charWidthComparator = new Comparator<Character>() {
     public int compare(Character o1, Character o2) {
       return (fm.charWidth(o1) > fm.charWidth(o2)) ? -1 : 1;
     }
   };
   java.util.List<Character> taken = new ArrayList<Character>(menu.length);
   char tempChar;
   char[] w; // temp char array
   Character[] word;

   for (JMenuItem j : menu) {
     if (j != null) {
       tempChar = j.getText().charAt(0);
       if (!taken.contains(tempChar)) {
         j.setMnemonic(tempChar);
         taken.add(tempChar);
       } else {
         w = j.getText().toCharArray();
         word = new Character[w.length];
         for (int i = 0; i < w.length; i++) {
           word[i] = new Character(w[i]);
         }
         Arrays.sort(word, charWidthComparator); // sorts in increasing order
         for (int i = 0; i < word.length; i++) {
           tempChar = word[i];
           if (! ( taken.contains(tempChar) || tempChar == ' ' ) ) {
             j.setMnemonic(tempChar);
             taken.add(tempChar);
             break;
           }
         }
       }
       if (j instanceof JMenu) {
         JMenu jm = (JMenu) j;
         JMenuItem[] items = new JMenuItem[jm.getItemCount()];
         for (int i = 0; i < items.length; i++) {
           items[i] = jm.getItem(i);
         }
         setMenuMnemonics(items);
       }
     }
   }
 }

2.) Change processing.app.Editor.buildMenuBar() to this:

protected void buildMenuBar() {
   JMenuBar menubar = new JMenuBar();
   menubar = new JMenuBar();
   JMenu[] menus = new JMenu[5];
   menus[0] = buildFileMenu();
   menus[1] = buildEditMenu();
   menus[2] = buildSketchMenu();
   menus[3] = buildToolsMenu();
   menus[4] = buildHelpMenu();
   for (JMenu j : menus) {
     menubar.add(j);
   }
   setMenuMnemonics(menus);
   setJMenuBar(menubar);
}
Page Index Toggle Pages: 1