001: package net.refractions.udig.project.ui.internal.wizard.url;
002:
003: import java.io.Serializable;
004: import java.net.MalformedURLException;
005: import java.net.URL;
006: import java.util.ArrayList;
007: import java.util.Arrays;
008: import java.util.List;
009: import java.util.Map;
010:
011: import net.refractions.udig.catalog.CatalogPlugin;
012: import net.refractions.udig.catalog.IService;
013: import net.refractions.udig.catalog.internal.ServiceFactoryImpl;
014: import net.refractions.udig.catalog.ui.UDIGConnectionPage;
015: import net.refractions.udig.project.ui.internal.Messages;
016: import net.refractions.udig.project.ui.internal.ProjectUIPlugin;
017:
018: import org.eclipse.core.runtime.IProgressMonitor;
019: import org.eclipse.jface.dialogs.IDialogSettings;
020: import org.eclipse.jface.wizard.IWizardPage;
021: import org.eclipse.jface.wizard.WizardPage;
022: import org.eclipse.swt.SWT;
023: import org.eclipse.swt.events.ModifyEvent;
024: import org.eclipse.swt.events.ModifyListener;
025: import org.eclipse.swt.events.SelectionEvent;
026: import org.eclipse.swt.layout.GridData;
027: import org.eclipse.swt.layout.GridLayout;
028: import org.eclipse.swt.widgets.Combo;
029: import org.eclipse.swt.widgets.Composite;
030: import org.eclipse.swt.widgets.Label;
031:
032: /**
033: * @author Amr Alam TODO To change the template for this generated type comment go to Window -
034: * Preferences - Java - Code Style - Code Templates
035: */
036: public class URLWizardPage extends WizardPage implements
037: ModifyListener, UDIGConnectionPage {
038:
039: final static String[] types = {};
040: /** <code>url</code> field */
041: protected Combo url;
042: private static final String URL_WIZARD = "URL_WIZARD"; //$NON-NLS-1$
043: private static final String URL_RECENT = "URL_RECENT"; //$NON-NLS-1$
044: private IDialogSettings settings;
045: private static final int COMBO_HISTORY_LENGTH = 15;
046:
047: /**
048: * Construct <code>URLWizardPage</code>.
049: */
050: public URLWizardPage() {
051: super (Messages.URLWizardPage_title);
052:
053: settings = ProjectUIPlugin.getDefault().getDialogSettings()
054: .getSection(URL_WIZARD);
055: if (settings == null) {
056: settings = ProjectUIPlugin.getDefault().getDialogSettings()
057: .addNewSection(URL_WIZARD);
058: }
059: }
060:
061: public String getId() {
062: // TODO Auto-generated method stub
063: return null;
064: }
065:
066: public boolean canProcess(Object object) {
067: URL url = CatalogPlugin.locateURL(object);
068: if (url == null) {
069: return false;
070: }
071: return true;
072: }
073:
074: public Map<String, Serializable> toParams(Object object) {
075: return null;
076: }
077:
078: public void createControl(Composite parent) {
079: String[] recentURLs = settings.getArray(URL_RECENT);
080: if (recentURLs == null) {
081: recentURLs = new String[0];
082: }
083:
084: GridData gridData;
085: Composite composite = new Composite(parent, SWT.NULL);
086:
087: GridLayout gridLayout = new GridLayout();
088: int columns = 1;
089: gridLayout.numColumns = columns;
090: composite.setLayout(gridLayout);
091:
092: gridData = new GridData();
093:
094: Label urlLabel = new Label(composite, SWT.NONE);
095: urlLabel.setText(Messages.URLWizardPage_label_url_text);
096: urlLabel.setLayoutData(gridData);
097:
098: gridData = new GridData(GridData.FILL_HORIZONTAL);
099: gridData.widthHint = 400;
100:
101: url = new Combo(composite, SWT.BORDER);
102: url.setItems(recentURLs);
103: url.setVisibleItemCount(15);
104: url.setLayoutData(gridData);
105: url.setText("http://"); //$NON-NLS-1$
106: url.addModifyListener(this );
107:
108: setControl(composite);
109: setPageComplete(true);
110: }
111:
112: public boolean isPageComplete() {
113: try {
114: new URL(url.getText());
115: } catch (MalformedURLException e) {
116: return false;
117: }
118: return true;
119: }
120:
121: public boolean canFlipToNextPage() {
122: // return canFlip;
123: IWizardPage[] pages = getWizard().getPages();
124: return isPageComplete()
125: && !pages[pages.length - 1].equals(this );
126: }
127:
128: /**
129: * Double click in list, or return from url control.
130: *
131: * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
132: * @param e
133: */
134: public void widgetDefaultSelected(SelectionEvent e) {
135: e.getClass();// kill warning
136: if (getWizard().canFinish()) {
137: getWizard().performFinish();
138: }
139: }
140:
141: /**
142: * This should be called using the Wizard .. job when next/finish is pressed.
143: */
144: public List<IService> getResources(IProgressMonitor monitor)
145: throws Exception {
146: URL location = new URL(url.getText());
147: ServiceFactoryImpl serviceFactory = new ServiceFactoryImpl();
148: List<IService> services = serviceFactory.acquire(location);
149: /*
150: * Success! Store the URL in history.
151: */
152: saveWidgetValues();
153: return services;
154: }
155:
156: public void modifyText(ModifyEvent e) {
157: try {
158: new URL(url.getText());
159: setErrorMessage(null);
160: } catch (MalformedURLException exception) {
161: setErrorMessage(Messages.URLWizardPage_error_invalidURL);
162: }
163: getWizard().getContainer().updateButtons();
164: }
165:
166: /**
167: * Saves the widget values
168: */
169: private void saveWidgetValues() {
170: // Update history
171: if (settings != null) {
172: String[] recentURLs = settings.getArray(URL_RECENT);
173: if (recentURLs == null) {
174: recentURLs = new String[0];
175: }
176: recentURLs = addToHistory(recentURLs, url.getText());
177: settings.put(URL_RECENT, recentURLs);
178: }
179: }
180:
181: /**
182: * Adds an entry to a history, while taking care of duplicate history items and excessively long
183: * histories. The assumption is made that all histories should be of length
184: * <code>COMBO_HISTORY_LENGTH</code>.
185: *
186: * @param history the current history
187: * @param newEntry the entry to add to the history
188: * @return the history with the new entry appended Stolen from
189: * org.eclipse.team.internal.ccvs.ui.wizards.ConfigurationWizardMainPage
190: */
191: private String[] addToHistory(String[] history, String newEntry) {
192: ArrayList<String> l = new ArrayList<String>(Arrays
193: .asList(history));
194: addToHistory(l, newEntry);
195: String[] r = new String[l.size()];
196: l.toArray(r);
197: return r;
198: }
199:
200: /**
201: * Adds an entry to a history, while taking care of duplicate history items and excessively long
202: * histories. The assumption is made that all histories should be of length
203: * <code>COMBO_HISTORY_LENGTH</code>.
204: *
205: * @param history the current history
206: * @param newEntry the entry to add to the history Stolen from
207: * org.eclipse.team.internal.ccvs.ui.wizards.ConfigurationWizardMainPage
208: */
209: private void addToHistory(List<String> history, String newEntry) {
210: history.remove(newEntry);
211: history.add(0, newEntry);
212:
213: // since only one new item was added, we can be over the limit
214: // by at most one item
215: if (history.size() > COMBO_HISTORY_LENGTH)
216: history.remove(COMBO_HISTORY_LENGTH);
217: }
218:
219: public Map<String, Serializable> getParams() {
220: // TODO Auto-generated method stub
221: return null;
222: }
223:
224: public List<URL> getURLs() {
225: // TODO Auto-generated method stub
226: return null;
227: }
228: }
|