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.ui;
018:
019: import net.refractions.udig.catalog.CatalogPlugin;
020: import net.refractions.udig.catalog.IGeoResource;
021: import net.refractions.udig.catalog.IResolve;
022: import net.refractions.udig.catalog.IResolve.Status;
023: import net.refractions.udig.catalog.ui.internal.Messages;
024:
025: import org.eclipse.jface.action.StatusLineManager;
026: import org.eclipse.jface.viewers.DecoratingLabelProvider;
027: import org.eclipse.jface.viewers.ISelection;
028: import org.eclipse.jface.viewers.ISelectionChangedListener;
029: import org.eclipse.jface.viewers.IStructuredSelection;
030: import org.eclipse.jface.viewers.SelectionChangedEvent;
031: import org.eclipse.jface.viewers.TreeViewer;
032: import org.eclipse.swt.SWT;
033: import org.eclipse.swt.widgets.Composite;
034:
035: /**
036: * Provides Tree view of the Registry.
037: * <p>
038: * Supports the following:
039: * <ul>
040: * <li>List
041: * <li>IService
042: * <li>IGeoReference
043: * <li>ICatalog
044: * <li>String
045: * </ul>
046: * </p>
047: * <p>
048: * To display a message or status please use a Collections.singletonList( "hello world" ).
049: * </p>
050: *
051: * @author jeichar
052: * @since 0.3
053: */
054: public class CatalogTreeViewer extends TreeViewer implements
055: ISelectionChangedListener {
056: private IMessageBoard messageBoard;
057:
058: /**
059: * Construct <code>CatalogTreeViewer</code>.
060: *
061: * @param parent
062: */
063: public CatalogTreeViewer(Composite parent, boolean titles) {
064: this (parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL
065: | SWT.BORDER, titles); // no border by
066: // default
067: }
068:
069: /**
070: * Construct <code>CatalogTreeViewer</code>.
071: *
072: * @param parent
073: */
074: public CatalogTreeViewer(Composite parent) {
075: this (parent, true); // no border by
076: // default
077: }
078:
079: /**
080: * Construct <code>CatalogTreeViewer</code>.
081: * <p>
082: * You will need to set your input:
083: *
084: * <pre><code>
085: * CatalogTreeViewer viewer = new CatalogTreeViewer(parent, SWT.DEFAULT);
086: * viewer.setInput(CatalogPlugin.getDefault().getLocalCatalog());
087: * </code></pre>
088: *
089: * </p>
090: *
091: * @param parent Parent component
092: * @param style The other constructor uses SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER
093: */
094: public CatalogTreeViewer(Composite parent, int style, boolean titles) {
095: super (parent, style | SWT.VIRTUAL);
096: setContentProvider(new ResolveContentProvider());
097: ResolveLabelProviderSimple resolveLabelProviderSimple = new ResolveLabelProviderSimple();
098: if (titles) {
099: setLabelProvider(new DecoratingLabelProvider(
100: resolveLabelProviderSimple,
101: new ResolveTitlesDecorator(
102: resolveLabelProviderSimple)));
103: } else {
104: setLabelProvider(resolveLabelProviderSimple);
105: }
106: setInput(CatalogPlugin.getDefault().getLocalCatalog());
107: setSorter(new CatalogViewerSorter());
108:
109: addSelectionChangedListener(this );
110:
111: }
112:
113: public void selectionChanged(SelectionChangedEvent event) {
114: if (messageBoard == null)
115: return;
116:
117: ISelection selection = event.getSelection();
118: if (selection instanceof IStructuredSelection) {
119: IStructuredSelection sel = (IStructuredSelection) selection;
120: if (sel.size() == 1) {
121: Object obj = sel.getFirstElement();
122: if (obj instanceof IResolve) {
123: IResolve resolve = (IResolve) obj;
124: if (resolve.getStatus() == Status.BROKEN) {
125: messageBoard.putMessage(
126: Messages.CatalogTreeViewer_broken,
127: IMessageBoard.Type.ERROR);
128: } else if (resolve.getStatus() == Status.RESTRICTED_ACCESS) {
129: messageBoard.putMessage(
130: Messages.CatalogTreeViewer_permission,
131: IMessageBoard.Type.ERROR);
132: } else {
133: messageBoard.putMessage(null,
134: IMessageBoard.Type.NORMAL);
135: }
136:
137: } else {
138: messageBoard.putMessage(null,
139: IMessageBoard.Type.NORMAL);
140: }
141: } else {
142: messageBoard
143: .putMessage(null, IMessageBoard.Type.NORMAL);
144: }
145: } else {
146: messageBoard.putMessage(null, IMessageBoard.Type.NORMAL);
147: }
148: }
149:
150: /**
151: * Sets the message board that this viewer will display status messages on.
152: *
153: * @param messageBoard
154: *
155: * @see StatusLineMessageBoardAdapter
156: * @see IResolve#getStatus()
157: * @see IResolve#getMessage()
158: */
159: public void setMessageBoard(IMessageBoard messageBoard) {
160: this.messageBoard = messageBoard;
161: }
162: }
|