Permalink
Browse files

View export functionality. Layout still needs work though.

  • Loading branch information...
1 parent e4731ff commit 8942420647701bef72ece9ebcd925a9af41ca656 @cderoove committed Jan 29, 2013
@@ -1,5 +1,10 @@
package exapus.gui.views.store;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import javax.xml.bind.JAXBException;
+
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
@@ -11,10 +16,14 @@
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.window.Window;
+import org.eclipse.rwt.RWT;
+import org.eclipse.rwt.widgets.ExternalBrowser;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
@@ -23,16 +32,23 @@
import exapus.gui.editors.view.ViewEditor;
import exapus.gui.editors.view.ViewEditorInput;
+import exapus.gui.util.Util;
import exapus.model.store.Store;
import exapus.model.view.Perspective;
import exapus.model.view.View;
public class StoreView extends ViewPart implements IDoubleClickListener {
+ static {
+ RWT.getServiceManager().registerServiceHandler(ViewDownloadServiceHandler.ID, new ViewDownloadServiceHandler());
+ }
+
public static final String ID = "exapus.gui.views.store.StoreView";
private ListViewer listView;
+ private Browser hidden;
+
private String promptForUniqueViewName(String dialogTitle) {
IInputValidator viewNameValidator = new IInputValidator() {
@Override
@@ -50,7 +66,7 @@ public String isValid(String newText) {
else
return null;
}
-
+
@Override
public void createPartControl(final Composite parent) {
parent.setLayout(new FillLayout());
@@ -59,20 +75,25 @@ public void createPartControl(final Composite parent) {
listView.addDoubleClickListener(this);
listView.setInput(Store.getCurrent());
+ hidden = new Browser(parent, SWT.NONE);
+ hidden.setVisible(false);
+ hidden.setSize(0, 0);
+
+
Action newViewAction = new Action() {
@Override
public void run() {
String name = promptForUniqueViewName("Create new view");
if(name != null)
Store.getCurrent().registerView(new View(name,Perspective.PROJECT_CENTRIC));
-
+
}
};
newViewAction.setText("Create new view");
newViewAction.setId("exapus.gui.views.store.actions.NewViewAction");
- newViewAction.setImageDescriptor(getImageDescriptor(ISharedImages.IMG_TOOL_NEW_WIZARD));
+ newViewAction.setImageDescriptor(getImageDescriptor("add.gif"));
registerAction(newViewAction);
-
+
final Action duplicateViewAction = new Action() {
@Override
public void run() {
@@ -93,7 +114,7 @@ public void run() {
};
duplicateViewAction.setText("Duplicate selected view");
duplicateViewAction.setId("exapus.gui.views.store.actions.DuplicateViewAction");
- duplicateViewAction.setImageDescriptor(getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
+ duplicateViewAction.setImageDescriptor(getImageDescriptor("clone_el.gif"));
duplicateViewAction.setEnabled(false);
registerAction(duplicateViewAction);
listView.addSelectionChangedListener(new ISelectionChangedListener() {
@@ -102,7 +123,54 @@ public void selectionChanged(SelectionChangedEvent event) {
duplicateViewAction.setEnabled(!event.getSelection().isEmpty());
}
});
-
+
+
+ final Action saveViewAction = new Action() {
+ @Override
+ public void run() {
+ IStructuredSelection selection = (IStructuredSelection) listView.getSelection();
+ if(selection.isEmpty())
+ return;
+ Object selected = selection.getFirstElement();
+ if(selected instanceof View) {
+ View selectedView = (View) selected;
+ String url = ViewDownloadServiceHandler.viewDownloadUrl(selectedView.getName());
+ hidden.setUrl(url);
+ }
+ }
+ };
+ saveViewAction.setText("Export selected view");
+ saveViewAction.setId("exapus.gui.views.store.actions.ExportViewAction");
+ saveViewAction.setImageDescriptor(getImageDescriptor("export.gif"));
+ saveViewAction.setEnabled(false);
+ registerAction(saveViewAction);
+ listView.addSelectionChangedListener(new ISelectionChangedListener() {
+ @Override
+ public void selectionChanged(SelectionChangedEvent event) {
+ saveViewAction.setEnabled(!event.getSelection().isEmpty());
+ }
+ });
+
+ final Action loadViewAction = new Action() {
+ @Override
+ public void run() {
+ FileDialog fileDialog = new FileDialog(getSite().getShell(), SWT.TITLE | SWT.MULTI);
+ fileDialog.setText( "Upload view files (*.xml)");
+ fileDialog.setAutoUpload(true);
+ fileDialog.open();
+ String[] fileNames = fileDialog.getFileNames();
+ for(String fileName : fileNames)
+ System.out.println(fileName);
+ }
+ };
+ loadViewAction.setText("Import view");
+ loadViewAction.setId("exapus.gui.views.store.actions.ImportViewAction");
+ loadViewAction.setImageDescriptor(getImageDescriptor("import.gif"));
+ loadViewAction.setEnabled(true);
+ registerAction(loadViewAction);
+
+
+
final Action deleteViewAction = new Action() {
@Override
public void run() {
@@ -122,7 +190,7 @@ public void run() {
};
deleteViewAction.setText("Delete selected view");
deleteViewAction.setId("exapus.gui.views.store.actions.DeleteViewAction");
- deleteViewAction.setImageDescriptor(getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));
+ deleteViewAction.setImageDescriptor(getImageDescriptor("delete.gif"));
deleteViewAction.setEnabled(false);
registerAction(deleteViewAction);
listView.addSelectionChangedListener(new ISelectionChangedListener() {
@@ -131,16 +199,16 @@ public void selectionChanged(SelectionChangedEvent event) {
deleteViewAction.setEnabled(!event.getSelection().isEmpty());
}
});
-
+
}
@Override
public void setFocus() {
listView.getControl().setFocus();
}
-
-
+
+
private void openViewEditorOn(View v) {
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
ViewEditorInput input = new ViewEditorInput(v.getName());
@@ -162,18 +230,18 @@ public void doubleClick(DoubleClickEvent event) {
openViewEditorOn((View) selected);
}
}
-
+
private void registerAction(Action action) {
getViewSite().getActionBars().getToolBarManager().add(action);
}
private IWorkbench getWorkBench() {
return getSite().getWorkbenchWindow().getWorkbench();
}
-
+
private ImageDescriptor getImageDescriptor(String name) {
- return getWorkBench().getSharedImages().getImageDescriptor(name);
+ return Util.getImageDescriptorFromPlugin(name);
}
-
+
}
@@ -0,0 +1,49 @@
+package exapus.gui.views.store;
+
+import java.io.File;
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
+
+import org.eclipse.rwt.RWT;
+import org.eclipse.rwt.service.IServiceHandler;
+
+import com.google.common.io.Files;
+
+import exapus.gui.editors.forest.graph.GraphViz;
+import exapus.model.store.Store;
+
+public class ViewDownloadServiceHandler implements IServiceHandler {
+
+ public static String ID = "viewDownloadServiceHandler";
+
+ public static String viewDownloadUrl(String viewName) {
+ StringBuilder url = new StringBuilder();
+ url.append(RWT.getRequest().getContextPath());
+ url.append(RWT.getRequest().getServletPath());
+ url.append("?");
+ url.append(IServiceHandler.REQUEST_PARAM );
+ url.append("="+ID);
+ url.append("&viewName=" );
+ url.append(viewName);
+ return RWT.getResponse().encodeURL(url.toString());
+ }
+
+
+ public void service() throws IOException, ServletException {
+ String viewName = RWT.getRequest().getParameter("viewName");
+ File xmlFile = Store.getCurrent().xmlForRegisteredView(viewName);
+ if(xmlFile != null) {
+ HttpServletResponse response = RWT.getResponse();
+ response.setContentType( "application/octet-stream" );
+ String contentDisposition = "attachment; filename=\"" + xmlFile.getName() + "\"";
+ response.setHeader("Content-Disposition", contentDisposition);
+ ServletOutputStream out = response.getOutputStream();
+ Files.copy(xmlFile, out);
+ out.close();
+ }
+ }
+}
+
@@ -133,6 +133,15 @@ public File graphForRegisteredView(String name) {
return getView(name).draw();
}
+ public File xmlForRegisteredView(String name) {
+ try {
+ return getView(name).xml();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
// This file should be located in the same dir as eclipse.ini
// I.e., for Mac OS: PATH_TO_THE_ECLPSE_DIR/Eclipse.app/Contents/MacOS/
private static final String CONFIG_FILENAME = "config.properties";
@@ -5,6 +5,7 @@
import java.util.ArrayList;
import java.util.List;
+import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
@@ -240,6 +241,9 @@ public static View fromView(View original) {
return duplicate;
}
-
+ public File xml() throws Exception {
+ ViewWriter viewWriter = new ViewWriter(this);
+ return viewWriter.writeTemporary();
+ }
}
@@ -1,6 +1,9 @@
package exapus.model.view;
import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
import java.io.PrintStream;
import javax.xml.bind.JAXBContext;
@@ -15,11 +18,17 @@ public ViewWriter(View view) {
this.view = view;
}
- public void write(PrintStream s) throws JAXBException {
+ public void write(OutputStream s) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(View.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(view, s);
}
+
+ public File writeTemporary() throws IOException, JAXBException {
+ File xmlFile = File.createTempFile(view.getName(), ".xml");
+ this.write(new FileOutputStream(xmlFile));
+ return xmlFile;
+ }
}

0 comments on commit 8942420

Please sign in to comment.