Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion z.ex.search/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.,\
plugin.xml
plugin.xml,\
icons/
3 changes: 3 additions & 0 deletions z.ex.search/icons/pdf.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions z.ex.search/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
id="z.ex.search.prevSearchEntry"
name="Previous Search Entry">
</command>
<command
defaultHandler="z.ex.search.BuildRCPScriptHandler"
id="z.ex.search.buildRCPScript"
name="Build RCP Script">
</command>
</extension>
<extension
point="org.eclipse.ui.bindings">
Expand All @@ -31,5 +36,35 @@
sequence="ALT+,">
</key>
</extension>
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="toolbar:org.eclipse.ui.main.toolbar">
<toolbar
id="z.ex.search.toolbar">
<command
commandId="z.ex.search.buildRCPScript"
icon="icons/pdf.svg"
style="push"
tooltip="Execute Build RCP Script">
</command>
</toolbar>
</menuContribution>
</extension>
<extension
point="org.eclipse.ui.preferencePages">
<page
class="z.ex.search.BuildScriptPreferencePage"
id="z.ex.search.preferences"
name="Build Script">
</page>
</extension>
<extension
point="org.eclipse.core.runtime.preferences">
<initializer
class="z.ex.search.PreferenceInitializer">
</initializer>
</extension>

</plugin>
154 changes: 154 additions & 0 deletions z.ex.search/src/z/ex/search/BuildRCPScriptHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package z.ex.search;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.preferences.ScopedPreferenceStore;

public class BuildRCPScriptHandler extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
// Read paths from preferences
IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, "z.ex.search");
String scriptPath = store.getString(PreferenceConstants.SCRIPT_PATH);
String pdfOutputPath = store.getString(PreferenceConstants.PDF_OUTPUT_PATH);

File scriptFile = new File(scriptPath);

if (!scriptFile.exists()) {
showError("Script not found at: " + scriptPath);
return null;
}

if (!scriptFile.canExecute()) {
showError("Script is not executable: " + scriptPath);
return null;
}

// Execute the script using Eclipse Jobs API
Job job = new Job("Building RCP") {
@Override
protected IStatus run(IProgressMonitor monitor) {
monitor.beginTask("Executing RCP build script", IProgressMonitor.UNKNOWN);

try {
ProcessBuilder pb = new ProcessBuilder(scriptPath);
pb.directory(scriptFile.getParentFile());
pb.redirectErrorStream(true);

Process process = pb.start();

// Read and collect output
StringBuilder output = new StringBuilder();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
if (monitor.isCanceled()) {
process.destroyForcibly();
return Status.CANCEL_STATUS;
}
}
}

int exitCode = process.waitFor();
final boolean success = exitCode == 0;

if (success) {
showSuccessDialog();
} else {
String message = "Build failed with exit code: " + exitCode +
"\n\n" + output.toString();
showErrorDialog(message);
}

return Status.OK_STATUS;

} catch (IOException e) {
return new Status(IStatus.ERROR, "z.ex.search",
"Error executing script: " + e.getMessage(), e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return Status.CANCEL_STATUS;
} finally {
monitor.done();
}
}
};

job.setUser(true); // Shows the job in the UI
job.schedule();

return null;
}

private void showError(String message) {
Display.getDefault().asyncExec(() -> {
MessageDialog.openError(
Display.getDefault().getActiveShell(),
"Build RCP Script Error",
message
);
});
}

private void showSuccessDialog() {
Display.getDefault().asyncExec(() -> {
boolean openPdf = MessageDialog.openQuestion(
Display.getDefault().getActiveShell(),
"Build RCP Script",
"RCP build script executed successfully.\n\nDo you want to open the PDF file?"
);

if (openPdf) {
openPdfFile();
}
});
}

private void showErrorDialog(String message) {
Display.getDefault().asyncExec(() -> {
MessageDialog.openError(
Display.getDefault().getActiveShell(),
"Build RCP Script Error",
message
);
});
}

private void openPdfFile() {
// Read PDF path from preferences
IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, "z.ex.search");
String pdfOutputPath = store.getString(PreferenceConstants.PDF_OUTPUT_PATH);

File pdfFile = new File(pdfOutputPath);

if (!pdfFile.exists()) {
showError("PDF file not found at: " + pdfOutputPath);
return;
}

// Use SWT Program to launch the PDF with system default application
boolean launched = Program.launch(pdfFile.getAbsolutePath());

if (!launched) {
showError("Failed to open PDF file. No application is associated with PDF files.");
}
}
}
36 changes: 36 additions & 0 deletions z.ex.search/src/z/ex/search/BuildScriptPreferencePage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package z.ex.search;

import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.FileFieldEditor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.preferences.ScopedPreferenceStore;

/**
* Preference page for configuring build script paths.
*/
public class BuildScriptPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {

public BuildScriptPreferencePage() {
super(GRID);
}

@Override
public void createFieldEditors() {
addField(new FileFieldEditor(PreferenceConstants.SCRIPT_PATH,
"&Build Script Path:",
getFieldEditorParent()));

addField(new FileFieldEditor(PreferenceConstants.PDF_OUTPUT_PATH,
"&PDF Output Path:",
getFieldEditorParent()));
}

@Override
public void init(IWorkbench workbench) {
setPreferenceStore(new ScopedPreferenceStore(InstanceScope.INSTANCE, "z.ex.search"));
setDescription("Configure paths for the RCP build script and PDF output.");
}

}
19 changes: 19 additions & 0 deletions z.ex.search/src/z/ex/search/PreferenceConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package z.ex.search;

import java.nio.file.Paths;

/**
* Constant definitions for plug-in preferences
*/
public class PreferenceConstants {

public static final String SCRIPT_PATH = "scriptPath";
public static final String PDF_OUTPUT_PATH = "pdfOutputPath";

// Default values
public static final String DEFAULT_SCRIPT_PATH = Paths.get(System.getProperty("user.home"),
"git", "content", "_scripts", "buildRCPScript.sh").toString();
public static final String DEFAULT_PDF_OUTPUT_PATH = Paths.get(System.getProperty("user.home"),
"git", "content", "output.pdf").toString();

}
19 changes: 19 additions & 0 deletions z.ex.search/src/z/ex/search/PreferenceInitializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package z.ex.search;

import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.ui.preferences.ScopedPreferenceStore;

/**
* Initializes default preference values.
*/
public class PreferenceInitializer extends AbstractPreferenceInitializer {

@Override
public void initializeDefaultPreferences() {
ScopedPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, "z.ex.search");
store.setDefault(PreferenceConstants.SCRIPT_PATH, PreferenceConstants.DEFAULT_SCRIPT_PATH);
store.setDefault(PreferenceConstants.PDF_OUTPUT_PATH, PreferenceConstants.DEFAULT_PDF_OUTPUT_PATH);
}

}