Skip to content

Instantly share code, notes, and snippets.

@amiraliakbari
Created October 28, 2014 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amiraliakbari/e44d0ee4d2343ea270a1 to your computer and use it in GitHub Desktop.
Save amiraliakbari/e44d0ee4d2343ea270a1 to your computer and use it in GitHub Desktop.
Useful Code Snippets fot Eclipse Plugins
void addSlice(IWorkbenchWindow workbench) {
IEditorPart editor = null;
for (IWorkbenchPage p : workbench.getPages()) {
editor = p.getActiveEditor();
if (editor != null) {
System.out.println("ED " + p.getLabel());
}
}
if (editor == null) {
System.err.println("Can not find any editors, skipped.");
return;
}
// First get editor filename
// reference: http://stackoverflow.com/a/943348/462865
IEditorInput input = editor.getEditorInput();
String fl = "";
if (input instanceof IFileEditorInput) {
fl = ((IFileEditorInput)input).getFile().getLocation().toOSString();
System.out.println("filename: " + fl);
}
if (fl.isEmpty()) {
System.err.println("can not get editor filename");
return;
}
if (!(editor instanceof AbstractTextEditor)) {
System.err.println("Not in a text editor, no code copied.");
return;
}
// Get Selected Function From Editor Selection
ITextSelection textSelection = (ITextSelection) editor.getSite().getSelectionProvider().getSelection();
String fn = "";
boolean fnSelected = true;
try {
fn = textSelection.getText();
} catch (Exception e) {
fnSelected = false;
}
if (fn.isEmpty()) {
fnSelected = false;
}
// Fallback to the text-box if nothing is selected
if (fnSelected) {
sliceFuncText.setText(fn);
} else {
System.out.print("!");
fn = sliceFuncText.getText();
}
if (fn.isEmpty()) {
Logger.get().log("no function selected");
sliceFuncText.setBackground(new Color(255, 120, 180));
return;
} else {
sliceFuncText.setBackground(new Color(255, 255, 255));
}
}
public ArrayList<Item> search() {
Logger.get().log("Search: " + searchExpression);
CloseableHttpClient httpClient = HttpClients.createDefault();
generateURL();
Logger.get().log("debug", "query=" + url);
HttpGet httpGet = new HttpGet(url);
HttpResponse response = null;
try {
response = httpClient.execute(httpGet);
} catch (IOException e1) {
e1.printStackTrace();
}
Logger.get().profile("retreived");
HttpEntity entity = null;
try {
entity = response.getEntity();
} catch(NullPointerException n) {
System.out.println("no internet connection!");
return null;
}
java.io.InputStream is = null;
try {
is = entity.getContent();
} catch (IllegalStateException | IOException e1) {
e1.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append((line + "\n"));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Logger.get().profile("read");
Gson gson = new Gson(); // Or use new GsonBuilder().create();
SearchResults jsonProperties = gson.fromJson(sb.toString(), SearchResults.class); // deserializes JSON into MyClass
item = jsonProperties.getItems();
Logger.get().profile("parsed");
return (ArrayList<Item>) item;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment