|
30 | 30 | package org.scijava.ui.swing.script; |
31 | 31 |
|
32 | 32 | import java.awt.Color; |
| 33 | +import java.awt.Cursor; |
33 | 34 | import java.awt.Dimension; |
34 | 35 | import java.awt.Font; |
35 | 36 | import java.awt.GridBagConstraints; |
|
108 | 109 | import javax.swing.JCheckBoxMenuItem; |
109 | 110 | import javax.swing.JFileChooser; |
110 | 111 | import javax.swing.JFrame; |
| 112 | +import javax.swing.JLabel; |
111 | 113 | import javax.swing.JMenu; |
112 | 114 | import javax.swing.JMenuBar; |
113 | 115 | import javax.swing.JMenuItem; |
@@ -214,7 +216,7 @@ public class TextEditor extends JFrame implements ActionListener, |
214 | 216 | openMacroFunctions, decreaseFontSize, increaseFontSize, chooseFontSize, |
215 | 217 | chooseTabSize, gitGrep, openInGitweb, replaceTabsWithSpaces, |
216 | 218 | replaceSpacesWithTabs, toggleWhiteSpaceLabeling, zapGremlins, |
217 | | - savePreferences, toggleAutoCompletionMenu; |
| 219 | + savePreferences, toggleAutoCompletionMenu, openClassOrPackageHelp; |
218 | 220 | private RecentFilesMenuItem openRecent; |
219 | 221 | private JMenu gitMenu, tabsMenu, fontSizeMenu, tabSizeMenu, toolsMenu, |
220 | 222 | runMenu, whiteSpaceMenu; |
@@ -483,6 +485,8 @@ public TextEditor(final Context context) { |
483 | 485 | openHelp = |
484 | 486 | addToMenu(toolsMenu, "Open Help for Class (with frames)...", 0, 0); |
485 | 487 | openHelp.setMnemonic(KeyEvent.VK_P); |
| 488 | + openClassOrPackageHelp = addToMenu(toolsMenu, "Source or javadoc for class or package...", 0, 0); |
| 489 | + openClassOrPackageHelp.setMnemonic(KeyEvent.VK_S); |
486 | 490 | openMacroFunctions = |
487 | 491 | addToMenu(toolsMenu, "Open Help on Macro Functions...", 0, 0); |
488 | 492 | openMacroFunctions.setMnemonic(KeyEvent.VK_H); |
@@ -1400,6 +1404,7 @@ else if (source == savePreferences) { |
1400 | 1404 | } |
1401 | 1405 | else if (source == openHelp) openHelp(null); |
1402 | 1406 | else if (source == openHelpWithoutFrames) openHelp(null, false); |
| 1407 | + else if (source == openClassOrPackageHelp) openClassOrPackageHelp(null); |
1403 | 1408 | else if (source == openMacroFunctions) try { |
1404 | 1409 | new MacroFunctions(this).openHelp(getTextArea().getSelectedText()); |
1405 | 1410 | } |
@@ -2664,6 +2669,79 @@ public void openHelp(String className, final boolean withFrames) { |
2664 | 2669 | handleException(e); |
2665 | 2670 | } |
2666 | 2671 | } |
| 2672 | + |
| 2673 | + /** |
| 2674 | + * @param text Either a classname, or a partial class name, or package name or any part of the fully qualified class name. |
| 2675 | + */ |
| 2676 | + public void openClassOrPackageHelp(String text) { |
| 2677 | + if (text == null) |
| 2678 | + text = getSelectedClassNameOrAsk(); |
| 2679 | + if (null == text) return; |
| 2680 | + new Thread(new FindClassSourceAndJavadoc(text)).start(); // fork away from event dispatch thread |
| 2681 | + } |
| 2682 | + |
| 2683 | + public class FindClassSourceAndJavadoc implements Runnable { |
| 2684 | + private final String text; |
| 2685 | + public FindClassSourceAndJavadoc(final String text) { |
| 2686 | + this.text = text; |
| 2687 | + } |
| 2688 | + @Override |
| 2689 | + public void run() { |
| 2690 | + setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
| 2691 | + final HashMap<String, ArrayList<String>> matches; |
| 2692 | + try { |
| 2693 | + matches = ClassUtil.findDocumentationForClass(text); |
| 2694 | + } finally { |
| 2695 | + setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
| 2696 | + } |
| 2697 | + if (matches.isEmpty()) { |
| 2698 | + JOptionPane.showMessageDialog(getEditorPane(), "No info found for:\n'" + text +'"'); |
| 2699 | + return; |
| 2700 | + } |
| 2701 | + final JPanel panel = new JPanel(); |
| 2702 | + final GridBagLayout gridbag = new GridBagLayout(); |
| 2703 | + final GridBagConstraints c = new GridBagConstraints(); |
| 2704 | + panel.setLayout(gridbag); |
| 2705 | + panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); |
| 2706 | + final List<String> keys = new ArrayList<String>(matches.keySet()); |
| 2707 | + Collections.sort(keys); |
| 2708 | + c.gridy = 0; |
| 2709 | + for (final String classname: keys) { |
| 2710 | + c.gridx = 0; |
| 2711 | + c.anchor = GridBagConstraints.EAST; |
| 2712 | + final JLabel class_label = new JLabel(classname); |
| 2713 | + gridbag.setConstraints(class_label, c); |
| 2714 | + panel.add(class_label); |
| 2715 | + for (final String url: matches.get(classname)) { |
| 2716 | + c.gridx += 1; |
| 2717 | + c.anchor = GridBagConstraints.WEST; |
| 2718 | + final JButton link = new JButton(url.endsWith("java") ? "Source" : "JavaDoc"); |
| 2719 | + gridbag.setConstraints(link, c); |
| 2720 | + panel.add(link); |
| 2721 | + link.addActionListener(new ActionListener() { |
| 2722 | + public void actionPerformed(final ActionEvent event) { |
| 2723 | + try { |
| 2724 | + platformService.open(new URL(url)); |
| 2725 | + } catch (Exception e) { |
| 2726 | + e.printStackTrace(); |
| 2727 | + } |
| 2728 | + } |
| 2729 | + }); |
| 2730 | + } |
| 2731 | + c.gridy += 1; |
| 2732 | + } |
| 2733 | + final JScrollPane jsp = new JScrollPane(panel); |
| 2734 | + //jsp.setPreferredSize(new Dimension(800, 500)); |
| 2735 | + SwingUtilities.invokeLater(new Runnable() { |
| 2736 | + public void run() { |
| 2737 | + final JFrame frame = new JFrame(text); |
| 2738 | + frame.getContentPane().add(jsp); |
| 2739 | + frame.pack(); |
| 2740 | + frame.setVisible(true); |
| 2741 | + } |
| 2742 | + }); |
| 2743 | + } |
| 2744 | + } |
2667 | 2745 |
|
2668 | 2746 | public void extractSourceJar() { |
2669 | 2747 | final File file = openWithDialog(null); |
|
0 commit comments