概要

ソースコード

(function() {
  function getFileName(code) {
    var r = code.match(/public(?:\s|final)+(?:class|interface|enum)\s+([^<{\s]+)/m);
    return (r ? r[1] : 'Unknown') + '.java';
  }
  function pre2text(pre) {
    var div = document.createElement('div');
    div.innerHTML = pre.innerHTML.replace(/<br[ \/]*>/ig, '\n').replace(/<.*?>/mg, '');
    return div.childNodes[0].nodeValue.replace(/\xA0/g, ' '); //replace &nbsp;
  }
  var listener = function(e) {
    alert(getFileName(pre2text(this)));
  },
  pre = document.getElementsByTagName('pre'),
  i = 0, len = pre.length;
  for(; i<len; i++) {
    pre[i].addEventListener('dblclick', listener, false);
  }
}());

Bookmarklet版

  • 名前
    • java_file_name
  • アドレス(YUI Compressorで圧縮)
    javascript:(function(){function c(h){var g=h.match(/public(?:\s|final)+(?:class|interface|enum)\s+([^<{\s]+)/m);return(g?g[1]:"Unknown")+".java"}function d(g){var h=document.createElement("div");h.innerHTML=g.innerHTML.replace(/<br[ \/]*>/ig,"\n").replace(/<.*?>/mg,"");return h.childNodes[0].nodeValue.replace(/\xA0/g," ")}var e=function(g){alert(c(d(this)))},f=document.getElementsByTagName("pre"),b=0,a=f.length;for(;b<a;b++){f[b].addEventListener("dblclick",e,false)}}());

制限

  • 対象はJavaのファイル名のみ
  • 字句解析は行わない
  • publicなクラスが存在しない場合には未対応
  • 以下の様な形式のコメントは考慮しない
    • public /* comment */ class Main {...}
function getFileName(code) {
  var ext = '.java', f = false, name = 'Unknown',
     array = code.split(/[{\s\r\n]+/),
     i, len = array.length;
  for(i=0;i<len;i++) {
    switch(array[i]) {
      case 'public':
        f = true;
        continue;
      case 'class':
      case 'interface':
      case 'enum':
        name = array[i+1];
        if(f) break;
        f = false;
        continue;
      case 'final':
        continue;
      default:
        f = false;
    }
  }
  return name+ext;
}

テスト用のサンプルソースコード

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.Position;
import javax.swing.tree.*;

	public
 class         
 		JTreeNextMatchTest
{
  public JComponent makeUI() {
    JTree tree = new JTree() {
      @Override public TreePath getNextMatch(
          String prefix, int startingRow, Position.Bias bias) {
        return null;
      }
    };
    JPanel p = new JPanel(new BorderLayout());
    p.add(new JScrollPane(tree));
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new JTreeNextMatchTest().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}
public interface CloseableTabbedPaneListener extends EventListener {
    /**
     * Informs all CloseableTabbedPaneListeners when a tab should be
     * closed
     * @param tabIndexToClose the index of the tab which should be closed
     * @return true if the tab can be closed, false otherwise
     */
    boolean closeTab(int tabIndexToClose);
}
// aaaaaaaaaaa
// bbbbbbb
public enum Flip {
    NONE, VERTICAL, HORIZONTAL
}
public final enum Flip {
    NONE, VERTICAL, HORIZONTAL
}
public /* comment aaa bbb ccc */ enum Flip {
    NONE, VERTICAL, HORIZONTAL
}
public enum /* comment aaa bbb ccc */ Flip {
    NONE, VERTICAL, HORIZONTAL
}
public enum テスト {
    NONE, VERTICAL, HORIZONTAL
}
enum Flip {
    NONE, VERTICAL, HORIZONTAL
}
public interface CloseableTabbedPaneListener extends EventListener {
    /**
     * Informs all CloseableTabbedPaneListeners when a tab should be&nbsp;&nbsp;&nbsp;&nbsp;aaa
     * closed &lt; &gt; &amp; &copyright; <>&©
     * @param tabIndexToClose the index of the tab which should be closed
     * @return true if the tab can be closed, false otherwise
     */
    boolean closeTab(int tabIndexToClose);
}
public class HashMapComboBoxModel<K, V> extends AbstractListModel<K> implements MutableComboBoxModel<K>, 
Serializable {
  // @Override ...
}

コメント