currently I am working on a plugin for Eclipse CDT.
What I want to achieve:
Prepend a tool (let us call it cov) before the commandline.
gcc -omain.o main.c
should become
cov gcc -omain.o main.c
I tried to write a custom BuildRunner, which extend ExternalBuildrunner, modifies the tools before invoking the external runner. I do this by editing the COmmandLinePattern
So far it works, if I do not restore the old CommandLinePattern.
Is there a way to update configuration to use the modified tools, and later after invocation write the old configurations back.
Before modifiyng, I backup the tool-commandlinepatterns, so that would not be the problem. I am missing the update step here I guess.
public class CovBuildRunner extends ExternalBuildRunner {
private final Set<String> VALUES;
public CovBuildRunner() {
VALUES = new HashSet<String>();
VALUES.add("gcc");
VALUES.add("g++");
}
#Override
public boolean invokeBuild(int kind, IProject project,
IConfiguration configuration, IBuilder builder, IConsole console,
IMarkerGenerator markerGenerator,
IncrementalProjectBuilder projectBuilder, IProgressMonitor monitor)
throws CoreException {
Map<ITool, String> cmdPatternBackup = new HashMap<>();
for (ITool tool : configuration.getTools()) {
if (cmdPatternBackup.containsKey(tool)) {
System.out.println("ERROR! TOOL ALREADY MODIFIED!");
} else if (this.supports(tool)) {
cmdPatternBackup.put(tool, tool.getCommandLinePattern());
tool.setCommandLinePattern("cov " + tool.getCommandLinePattern());
System.out.println(
configuration.getToolCommand(tool));
}
}
configuration.getToolChain().set;
boolean success = invokeExternalBuild(kind, project, configuration, builder, console, markerGenerator, projectBuilder, monitor);
for (ITool tool : cmdPatternBackup.keySet()) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Setting back " + tool.getName() + "\n" + tool.getCommandLinePattern());
tool.setCommandLinePattern(cmdPatternBackup.get(tool));
}
return success;
}
private boolean supports(ITool tool) {
String command = tool.getToolCommand();
return VALUES.contains(command);
}
}
Related
I would like to create a command-line tool that starts an OSGi framework, in order to reuse code that is relying on OSGi.
In the answer accessing command-line arguments from OSGi bundle, I got how I can read the command line arguments:
#Component
public class Example {
String[] args;
#Activate
void activate() {
System.out.println("Hello World");
System.out.println(args.length + " args:");
for (String s : args) {
System.out.println(" - " + s);
}
}
#Reference(target = "(launcher.arguments=*)")
void args(Object object, Map<String, Object> map) {
if (map.containsKey("launcher.arguments")) {
args = (String[]) map.get("launcher.arguments");
} else {
args = new String[] {};
}
}
}
But now when I run the assembled jar (bnd-export-maven-plugin) like this:
java -jar <path-to>/application.jar lorem ipsum
I get the expected output, but the application does not terminate.
After having read 4.2.6 Stopping a Framework, I was thinking that I need to call stop() on the system bundle. I have tried to change my code to:
#Activate
void activate(BundleContext bundleContext) {
System.out.println("Hello World");
System.out.println(args.length + " args:");
for (String s : args) {
System.out.println(" - " + s);
}
try {
bundleContext.getBundle().stop();
} catch (BundleException e) {
e.printStackTrace();
}
}
But it does not seems to work like this.
If you want the system bundle to stop you must do (notice the 0):
bundleContext.getBundle(0).stop();
To do this hyper correctly, you should do this in another thread.
#Component
public class ServiceComponent {
#Activate
void activate(BundleContext c) {
CompletableFuture.runAsync( ()-> {
try {
c.getBundle(0).stop();
} catch (BundleException e) {
e.printStackTrace();
}
} );
}
}
This is, of course, a suicide component ...
i'm using thread to resolve the problem of GUI freeze. But with thread i'm facing a problem that i'm unable to pass format of the report as argument in run method or even with the help of constructor i'm unable to do it.....
public class BirtReportExportCon implements Runnable {
#FXML
Button exportButton;
#FXML
CheckBox pdfCheckBox;
#FXML
CheckBox xlsCheckBox;
#FXML
CheckBox docCheckBox;
#FXML
CheckBox mailCheckBox;
public String fileFormat;
Allow to Check Single CheckBox on Gui
public void eventCheckBoxPdf() {
if (pdfCheckBox.isSelected() == true) {
xlsCheckBox.setSelected(false);
docCheckBox.setSelected(false);
}
}
public void eventCheckBoxXls() {
if (xlsCheckBox.isSelected() == true) {
pdfCheckBox.setSelected(false);
docCheckBox.setSelected(false);
}
}
public void eventCheckBoxDoc() {
if (docCheckBox.isSelected() == true) {
pdfCheckBox.setSelected(false);
xlsCheckBox.setSelected(false);
}
}
Provide the Chosen fileFormat
public void onButtonClick() throws EngineException {
if (docCheckBox.isSelected() == true) {
fileFormat = "docx"; // I WANT THIS FILE FORMAT IN MY RUN METHOD
Runnable r = new BirtReportExportCon();
new Thread(r).start();
}
else if (pdfCheckBox.isSelected() == true) {
fileFormat = "pdf";
Runnable r = new BirtReportExportCon();
new Thread(r).start();
}
else if (xlsCheckBox.isSelected() == true) {
fileFormat = "xls";
Runnable r = new BirtReportExportCon();
new Thread(r).start();
}
}
Run Method
public void run()
{
try
{
exportFile(fileFormat); // HERE I WANT THAT SO I CAN ABLE TO CREATE REPORT OF REQUIRED FORMAT
}
catch (EngineException e) {
e.printStackTrace();
}
}
save report and open the report
public void exportFile(String fileFormat) throws EngineException {
String output = "output path";
String reportDesignFilePath = "report path";
try {
EngineConfig configure = new EngineConfig();
Platform.startup(configure);
IReportEngineFactory reportEngineFactory = (IReportEngineFactory) Platform
.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
IReportEngine engine = reportEngineFactory.createReportEngine(configure);
engine.changeLogLevel(Level.WARNING);
IReportRunnable runnable = engine.openReportDesign(reportDesignFilePath);
IRunAndRenderTask task = engine.createRunAndRenderTask(runnable);
IRenderOption option = new PDFRenderOption();
option.setOutputFormat(fileFormat);
option.setOutputFileName(output + fileFormat);
task.setRenderOption(option);
task.run();
task.close();
} catch (Exception e) {
e.printStackTrace();
}
// Open Created File
File fileOpen = new File(output + fileFormat);
if (fileOpen.exists()) {
if (Desktop.isDesktopSupported()) {
try {
Desktop desktop = Desktop.getDesktop();
desktop.open(fileOpen);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
I had a similar problem like this. I think the problem lies in the fileOpening stage. The Desktop class you are using comes from java.awt package.When you use the Desktop class then the JAVAFX thread gets blocked as commented by a user in the link given at the bottom of this answer. But the user has a low reputation (only 11)so we cannot rely on him.
To make your application unfreeze, you will have to create a new Thread.
Here is a part of my code, i used in my application and this code worked perfectly. I have also put a link to a github issue of my application where i stated the freezing problem, similar to yours. The issue was created 2 days ago.
#FXML
void openWithAction(ActionEvent event) {
boolean flag = false;
Task task = new Task<Void>() {
#Override
protected Void call() throws Exception {
try {
Desktop.getDesktop().open(new File(fileModel.getFileLocation()));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
};
new Thread(task).start();
}
Github issue link:
https://github.com/karanpant/SearchEverything/issues/3
I also suggest you to use concurrency provided by JavaFX.
Here is the other SO post link. Hope this helps.
JavaFX Freeze on Desktop.open(file), Desktop.browse(uri)
EDIT: I am sorry if i don't understand your question . Is your question about application freezing or about not being able to pass a parameter or about not being able to pass a parameter because of application freezing.
Try something like this:
if ( docCheckBox.isSelected() == true ) {
BirtReportExportCon r = new BirtReportExportCon();
r.fileFormat = "docx"; // I WANT THIS FILE FORMAT IN MY RUN METHOD
new Thread(r).start();
}
You should run this code on the Swing thread instead of calling it from the Java FX thread. Like the following:
#FXML
void openWithAction(ActionEvent event) {
SwingUtilities.invokeLater( () -> Desktop.getDesktop().
open(new File(fileModel.
getFileLocation())));
}
I want to run an action (with a rule) when a file enters the folder in my alfresco repository. The file needs to be moved to a new folder. The new folder will be named after the metadata property "subject" from the file I uploaded.
I am not able to figure out how to do this. Who got any tips?
(A repository webscript is also an option).
This is how I see it:
import java.util.List;
public class MoveExecuter extends ActionExecuterAbstractBase {
public static final String DESTINATION_FOLDER = "destination-folder";
private FileFolderService fileFolderService;
private NodeService nodeService;
#Override
protected void addParameterDefinitions(List<ParameterDefinition> paramList) {
paramList.add(
new ParameterDefinitionImpl(DESTINATION_FOLDER,
DataTypeDefinition.NODE_REF,
true,
getParamDisplayLabel(METADATA VALUE FROM FIELD SUBJECT FROM INCOMING FILE)));}
public void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef) {
NodeRef destinationParent = (NodeRef)ruleAction.getParameterValue(DESTINATION_FOLDER);
// if the node exists
if (this.nodeService.exists(destinationParent) == true) {
try {
fileFolderService.move(incomingfile, destinationParent, null);
} catch (FileNotFoundException e) {
// Do nothing
}
if (this.nodeService.exists(destinationParent) == false) {
try {
nodeService.createNode(parentRef, assocTypeQName, assocQName, "metadata field subject");
fileFolderService.move(incomingfile, destinationParent, null);
} catch (FileNotFoundException e) {
// Do nothing
}
}
}
}
For such a simple action I'd just use a JavaScript instead of a java Action.
Install the JavaScript addon from googlecode or github (newer version)
And just write your Javascript code according the api and run it in runtime in the console to test your code.
I am creating a dynamic IProject in eclipse. This IProject acts like the "New Plug-in with JAR archives" project in eclipse. Then I point to the reference of this outside jar file. Which btw I extract then link to the temporary IProject.
<linkedResources>
<link>
<name>SampleTestSuite</name>
<type>2</type>
<location>D:/eclipse-rcp-indigo/tests/SampleTestSuite</location>
</link>
</linkedResources>
Now, I'm planning to make this IProject Run-As JUnit. I want to delete the extracted and linked jar file after the JUnit. This is my code below in running JUnit but after file deletion it will throw an error because the linkedresource will not be there. Any ideas how to monitor if JUnit is already finished so that the linkedresource will be deleted?
Activator.getDisplay().syncExec(new Runnable() {
public void run() {
Job job = new Job("Test Runner JUnit") {
#Override
protected IStatus run(IProgressMonitor monitor) {
monitor.beginTask("Launching JUnit", 100);
try {
ILaunch launch = new Launch(launchConfiguration,
"run", null);
launch.setAttribute(
"org.eclipse.debug.ui.ATTR_CONSOLE_ENCODING",
DebugPlugin.getDefault().getLaunchManager()
.getEncoding(launchConfiguration));
DebugPlugin.getDefault().getLaunchManager()
.addLaunch(launch);
launchDelegate.launch(launchConfiguration, "run",
launch, monitor);
} catch (CoreException e) {
return e.getStatus();
} finally {
monitor.done();
}
return Status.OK_STATUS;
}
};
job.addJobChangeListener(new JobChangeAdapter() {
public void done(IJobChangeEvent event) {
if (event.getResult().isOK()) {
System.out.println("Job completed successfully");
// delete the file here
} else
System.out
.println("Job did not complete successfully");
}
});
job.setPriority(Job.INTERACTIVE);
job.schedule();
}
});
I just added this one
IProcess[] processes = launch.getProcesses();
for (IProcess process : processes) {
while (!process.isTerminated()) {
}
//delete here
}
I need to know which method is called inside eclipse when I press "CTRL+ SHIFT + O" (Organise Imports), in order to invoke it after a code generation. What the name of this method and where can I find it (Package.Interface)
Thanks
"Organize Imports" action is contributed by org.eclipse.jdt.ui.actions.OrganizeImportsAction, which, in turn, calls org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.
Finaly Managed to do it with this code (targetSite is a IWorkbench site initialized at the ame time as shell):
#Override
public void postLaunchAction(final IProject project, final IProgressMonitor monitor) throws CoreException {
super.postLaunchAction(project, monitor);
Runnable job = new Runnable() {
#Override
public void run() {
OrganizeImportsAction org = new OrganizeImportsAction(SpringServicesAction.this.targetSite);
try {
IJavaProject prj = null;
if (project.hasNature("org.eclipse.jdt.core.javanature")) {
prj = JavaCore.create(project);
}
IStructuredSelection selection = new StructuredSelection(prj);
org.run(selection);
} catch (CoreException ce) {
ce.printStackTrace();
}
}
};
this.shell.getDisplay().syncExec(job);
}
For reference, this is how I did it:
I made a large automated refactor in the codebase in our project. Due to a (i think so) bug in eclipse with refactoring static methods which are staticly imported in another file, i had to call organize imports after each refactor (also because I commit every move to git automaticly):
private void organizeImports(ICompilationUnit cu)
throws OperationCanceledException, CoreException {
cu.becomeWorkingCopy(null);
CompilationUnit unit = cu.reconcile(AST.JLS4, false, null, pm);
NullProgressMonitor pm = new NullProgressMonitor();
OrganizeImportsOperation op = new OrganizeImportsOperation(cu, unit,
true, true, true, null);
TextEdit edit = op.createTextEdit(pm);
if (edit == null) {
return;
}
JavaModelUtil.applyEdit(cu, edit, true, pm);
cu.commitWorkingCopy(true, pm);
cu.save(pm, true);
}
Disadvantagde: Discouraged access. If somebody has an idea to call this action properly without creating a new runnable and without using a shell etc., please comment.