001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.catalog.ui.workflow;
016:
017: import java.util.ArrayList;
018: import java.util.Arrays;
019: import java.util.Collection;
020: import java.util.Comparator;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.Set;
024: import java.util.Map.Entry;
025:
026: import net.refractions.udig.catalog.ui.internal.Messages;
027: import net.refractions.udig.catalog.ui.workflow.ConnectionFailureState.Data;
028:
029: import org.eclipse.jface.viewers.ColumnWeightData;
030: import org.eclipse.jface.viewers.ILabelProvider;
031: import org.eclipse.jface.viewers.ILabelProviderListener;
032: import org.eclipse.jface.viewers.ISelectionChangedListener;
033: import org.eclipse.jface.viewers.IStructuredSelection;
034: import org.eclipse.jface.viewers.ITreeContentProvider;
035: import org.eclipse.jface.viewers.SelectionChangedEvent;
036: import org.eclipse.jface.viewers.TableLayout;
037: import org.eclipse.jface.viewers.TreeViewer;
038: import org.eclipse.jface.viewers.Viewer;
039: import org.eclipse.jface.viewers.ViewerSorter;
040: import org.eclipse.swt.SWT;
041: import org.eclipse.swt.custom.SashForm;
042: import org.eclipse.swt.graphics.Image;
043: import org.eclipse.swt.layout.FillLayout;
044: import org.eclipse.swt.widgets.Composite;
045: import org.eclipse.swt.widgets.Text;
046: import org.eclipse.swt.widgets.Tree;
047: import org.eclipse.swt.widgets.TreeItem;
048:
049: /**
050: * Wizard Page associated with ConnectionFailurePage
051: *
052: * @author Jesse
053: * @since 1.1.0
054: */
055: public class ConnectionFailurePage extends WorkflowWizardPage implements
056: ILabelProvider, ITreeContentProvider {
057:
058: private TreeViewer viewer;
059: private Text details;
060: private static String canProcess = Messages.ConnectionFailurePage_canProcess;
061:
062: public ConnectionFailurePage() {
063: super (Messages.ConnectionFailurePage_title);
064: setTitle(Messages.ConnectionFailurePage_displayedTitle);
065: setMessage(Messages.ConnectionFailurePage_message);
066: }
067:
068: public void createControl(Composite parent) {
069: SashForm form = new SashForm(parent, SWT.VERTICAL);
070: Tree tree = new Tree(form, SWT.V_SCROLL | SWT.SINGLE);
071: TableLayout layout = new TableLayout();
072: layout.addColumnData(new ColumnWeightData(100, false));
073: tree.setLayout(layout);
074: tree.setLinesVisible(true);
075: viewer = new TreeViewer(tree);
076:
077: viewer.setContentProvider(this );
078: viewer.setLabelProvider(this );
079:
080: viewer
081: .addPostSelectionChangedListener(new ISelectionChangedListener() {
082:
083: public void selectionChanged(
084: SelectionChangedEvent event) {
085: setDetails();
086: }
087: });
088:
089: viewer.setSorter(new ViewerSorter() {
090: @Override
091: public void sort(Viewer viewer, Object[] elements) {
092: Arrays.sort(elements, new Comparator<Object>() {
093:
094: public int compare(Object o1, Object o2) {
095: return 0;
096: }
097:
098: });
099: }
100:
101: });
102:
103: Composite composite = new Composite(form, SWT.BORDER);
104: composite.setLayout(new FillLayout());
105: details = new Text(composite, SWT.BORDER | SWT.WRAP
106: | SWT.READ_ONLY | SWT.V_SCROLL);
107:
108: form.setWeights(new int[] { 75, 25 });
109:
110: setControl(form);
111: }
112:
113: private void setDetails() {
114: Object firstElement = ((IStructuredSelection) viewer
115: .getSelection()).getFirstElement();
116: if (firstElement instanceof Data) {
117: Data data = (Data) firstElement;
118: String string = data.message;
119: if (string == null)
120: string = canProcess;
121: details.setText(string);
122: } else {
123: details.setText(Messages.ConnectionFailurePage_selectChild);
124: }
125: }
126:
127: @Override
128: public void shown() {
129: viewer.setInput(getState().getReports());
130: if (viewer.getTree().getItemCount() > 0) {
131: TreeItem item = viewer.getTree().getItem(0);
132: viewer.getTree().setSelection(item);
133:
134: setDetails();
135: }
136: }
137:
138: public ConnectionFailureState getState() {
139: return (ConnectionFailureState) super .getState();
140: }
141:
142: @Override
143: public boolean canFlipToNextPage() {
144: return false;
145: }
146:
147: public void addListener(ILabelProviderListener listener) {
148: }
149:
150: public boolean isLabelProperty(Object element, String property) {
151: return false;
152: }
153:
154: public void removeListener(ILabelProviderListener listener) {
155: }
156:
157: public Image getImage(Object element) {
158: return null;
159: }
160:
161: @SuppressWarnings("unchecked")
162: public String getText(Object element) {
163: if (element instanceof Entry) {
164: Entry<String, List<Data>> entry = (Entry<String, List<Data>>) element;
165: return entry.getValue().get(0).name;
166: }
167: if (element instanceof Data) {
168: Data data = (Data) element;
169: if (getParent(element) == getState().getReports())
170: return data.name;
171: return data.url.toString();
172: }
173: return null;
174: }
175:
176: @SuppressWarnings("unchecked")
177: public Object[] getChildren(Object parentElement) {
178: if (parentElement instanceof Map) {
179: Map<String, List<Data>> map = (Map<String, List<Data>>) parentElement;
180: if (map.values().iterator().next().size() == 1) {
181:
182: // no need for hierarchy since each element only has a single data.
183: Collection<List<Data>> values = map.values();
184: List<Data> all = new ArrayList<Data>();
185: for (List<Data> list : values) {
186: all.addAll(list);
187: }
188: return all.toArray();
189: } else {
190: return map.entrySet().toArray();
191: }
192: }
193: if (parentElement instanceof Entry) {
194: Entry<String, List<Data>> entry = (Entry<String, List<Data>>) parentElement;
195: return entry.getValue().toArray();
196: }
197: if (parentElement instanceof Data) {
198: return null;
199: }
200: return null;
201: }
202:
203: public Object getParent(Object parentElement) {
204: if (parentElement instanceof Map) {
205: return null;
206: }
207: if (parentElement instanceof Entry) {
208: return getState().getReports();
209: }
210: if (parentElement instanceof Data) {
211: Set<Entry<String, List<Data>>> set = getState()
212: .getReports().entrySet();
213: Data data = (Data) parentElement;
214: for (Entry<String, List<Data>> entry : set) {
215: if (entry.getKey().equals(data.id)) {
216: if (entry.getValue().size() == 1)
217: return getState().getReports();
218: return entry;
219: }
220: }
221: }
222: return null;
223: }
224:
225: public boolean hasChildren(Object parentElement) {
226: if (parentElement instanceof Map) {
227: return true;
228: }
229: if (parentElement instanceof Entry) {
230: return true;
231: }
232: if (parentElement instanceof Data) {
233: return false;
234: }
235: return false;
236: }
237:
238: public Object[] getElements(Object elements) {
239: return getChildren(elements);
240: }
241:
242: public void inputChanged(Viewer viewer, Object oldInput,
243: Object newInput) {
244: }
245: }
|