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.mapgraphic.internal;
016:
017: import java.io.IOException;
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import net.refractions.udig.catalog.CatalogPlugin;
023: import net.refractions.udig.catalog.IGeoResource;
024: import net.refractions.udig.catalog.IService;
025: import net.refractions.udig.catalog.ui.ResolveLabelProviderSimple;
026: import net.refractions.udig.catalog.ui.ResolveTitlesDecorator;
027: import net.refractions.udig.mapgraphic.MapGraphicPlugin;
028: import net.refractions.udig.project.ui.ApplicationGIS;
029: import net.refractions.udig.ui.ProgressManager;
030:
031: import org.eclipse.jface.action.IAction;
032: import org.eclipse.jface.dialogs.IDialogConstants;
033: import org.eclipse.jface.dialogs.TitleAreaDialog;
034: import org.eclipse.jface.viewers.DecoratingLabelProvider;
035: import org.eclipse.jface.viewers.DoubleClickEvent;
036: import org.eclipse.jface.viewers.IDoubleClickListener;
037: import org.eclipse.jface.viewers.ISelection;
038: import org.eclipse.jface.viewers.ISelectionChangedListener;
039: import org.eclipse.jface.viewers.IStructuredSelection;
040: import org.eclipse.jface.viewers.ITreeContentProvider;
041: import org.eclipse.jface.viewers.SelectionChangedEvent;
042: import org.eclipse.jface.viewers.TreeViewer;
043: import org.eclipse.jface.viewers.Viewer;
044: import org.eclipse.swt.SWT;
045: import org.eclipse.swt.graphics.Point;
046: import org.eclipse.swt.layout.GridData;
047: import org.eclipse.swt.widgets.Composite;
048: import org.eclipse.swt.widgets.Control;
049: import org.eclipse.swt.widgets.Display;
050: import org.eclipse.swt.widgets.Shell;
051: import org.eclipse.ui.IWorkbenchWindow;
052: import org.eclipse.ui.IWorkbenchWindowActionDelegate;
053:
054: /**
055: * Opens a dialog with all the MapGraphics
056: *
057: * @author Jesse
058: * @since 1.1.0
059: */
060: public class OtherAction implements IWorkbenchWindowActionDelegate,
061: ITreeContentProvider {
062:
063: public void dispose() {
064: }
065:
066: public void init(IWorkbenchWindow window) {
067: }
068:
069: public void run(IAction action) {
070: TitleAreaDialog d = new TitleAreaDialog(Display.getCurrent()
071: .getActiveShell()) {
072: private TreeViewer viewer;
073:
074: @Override
075: protected int getShellStyle() {
076: return SWT.RESIZE | SWT.MAX | SWT.CLOSE | SWT.MIN
077: | SWT.APPLICATION_MODAL;
078: }
079:
080: @Override
081: protected void configureShell(Shell newShell) {
082: newShell.setText(Messages.OtherAction_shellText);
083: super .configureShell(newShell);
084: }
085:
086: @Override
087: protected Point getInitialSize() {
088: return new Point(400, 400);
089: }
090:
091: @Override
092: protected Control createContents(Composite parent) {
093: Control control = super .createContents(parent);
094: setTitle(Messages.OtherAction_wizardTitle);
095: setMessage(Messages.OtherAction_message1);
096: return control;
097: }
098:
099: @Override
100: protected Control createDialogArea(Composite parent) {
101: Composite composite = (Composite) super
102: .createDialogArea(parent);
103: org.eclipse.swt.widgets.Tree tree = new org.eclipse.swt.widgets.Tree(
104: composite, SWT.V_SCROLL | SWT.MULTI);
105: viewer = new TreeViewer(tree);
106:
107: GridData gridData = new GridData(GridData.FILL_BOTH);
108: gridData.heightHint = SWT.DEFAULT;
109: gridData.widthHint = SWT.DEFAULT;
110: gridData.verticalSpan = 4;
111: tree.setLayoutData(gridData);
112: viewer.setContentProvider(OtherAction.this );
113: ResolveLabelProviderSimple resolveLabelProviderSimple = new ResolveLabelProviderSimple();
114: ResolveTitlesDecorator resolveTitlesDecorator = new ResolveTitlesDecorator(
115: resolveLabelProviderSimple, true);
116: viewer.setLabelProvider(new DecoratingLabelProvider(
117: resolveLabelProviderSimple,
118: resolveTitlesDecorator));
119: viewer.setInput(CatalogPlugin.getDefault()
120: .getLocalCatalog().getById(IService.class,
121: MapGraphicService.SERVICE_URL,
122: ProgressManager.instance().get()));
123: viewer
124: .addPostSelectionChangedListener(new ISelectionChangedListener() {
125:
126: public void selectionChanged(
127: SelectionChangedEvent event) {
128: IStructuredSelection s = (IStructuredSelection) event
129: .getSelection();
130: if (s.isEmpty())
131: return;
132:
133: String title = null;
134: try {
135: title = ((IGeoResource) s
136: .getFirstElement())
137: .getInfo(null).getTitle();
138: } catch (IOException e) {
139: MapGraphicPlugin.log("", e); //$NON-NLS-1$
140: }
141: if (s.size() == 1 && title != null) {
142: setMessage(Messages.OtherAction_p1
143: + title
144: + Messages.OtherAction_p2);
145: } else {
146: setMessage(Messages.OtherAction_addAll);
147: }
148: }
149:
150: });
151: viewer
152: .addDoubleClickListener(new IDoubleClickListener() {
153:
154: public void doubleClick(
155: DoubleClickEvent event) {
156: buttonPressed(IDialogConstants.OK_ID);
157: }
158:
159: });
160: return composite;
161: }
162:
163: @Override
164: protected void createButtonsForButtonBar(Composite parent) {
165: createButton(parent, IDialogConstants.CANCEL_ID,
166: IDialogConstants.CANCEL_LABEL, false);
167: createButton(parent, IDialogConstants.OK_ID,
168: Messages.OtherAction_addButton, true);
169: }
170:
171: @Override
172: protected void buttonPressed(int buttonId) {
173: if (buttonId == IDialogConstants.OK_ID) {
174: IStructuredSelection selection = (IStructuredSelection) viewer
175: .getSelection();
176: Iterator iter = selection.iterator();
177: List<IGeoResource> resourceList = new ArrayList<IGeoResource>();
178: while (iter.hasNext())
179: resourceList.add((IGeoResource) iter.next());
180: ApplicationGIS.addLayersToMap(null, resourceList,
181: -1);
182: }
183: super .buttonPressed(buttonId);
184: }
185: };
186: d.open();
187:
188: }
189:
190: public void selectionChanged(IAction action, ISelection selection) {
191: }
192:
193: public Object[] getElements(Object inputElement) {
194: if (inputElement instanceof IService) {
195: try {
196: return ((IService) inputElement).members(
197: ProgressManager.instance().get()).toArray();
198: } catch (IOException e) {
199: throw (RuntimeException) new RuntimeException()
200: .initCause(e);
201: }
202: }
203: return null;
204: }
205:
206: public void inputChanged(Viewer viewer, Object oldInput,
207: Object newInput) {
208: }
209:
210: public Object[] getChildren(Object parentElement) {
211: return getElements(parentElement);
212: }
213:
214: public Object getParent(Object element) {
215: return null;
216: }
217:
218: public boolean hasChildren(Object element) {
219:
220: if (element instanceof IService) {
221: return true;
222: }
223: return false;
224: }
225:
226: }
|