File: FunctionFinder.java

package info (click to toggle)
imagej 1.46a-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,248 kB
  • sloc: java: 89,778; sh: 311; xml: 51; makefile: 6
file content (163 lines) | stat: -rw-r--r-- 5,754 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package ij.macro;
import ij.*;
import ij.plugin.*;
import ij.plugin.frame.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Hashtable;
import java.util.Arrays;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Set;

/** This class implements the text editor's Macros/Find Functions command.
It was written by jerome.mutterer at ibmp.fr, and is based on Mark Longair's CommandFinder plugin.
*/

public class FunctionFinder implements TextListener,  WindowListener, KeyListener, ItemListener {
    Dialog d;
    TextField prompt;
    List completions;
    String [] commands ;
    
    public FunctionFinder() {
        String exists = IJ.runMacro("return File.exists(getDirectory('macros')+'functions.html');");
        if (exists=="0")	{
            String installLocalMacroFunctionsFile = "functions = File.openUrlAsString('"+IJ.URL+"/developer/macro/functions.html');\n"+
                    "f = File.open(getDirectory('macros')+'functions.html');\n"+
                    "print (f, functions);\n"+
                    "File.close(f);";
            try { IJ.runMacro(installLocalMacroFunctionsFile);
            } catch (Throwable e) { IJ.error("Problem downloading functions.html"); return;}
        }
        String f = IJ.runMacro("return File.openAsString(getDirectory('macros')+'functions.html');");
        String [] l = f.split("\n");
        commands= new String [l.length];
        int c=0;
        for (int i=0; i<l.length; i++) {
            String line = l[i];
            if (line.startsWith("<b>")) {
                commands[c]=line.substring(line.indexOf("<b>")+3,line.indexOf("</b>"));
                c++;
            }
        }
        if (c==0) {
        	IJ.error("ImageJ/macros/functions.html is corrupted");
        	return;
        }
        
        ImageJ imageJ = IJ.getInstance();
        d = new Dialog(imageJ, "Built-in Functions");
        d.setLayout(new BorderLayout());
        d.addWindowListener(this);
        Panel northPanel = new Panel();
        prompt = new TextField("", 30);
        prompt.addTextListener(this);
        prompt.addKeyListener(this);
        northPanel.add(prompt);
        d.add(northPanel, BorderLayout.NORTH);
        completions = new List(12);
        completions.addKeyListener(this);
        populateList("");
        d.add(completions, BorderLayout.CENTER);
        d.pack();
        
        Frame frame = WindowManager.getFrontWindow();
        if (frame==null) return;
        java.awt.Point posi=frame.getLocationOnScreen();
        int initialX = (int)posi.getX() + 38;
        int initialY = (int)posi.getY() + 84;
        d.setLocation(initialX,initialY);
        d.setVisible(true);
        d.toFront();
    }

    public void populateList(String matchingSubstring) {
        String substring = matchingSubstring.toLowerCase();
        completions.removeAll();
        try {
            for(int i=0; i<commands.length; ++i) {
                String commandName = commands[i];
                if (commandName.length()==0)
                    continue;
                String lowerCommandName = commandName.toLowerCase();
                if( lowerCommandName.indexOf(substring) >= 0 ) {
                    completions.add(commands[i]);
                }
            }
        } catch (Exception e){}
    }
    
    public void edPaste(String arg) {
        Frame frame = WindowManager.getFrontWindow();
        try {
            TextArea ta = ((Editor)frame).getTextArea();
            int start = ta.getSelectionStart( );
            int end = ta.getSelectionEnd( );
            try {
                ta.replaceRange(arg.substring(0,arg.length()), start, end);
            } catch (Exception e) { }
            if (IJ.isMacOSX())
                ta.setCaretPosition(start+arg.length());
        } catch (Exception e) { }
        
    }
    public void itemStateChanged(ItemEvent ie) {
        populateList(prompt.getText());
    }
    
    protected void runFromLabel(String listLabel) {
        edPaste(listLabel);
        d.dispose();
    }
    
    public void keyPressed(KeyEvent ke) {
        int key = ke.getKeyCode();
        int items = completions.getItemCount();
        Object source = ke.getSource();
        if (source==prompt) {
            if (key==KeyEvent.VK_ENTER) {
                if (1==items) {
                    String selected = completions.getItem(0);
                    runFromLabel(selected);
                }
            } else if (key==KeyEvent.VK_UP) {
                completions.requestFocus();
                if(items>0)
                    completions.select(completions.getItemCount()-1);
            } else if (key==KeyEvent.VK_ESCAPE) {
                d.dispose();
            } else if (key==KeyEvent.VK_DOWN)  {
                completions.requestFocus();
                if (items>0)
                    completions.select(0);
            }
        } else if (source==completions) {
            if (key==KeyEvent.VK_ENTER) {
                String selected = completions.getSelectedItem();
                if (selected!=null)
                    runFromLabel(selected);
            }
        }
    }
    
    public void keyReleased(KeyEvent ke) { }
    
    public void keyTyped(KeyEvent ke) { }
    
    public void textValueChanged(TextEvent te) {
        populateList(prompt.getText());
    }
        
    public void windowClosing(WindowEvent e) {
        d.dispose();
    }
    
    public void windowActivated(WindowEvent e) { }
    public void windowDeactivated(WindowEvent e) { }
    public void windowClosed(WindowEvent e) { }
    public void windowOpened(WindowEvent e) { }
    public void windowIconified(WindowEvent e) { }
    public void windowDeiconified(WindowEvent e) { }
}