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 com.vividsolutions.jts.util.Assert;
036: import com.vividsolutions.jump.I18N;
037: import com.vividsolutions.jump.workbench.WorkbenchContext;
038: import com.vividsolutions.jump.workbench.model.LayerManager;
039: import com.vividsolutions.jump.workbench.model.UndoableCommand;
040: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
041: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
042: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
043: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
044:
045: public class AddNewCategoryPlugIn extends AbstractPlugIn {
046: private final static String NEW_CATEGORY_NAME = I18N
047: .get("ui.plugin.AbstractNewCategoryPlugIn.new-category");
048:
049: public AddNewCategoryPlugIn() {
050: }
051:
052: public boolean execute(final PlugInContext context)
053: throws Exception {
054: final String categoryName = findNewCategoryName(context
055: .getLayerManager());
056: execute(new UndoableCommand(getName()) {
057: public void execute() {
058: context.getLayerManager().addCategory(categoryName);
059: }
060:
061: public void unexecute() {
062: Assert
063: .isTrue(
064: context.getLayerManager().getCategory(
065: categoryName).isEmpty(),
066: I18N
067: .get("ui.plugin.AbstractNewCategoryPlugIn.this-can-happen-when-a-plug-in-calls"));
068: context.getLayerManager().removeIfEmpty(
069: context.getLayerManager().getCategory(
070: categoryName));
071: }
072: }, context);
073:
074: return true;
075: }
076:
077: private String findNewCategoryName(LayerManager layerManager) {
078: if (layerManager.getCategory(NEW_CATEGORY_NAME) == null) {
079: return NEW_CATEGORY_NAME;
080: }
081:
082: int i = 2;
083: String newName;
084:
085: do {
086: newName = NEW_CATEGORY_NAME + " (" + i + ")";
087: i++;
088: } while (layerManager.getCategory(newName) != null);
089:
090: return newName;
091: }
092:
093: public MultiEnableCheck createEnableCheck(
094: WorkbenchContext workbenchContext) {
095: EnableCheckFactory checkFactory = new EnableCheckFactory(
096: workbenchContext);
097:
098: return new MultiEnableCheck().add(checkFactory
099: .createWindowWithLayerViewPanelMustBeActiveCheck());
100: }
101: }
|