Révision 1974
tmp/org.txm.rcp/src/main/java/org/txm/rcp/menu/MacrosContribution.java (revision 1974) | ||
---|---|---|
1 |
package org.txm.rcp.menu; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
|
|
5 |
import org.eclipse.jface.action.ContributionItem; |
|
6 |
import org.eclipse.osgi.util.NLS; |
|
7 |
import org.eclipse.swt.SWT; |
|
8 |
import org.eclipse.swt.events.SelectionAdapter; |
|
9 |
import org.eclipse.swt.events.SelectionEvent; |
|
10 |
import org.eclipse.swt.widgets.Menu; |
|
11 |
import org.eclipse.swt.widgets.MenuItem; |
|
12 |
import org.eclipse.ui.IWorkbenchPart; |
|
13 |
import org.eclipse.ui.IWorkbenchWindow; |
|
14 |
import org.txm.Toolbox; |
|
15 |
import org.txm.rcp.TXMWindows; |
|
16 |
import org.txm.rcp.handlers.scripts.ExecuteGroovyMacro; |
|
17 |
import org.txm.rcp.views.corpora.CorporaView; |
|
18 |
import org.txm.utils.logger.Log; |
|
19 |
|
|
20 |
public class MacrosContribution extends ContributionItem { |
|
21 |
|
|
22 |
public MacrosContribution() { |
|
23 |
} |
|
24 |
|
|
25 |
public MacrosContribution(String id) { |
|
26 |
super(id); |
|
27 |
} |
|
28 |
|
|
29 |
@Override |
|
30 |
public void fill(Menu menu, int index) { |
|
31 |
|
|
32 |
|
|
33 |
//create the menu item |
|
34 |
final File macroDirectory = new File(Toolbox.getTxmHomePath(), "scripts/groovy/user/org/txm/macro/ui"); |
|
35 |
File[] files = macroDirectory.listFiles(); |
|
36 |
if (files == null || files.length == 0) { |
|
37 |
MenuItem menuItem = new MenuItem(menu, SWT.CHECK, index); |
|
38 |
menuItem.setText("<no macro>"); |
|
39 |
menuItem.addSelectionListener(new SelectionAdapter() { |
|
40 |
|
|
41 |
public void widgetSelected(SelectionEvent e) { |
|
42 |
Log.info(NLS.bind("To populate this menu, you must create macros in the {0} directory .", macroDirectory)); |
|
43 |
} |
|
44 |
}); |
|
45 |
return; |
|
46 |
} |
|
47 |
for (File f : files) { |
|
48 |
if (f.getName().endsWith("Macro.groovy")) { |
|
49 |
MenuItem menuItem = new MenuItem(menu, SWT.CHECK, index); |
|
50 |
menuItem.setText(f.getName()); |
|
51 |
menuItem.addSelectionListener(new MacroSelectionAdapter(f)); |
|
52 |
} |
|
53 |
} |
|
54 |
} |
|
55 |
|
|
56 |
public class MacroSelectionAdapter extends SelectionAdapter { |
|
57 |
File macro; |
|
58 |
public MacroSelectionAdapter(File macro) { |
|
59 |
this.macro = macro; |
|
60 |
} |
|
61 |
|
|
62 |
public void widgetSelected(SelectionEvent e) { |
|
63 |
IWorkbenchWindow acWindow = TXMWindows.getActiveWindow(); |
|
64 |
IWorkbenchPart page = acWindow.getActivePage().getActivePart(); |
|
65 |
ExecuteGroovyMacro.execute(macro.getAbsolutePath(), page, CorporaView.getInstance().getTreeViewer().getSelection(), ""); |
|
66 |
} |
|
67 |
} |
|
68 |
} |
tmp/org.txm.rcp/src/main/java/org/txm/rcp/menu/MacrosMenuContribution.java (revision 1974) | ||
---|---|---|
1 |
package org.txm.rcp.menu; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.util.Arrays; |
|
5 |
import java.util.Comparator; |
|
6 |
|
|
7 |
import org.eclipse.jface.action.ContributionItem; |
|
8 |
import org.eclipse.osgi.util.NLS; |
|
9 |
import org.eclipse.swt.SWT; |
|
10 |
import org.eclipse.swt.events.MenuEvent; |
|
11 |
import org.eclipse.swt.events.MenuListener; |
|
12 |
import org.eclipse.swt.events.SelectionAdapter; |
|
13 |
import org.eclipse.swt.events.SelectionEvent; |
|
14 |
import org.eclipse.swt.widgets.Menu; |
|
15 |
import org.eclipse.swt.widgets.MenuItem; |
|
16 |
import org.eclipse.ui.IWorkbenchPart; |
|
17 |
import org.eclipse.ui.IWorkbenchWindow; |
|
18 |
import org.txm.Toolbox; |
|
19 |
import org.txm.rcp.TXMWindows; |
|
20 |
import org.txm.rcp.handlers.scripts.ExecuteGroovyMacro; |
|
21 |
import org.txm.rcp.views.corpora.CorporaView; |
|
22 |
import org.txm.utils.logger.Log; |
|
23 |
|
|
24 |
public class MacrosMenuContribution extends ContributionItem { |
|
25 |
|
|
26 |
Menu menu; |
|
27 |
int index; |
|
28 |
public MacrosMenuContribution() { |
|
29 |
super(); |
|
30 |
} |
|
31 |
public MacrosMenuContribution(String id) { |
|
32 |
super(id); |
|
33 |
} |
|
34 |
|
|
35 |
@Override |
|
36 |
public void fill(Menu menu, int index) { |
|
37 |
this.menu = menu; |
|
38 |
this.index = index; |
|
39 |
|
|
40 |
|
|
41 |
menu.addMenuListener(new MenuListener() { |
|
42 |
|
|
43 |
@Override |
|
44 |
public void menuShown(MenuEvent e) { |
|
45 |
onOpen(MacrosMenuContribution.this.menu, MacrosMenuContribution.this.index); |
|
46 |
} |
|
47 |
|
|
48 |
@Override |
|
49 |
public void menuHidden(MenuEvent e) { } |
|
50 |
}); |
|
51 |
} |
|
52 |
|
|
53 |
public File getMacroDirectory() { |
|
54 |
//create the menu item |
|
55 |
String w = Toolbox.getTxmHomePath(); |
|
56 |
if (w == null || w.length() == 0) return null; |
|
57 |
|
|
58 |
return new File(w, "scripts/groovy/user/org/txm/macro/ui/menu/main"); |
|
59 |
} |
|
60 |
|
|
61 |
public void onOpen(Menu menu, int index) { |
|
62 |
|
|
63 |
File macroDirectory = getMacroDirectory(); |
|
64 |
if (macroDirectory == null) return; |
|
65 |
|
|
66 |
File[] files = macroDirectory.listFiles(); |
|
67 |
if (files == null || files.length == 0) { |
|
68 |
|
|
69 |
MenuItem[] menuItems = menu.getItems(); |
|
70 |
for (int i = 0; i < menuItems.length; i++) { |
|
71 |
menuItems[i].dispose(); |
|
72 |
} |
|
73 |
|
|
74 |
MenuItem menuItem = new MenuItem(menu, SWT.CHECK, index); |
|
75 |
menuItem.setText("<no macro>"); |
|
76 |
menuItem.addSelectionListener(new SelectionAdapter() { |
|
77 |
public void widgetSelected(SelectionEvent e) { |
|
78 |
Log.info(NLS.bind("To populate this menu, you must create macros in the ''{0}'' directory.", getMacroDirectory())); |
|
79 |
} |
|
80 |
}); |
|
81 |
return; |
|
82 |
} |
|
83 |
|
|
84 |
populate(menu, macroDirectory, index); |
|
85 |
} |
|
86 |
|
|
87 |
public static void populate(Menu menu, File directory, int index) { |
|
88 |
|
|
89 |
// First empty |
|
90 |
MenuItem[] menuItems = menu.getItems(); |
|
91 |
for (int i = 0; i < menuItems.length; i++) { |
|
92 |
menuItems[i].dispose(); |
|
93 |
} |
|
94 |
|
|
95 |
// Find files |
|
96 |
File[] files = directory.listFiles(); |
|
97 |
|
|
98 |
// first are directories |
|
99 |
Arrays.sort(files, new Comparator<File>() { |
|
100 |
@Override |
|
101 |
public int compare(File arg0, File arg1) { |
|
102 |
if (arg0.isDirectory()) { |
|
103 |
if (arg1.isDirectory()) { |
|
104 |
return arg0.getName().compareTo(arg1.getName()); |
|
105 |
} else { |
|
106 |
return -1; |
|
107 |
} |
|
108 |
} else if (arg1.isFile()) { |
|
109 |
return arg0.getName().compareTo(arg1.getName()); |
|
110 |
} else { |
|
111 |
return 1; |
|
112 |
} |
|
113 |
} |
|
114 |
|
|
115 |
}); |
|
116 |
for (File f : files) { |
|
117 |
if (f.getName().endsWith("Macro.groovy")) { |
|
118 |
MenuItem menuItem = new MenuItem(menu, SWT.PUSH); |
|
119 |
menuItem.setText(f.getName()); |
|
120 |
menuItem.addSelectionListener(new MacroSelectionAdapter(f)); |
|
121 |
} else if (f.isDirectory()) { |
|
122 |
MenuItem subMenuItem = new MenuItem(menu, SWT.CASCADE); |
|
123 |
subMenuItem.setText(f.getName()); |
|
124 |
Menu submenu = new Menu(menu); |
|
125 |
subMenuItem.setMenu(submenu); |
|
126 |
populate(submenu, f, 0); |
|
127 |
} |
|
128 |
} |
|
129 |
} |
|
130 |
|
|
131 |
public static class MacroSelectionAdapter extends SelectionAdapter { |
|
132 |
File macro; |
|
133 |
public MacroSelectionAdapter(File macro) { |
|
134 |
this.macro = macro; |
|
135 |
} |
|
136 |
|
|
137 |
public void widgetSelected(SelectionEvent e) { |
|
138 |
IWorkbenchWindow acWindow = TXMWindows.getActiveWindow(); |
|
139 |
IWorkbenchPart page = acWindow.getActivePage().getActivePart(); |
|
140 |
ExecuteGroovyMacro.execute(macro.getAbsolutePath(), page, CorporaView.getInstance().getTreeViewer().getSelection(), ""); |
|
141 |
} |
|
142 |
} |
|
143 |
|
|
144 |
public static class MacroDirectoryMenu extends Menu { |
|
145 |
|
|
146 |
File directory; |
|
147 |
int index; |
|
148 |
|
|
149 |
public MacroDirectoryMenu(Menu parentMenu, File dir, int i) { |
|
150 |
super(parentMenu); |
|
151 |
this.directory = dir; |
|
152 |
this.index = i; |
|
153 |
this.addMenuListener(new MenuListener() { |
|
154 |
|
|
155 |
@Override |
|
156 |
public void menuShown(MenuEvent e) { |
|
157 |
populate(MacroDirectoryMenu.this, directory, index); |
|
158 |
} |
|
159 |
|
|
160 |
@Override |
|
161 |
public void menuHidden(MenuEvent e) { } |
|
162 |
}); |
|
163 |
} |
|
164 |
} |
|
165 |
} |
|
0 | 166 |
tmp/org.txm.rcp/src/main/java/org/txm/rcp/menu/MacrosCorporaMenuContribution.java (revision 1974) | ||
---|---|---|
1 |
package org.txm.rcp.menu; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
|
|
5 |
import org.txm.Toolbox; |
|
6 |
|
|
7 |
public class MacrosCorporaMenuContribution extends MacrosMenuContribution { |
|
8 |
|
|
9 |
public MacrosCorporaMenuContribution() { |
|
10 |
super(); |
|
11 |
} |
|
12 |
|
|
13 |
public MacrosCorporaMenuContribution(String id) { |
|
14 |
super(id); |
|
15 |
} |
|
16 |
|
|
17 |
public File getMacroDirectory() { |
|
18 |
//create the menu item |
|
19 |
String w = Toolbox.getTxmHomePath(); |
|
20 |
if (w == null || w.length() == 0) return null; |
|
21 |
|
|
22 |
return new File(w, "scripts/groovy/user/org/txm/macro/ui/menu/window"); |
|
23 |
} |
|
24 |
} |
|
0 | 25 |
tmp/org.txm.rcp/src/main/java/org/txm/rcp/menu/MacrosEditorMenuContribution.java (revision 1974) | ||
---|---|---|
1 |
package org.txm.rcp.menu; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
|
|
5 |
import org.txm.Toolbox; |
|
6 |
|
|
7 |
public class MacrosEditorMenuContribution extends MacrosMenuContribution { |
|
8 |
|
|
9 |
public MacrosEditorMenuContribution() { |
|
10 |
super(); |
|
11 |
} |
|
12 |
|
|
13 |
public MacrosEditorMenuContribution(String id) { |
|
14 |
super(id); |
|
15 |
} |
|
16 |
|
|
17 |
public File getMacroDirectory() { |
|
18 |
//create the menu item |
|
19 |
String w = Toolbox.getTxmHomePath(); |
|
20 |
if (w == null || w.length() == 0) return null; |
|
21 |
|
|
22 |
return new File(w, "scripts/groovy/user/org/txm/macro/ui/menu/object"); |
|
23 |
} |
|
24 |
} |
|
0 | 25 |
tmp/org.txm.rcp/plugin.xml (revision 1974) | ||
---|---|---|
1057 | 1057 |
id="menu.macros" |
1058 | 1058 |
label="Macros"> |
1059 | 1059 |
<dynamic |
1060 |
class="org.txm.rcp.menu.MacrosContribution" |
|
1060 |
class="org.txm.rcp.menu.MacrosMenuContribution"
|
|
1061 | 1061 |
id="org.txm.rcp.menu.MacrosContribution"> |
1062 | 1062 |
</dynamic> |
1063 | 1063 |
</menu> |
... | ... | |
1977 | 1977 |
name="org.txm.rcp.corporaview.tools.misc" |
1978 | 1978 |
visible="true"> |
1979 | 1979 |
</separator> |
1980 |
<menu |
|
1981 |
icon="icons/objects/script.png" |
|
1982 |
id="org.txm.rcp.corporaview.macros" |
|
1983 |
label="Macros"> |
|
1984 |
<dynamic |
|
1985 |
class="org.txm.rcp.menu.MacrosCorporaMenuContribution" |
|
1986 |
id="org.txm.rcp.menu.MacrosCorporaMenuContribution"> |
|
1987 |
</dynamic> |
|
1988 |
</menu> |
|
1980 | 1989 |
<separator |
1981 | 1990 |
name="org.txm.rcp.corporaview.text" |
1982 | 1991 |
visible="true"> |
Formats disponibles : Unified diff