001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.catalog.internal.arcsde.ui;
018:
019: import java.io.Serializable;
020: import java.net.URL;
021: import java.sql.Connection;
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.HashMap;
025: import java.util.List;
026: import java.util.Map;
027:
028: import net.refractions.udig.catalog.IService;
029: import net.refractions.udig.catalog.arcsde.internal.Messages;
030: import net.refractions.udig.catalog.internal.arcsde.ArcServiceExtension;
031: import net.refractions.udig.catalog.internal.arcsde.ArcsdePlugin;
032: import net.refractions.udig.catalog.ui.UDIGConnectionPage;
033: import net.refractions.udig.catalog.ui.preferences.AbstractProprietaryDatastoreWizardPage;
034: import net.refractions.udig.catalog.ui.preferences.AbstractProprietaryJarPreferencePage;
035:
036: import org.eclipse.core.runtime.IProgressMonitor;
037: import org.eclipse.jface.dialogs.IDialogSettings;
038: import org.eclipse.swt.custom.CCombo;
039: import org.eclipse.swt.events.ModifyEvent;
040: import org.eclipse.swt.events.ModifyListener;
041: import org.eclipse.swt.widgets.Composite;
042: import org.eclipse.swt.widgets.Group;
043: import org.eclipse.swt.widgets.Text;
044: import org.geotools.data.DataStoreFactorySpi;
045: import org.geotools.data.DataStoreFactorySpi.Param;
046: import org.geotools.data.arcsde.ArcSDEDataStoreFactory;
047:
048: /**
049: * Provides ...TODO summary sentence
050: * <p>
051: * TODO Description
052: * </p>
053: * @author dzwiers
054: * @since 0.6
055: */
056: public class ArcSDEWizardPage extends
057: AbstractProprietaryDatastoreWizardPage implements
058: UDIGConnectionPage {
059: private static final String ARCSDE_WIZARD = "ARCSDE_WIZARD"; //$NON-NLS-1$
060: private static final String ARCSDE_RECENT = "ARCSDE_RECENT"; //$NON-NLS-1$
061: private IDialogSettings settings;
062: private static final int COMBO_HISTORY_LENGTH = 15;
063: ArrayList<DataBaseConnInfo> dbData;
064:
065: public ArcSDEWizardPage() {
066: super (Messages.ArcSDEWizardPage_title);
067: settings = ArcsdePlugin.getDefault().getDialogSettings()
068: .getSection(ARCSDE_WIZARD);
069: if (settings == null) {
070: settings = ArcsdePlugin.getDefault().getDialogSettings()
071: .addNewSection(ARCSDE_WIZARD);
072: }
073: }
074:
075: /**
076: * TODO summary sentence for getDataStoreFactorySpi ...
077: *
078: * @return x
079: */
080: protected DataStoreFactorySpi getDataStoreFactorySpi() {
081: return factory;
082: }
083:
084: private static ArcSDEDataStoreFactory factory = new ArcSDEDataStoreFactory();
085:
086: public String getId() {
087: return "net.refractions.udig.catalog.ui.arcsde"; //$NON-NLS-1$
088: }
089:
090: /**
091: * TODO summary sentence for getParams ...
092: *
093: * @return x
094: */
095: public Map<String, Serializable> getParams() {
096: Map<String, Serializable> params = new HashMap<String, Serializable>();
097: Param[] dbParams = factory.getParametersInfo();
098: params.put(dbParams[1].key, "arcsde"); //$NON-NLS-1$
099: params.put(dbParams[2].key, getHostText());
100: String port1 = getPortText();
101: try {
102: params.put(dbParams[3].key, Integer.valueOf(port1));
103: } catch (NumberFormatException e) {
104: params.put(dbParams[3].key, Integer.valueOf(5432));
105: }
106:
107: String db = getDBText();
108: params.put(dbParams[4].key, db);
109:
110: String user1 = getUserText();
111: params.put(dbParams[5].key, user1);
112: String pass1 = getPassText();
113: params.put(dbParams[6].key, pass1);
114:
115: return params;
116: }
117:
118: /**
119: *
120: * @return
121: */
122: protected String getPortText() {
123: return this .port.getText();
124: }
125:
126: /**
127: *
128: * @return
129: */
130: protected String getPassText() {
131: return this .pass.getText();
132: }
133:
134: /**
135: *
136: * @return
137: */
138: protected String getUserText() {
139: return this .user.getText();
140: }
141:
142: /**
143: *
144: * @return
145: */
146: protected String getDBText() {
147: return ((Text) database).getText();
148: }
149:
150: /**
151: *
152: * @return
153: */
154: protected String getHostText() {
155: return ((CCombo) host).getText();
156: }
157:
158: public List<URL> getURLs() {
159: return null;
160: }
161:
162: /**
163: * TODO summary sentence for isDBCombo ...
164: *
165: * @return x
166: */
167: protected boolean isDBCombo() {
168: // instance?
169: return false;
170: }
171:
172: protected boolean isHostCombo() {
173: return true;
174: }
175:
176: /**
177: * TODO summary sentence for hasSchema ...
178: *
179: * @return x
180: */
181: protected boolean hasSchema() {
182: return false;
183: }
184:
185: /**
186: * TODO summary sentence for createAdvancedControl ...
187: *
188: * @return x
189: */
190: protected Group createAdvancedControl(Composite arg0) {
191: return null;
192: }
193:
194: /**
195: * TODO summary sentence for getConnection ...
196: *
197: * @return null
198: */
199: protected Connection getConnection() {
200: return null;
201: }
202:
203: /**
204: * TODO summary sentence for populateDB ...
205: *
206: *
207: */
208: protected void populateDB() {
209: // do nothing
210: }
211:
212: /**
213: * TODO summary sentence for populateSchema ...
214: *
215: *
216: */
217: protected void populateSchema() {
218: // do nothing
219: }
220:
221: @Override
222: protected boolean doIsPageComplete() {
223: Map<String, Serializable> p = getParams();
224: if (p == null)
225: return false;
226: boolean r = factory.canProcess(p);
227: return r;
228: }
229:
230: /*
231: * @see net.refractions.udig.catalog.ui.UDIGImportPage#getResources(org.eclipse.core.runtime.IProgressMonitor)
232: */
233: public List<IService> getResources(IProgressMonitor monitor)
234: throws Exception {
235: if (!isPageComplete())
236: return null;
237:
238: ArcServiceExtension creator = new ArcServiceExtension();
239:
240: IService service = creator.createService(null, getParams());
241: service.getInfo(monitor); // load
242:
243: List<IService> servers = new ArrayList<IService>();
244: servers.add(service);
245:
246: /*
247: * Success! Store the URL in history.
248: */
249: saveWidgetValues();
250:
251: return servers;
252: }
253:
254: /**
255: * Saves the widget values
256: */
257: private void saveWidgetValues() {
258: // Update history
259: if (settings != null) {
260: String[] recentARCSDEs = settings.getArray(ARCSDE_RECENT);
261: if (recentARCSDEs == null) {
262: recentARCSDEs = new String[0];
263: }
264: String dbs = new DataBaseConnInfo(getHostText(), port
265: .getText(), user.getText(), pass.getText(),
266: getDBText(), schema.getText()).toString();
267: recentARCSDEs = addToHistory(recentARCSDEs, dbs);
268: settings.put(ARCSDE_RECENT, recentARCSDEs);
269: }
270: }
271:
272: /**
273: * Adds an entry to a history, while taking care of duplicate history items
274: * and excessively long histories. The assumption is made that all histories
275: * should be of length <code>COMBO_HISTORY_LENGTH</code>.
276: *
277: * @param history the current history
278: * @param newEntry the entry to add to the history
279: * @return the history with the new entry appended
280: * Stolen from org.eclipse.team.internal.ccvs.ui.wizards.ConfigurationWizardMainPage
281: */
282: private String[] addToHistory(String[] history, String newEntry) {
283: ArrayList<String> l = new ArrayList<String>(Arrays
284: .asList(history));
285: addToHistory(l, newEntry);
286: String[] r = new String[l.size()];
287: l.toArray(r);
288: return r;
289: }
290:
291: /**
292: * Adds an entry to a history, while taking care of duplicate history items
293: * and excessively long histories. The assumption is made that all histories
294: * should be of length <code>COMBO_HISTORY_LENGTH</code>.
295: *
296: * @param history the current history
297: * @param newEntry the entry to add to the history
298: * Stolen from org.eclipse.team.internal.ccvs.ui.wizards.ConfigurationWizardMainPage
299: */
300: private void addToHistory(List<String> history, String newEntry) {
301: history.remove(newEntry);
302: history.add(0, newEntry);
303:
304: // since only one new item was added, we can be over the limit
305: // by at most one item
306: if (history.size() > COMBO_HISTORY_LENGTH)
307: history.remove(COMBO_HISTORY_LENGTH);
308: }
309:
310: @Override
311: protected void doCreateWizardPage(Composite parent) {
312: String[] recentARCSDEs = settings.getArray(ARCSDE_RECENT);
313: ArrayList<String> hosts = new ArrayList<String>();
314: dbData = new ArrayList<DataBaseConnInfo>();
315: if (recentARCSDEs != null) {
316: for (String recent : recentARCSDEs) {
317: DataBaseConnInfo dbs = new DataBaseConnInfo(recent);
318: dbData.add(dbs);
319: hosts.add(dbs.getHost());
320: }
321: }
322: if (hosts.size() > 0) {
323: ((CCombo) host).setItems(hosts.toArray(new String[0]));
324: ((CCombo) host).addModifyListener(new ModifyListener() {
325: public void modifyText(ModifyEvent e) {
326: if (e.widget != null) {
327: for (DataBaseConnInfo db : dbData) {
328: if (db.getHost().equalsIgnoreCase(
329: getHostText())) {
330: port.setText(db.getPort());
331: user.setText(db.getUser());
332: pass.setText(db.getPass());
333: ((Text) database).setText(db.getDb());
334: schema.setText(db.getSchema());
335: break;
336: }
337: }
338: }
339: }
340: });
341: }
342: }
343:
344: @Override
345: protected String getDriversMessage() {
346: return Messages.ArcSDEWizardPage_MissingDrivers;
347: }
348:
349: @Override
350: protected AbstractProprietaryJarPreferencePage getPreferencePage() {
351: return new ArcSDEPreferences();
352: }
353:
354: @Override
355: protected String getRestartMessage() {
356: return Messages.ArcSDEWizardPage_restartApp;
357: }
358: }
|