adding kml import and export
[udig:corrupt-do_not_use.git] / plugins / eu.udig.tools.jgrass / src / eu / udig / tools / jgrass / kml / wizard / KmlExportWizardPage.java
1 /*
2  * JGrass - Free Open Source Java GIS http://www.jgrass.org 
3  * (C) HydroloGIS - www.hydrologis.com 
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package eu.udig.tools.jgrass.kml.wizard;
19
20 import java.io.File;
21
22 import net.refractions.udig.catalog.IGeoResource;
23 import net.refractions.udig.project.ILayer;
24 import net.refractions.udig.project.ui.ApplicationGIS;
25
26 import org.eclipse.jface.wizard.WizardPage;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.FileDialog;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Text;
35
36 /**
37  * @author Andrea Antonello (www.hydrologis.com)
38  */
39 public class KmlExportWizardPage extends WizardPage {
40     public static final String ID = "eu.udig.tools.jgrass.kml.wizard.KmlExportWizardPage"; //$NON-NLS-1$
41
42     private Text outFileText;
43     private IGeoResource geoResource;
44
45     private String filePath;
46
47     public KmlExportWizardPage() {
48         super(ID);
49         setTitle("Export kml");
50         setDescription("Export feature layer to kml");
51     }
52
53     public void createControl( Composite parent ) {
54
55         Composite mainComposite = new Composite(parent, SWT.NONE);
56         GridLayout gridLayout = new GridLayout(3, false);
57         gridLayout.verticalSpacing = 10;
58         mainComposite.setLayout(gridLayout);
59
60         ILayer selectedLayer = ApplicationGIS.getActiveMap().getEditManager().getSelectedLayer();
61         geoResource = selectedLayer.getGeoResource();
62
63         /*
64          * layer selected
65          */
66         Label selectedLayerLabel = new Label(mainComposite, SWT.NONE);
67         GridData selectedLayerLabelGd = new GridData(SWT.BEGINNING, SWT.CENTER, false, false);
68         selectedLayerLabelGd.horizontalSpan = 3;
69         selectedLayerLabel.setLayoutData(selectedLayerLabelGd);
70         selectedLayerLabel.setText("Selected layer to export: " + selectedLayer.getName());
71
72         /*
73          * output file
74          */
75         Label outFileLabel = new Label(mainComposite, SWT.NONE);
76         outFileLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
77         outFileLabel.setText("Kml file to save");
78
79         outFileText = new Text(mainComposite, SWT.BORDER);
80         outFileText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
81         outFileText.setEditable(false);
82
83         final Button outFolderButton = new Button(mainComposite, SWT.PUSH);
84         outFolderButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
85         outFolderButton.setText("..."); //$NON-NLS-1$
86         outFolderButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter(){
87
88             public void widgetSelected( org.eclipse.swt.events.SelectionEvent e ) {
89                 FileDialog saveKmlDialog = new FileDialog(outFolderButton.getShell(), SWT.OPEN);
90                 saveKmlDialog.setFilterExtensions(new String[]{"*.kml"}); //$NON-NLS-1$
91                 String path = saveKmlDialog.open();
92                 if (path == null || path.length() < 1) {
93                     outFileText.setText(""); //$NON-NLS-1$
94                 } else {
95                     outFileText.setText(path);
96                     filePath = path;
97                 }
98                 checkFinish();
99             }
100         });
101
102         setControl(mainComposite);
103
104     }
105
106     public String getFilePath() {
107         return filePath;
108     }
109
110     public IGeoResource getGeoResource() {
111         return geoResource;
112     }
113
114     public void dispose() {
115         super.dispose();
116     }
117
118     private void checkFinish() {
119         if (filePath == null) {
120             KmlExportWizard.canFinish = false;
121         } else {
122             File file = new File(filePath);
123             File parentFolder = file.getParentFile();
124             if (parentFolder.isDirectory()) {
125                 KmlExportWizard.canFinish = true;
126             } else {
127                 KmlExportWizard.canFinish = false;
128             }
129         }
130         getWizard().getContainer().updateButtons();
131     }
132
133 }