001: package net.refractions.udig.catalog.ui;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import net.refractions.udig.catalog.ui.internal.Messages;
007: import net.refractions.udig.catalog.ui.workflow.DataSourceSelectionState;
008: import net.refractions.udig.catalog.ui.workflow.WorkflowWizardPage;
009: import net.refractions.udig.internal.ui.UiPlugin;
010:
011: import org.eclipse.jface.dialogs.Dialog;
012: import org.eclipse.jface.dialogs.IDialogConstants;
013: import org.eclipse.jface.resource.ImageDescriptor;
014: import org.eclipse.jface.resource.ImageRegistry;
015: import org.eclipse.jface.viewers.ArrayContentProvider;
016: import org.eclipse.jface.viewers.DoubleClickEvent;
017: import org.eclipse.jface.viewers.IDoubleClickListener;
018: import org.eclipse.jface.viewers.ISelectionChangedListener;
019: import org.eclipse.jface.viewers.IStructuredSelection;
020: import org.eclipse.jface.viewers.LabelProvider;
021: import org.eclipse.jface.viewers.SelectionChangedEvent;
022: import org.eclipse.jface.viewers.StructuredSelection;
023: import org.eclipse.jface.viewers.TableViewer;
024: import org.eclipse.jface.viewers.Viewer;
025: import org.eclipse.jface.wizard.IWizardContainer;
026: import org.eclipse.swt.SWT;
027: import org.eclipse.swt.graphics.Image;
028: import org.eclipse.swt.layout.FillLayout;
029: import org.eclipse.swt.widgets.Button;
030: import org.eclipse.swt.widgets.Composite;
031: import org.eclipse.swt.widgets.Control;
032: import org.eclipse.swt.widgets.Event;
033:
034: /**
035: * This is a wizard page which is used to select from a set of data sources.
036: * <p>
037: * Each data source is described an instance of
038: *
039: * @see net.refractions.udig.catalog.ui.UDIGImportPageDescriptor.
040: * </p>
041: * <p>
042: * Client code may instantiate objects of this class,but may only call
043: * setSelectedDescriptor(UDIGImportPageDescriptor). All other methods are
044: * wizard lifecycle methods.page
045: * </p>
046: * @author Justin Deoliveira,Refractions Research Inc.,jdeolive@refractions.net
047: */
048: public class DataSourceSelectionPage extends WorkflowWizardPage
049: implements ISelectionChangedListener {
050:
051: /** the selected extension * */
052: private IStructuredSelection selected;
053:
054: /** list of wizard page extension* */
055: private List<UDIGConnectionFactoryDescriptor> descriptors;
056:
057: /** the viewer to select * */
058: private WizardViewer viewer;
059:
060: public DataSourceSelectionPage() {
061: super ("dataSourceWizardPage"); //$NON-NLS-1$
062: setTitle(Messages.DataSourceSelectionPage_pageTitle);
063: setDescription(Messages.DataSourceSelectionPage_defaultMessage);
064: }
065:
066: /**
067: * Sets the selected set of import page descriptiors.
068: *
069: * @param ids
070: * A list of import page identifiers.
071: *
072: */
073: public void select(List<String> ids) {
074: descriptors = ConnectionFactoryManager.instance()
075: .getConnectionFactoryDescriptors(ids);
076: setSelection(descriptors);
077: }
078:
079: /**
080: * Sets the selected import page descriptor.
081: *
082: * @param id
083: * The id of the import page to select.
084: */
085: public void select(String id) {
086: ArrayList<String> l = new ArrayList<String>();
087: l.add(id);
088: select(l);
089: }
090:
091: /**
092: * Sets the selected import page descriptor.
093: *
094: * @param descriptor
095: * The descriptor to be selected.
096: */
097: public void setSelection(
098: List<UDIGConnectionFactoryDescriptor> descriptors) {
099: selected = new StructuredSelection(descriptors);
100: if (viewer != null) {
101: // only set first in viewer
102: if (!selected.isEmpty()) {
103: viewer.setSelection(new StructuredSelection(selected
104: .getFirstElement()));
105: } else {
106: viewer.setSelection(new StructuredSelection());
107: }
108: }
109: }
110:
111: @Override
112: public boolean canFlipToNextPage() {
113: boolean more = super .canFlipToNextPage();
114: if (more) {
115: // if selection set, we are done
116: if (selected != null && selected.size() == 1) {
117: return true;
118: }
119:
120: // if there is only one choice, we are also done
121: if (descriptors != null && descriptors.size() == 1)
122:
123: return true;
124:
125: return false;
126: }
127:
128: return false;
129: }
130:
131: /**
132: * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
133: */
134: public void createControl(Composite parent) {
135:
136: Composite comp = new Composite(parent, SWT.NONE);
137: comp.setLayout(new FillLayout());
138:
139: viewer = new WizardViewer(comp, SWT.SINGLE | SWT.BORDER);
140: viewer.setInput(getDescriptors().toArray());
141: viewer.addDoubleClickListener(new IDoubleClickListener() {
142: public void doubleClick(DoubleClickEvent event) {
143: IWizardContainer container = getContainer();
144: if (container instanceof Dialog) {
145: Dialog d = (Dialog) container;
146: Button button = findButton(d.buttonBar,
147: IDialogConstants.NEXT_ID);
148: if (button != null)
149: button.notifyListeners(SWT.Selection,
150: new Event());
151: }
152: }
153: });
154: viewer.addSelectionChangedListener(this );
155:
156: // check the state for an initial selection
157: DataSourceSelectionState state = (DataSourceSelectionState) getState();
158: if (state.getDescriptor() != null)
159: viewer.setSelection(new StructuredSelection(state
160: .getDescriptor()));
161:
162: setControl(comp);
163: }
164:
165: protected Button findButton(Control buttonBar, int buttonID) {
166: if (buttonBar instanceof Composite) {
167: Composite composite = (Composite) buttonBar;
168: Control[] children = composite.getChildren();
169: for (Control control : children) {
170: if (control instanceof Button) {
171: Button button = (Button) control;
172: if (((Integer) button.getData()).intValue() == buttonID)
173: return button;
174: } else if (control instanceof Composite) {
175: Button button = findButton(control, buttonID);
176: if (button != null)
177: return button;
178: }
179: }
180: }
181: if (buttonBar instanceof Button) {
182: Button button = (Button) buttonBar;
183: if (((Integer) button.getData()).intValue() == buttonID)
184: return button;
185: }
186:
187: return null;
188: }
189:
190: /**
191: * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
192: */
193: public void selectionChanged(SelectionChangedEvent event) {
194: selected = (IStructuredSelection) event.getSelection();
195: if (selected != null && !selected.isEmpty()) {
196: UDIGConnectionFactoryDescriptor descriptor = (UDIGConnectionFactoryDescriptor) selected
197: .getFirstElement();
198:
199: // change the page description + image
200: setImageDescriptor(descriptor.getDescriptionImage());
201: setMessage(descriptor.getDescription());
202:
203: // update the underlying state
204: DataSourceSelectionState state = (DataSourceSelectionState) getState();
205: state.setDescriptor(descriptor);
206: }
207:
208: // update the container buttons
209: getWizard().getContainer().updateButtons();
210: }
211:
212: public Viewer getViewer() {
213: return viewer;
214: }
215:
216: public List<UDIGConnectionFactoryDescriptor> getDescriptors() {
217:
218: List<UDIGConnectionFactoryDescriptor> connectionFactoryDescriptors = ConnectionFactoryManager
219: .instance().getConnectionFactoryDescriptors();
220: return connectionFactoryDescriptors;
221: }
222:
223: protected void saveSelectedDescriptor() {
224: IStructuredSelection selection = (IStructuredSelection) viewer
225: .getSelection();
226: if (selection.isEmpty())
227: return;
228: }
229:
230: private static class WizardViewer extends TableViewer {
231:
232: public WizardViewer(Composite parent, int style) {
233: super (parent, style);
234:
235: setContentProvider(new ArrayContentProvider());
236: setLabelProvider(new LabelProvider() {
237: public String getText(Object object) {
238: UDIGConnectionFactoryDescriptor descriptor = (UDIGConnectionFactoryDescriptor) object;
239:
240: return descriptor.getLabel();
241: }
242:
243: public Image getImage(Object object) {
244: UDIGConnectionFactoryDescriptor descriptor = (UDIGConnectionFactoryDescriptor) object;
245:
246: String id = descriptor.getId();
247: ImageRegistry registry = UiPlugin.getDefault()
248: .getImageRegistry();
249: ImageDescriptor image = descriptor.getImage();
250: synchronized (registry) {
251: if (registry.get(id) == null && image != null) {
252: registry.put(id, image);
253: }
254:
255: return registry.get(id);
256: }
257: }
258: });
259: }
260: }
261: }
|