Skip to content

Instantly share code, notes, and snippets.

@mbruch
Last active December 17, 2015 00:59
Show Gist options
  • Save mbruch/5524835 to your computer and use it in GitHub Desktop.
Save mbruch/5524835 to your computer and use it in GitHub Desktop.
{"fields":
[{
"type":"string",
"name":"Firstname",
"value":"Max"
},
{
"type":"string",
"name":"Lastname",
"value":"Mustermann"
},
{
"type":"string",
"name":"Job Role",
"value":"Chief Architect"
},
{
"type":"string",
"name":"Company",
"value":"Codetrails UG"
},
{
"type":"string",
"name":"Street",
"value":"Am Kirschbaum 7"
}]
}
package com.example.miniscout.client.editor;
import java.net.URI;
import org.eclipse.ui.internal.part.NullEditorInput;
public class ContactEditorInput extends NullEditorInput {
private URI input;
public ContactEditorInput(URI input) {
this.input = input;
}
public URI getInput() {
return input;
}
}
package com.example.miniscout.client.editor;
import static com.google.common.base.Charsets.UTF_8;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import com.google.common.base.Throwables;
import com.google.common.io.CharStreams;
import com.google.common.io.Files;
public class DatabaseService {
public static String load(URI uri) {
try {
return CharStreams.toString(Files.newReader(new File(uri), UTF_8));
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
public static void save(String data, URI uri) {
try {
if (uri.getScheme().equals("file")) {
Files.write(data, new File(uri), UTF_8);
} else
throw new IllegalArgumentException("scheme " + uri + " not supported");
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
}
package com.example.miniscout.client.editor;
import java.net.URI;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
public class Editor extends EditorPart {
private Text area;
private String content;
private URI input;
@Override
public void init(IEditorSite site, IEditorInput editorInput)
throws PartInitException {
setSite(site);
setInput(editorInput);
this.input = ((ContactEditorInput) editorInput).getInput();
content = DatabaseService.load(this.input);
}
@Override
public void createPartControl(Composite parent) {
area = new Text(parent, SWT.MULTI);
area.setText(content);
}
@Override
public void doSave(IProgressMonitor monitor) {
// not implemented yet
}
@Override
public boolean isDirty() {
// not implemented yet
return false;
}
@Override
public boolean isSaveAsAllowed() {
return false;
}
@Override
public void doSaveAs() {
}
@Override
public void setFocus() {
}
}
package com.example.miniscout.client.editor;
import java.io.File;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
public class OpenHandler extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbench wb = PlatformUI.getWorkbench();
IWorkbenchWindow ww = wb.getActiveWorkbenchWindow();
Shell shell = ww.getShell();
FileDialog dialog = new FileDialog(shell);
dialog.setFilterExtensions(new String[] { "*.json" });
String file = dialog.open();
if (file != null) {
openEditor(ww, file);
}
return true;
}
private void openEditor(IWorkbenchWindow ww, final String file) {
final File in = new File(file);
try {
ww.getActivePage().openEditor(new ContactEditorInput(in.toURI()),
"com.example.miniscout.client.editor");
} catch (PartInitException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment