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 javax.swing.JComponent;
036:
037: import com.vividsolutions.jump.I18N;
038: import com.vividsolutions.jump.workbench.WorkbenchContext;
039: import com.vividsolutions.jump.workbench.model.Category;
040: import com.vividsolutions.jump.workbench.model.Layerable;
041: import com.vividsolutions.jump.workbench.model.UndoableCommand;
042: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
043: import com.vividsolutions.jump.workbench.plugin.EnableCheck;
044: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
045: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
046: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
047: import com.vividsolutions.jump.workbench.ui.LayerNamePanel;
048:
049: public class MoveLayerablePlugIn extends AbstractPlugIn {
050: public static final MoveLayerablePlugIn UP = new MoveLayerablePlugIn(
051: -1) {
052: public String getName() {
053: return I18N
054: .get("ui.plugin.MoveLayerablePlugIn.move-layer-up");
055: }
056:
057: public MultiEnableCheck createEnableCheck(
058: final WorkbenchContext workbenchContext) {
059: return super .createEnableCheck(workbenchContext).add(
060: new EnableCheck() {
061: public String check(JComponent component) {
062: return (index(selectedLayerable(workbenchContext
063: .getLayerNamePanel())) == 0) ? I18N
064: .get("ui.plugin.MoveLayerablePlugIn.layer-is-already-at-the-top")
065: : null;
066: }
067: });
068: }
069: };
070:
071: public static final MoveLayerablePlugIn DOWN = new MoveLayerablePlugIn(
072: 1) {
073: public String getName() {
074: return I18N
075: .get("ui.plugin.MoveLayerablePlugIn.move-layer-down");
076: }
077:
078: public MultiEnableCheck createEnableCheck(
079: final WorkbenchContext workbenchContext) {
080: return super .createEnableCheck(workbenchContext).add(
081: new EnableCheck() {
082: public String check(JComponent component) {
083: return (index(selectedLayerable(workbenchContext
084: .getLayerNamePanel())) == (workbenchContext
085: .getLayerViewPanel()
086: .getLayerManager()
087: .getCategory(
088: selectedLayerable(workbenchContext
089: .getLayerNamePanel()))
090: .getLayerables().size() - 1)) ? I18N
091: .get("ui.plugin.MoveLayerablePlugIn.layer-is-already-at-the-bottom")
092: : null;
093: }
094: });
095: }
096: };
097:
098: private int displacement;
099:
100: private MoveLayerablePlugIn(int displacement) {
101: this .displacement = displacement;
102: }
103:
104: protected Layerable selectedLayerable(LayerNamePanel layerNamePanel) {
105: return (Layerable) layerNamePanel
106: .selectedNodes(Layerable.class).iterator().next();
107: }
108:
109: public boolean execute(final PlugInContext context)
110: throws Exception {
111: final Layerable layerable = selectedLayerable(context
112: .getLayerNamePanel());
113: final int index = index(layerable);
114: final Category category = context.getLayerManager()
115: .getCategory(layerable);
116: execute(new UndoableCommand(getName()) {
117: public void execute() {
118: moveLayerable(index + displacement);
119: }
120:
121: public void unexecute() {
122: moveLayerable(index);
123: }
124:
125: private void moveLayerable(int newIndex) {
126: context.getLayerManager().remove(layerable);
127: category.add(newIndex, layerable);
128: }
129: }, context);
130:
131: return true;
132: }
133:
134: protected int index(Layerable layerable) {
135: return layerable.getLayerManager().getCategory(layerable)
136: .indexOf(layerable);
137: }
138:
139: public MultiEnableCheck createEnableCheck(
140: final WorkbenchContext workbenchContext) {
141: EnableCheckFactory checkFactory = new EnableCheckFactory(
142: workbenchContext);
143:
144: return new MultiEnableCheck()
145: .add(
146: checkFactory
147: .createWindowWithLayerNamePanelMustBeActiveCheck())
148: .add(
149: checkFactory
150: .createExactlyNLayerablesMustBeSelectedCheck(
151: 1, Layerable.class));
152: }
153: }
|