001: package net.refractions.udig.catalog.ui;
002:
003: import java.io.ByteArrayOutputStream;
004: import java.io.PrintStream;
005: import java.util.Map;
006:
007: import net.refractions.udig.catalog.IService;
008: import net.refractions.udig.catalog.internal.ui.ConnectionPage;
009: import net.refractions.udig.catalog.ui.internal.Messages;
010: import net.refractions.udig.catalog.ui.workflow.ConnectionErrorState;
011: import net.refractions.udig.catalog.ui.workflow.WorkflowWizardPage;
012:
013: import org.eclipse.jface.viewers.ArrayContentProvider;
014: import org.eclipse.jface.viewers.ISelectionChangedListener;
015: import org.eclipse.jface.viewers.IStructuredSelection;
016: import org.eclipse.jface.viewers.LabelProvider;
017: import org.eclipse.jface.viewers.ListViewer;
018: import org.eclipse.jface.viewers.SelectionChangedEvent;
019: import org.eclipse.jface.wizard.IWizardPage;
020: import org.eclipse.swt.SWT;
021: import org.eclipse.swt.layout.FormAttachment;
022: import org.eclipse.swt.layout.FormData;
023: import org.eclipse.swt.layout.FormLayout;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Control;
026: import org.eclipse.swt.widgets.Label;
027: import org.eclipse.swt.widgets.Text;
028:
029: /**
030: * A wizard page which displays a set of IConnectionErrorHandler objects.
031: * <p>
032: * This page is intended for use in a wizard extending
033: *
034: * @see net.refractions.udig.catalog.ui.UDIGImportPage to connect to a service.
035: * </p>
036: * @author Justin Deoliveira
037: */
038: public class ConnectionErrorPage extends WorkflowWizardPage {
039:
040: /** the current handler * */
041: IConnectionErrorHandler handler;
042:
043: public ConnectionErrorPage() {
044: super (Messages.ConnectionErrorPage_pageName);
045:
046: setDescription(Messages.ConnectionErrorPage_pageDescription);
047: setTitle(Messages.ConnectionErrorPage_pageTitle);
048: }
049:
050: @Override
051: public boolean isPageComplete() {
052: return handler.isComplete();
053: }
054:
055: @Override
056: public boolean canFlipToNextPage() {
057: if (handler != null)
058: return handler.canRecover();
059:
060: return false;
061: }
062:
063: public void createControl(Composite parent) {
064:
065: ConnectionErrorState state = (ConnectionErrorState) getState();
066:
067: handler = new SimpleConnectionErrorHandler(state.getErrors());
068: handler.createControl(parent);
069:
070: setControl(handler.getControl());
071: }
072:
073: @Override
074: public IWizardPage getNextPage() {
075: if (handler.canRecover() && handler.isComplete()) {
076: ConnectionPage page = (ConnectionPage) getPreviousPage();
077: return page.getNextPage();
078: }
079:
080: return super .getNextPage();
081: }
082:
083: static class SimpleConnectionErrorHandler extends
084: IConnectionErrorHandler {
085:
086: Map<IService, Throwable> errors;
087:
088: SimpleConnectionErrorHandler(Map<IService, Throwable> errors) {
089: this .errors = errors;
090:
091: setName("Simple"); //$NON-NLS-1$
092: }
093:
094: @Override
095: public boolean canHandle(IService service, Throwable t) {
096: return true;
097: }
098:
099: @Override
100: public boolean canRecover() {
101: return false;
102: }
103:
104: @Override
105: protected Control create(Composite parent) {
106: Composite root = new Composite(parent, SWT.NONE);
107: root.setLayout(new FormLayout());
108:
109: Label label = new Label(root, SWT.LEFT);
110: label.setText(Messages.ConnectionErrorPage_message);
111:
112: ListViewer listViewer = new ListViewer(root);
113: listViewer.setLabelProvider(new LabelProvider() {
114: @Override
115: public String getText(Object element) {
116: IService service = (IService) element;
117: return service.getIdentifier().toString();
118: }
119: });
120: listViewer.setContentProvider(new ArrayContentProvider());
121: listViewer.setInput(errors.keySet().toArray());
122:
123: final Text text = new Text(root, SWT.BORDER | SWT.MULTI
124: | SWT.V_SCROLL | SWT.SCROLL_LINE);
125:
126: FormData layoutData = new FormData();
127: layoutData.top = new FormAttachment(0);
128: layoutData.left = new FormAttachment(0);
129: label.setLayoutData(layoutData);
130:
131: layoutData = new FormData();
132: layoutData.top = new FormAttachment(label, 2, SWT.BOTTOM);
133: layoutData.left = new FormAttachment(0);
134: layoutData.right = new FormAttachment(100);
135: layoutData.bottom = new FormAttachment(50);
136: listViewer.getList().setLayoutData(layoutData);
137:
138: layoutData = new FormData();
139: layoutData.top = new FormAttachment(listViewer.getList(),
140: 2, SWT.BOTTOM);
141: layoutData.left = new FormAttachment(0);
142: layoutData.right = new FormAttachment(100);
143: layoutData.bottom = new FormAttachment(100);
144: text.setLayoutData(layoutData);
145:
146: listViewer
147: .addSelectionChangedListener(new ISelectionChangedListener() {
148:
149: public void selectionChanged(
150: SelectionChangedEvent event) {
151: IStructuredSelection selection = (IStructuredSelection) event
152: .getSelection();
153: if (selection == null
154: || selection.isEmpty())
155: return;
156:
157: IService service = (IService) selection
158: .getFirstElement();
159: Throwable t = errors.get(service);
160:
161: ByteArrayOutputStream out = new ByteArrayOutputStream();
162: t.printStackTrace(new PrintStream(out));
163: text.setText(new String(out.toByteArray()));
164: }
165: });
166:
167: return root;
168: }
169:
170: }
171: }
|