Révision 3069
tmp/org.txm.rcp/src/main/java/org/txm/rcp/handlers/scripts/CreateMacro.java (revision 3069) | ||
---|---|---|
30 | 30 |
import java.io.File; |
31 | 31 |
import java.io.IOException; |
32 | 32 |
import java.io.PrintWriter; |
33 |
import java.util.Calendar; |
|
33 | 34 |
|
34 | 35 |
import org.eclipse.core.commands.AbstractHandler; |
35 | 36 |
import org.eclipse.core.commands.ExecutionEvent; |
... | ... | |
52 | 53 |
import org.txm.rcp.views.fileexplorer.MacroExplorer; |
53 | 54 |
import org.txm.utils.AsciiUtils; |
54 | 55 |
import org.txm.utils.io.IOUtils; |
56 |
|
|
55 | 57 |
// TODO: Auto-generated Javadoc |
56 | 58 |
/** |
57 | 59 |
* Handler: creates a macro template |
58 | 60 |
*/ |
59 | 61 |
public class CreateMacro extends AbstractHandler { |
60 |
|
|
62 |
|
|
61 | 63 |
private static final String ID = "org.txm.rcp.commands.CreateMacro"; //$NON-NLS-1$ |
62 |
/* (non-Javadoc) |
|
64 |
/* |
|
65 |
* (non-Javadoc) |
|
63 | 66 |
* @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) |
64 | 67 |
*/ |
68 |
|
|
65 | 69 |
@Override |
66 | 70 |
public Object execute(ExecutionEvent event) throws ExecutionException { |
67 |
|
|
68 |
//get scripts dir |
|
71 |
|
|
72 |
// get scripts dir
|
|
69 | 73 |
String scriptRootDir = Toolbox.getTxmHomePath() + "/scripts"; |
70 | 74 |
File currentRootDir = new File(scriptRootDir, "macro"); //$NON-NLS-1$ |
71 |
|
|
72 |
//if selection is a file, get its path |
|
75 |
|
|
76 |
// if selection is a file, get its path
|
|
73 | 77 |
String dir = null; |
74 | 78 |
IWorkbenchWindow activeW = HandlerUtil.getActiveWorkbenchWindow(event); |
75 | 79 |
if (activeW != null) { |
... | ... | |
80 | 84 |
IStructuredSelection sel = (IStructuredSelection) selection; |
81 | 85 |
Object obj = sel.getFirstElement(); |
82 | 86 |
if (obj instanceof File) { |
83 |
File current = ((File)obj); |
|
87 |
File current = ((File) obj);
|
|
84 | 88 |
if (current.isDirectory()) { |
85 | 89 |
dir = current.getAbsolutePath(); |
86 |
} else { |
|
90 |
} |
|
91 |
else { |
|
87 | 92 |
dir = current.getParent(); |
88 | 93 |
} |
89 | 94 |
} |
90 | 95 |
} |
91 | 96 |
} |
92 | 97 |
} |
93 |
|
|
98 |
|
|
94 | 99 |
if (dir == null) { |
95 | 100 |
dir = currentRootDir.getAbsolutePath(); |
96 | 101 |
} |
97 | 102 |
|
98 |
//ask new file name |
|
103 |
// ask new file name
|
|
99 | 104 |
Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell(); |
100 | 105 |
InputDialog dialog = new InputDialog(shell, TXMUIMessages.macroName, TXMUIMessages.pleaseWriteTheMacroName, "", null); //$NON-NLS-1$ |
101 | 106 |
|
... | ... | |
114 | 119 |
if (idx > 0) { |
115 | 120 |
String sub = dir.substring(idx + "scripts/macro/org/txm/macro/".length(), dir.length()).replaceAll("/", ".").replaceAll("\\\\", "."); |
116 | 121 |
if (sub.trim().length() > 0) { |
117 |
packagePath += "."+sub;
|
|
122 |
packagePath += "." + sub;
|
|
118 | 123 |
} |
119 | 124 |
} |
120 | 125 |
name = name.replaceAll("\\p{Punct}", ""); //$NON-NLS-1$ //$NON-NLS-2$ |
121 | 126 |
name = AsciiUtils.convertNonAscii(name); |
122 | 127 |
name = AsciiUtils.removePunct(name); |
123 |
name = name.substring(0, 1).toUpperCase()+name.substring(1);
|
|
128 |
name = name.substring(0, 1).toUpperCase() + name.substring(1);
|
|
124 | 129 |
StringBuffer tmp = new StringBuffer(); |
125 |
for (int i = 0 ; i < name.length() ; i++) {
|
|
130 |
for (int i = 0; i < name.length(); i++) {
|
|
126 | 131 |
if (name.charAt(i) == ' ') { |
127 | 132 |
i++; |
128 |
if (i <name.length() ) |
|
129 |
tmp.append((""+name.charAt(i)).toUpperCase()); //$NON-NLS-1$ |
|
130 |
} else { |
|
133 |
if (i < name.length()) |
|
134 |
tmp.append(("" + name.charAt(i)).toUpperCase()); //$NON-NLS-1$ |
|
135 |
} |
|
136 |
else { |
|
131 | 137 |
tmp.append(name.charAt(i)); |
132 | 138 |
} |
133 | 139 |
} |
134 | 140 |
name = tmp.toString(); |
135 | 141 |
if (name == null) return false; |
136 |
|
|
137 |
//create the new file |
|
138 |
File newfile = new File(dir, name+"Macro.groovy"); //$NON-NLS-1$
|
|
142 |
|
|
143 |
// create the new file
|
|
144 |
File newfile = new File(dir, name + "Macro.groovy"); //$NON-NLS-1$
|
|
139 | 145 |
LastOpened.set(ID, newfile.getParent(), newfile.getName()); |
140 | 146 |
if (!newfile.exists() && newfile.getParentFile().canWrite()) { |
141 | 147 |
try { |
... | ... | |
143 | 149 |
System.out.println(NLS.bind(TXMUIMessages.newFileColonP0, newfile.getAbsolutePath())); |
144 | 150 |
|
145 | 151 |
PrintWriter writer = IOUtils.getWriter(newfile, "UTF-8"); //$NON-NLS-1$ |
152 |
|
|
153 |
writer.println("// Copyright © " + Calendar.getInstance().get(Calendar.YEAR) + " MY_INSTITUTION"); |
|
154 |
writer.println("// Licensed under the terms of the GNU General Public License version 3 (http://www.gnu.org/licenses/gpl-3.0.html)"); |
|
155 |
writer.println("// @author " + System.getProperty("user.name")); |
|
156 |
|
|
157 |
writer.println(""); //$NON-NLS-1$ |
|
146 | 158 |
writer.println("// STANDARD DECLARATIONS"); //$NON-NLS-1$ |
147 |
writer.println("package "+packagePath); //$NON-NLS-1$ |
|
159 |
writer.println("package " + packagePath); //$NON-NLS-1$ |
|
160 |
|
|
148 | 161 |
writer.println(""); //$NON-NLS-1$ |
149 | 162 |
writer.println("import org.kohsuke.args4j.*"); //$NON-NLS-1$ |
150 | 163 |
writer.println("import groovy.transform.Field"); //$NON-NLS-1$ |
151 | 164 |
writer.println("import org.txm.rcp.swt.widget.parameters.*"); //$NON-NLS-1$ |
165 |
|
|
152 | 166 |
writer.println(""); //$NON-NLS-1$ |
153 | 167 |
writer.println("// BEGINNING OF PARAMETERS"); //$NON-NLS-1$ |
154 | 168 |
writer.println(""); //$NON-NLS-1$ |
... | ... | |
191 | 205 |
writer.println("// Open the parameters input dialog box"); //$NON-NLS-1$ |
192 | 206 |
writer.println("if (!ParametersDialog.open(this)) return;"); //$NON-NLS-1$ |
193 | 207 |
writer.println(""); //$NON-NLS-1$ |
194 |
writer.println("// END OF PARAMETERS"); //$NON-NLS-1$
|
|
208 |
writer.println("// END OF PARAMETERS"); //$NON-NLS-1$ |
|
195 | 209 |
writer.println(""); //$NON-NLS-1$ |
196 | 210 |
writer.println("println \"corpora selection: \"+corpusViewSelection"); //$NON-NLS-1$ |
197 | 211 |
writer.close(); |
... | ... | |
200 | 214 |
Explorer.refresh(newfile); |
201 | 215 |
MacroExplorer.refresh(newfile); |
202 | 216 |
} |
203 |
} catch (IOException e) { |
|
217 |
} |
|
218 |
catch (IOException e) { |
|
204 | 219 |
System.out.println(e.getLocalizedMessage()); |
205 | 220 |
return false; |
206 | 221 |
} |
... | ... | |
209 | 224 |
|
210 | 225 |
return true; |
211 | 226 |
} |
212 |
} |
|
227 |
} |
Formats disponibles : Unified diff