Export SLD Plugin TutorialGoalsAfter completing this tutorial, you will have gained the skills to:
Create a New Plugin
Configuring Your New Plug-inIn this section you will configure the feature editor plug-in. Specifically, you will specify dependencies on other plug-ins in order to create a new feature editor.
Import Resources Into ProjectIn this section we are going to set up an icon directory and import the pictures we will use for our export SLD operation.
Define a New Extension
Create a New Operation
Implementing a export operation
Add the following code to your created class public class ExportSLD implements IOp { public class QueryAndSave implements Runnable { private String out; private Layer layer; private IProgressMonitor monitor; private Display display; public QueryAndSave( Layer layer, String out, Display display, IProgressMonitor monitor ) { this.layer=layer; this.out=out; this.display=display; this.monitor=monitor; } File file; public void run() { do{ if (!getFile()) return; boolean write=true; if( file.exists() ){ write=MessageDialog.openConfirm(display.getActiveShell(), Messages.getString("ExportSLD.saveAs"), //$NON-NLS-1$ file.getAbsolutePath()+Messages.getString("ExportSLD.exists") + //$NON-NLS-1$ Messages.getString("ExportSLD.replace")); //$NON-NLS-1$ } if( write ){ try { FileWriter writer=new FileWriter(file, false); writer.write(out); writer.close(); } catch (Exception e) { file=null; MessageDialog.openError(display.getActiveShell(), Messages.getString("ExportSLD.saveAs"),//$NON-NLS-1$ Messages.getString("ExportSLD.modifyError")); //$NON-NLS-1$ } }else{ file=null; } }while( file==null ); } private boolean getFile() { FileDialog fileDialog=new FileDialog(display.getActiveShell(),SWT.SAVE); fileDialog.setFilterExtensions(new String[]{"*.sld"}); //$NON-NLS-1$ fileDialog.setFilterNames(new String[]{Messages.getString("ExportSLD.SLD")}); //$NON-NLS-1$ String name=layer.getName(); if( name==null ){ try { name=layer.getGeoResource().getInfo(monitor).getTitle(); } catch (IOException e) { ExportPlugin.log("error getting name from layer's georesource", e); //$NON-NLS-1$ } if( name==null ){ try { name=layer.getGeoResource().getInfo(monitor).getName(); } catch (IOException e) { ExportPlugin.log("error getting name from layer's georesource", e); //$NON-NLS-1$ } } } if( name!=null) fileDialog.setFileName(name+".sld"); //$NON-NLS-1$ String path=fileDialog.open(); if( path==null) return false; file=new File(path); return true; } } public void op( Display display, Object target, IProgressMonitor monitor ) throws Exception { Layer layer=(Layer) target; Style style=(Style) layer.getStyleBlackboard().get(SLDContent.ID); // serialize out the style objects SLDTransformer sldWriter = new SLDTransformer(); String out = ""; //$NON-NLS-1$ try { out = sldWriter.transform(style); } catch (TransformerException e) { ExportPlugin.log(null, e); e.printStackTrace(); } catch (Exception e) { ExportPlugin.log(null, e); } display.asyncExec(new QueryAndSave(layer, out, display, monitor)); } }
Testing The Plug-in
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
(c) Copyright (c) 2004,2005 Refractions Research Inc. and others. |