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.project.ui.internal;
018:
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import net.refractions.udig.project.internal.Layer;
023: import net.refractions.udig.project.ui.ApplicationGIS;
024: import net.refractions.udig.project.ui.internal.tool.display.ModalToolCategory;
025:
026: import org.eclipse.jface.action.ContributionItem;
027: import org.eclipse.jface.action.MenuManager;
028: import org.eclipse.jface.viewers.IStructuredSelection;
029: import org.eclipse.swt.SWT;
030: import org.eclipse.swt.widgets.Event;
031: import org.eclipse.swt.widgets.Menu;
032: import org.eclipse.swt.widgets.MenuItem;
033: import org.eclipse.ui.PlatformUI;
034:
035: /**
036: * Creates a sub menu that permits the applicability of a layer to be set.
037: *
038: * @author jeichar
039: * @since 0.9.0
040: */
041: public class LayerApplicabilityMenuCreator {
042: MenuManager manager = new MenuManager(
043: Messages.LayerApplicabilityMenuCreator_ApplicabilityMenuName);
044:
045: /**
046: * Construct <code>LayerApplicabilityMenuCreator</code>.
047: */
048: public LayerApplicabilityMenuCreator() {
049:
050: List<ModalToolCategory> list = ApplicationGIS.getToolManager()
051: .getModalToolCategories();
052: for (ModalToolCategory element : list) {
053: manager.add(new ApplicabilityAction(element));
054:
055: }
056: }
057:
058: /**
059: * Gets the Applicability Menu
060: *
061: * @return
062: */
063: public MenuManager getMenuManager() {
064: return manager;
065: }
066:
067: private static class ApplicabilityAction extends ContributionItem {
068:
069: private MenuItem menuItem;
070: private ModalToolCategory category;
071:
072: /**
073: * Construct <code>LayerApplicabilityMenuCreator.ApplicabilityAction</code>.
074: */
075: public ApplicabilityAction(ModalToolCategory category) {
076: this .category = category;
077: }
078:
079: /**
080: * @see org.eclipse.jface.action.Action#runWithEvent(org.eclipse.swt.widgets.Event)
081: */
082: public void runWithEvent(Event event) {
083: IStructuredSelection selection = (IStructuredSelection) PlatformUI
084: .getWorkbench().getActiveWorkbenchWindow()
085: .getSelectionService().getSelection();
086: for (Iterator iter = selection.iterator(); iter.hasNext();) {
087: Layer layer = (Layer) iter.next();
088: layer.setApplicable(category.getId(), !menuItem
089: .getSelection());
090: }
091: }
092:
093: /**
094: * @see org.eclipse.jface.action.ContributionItem#fill(org.eclipse.swt.widgets.Menu, int)
095: */
096: public void fill(Menu menu, int index) {
097: if (menuItem != null && !menuItem.isDisposed())
098: menuItem.dispose();
099: menuItem = new MenuItem(menu, SWT.CHECK, index);
100: IStructuredSelection selection = (IStructuredSelection) PlatformUI
101: .getWorkbench().getActiveWorkbenchWindow()
102: .getSelectionService().getSelection();
103: Layer layer = (Layer) selection.getFirstElement();
104: ModalToolCategory modalToolCategory = category;
105: if (category != null) {
106: menuItem.setSelection(layer
107: .isApplicable(modalToolCategory.getId()));
108: menuItem.setText(modalToolCategory.getName());
109: }
110: }
111:
112: /**
113: * @see org.eclipse.jface.action.ContributionItem#dispose()
114: */
115: public void dispose() {
116: super.dispose();
117: menuItem.dispose();
118: }
119: }
120:
121: }
|