0001: /*
0002: * uDig - User Friendly Desktop Internet GIS client
0003: * http://udig.refractions.net
0004: * (C) 2004, Refractions Research Inc.
0005: *
0006: * This library is free software; you can redistribute it and/or
0007: * modify it under the terms of the GNU Lesser General Public
0008: * License as published by the Free Software Foundation;
0009: * version 2.1 of the License.
0010: *
0011: * This library is distributed in the hope that it will be useful,
0012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0014: * Lesser General Public License for more details.
0015: *
0016: */
0017: package net.refractions.udig.project.ui.internal;
0018:
0019: import java.util.ArrayList;
0020: import java.util.Iterator;
0021: import java.util.List;
0022:
0023: import net.refractions.udig.internal.ui.IDropTargetProvider;
0024: import net.refractions.udig.project.BlackboardEvent;
0025: import net.refractions.udig.project.EditManagerEvent;
0026: import net.refractions.udig.project.IBlackboard;
0027: import net.refractions.udig.project.IBlackboardListener;
0028: import net.refractions.udig.project.IEditManagerListener;
0029: import net.refractions.udig.project.ILayer;
0030: import net.refractions.udig.project.IMap;
0031: import net.refractions.udig.project.IProjectElement;
0032: import net.refractions.udig.project.internal.ContextModel;
0033: import net.refractions.udig.project.internal.Layer;
0034: import net.refractions.udig.project.internal.Map;
0035: import net.refractions.udig.project.internal.ProjectFactory;
0036: import net.refractions.udig.project.internal.ProjectPackage;
0037: import net.refractions.udig.project.internal.ProjectPlugin;
0038: import net.refractions.udig.project.ui.AdapterFactoryLabelProviderDecorator;
0039: import net.refractions.udig.project.ui.ApplicationGIS;
0040: import net.refractions.udig.project.ui.internal.actions.Delete;
0041: import net.refractions.udig.project.ui.internal.actions.MylarAction;
0042: import net.refractions.udig.project.ui.tool.IToolManager;
0043: import net.refractions.udig.ui.PlatformGIS;
0044: import net.refractions.udig.ui.UDIGDragDropUtilities;
0045: import net.refractions.udig.ui.ZoomingDialog;
0046:
0047: import org.eclipse.core.runtime.IAdaptable;
0048: import org.eclipse.emf.common.notify.Adapter;
0049: import org.eclipse.emf.common.notify.Notification;
0050: import org.eclipse.emf.common.notify.impl.AdapterImpl;
0051: import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
0052: import org.eclipse.jface.action.Action;
0053: import org.eclipse.jface.action.GroupMarker;
0054: import org.eclipse.jface.action.IAction;
0055: import org.eclipse.jface.action.IMenuListener;
0056: import org.eclipse.jface.action.IMenuManager;
0057: import org.eclipse.jface.action.IToolBarManager;
0058: import org.eclipse.jface.action.MenuManager;
0059: import org.eclipse.jface.action.Separator;
0060: import org.eclipse.jface.viewers.CheckStateChangedEvent;
0061: import org.eclipse.jface.viewers.CheckboxTreeViewer;
0062: import org.eclipse.jface.viewers.DoubleClickEvent;
0063: import org.eclipse.jface.viewers.ICheckStateListener;
0064: import org.eclipse.jface.viewers.IDoubleClickListener;
0065: import org.eclipse.jface.viewers.ILabelProviderListener;
0066: import org.eclipse.jface.viewers.ISelection;
0067: import org.eclipse.jface.viewers.ISelectionChangedListener;
0068: import org.eclipse.jface.viewers.IStructuredSelection;
0069: import org.eclipse.jface.viewers.LabelProviderChangedEvent;
0070: import org.eclipse.jface.viewers.SelectionChangedEvent;
0071: import org.eclipse.jface.viewers.StructuredSelection;
0072: import org.eclipse.jface.viewers.Viewer;
0073: import org.eclipse.jface.window.SameShellProvider;
0074: import org.eclipse.swt.SWT;
0075: import org.eclipse.swt.dnd.DropTargetEvent;
0076: import org.eclipse.swt.widgets.Composite;
0077: import org.eclipse.swt.widgets.Display;
0078: import org.eclipse.swt.widgets.Event;
0079: import org.eclipse.swt.widgets.Menu;
0080: import org.eclipse.swt.widgets.Shell;
0081: import org.eclipse.ui.IActionBars;
0082: import org.eclipse.ui.IEditorPart;
0083: import org.eclipse.ui.IKeyBindingService;
0084: import org.eclipse.ui.IPartListener;
0085: import org.eclipse.ui.ISelectionListener;
0086: import org.eclipse.ui.IViewSite;
0087: import org.eclipse.ui.IWorkbenchActionConstants;
0088: import org.eclipse.ui.IWorkbenchPart;
0089: import org.eclipse.ui.PartInitException;
0090: import org.eclipse.ui.PlatformUI;
0091: import org.eclipse.ui.actions.ActionFactory;
0092: import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
0093: import org.eclipse.ui.dialogs.PropertyDialogAction;
0094: import org.eclipse.ui.part.EditorPart;
0095: import org.eclipse.ui.part.ViewPart;
0096: import org.eclipse.ui.views.properties.IPropertySheetPage;
0097:
0098: /**
0099: * The Layers View.
0100: *
0101: * @author jeichar
0102: * @since 0.6.0
0103: */
0104: public class LayersView extends ViewPart implements
0105: IDropTargetProvider, IDoubleClickListener,
0106: ISelectionChangedListener {
0107:
0108: public static final String ID = "net.refractions.udig.project.ui.layerManager"; //$NON-NLS-1$
0109:
0110: /**
0111: * @see org.eclipse.ui.IWorkbenchPart#dispose()
0112: */
0113: public void dispose() {
0114: disposeInternal();
0115: super .dispose();
0116: }
0117:
0118: protected void disposeInternal() {
0119: if (PlatformUI.getWorkbench().isClosing()) {
0120: ProjectPlugin.getPlugin().turnOffEvents();
0121: }
0122: if (currentMap != null) {
0123: currentMap.removeDeepAdapter(checkboxContextListener);
0124: }
0125:
0126: labelProvider.dispose();
0127: labelProvider.removeListener(labelProviderListener);
0128: labelProvider = null;
0129: labelProviderListener = null;
0130:
0131: getSite().getWorkbenchWindow().getPartService()
0132: .removePartListener(partServiceListener);
0133: }
0134:
0135: /**
0136: * @return Returns the currentMap.
0137: */
0138: public synchronized Map getCurrentMap() {
0139: return currentMap;
0140: }
0141:
0142: IMap placeholder = ProjectFactory.eINSTANCE.createMap();
0143:
0144: class EditManagerListener implements IEditManagerListener {
0145: Map map;
0146:
0147: synchronized void setCurrentMap(final Map currentMap) {
0148: this .map = currentMap;
0149: }
0150:
0151: public void changed(final EditManagerEvent event) {
0152: if (getCurrentMap() != map) {
0153: map.getEditManager().removeListener(this );
0154: return;
0155: }
0156: if (event.getType() == EditManagerEvent.SELECTED_LAYER) {
0157:
0158: Runnable runnable = new Runnable() {
0159: public void run() {
0160: if (((IStructuredSelection) viewer
0161: .getSelection()).getFirstElement() != event
0162: .getNewValue()) {
0163: StructuredSelection structuredSelection = new StructuredSelection(
0164: event.getNewValue());
0165: getSite().getSelectionProvider()
0166: .setSelection(structuredSelection);
0167: }
0168: if (mylarOn()) {
0169: viewer.update(map.getLayersInternal()
0170: .toArray(), null);
0171: }
0172: }
0173:
0174: private boolean mylarOn() {
0175: Object on = map.getBlackboard().get(
0176: MylarAction.KEY);
0177: if (on instanceof Boolean)
0178: return ((Boolean) on).booleanValue();
0179: return false;
0180: }
0181: };
0182: if (Display.getCurrent() == null)
0183: Display.getDefault().asyncExec(runnable);
0184: else
0185: runnable.run();
0186: }
0187: }
0188:
0189: }
0190:
0191: private EditManagerListener editManagerListener;
0192:
0193: private IBlackboardListener mylarListener = new IBlackboardListener() {
0194:
0195: public void blackBoardChanged(BlackboardEvent event) {
0196: if (event.getKey() == MylarAction.KEY
0197: && event.getOldValue() != event.getNewValue()) {
0198: updateViewer();
0199: }
0200: }
0201:
0202: public void blackBoardCleared(IBlackboard source) {
0203: updateViewer();
0204: }
0205:
0206: };
0207:
0208: private void updateViewer() {
0209: Runnable runnable = new Runnable() {
0210: public void run() {
0211: viewer.update(getCurrentMap().getMapLayers().toArray(),
0212: new String[] { MylarAction.KEY });
0213: }
0214: };
0215: if (Display.getCurrent() == null)
0216: Display.getDefault().asyncExec(runnable);
0217: else
0218: runnable.run();
0219: }
0220:
0221: /**
0222: * @param currentMap The currentMap to set.
0223: */
0224: @SuppressWarnings("unchecked")
0225: public synchronized void setCurrentMap(final Map currentMap) {
0226:
0227: if (this .currentMap != null) {
0228: this .currentMap.removeDeepAdapter(checkboxContextListener);
0229: this .currentMap.getBlackboard().removeListener(
0230: mylarListener);
0231: }
0232:
0233: this .currentMap = currentMap;
0234: if (viewer != null)
0235: viewer.setInput(currentMap == null ? placeholder
0236: : currentMap);
0237:
0238: if (editManagerListener == null)
0239: editManagerListener = new EditManagerListener();
0240:
0241: if (currentMap != null) {
0242: editManagerListener.setCurrentMap(currentMap);
0243: this .currentMap.getBlackboard().addListener(mylarListener);
0244: currentMap.addDeepAdapter(checkboxContextListener);
0245:
0246: if (!(currentMap.getEditManager())
0247: .containsListener(editManagerListener))
0248: currentMap.getEditManager().addListener(
0249: editManagerListener);
0250:
0251: Object selectedLayer = currentMap.getEditManager()
0252: .getSelectedLayer();
0253: if (selectedLayer != null && viewer != null) {
0254: viewer.setSelection(new StructuredSelection(
0255: selectedLayer));
0256: }
0257: updateCheckboxes();
0258: }
0259: }
0260:
0261: /**
0262: * TODO Seems we don't really need that LayersView listens
0263: * selection changing. To display layers we need to listen only
0264: * activating of MapEditor. Also it solves some problems and bugs with
0265: * listeners hell during LayersView closing and opening multiple times.
0266: *
0267: *
0268: * @author Vitalus
0269: *
0270: */
0271: private class MapEditorListener implements IPartListener,
0272: ISelectionChangedListener {
0273:
0274: /*
0275: * Basically: If an editor is activated, then we ask it if it can turn into a map. If the
0276: * selection changes, we ask if the selection can turn into a map. If it can't, we ask the
0277: * editor part again. If the editor part cannot serve up a map, blank out the layers view.
0278: * (This way editor parts with only one map will still display the layers if something else
0279: * is selected.
0280: *
0281: * Comment by Vitalus: do we really need?
0282: */
0283:
0284: private IWorkbenchPart currentPart;
0285:
0286: /**
0287: * @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)
0288: */
0289: public void partActivated(IWorkbenchPart part) {
0290: if (part == currentPart)
0291: return;
0292: if (part instanceof IEditorPart) {
0293: currentPart = part;
0294: IAdaptable adaptable = (IAdaptable) part;
0295: Object obj = adaptable.getAdapter(Map.class);
0296:
0297: if (obj instanceof Map) {
0298: // if (part.getSite() != null) {
0299: // ISelectionProvider provider = part.getSite().getSelectionProvider();
0300: // if (provider != null) {
0301: // provider.addSelectionChangedListener(this);
0302: // }
0303: // }
0304: setCurrentMap(((Map) obj));
0305: }
0306: }
0307: }
0308:
0309: // private LayerApplicabilityMenuCreator applicabilityCreator;
0310: // private LayerApplicabilityMenuCreator getApplicabilityMenu() {
0311: // if (applicabilityCreator == null) {
0312: // applicabilityCreator = new LayerApplicabilityMenuCreator();
0313: // }
0314: //
0315: // return applicabilityCreator;
0316: // }
0317: // private void addLayersMenu(EditorPart editor) {
0318: // IMenuManager
0319: // manager=editor.getEditorSite().getActionBars().getMenuManager();
0320: // IMenuManager layerMenu=manager.findMenuUsingPath("layer");
0321: // //$NON-NLS-1$
0322: // layerMenu.add(getApplicabilityMenu().getMenuManager());
0323: // editor.getEditorSite().getActionBars().updateActionBars();
0324: // }
0325:
0326: /**
0327: * @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)
0328: */
0329: public void partBroughtToTop(IWorkbenchPart part) {
0330: partActivated(part);
0331: }
0332:
0333: /**
0334: * @see org.eclipse.ui.IPartListener#partClosed(org.eclipse.ui.IWorkbenchPart)
0335: */
0336: public void partClosed(IWorkbenchPart part) {
0337: if (part == this ) {
0338: disposeInternal();
0339: return;
0340: }
0341: if (part != currentPart)
0342: return;
0343: // if (part.getSite() != null) {
0344: // ISelectionProvider provider = part.getSite().getSelectionProvider();
0345: // if (provider != null) {
0346: // provider.removeSelectionChangedListener(this);
0347: // }
0348: // }
0349: currentPart = null;
0350: if (part instanceof IEditorPart) {
0351: setCurrentMap(null);
0352: if (part.getSite().getPage().getEditorReferences().length == 0)
0353: removeApplicabilityMenu((EditorPart) part);
0354: viewer.refresh(true);
0355: }
0356: }
0357:
0358: private void removeApplicabilityMenu(EditorPart part) {
0359:
0360: // IMenuManager manager = part.getEditorSite().getActionBars()
0361: // .getMenuManager();
0362: // manager.findMenuUsingPath("layer/,)
0363: }
0364:
0365: /**
0366: * @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)
0367: */
0368: public void partDeactivated(IWorkbenchPart part) {
0369: }
0370:
0371: /**
0372: * @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
0373: */
0374: public void partOpened(IWorkbenchPart part) {
0375: // partActivated(part);
0376: }
0377:
0378: public void selectionChanged(SelectionChangedEvent event) {
0379: // StructuredSelection selection = (StructuredSelection) event.getSelection();
0380: // Map found = null;
0381: // Iterator iter = selection.iterator();
0382: // while( iter.hasNext() ) {
0383: // Object obj = iter.next();
0384: // Map map = null;
0385: // if (obj instanceof Map) {
0386: // map = (Map) obj;
0387: // } else if (obj instanceof IAdaptable) {
0388: // IAdaptable adaptable = (IAdaptable) obj;
0389: // map = (Map) adaptable.getAdapter(Map.class);
0390: // }
0391: // if (map != null) {
0392: // // Abort if we find two valid maps - want only one
0393: // if (found != null) {
0394: // map = null;
0395: // break;
0396: // } else {
0397: // found = map;
0398: // }
0399: // }
0400: // }
0401: // if (found != null) {
0402: // setCurrentMap(found);
0403: // } else {
0404: // partBroughtToTop(currentPart);
0405: // }
0406: }
0407: }
0408:
0409: private abstract class LayerAction extends Action implements
0410: ISelectionListener {
0411:
0412: protected IStructuredSelection selection;
0413:
0414: /**
0415: * Construct <code>LayerAction</code>.
0416: */
0417: public LayerAction() {
0418: getSite().getPage().addSelectionListener(this );
0419: }
0420:
0421: /**
0422: * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart,
0423: * org.eclipse.jface.viewers.ISelection)
0424: */
0425: public void selectionChanged(IWorkbenchPart part,
0426: ISelection selection) {
0427: if (!(selection instanceof IStructuredSelection))
0428: return;
0429: this .selection = (IStructuredSelection) selection;
0430: if (part instanceof LayersView && selection != null
0431: && !selection.isEmpty())
0432: setEnabled(true);
0433: }
0434:
0435: /**
0436: * @see org.eclipse.jface.action.Action#setEnabled(boolean)
0437: */
0438: public void setEnabled(boolean enabled) {
0439: super .setEnabled(false);
0440:
0441: if (!enabled || selection == null || selection.isEmpty())
0442: return;
0443:
0444: for (Iterator iter = selection.iterator(); iter.hasNext();) {
0445: Object obj = iter.next();
0446: if (!(obj instanceof Layer))
0447: return;
0448: }
0449: super .setEnabled(true);
0450: }
0451:
0452: }
0453:
0454: CheckboxTreeViewer viewer;
0455:
0456: net.refractions.udig.project.ui.internal.LayersView.LayerAction downAction;
0457:
0458: Map currentMap;
0459:
0460: net.refractions.udig.project.ui.internal.LayersView.LayerAction upAction;
0461:
0462: private MapEditorListener partServiceListener = new MapEditorListener();
0463:
0464: void updateLabels() {
0465: final Map currentMap;
0466: synchronized (this ) {
0467: currentMap = this .currentMap;
0468: }
0469: if (currentMap == null)
0470: return;
0471:
0472: PlatformGIS.syncInDisplayThread(new Runnable() {
0473: public void run() {
0474: if (PlatformUI.getWorkbench().isClosing())
0475: return;
0476: if (viewer != null) {
0477: viewer.refresh(true);
0478: }
0479: }
0480: });
0481: }
0482:
0483: void updateCheckboxes() {
0484: final Map currentMap;
0485: synchronized (this ) {
0486: currentMap = this .currentMap;
0487: }
0488: if (currentMap == null)
0489: return;
0490: final List<Layer> checkedLayers = new ArrayList<Layer>();
0491: for (Layer layer : currentMap.getLayersInternal())
0492: if (layer.isVisible())
0493: checkedLayers.add(layer);
0494: PlatformGIS.syncInDisplayThread(new Runnable() {
0495: public void run() {
0496: if (PlatformUI.getWorkbench().isClosing())
0497: return;
0498: if (viewer != null) {
0499: viewer.refresh(true);
0500: viewer.setCheckedElements(checkedLayers.toArray());
0501: ILayer selectedLayer = currentMap.getEditManager()
0502: .getSelectedLayer();
0503: if (selectedLayer != null)
0504: viewer.setSelection(new StructuredSelection(
0505: selectedLayer), true);
0506: }
0507: }
0508: });
0509: }
0510:
0511: void updateCheckbox(final Layer layer) {
0512: PlatformGIS.syncInDisplayThread(new Runnable() {
0513: public void run() {
0514: if (PlatformUI.getWorkbench().isClosing())
0515: return;
0516: viewer.refresh(false);
0517: viewer.setChecked(layer, layer.isVisible());
0518: }
0519: });
0520: }
0521:
0522: Adapter checkboxContextListener = new AdapterImpl() {
0523: @SuppressWarnings("unchecked")
0524: public void notifyChanged(final Notification msg) {
0525:
0526: if (msg.getNotifier() instanceof ContextModel) {
0527: ContextModel contextModel = (ContextModel) msg
0528: .getNotifier();
0529: Map map = contextModel.getMap();
0530:
0531: if (getCurrentMap() != map) {
0532: //Just in case
0533: map.removeDeepAdapter(this );
0534: return;
0535: }
0536:
0537: if (PlatformUI.getWorkbench().isClosing())
0538: contextModel.eAdapters().remove(this );
0539:
0540: if (msg.getFeatureID(ContextModel.class) == ProjectPackage.CONTEXT_MODEL__LAYERS) {
0541: switch (msg.getEventType()) {
0542: case Notification.ADD: {
0543: Layer layer = (Layer) msg.getNewValue();
0544: updateCheckbox(layer);
0545: break;
0546: }
0547: case Notification.ADD_MANY: {
0548: updateCheckboxes();
0549: break;
0550: }
0551: case Notification.SET: {
0552: Layer layer = (Layer) msg.getNewValue();
0553: updateCheckbox(layer);
0554: break;
0555: }
0556: }
0557: }
0558: } else if (msg.getNotifier() instanceof Layer) {
0559: Layer layer = (Layer) msg.getNotifier();
0560: if (getCurrentMap() != layer.getMapInternal()) {
0561: //Just in case
0562: layer.getMapInternal().removeDeepAdapter(this );
0563: return;
0564: }
0565: if (msg.getFeatureID(Layer.class) == ProjectPackage.LAYER__VISIBLE)
0566: if (msg.getNewBooleanValue() != msg
0567: .getOldBooleanValue()) {
0568: if (Display.getCurrent() == null) {
0569: viewer.getControl().getDisplay().asyncExec(
0570: new Runnable() {
0571: public void run() {
0572: viewer
0573: .setChecked(
0574: msg
0575: .getNotifier(),
0576: msg
0577: .getNewBooleanValue());
0578: }
0579: });
0580: } else {
0581: viewer.setChecked(msg.getNotifier(), msg
0582: .getNewBooleanValue());
0583: }
0584: }
0585: }
0586: }
0587: };
0588:
0589: private AdapterFactoryContentProvider contentProvider;
0590:
0591: private AdapterFactoryLabelProviderDecorator labelProvider;
0592:
0593: private Action propertiesAction;
0594:
0595: private IAction deleteAction;
0596:
0597: private static LayersView instance;
0598:
0599: /**
0600: * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
0601: */
0602: public void createPartControl(Composite parent) {
0603:
0604: getSite().getWorkbenchWindow().getPartService()
0605: .addPartListener(partServiceListener);
0606:
0607: viewer = new CheckboxTreeViewer(parent, SWT.MULTI);
0608: contentProvider = new AdapterFactoryContentProvider(
0609: ProjectUIPlugin.getDefault().getAdapterFactory()) {
0610: @Override
0611: public void notifyChanged(Notification notification) {
0612: super .notifyChanged(notification);
0613: switch (notification.getFeatureID(Map.class)) {
0614: case ProjectPackage.MAP__CONTEXT_MODEL: {
0615: if (notification.getNotifier() == getCurrentMap()) {
0616: updateCheckboxes();
0617: }
0618: break;
0619: }
0620: }
0621: }
0622: };
0623:
0624: viewer.setContentProvider(contentProvider);
0625: labelProvider = new AdapterFactoryLabelProviderDecorator(
0626: ProjectExplorer.getProjectExplorer()
0627: .getAdapterFactory(), viewer);
0628: viewer.setLabelProvider(labelProvider);
0629: /*
0630: * In dispose() method we need to remove this listener manually!
0631: */
0632: labelProvider.addListener(labelProviderListener);
0633:
0634: if (getSite().getPage().getActiveEditor() != null
0635: && getSite().getPage().getActiveEditor() instanceof MapEditor) {
0636: setCurrentMap(((MapEditor) getSite().getPage()
0637: .getActiveEditor()).getMap());
0638:
0639: }
0640: viewer.setSorter(new ViewerLayerSorter());
0641:
0642: // sets the layer visibility to match the check box setting.
0643: viewer.addCheckStateListener(new ICheckStateListener() {
0644: public void checkStateChanged(CheckStateChangedEvent event) {
0645: if (((Layer) event.getElement()).isVisible() != event
0646: .getChecked())
0647: ((Layer) event.getElement()).setVisible(event
0648: .getChecked());
0649: }
0650: });
0651: createContextMenuFor(viewer);
0652: addMenuActions();
0653: addToobarActions();
0654: setGlobalActions();
0655:
0656: getViewSite().setSelectionProvider(viewer);
0657: viewer.addSelectionChangedListener(this );
0658: viewer.addDoubleClickListener(this );
0659:
0660: UDIGDragDropUtilities.addDragDropSupport(viewer, this );
0661:
0662: }
0663:
0664: private ILabelProviderListener labelProviderListener = new ILabelProviderListener() {
0665: public void labelProviderChanged(LabelProviderChangedEvent event) {
0666: updateLabels();
0667: }
0668: };
0669:
0670: /**
0671: * Updates the viewer with new selected layer.
0672: *
0673: * @param newSelection
0674: */
0675: protected void updateSelection(final ILayer newSelection) {
0676: PlatformGIS.syncInDisplayThread(new Runnable() {
0677: public void run() {
0678: if (PlatformUI.getWorkbench().isClosing())
0679: return;
0680:
0681: if (getCurrentMap().getLayersInternal().size() > 0) {
0682: if (getCurrentMap() != null) {
0683: if (getCurrentMap().getLayersInternal()
0684: .contains(newSelection)) {
0685: viewer
0686: .setSelection(new StructuredSelection(
0687: newSelection));
0688: }
0689: }
0690: }
0691: }
0692: });
0693: }
0694:
0695: private void setGlobalActions() {
0696: IToolManager toolManager = ApplicationGIS.getToolManager();
0697: IActionBars actionBars = getViewSite().getActionBars();
0698: toolManager.contributeGlobalActions(this , actionBars);
0699: toolManager.registerActionsWithPart(this );
0700: IKeyBindingService service = getSite().getKeyBindingService();
0701: Action action = new Action() {
0702: Delete delete = new Delete();
0703:
0704: @Override
0705: public void run() {
0706: delete.selectionChanged(this , viewer.getSelection());
0707: delete.run(this );
0708: }
0709: };
0710:
0711: action.setActionDefinitionId("org.eclipse.ui.edit.delete"); //$NON-NLS-1$
0712: actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(),
0713: action);
0714: service.registerAction(action);
0715:
0716: }
0717:
0718: /**
0719: * This is how the framework determines which interfaces we implement. <!-- begin-user-doc -->
0720: * <!-- end-user-doc -->
0721: *
0722: * @param key The desired class
0723: * @return An object of type key or null;
0724: */
0725: public Object getAdapter(Class key) {
0726: if (key.equals(IPropertySheetPage.class))
0727: return ProjectUIPlugin.getDefault().getPropertySheetPage();
0728: return super .getAdapter(key);
0729: }
0730:
0731: private void addToobarActions() {
0732: IToolBarManager mgr = getViewSite().getActionBars()
0733: .getToolBarManager();
0734: mgr.add(upAction());
0735: mgr.add(downAction());
0736: }
0737:
0738: /**
0739: * Create an action that moves a layer down in the rendering order
0740: */
0741: private LayerAction downAction() {
0742: downAction = new LayerAction() {
0743: /**
0744: * @see org.eclipse.jface.action.Action#run()
0745: */
0746: public void run() {
0747: // System.out.println("down");
0748: for (Iterator iter = selection.iterator(); iter
0749: .hasNext();) {
0750: Layer layer = (Layer) iter.next();
0751: getCurrentMap().getContextModel().lowerLayer(layer);
0752: }
0753: }
0754: };
0755: downAction.setEnabled(false);
0756: downAction.setToolTipText(Messages.LayersView_down_tooltip);
0757: downAction.setImageDescriptor(Images
0758: .getDescriptor(ImageConstants.DOWN_CO));
0759: return downAction;
0760: }
0761:
0762: /**
0763: * Create an action that moves a layer up in the rendering order
0764: */
0765: private LayerAction upAction() {
0766: upAction = new LayerAction() {
0767: /**
0768: * @see org.eclipse.jface.action.Action#run()
0769: */
0770: public void run() {
0771: for (Iterator iter = selection.iterator(); iter
0772: .hasNext();) {
0773: Layer layer = (Layer) iter.next();
0774: getCurrentMap().getContextModel().raiseLayer(layer);
0775: }
0776: }
0777: };
0778: upAction.setEnabled(false);
0779: upAction.setToolTipText(Messages.LayersView_up_tooltip);
0780: upAction.setImageDescriptor(Images
0781: .getDescriptor(ImageConstants.UP_CO));
0782: return upAction;
0783: }
0784:
0785: private void addMenuActions() {
0786: // do nothing
0787: }
0788:
0789: /**
0790: * Creates a context menu
0791: *
0792: * @param viewer2
0793: */
0794: private void createContextMenuFor(final Viewer viewer2) {
0795: final MenuManager contextMenu = new MenuManager();
0796:
0797: contextMenu.setRemoveAllWhenShown(true);
0798: contextMenu.addMenuListener(new IMenuListener() {
0799:
0800: public void menuAboutToShow(IMenuManager mgr) {
0801: contextMenu.add(ApplicationGIS.getToolManager()
0802: .getCOPYAction(LayersView.this ));
0803: contextMenu.add(ApplicationGIS.getToolManager()
0804: .getPASTEAction(LayersView.this ));
0805: contextMenu.add(getDeleteAction());
0806: contextMenu.add(new Separator());
0807: contextMenu.add(new GroupMarker(
0808: IWorkbenchActionConstants.MB_ADDITIONS));
0809: // LayerApplicabilityMenuCreator creator = getApplicabilityMenu();
0810: // if (creator != null)
0811: // contextMenu.add(creator.getMenuManager());
0812: contextMenu.add(ApplicationGIS.getToolManager()
0813: .createOperationsContextMenu(
0814: viewer.getSelection()));
0815: contextMenu.add(new Separator());
0816: contextMenu.add(ActionFactory.EXPORT.create(getSite()
0817: .getWorkbenchWindow()));
0818: contextMenu.add(new Separator());
0819: if (viewer.getTree().getSelectionCount() == 1)
0820: contextMenu.add(getPropertiesAction());
0821: }
0822:
0823: private LayerApplicabilityMenuCreator applicabilityCreator;
0824:
0825: private LayerApplicabilityMenuCreator getApplicabilityMenu() {
0826: if (applicabilityCreator == null) {
0827: applicabilityCreator = new LayerApplicabilityMenuCreator();
0828: }
0829:
0830: IStructuredSelection selection = (IStructuredSelection) viewer2
0831: .getSelection();
0832: for (Iterator iter = selection.iterator(); iter
0833: .hasNext();) {
0834: Object element = iter.next();
0835: if (!(element instanceof Layer))
0836: return null;
0837: }
0838:
0839: return applicabilityCreator;
0840: }
0841:
0842: });
0843:
0844: // Create menu.
0845: Menu menu = contextMenu.createContextMenu(viewer2.getControl());
0846: viewer2.getControl().setMenu(menu);
0847:
0848: // Register menu for extension.
0849: getSite().registerContextMenu(contextMenu, viewer2);
0850:
0851: }
0852:
0853: private IAction getDeleteAction() {
0854: if (deleteAction == null) {
0855: deleteAction = new Action() {
0856: @Override
0857: public void run() {
0858: Delete delete = new Delete();
0859: ISelection s = PlatformUI.getWorkbench()
0860: .getActiveWorkbenchWindow()
0861: .getSelectionService().getSelection();
0862: delete.selectionChanged(this , s);
0863: delete.run(this );
0864: }
0865: };
0866: deleteAction
0867: .setActionDefinitionId("org.eclipse.ui.edit.delete"); //$NON-NLS-1$
0868: IWorkbenchAction actionTemplate = ActionFactory.DELETE
0869: .create(PlatformUI.getWorkbench()
0870: .getActiveWorkbenchWindow());
0871: deleteAction.setText(actionTemplate.getText());
0872: deleteAction
0873: .setToolTipText(actionTemplate.getToolTipText());
0874: deleteAction.setImageDescriptor(actionTemplate
0875: .getImageDescriptor());
0876: deleteAction
0877: .setDescription(actionTemplate.getDescription());
0878: deleteAction.setDisabledImageDescriptor(actionTemplate
0879: .getDisabledImageDescriptor());
0880: }
0881: return deleteAction;
0882: }
0883:
0884: protected IAction getPropertiesAction() {
0885: if (propertiesAction == null) {
0886: final Shell shell = PlatformUI.getWorkbench()
0887: .getActiveWorkbenchWindow().getShell();
0888: // propertiesAction=ActionFactory.PROPERTIES.create(getSite().getWorkbenchWindow());
0889: // propertiesAction.setEnabled(true);
0890:
0891: final PropertyDialogAction tmp = new PropertyDialogAction(
0892: new SameShellProvider(shell), getViewer());
0893:
0894: propertiesAction = new Action() {
0895: @Override
0896: public void runWithEvent(Event event) {
0897: ZoomingDialog dialog = new ZoomingDialog(shell, tmp
0898: .createDialog(), ZoomingDialog
0899: .calculateBounds(viewer.getTree()
0900: .getSelection()[0], -1));
0901: dialog.open();
0902: }
0903: };
0904:
0905: propertiesAction.setText(tmp.getText());
0906: propertiesAction.setActionDefinitionId(tmp
0907: .getActionDefinitionId());
0908: propertiesAction.setDescription(tmp.getDescription());
0909: propertiesAction.setHoverImageDescriptor(tmp
0910: .getHoverImageDescriptor());
0911: propertiesAction.setImageDescriptor(tmp
0912: .getImageDescriptor());
0913: propertiesAction.setToolTipText(tmp.getToolTipText());
0914:
0915: }
0916: getViewSite().getActionBars().setGlobalActionHandler(
0917: ActionFactory.PROPERTIES.getId(), propertiesAction);
0918: return propertiesAction;
0919: }
0920:
0921: /**
0922: * @see org.eclipse.ui.IWorkbenchPart#setFocus()
0923: */
0924: public void setFocus() {
0925: viewer.getControl().setFocus();
0926: }
0927:
0928: public static Viewer getViewer() {
0929: LayersView viewPart = getViewPart();
0930: if (viewPart == null)
0931: return null;
0932: return viewPart.viewer;
0933: }
0934:
0935: @Override
0936: public void init(IViewSite site) throws PartInitException {
0937: super .init(site);
0938: instance = this ;
0939: }
0940:
0941: public static LayersView getViewPart() {
0942: return instance;
0943: }
0944:
0945: public Object getTarget(DropTargetEvent event) {
0946: if (getCurrentMap() == null)
0947: return this ;
0948: List<ILayer> mapLayers = getCurrentMap().getMapLayers();
0949: if (mapLayers.isEmpty())
0950: return this ;
0951:
0952: return mapLayers.get(mapLayers.size() - 1);
0953: }
0954:
0955: public void doubleClick(DoubleClickEvent event) {
0956:
0957: final Object obj = ((IStructuredSelection) viewer
0958: .getSelection()).getFirstElement();
0959: if (!(obj instanceof IProjectElement)) {
0960: Display.getDefault().asyncExec(new Runnable() {
0961:
0962: public void run() {
0963: // TODO for future when styling is in properties view
0964: // getPropertiesAction().runWithEvent(new Event());
0965:
0966: }
0967: });
0968: return;
0969: }
0970:
0971: }
0972:
0973: public void selectionChanged(SelectionChangedEvent event) {
0974: if (event.getSelection().isEmpty()) {
0975: return;
0976: }
0977:
0978: if (!(event.getSelection() instanceof IStructuredSelection))
0979: return;
0980: IStructuredSelection structured = (IStructuredSelection) event
0981: .getSelection();
0982: Object firstElement = structured.getFirstElement();
0983: if (firstElement instanceof ILayer) {
0984: ILayer layer = ((ILayer) firstElement);
0985: if (layer.getStatus() == ILayer.ERROR) {
0986: getViewSite().getActionBars().getStatusLineManager()
0987: .setErrorMessage(layer.getStatusMessage());
0988: } else {
0989: getViewSite().getActionBars().getStatusLineManager()
0990: .setErrorMessage(null);
0991: getViewSite().getActionBars().getStatusLineManager()
0992: .setMessage(layer.getStatusMessage());
0993:
0994: }
0995: } else {
0996: getViewSite().getActionBars().getStatusLineManager()
0997: .setMessage(null);
0998: getViewSite().getActionBars().getStatusLineManager()
0999: .setErrorMessage(null);
1000: }
1001:
1002: }
1003: }
|