001: /**
002: *
003: */package net.refractions.udig.catalog.hsql;
004:
005: import java.io.File;
006: import java.io.FilenameFilter;
007: import java.io.IOException;
008: import java.io.Serializable;
009: import java.lang.reflect.InvocationTargetException;
010: import java.net.URI;
011: import java.net.URISyntaxException;
012: import java.net.URL;
013: import java.util.Map;
014:
015: import net.refractions.udig.catalog.IService;
016: import net.refractions.udig.catalog.hsql.internal.HsqlServiceExtension;
017: import net.refractions.udig.catalog.hsql.internal.Messages;
018: import net.refractions.udig.catalog.ui.UDIGConnectionPage;
019:
020: import org.eclipse.core.runtime.IProgressMonitor;
021: import org.eclipse.core.runtime.NullProgressMonitor;
022: import org.eclipse.core.runtime.Platform;
023: import org.eclipse.jface.operation.IRunnableWithProgress;
024: import org.eclipse.jface.wizard.WizardPage;
025: import org.eclipse.swt.SWT;
026: import org.eclipse.swt.widgets.Composite;
027: import org.geotools.data.DataStore;
028: import org.geotools.data.FeatureSource;
029: import org.geotools.feature.AttributeType;
030: import org.geotools.feature.AttributeTypeFactory;
031: import org.geotools.feature.FeatureTypes;
032: import org.geotools.feature.SchemaException;
033:
034: /**
035: *
036: * @author jones
037: */
038: public class NewLayerConnectionPage extends WizardPage implements
039: UDIGConnectionPage {
040:
041: private Map<String, Serializable> params;
042:
043: public NewLayerConnectionPage() {
044: super (Messages.NewLayerConnectionPage_pageName);
045:
046: }
047:
048: public Map<String, Serializable> getParams() {
049: return params;
050: }
051:
052: public void createControl(Composite parent) {
053: setControl(new Composite(parent, SWT.NONE));
054: try {
055: getContainer().run(false, true,
056: new IRunnableWithProgress() {
057:
058: @SuppressWarnings("nls")//$NON-NLS-1$
059: public void run(IProgressMonitor monitor)
060: throws InvocationTargetException,
061: InterruptedException {
062: try {
063: File f = Platform.getLocation()
064: .toFile();
065: String[] files = f
066: .list(new FilenameFilter() {
067:
068: public boolean accept(
069: File dir,
070: String name) {
071: if (name
072: .contains(".hsql.data")) //$NON-NLS-1$
073: return true;
074: return false;
075: }
076:
077: });
078: File nextFile = null;
079: for (String string : files) {
080: if (!string.startsWith("newData")) { //$NON-NLS-1$
081: continue;
082: }
083: try {
084:
085: String lastIndex = string
086: .substring(
087: string
088: .lastIndexOf("_") + 1, string.indexOf(".hsql")); //$NON-NLS-1$ //$NON-NLS-2$
089: int index = Integer
090: .parseInt(lastIndex); //$NON-NLS-2$
091: String next = string
092: .substring(
093: 0,
094: string
095: .lastIndexOf("_")); //$NON-NLS-1$
096: next += "_" + ++index + ".hsql"; //$NON-NLS-1$ //$NON-NLS-2$
097: nextFile = new File(next);
098: if (!nextFile.exists())
099: break;
100: } catch (NumberFormatException e) {
101: continue;
102: }
103: }
104: if (nextFile == null) {
105: nextFile = new File(f.toString()
106: + File.separator
107: + "newData_1.hsql"); //$NON-NLS-1$
108: }
109: HsqlServiceExtension ext = new HsqlServiceExtension();
110: URL id = nextFile.toURL();
111: params = ext.createParams(id);
112: IService service = ext.createService(
113: id, params);
114: DataStore ds = service.resolve(
115: DataStore.class,
116: new NullProgressMonitor());
117: AttributeType at = AttributeTypeFactory
118: .newAttributeType(
119: Messages.NewLayerConnectionPage_geomAtt,
120: com.vividsolutions.jts.geom.Geometry.class);
121:
122: ds
123: .createSchema(FeatureTypes
124: .newFeatureType(
125: new AttributeType[] { at },
126: "New_Type", new URI("http://udig.refractions.net"), false)); //$NON-NLS-1$ //$NON-NLS-2$
127: FeatureSource featureSource = ds
128: .getFeatureSource("New_Type"); //$NON-NLS-1$
129: assert featureSource != null;
130: } catch (IOException e) {
131: throw new InvocationTargetException(e);
132: } catch (SchemaException e) {
133: throw new InvocationTargetException(e);
134: } catch (URISyntaxException e) {
135: throw new InvocationTargetException(e);
136: } finally {
137: monitor.done();
138: }
139: }
140:
141: });
142: } catch (Exception e) {
143: // TODO Handle InvocationTargetException
144: throw (RuntimeException) new RuntimeException()
145: .initCause(e);
146: }
147: }
148:
149: }
|