001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.ui.plugin;
034:
035: import java.util.ArrayList;
036: import java.util.Collection;
037: import java.util.Collections;
038: import java.util.Comparator;
039: import java.util.Iterator;
040: import java.util.List;
041:
042: import javax.swing.JComponent;
043:
044: import com.vividsolutions.jts.util.Assert;
045: import com.vividsolutions.jump.I18N;
046: import com.vividsolutions.jump.util.OrderedMap;
047: import com.vividsolutions.jump.workbench.WorkbenchContext;
048: import com.vividsolutions.jump.workbench.model.Category;
049: import com.vividsolutions.jump.workbench.model.LayerManager;
050: import com.vividsolutions.jump.workbench.model.Layerable;
051: import com.vividsolutions.jump.workbench.model.StandardCategoryNames;
052: import com.vividsolutions.jump.workbench.model.UndoableCommand;
053: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
054: import com.vividsolutions.jump.workbench.plugin.EnableCheck;
055: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
056: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
057: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
058: import com.vividsolutions.jump.workbench.ui.LayerNamePanel;
059:
060: public class RemoveSelectedCategoriesPlugIn extends AbstractPlugIn {
061: public RemoveSelectedCategoriesPlugIn() {
062: }
063:
064: private Category pickUnselectedCategory(
065: LayerNamePanel layerNamePanel, LayerManager layerManager) {
066: Collection selectedCategories = layerNamePanel
067: .getSelectedCategories();
068: Category workingCategory = layerManager
069: .getCategory(StandardCategoryNames.WORKING);
070:
071: if ((workingCategory != null)
072: && !selectedCategories.contains(workingCategory)) {
073: return workingCategory;
074: }
075:
076: for (Iterator i = layerManager.getCategories().iterator(); i
077: .hasNext();) {
078: Category category = (Category) i.next();
079:
080: if (!selectedCategories.contains(category)) {
081: return category;
082: }
083: }
084:
085: return null;
086: }
087:
088: public boolean execute(PlugInContext context) throws Exception {
089: execute(
090: toCategorySpecToLayerablesMap(toOrderedCategories(context
091: .getLayerNamePanel().getSelectedCategories())),
092: pickUnselectedCategory(context.getLayerNamePanel(),
093: context.getLayerManager()), context);
094:
095: return true;
096: }
097:
098: private List toOrderedCategories(Collection unorderedCategories) {
099: ArrayList orderedCategories = new ArrayList(unorderedCategories);
100: Collections.sort(orderedCategories, new Comparator() {
101: public int compare(Object o1, Object o2) {
102: Category c1 = (Category) o1;
103: Category c2 = (Category) o2;
104:
105: return new Integer(c1.getLayerManager().indexOf(c1))
106: .compareTo(new Integer(c2.getLayerManager()
107: .indexOf(c2)));
108: }
109: });
110:
111: return orderedCategories;
112: }
113:
114: private OrderedMap toCategorySpecToLayerablesMap(
115: List selectedCategoriesInOrder) {
116: //Need OrderedMap so that categories get re-inserted in the correct order.
117: //[Jon Aquino]
118: OrderedMap map = new OrderedMap();
119:
120: for (Iterator i = selectedCategoriesInOrder.iterator(); i
121: .hasNext();) {
122: Category category = (Category) i.next();
123:
124: //new ArrayList because #getLayers returns a view of the category's
125: //layers, which will be cleared. [Jon Aquino]
126: map.put(new CategorySpec(category.getName(), category
127: .getLayerManager().indexOf(category)),
128: new ArrayList(category.getLayerables()));
129: }
130:
131: return map;
132: }
133:
134: private void execute(
135: final OrderedMap originalCategorySpecToLayerablesMap,
136: final Category newCategory, final PlugInContext context)
137: throws Exception {
138: execute(new UndoableCommand(getName()) {
139: public void execute() {
140: for (Iterator i = originalCategorySpecToLayerablesMap
141: .keyList().iterator(); i.hasNext();) {
142: final CategorySpec originalCategorySpec = (CategorySpec) i
143: .next();
144: List layers = (List) originalCategorySpecToLayerablesMap
145: .get(originalCategorySpec);
146:
147: for (Iterator j = layers.iterator(); j.hasNext();) {
148: final Layerable layerable = (Layerable) j
149: .next();
150: context.getLayerManager().remove(layerable);
151: context.getLayerManager().addLayerable(
152: newCategory.getName(), layerable);
153: }
154:
155: context.getLayerManager().removeIfEmpty(
156: context.getLayerManager().getCategory(
157: originalCategorySpec.name));
158: }
159: }
160:
161: public void unexecute() {
162: for (Iterator i = originalCategorySpecToLayerablesMap
163: .keyList().iterator(); i.hasNext();) {
164: final CategorySpec originalCategorySpec = (CategorySpec) i
165: .next();
166: List layers = (List) originalCategorySpecToLayerablesMap
167: .get(originalCategorySpec);
168: Assert.isTrue(null == context.getLayerManager()
169: .getCategory(originalCategorySpec.name));
170: context.getLayerManager().addCategory(
171: originalCategorySpec.name,
172: originalCategorySpec.index);
173:
174: for (Iterator j = layers.iterator(); j.hasNext();) {
175: final Layerable layerable = (Layerable) j
176: .next();
177: Assert.isTrue(context.getLayerManager()
178: .getCategory(layerable) == newCategory);
179: context.getLayerManager().remove(layerable);
180: context.getLayerManager().addLayerable(
181: originalCategorySpec.name, layerable);
182: }
183: }
184: }
185: }, context);
186: }
187:
188: public MultiEnableCheck createEnableCheck(
189: final WorkbenchContext workbenchContext) {
190: EnableCheckFactory checkFactory = new EnableCheckFactory(
191: workbenchContext);
192:
193: return new MultiEnableCheck()
194: .add(
195: checkFactory
196: .createWindowWithLayerNamePanelMustBeActiveCheck())
197: .add(
198: checkFactory
199: .createAtLeastNCategoriesMustBeSelectedCheck(1))
200: .add(new EnableCheck() {
201: public String check(JComponent component) {
202: return (pickUnselectedCategory(workbenchContext
203: .getLayerNamePanel(), workbenchContext
204: .getLayerManager()) == null) ? I18N
205: .get("ui.plugin.RemoveSelectedCategoriesPlugIn.at-least-1-category-must-be-left-unselected")
206: : null;
207: }
208: });
209: }
210:
211: private static class CategorySpec {
212: private int index;
213: private String name;
214:
215: public CategorySpec(String name, int index) {
216: this.name = name;
217: this.index = index;
218: }
219: }
220: }
|