001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.printing.ui.internal.editor;
016:
017: import net.refractions.udig.printing.model.Box;
018: import net.refractions.udig.printing.model.impl.LabelBoxPrinter;
019: import net.refractions.udig.printing.ui.IBoxEditAction;
020: import net.refractions.udig.printing.ui.internal.editor.figures.BoxFigure;
021: import net.refractions.udig.printing.ui.internal.editor.parts.BoxPart;
022: import net.refractions.udig.printing.ui.internal.editor.parts.LabelCellEditorLocator;
023: import net.refractions.udig.printing.ui.internal.editor.parts.LabelDirectEditManager;
024:
025: import org.eclipse.gef.commands.Command;
026: import org.eclipse.jface.viewers.TextCellEditor;
027: import org.eclipse.swt.widgets.Text;
028:
029: /**
030: * Renames a LabelBox
031: *
032: * @author Jesse
033: * @since 1.1.0
034: */
035: public class RenameLabelBox implements IBoxEditAction {
036:
037: private BoxPart owner;
038: private LabelDirectEditManager manager;
039: private String value;
040: private boolean disposed = false;
041: private LabelBoxPrinter labelBox;
042:
043: public void perform() {
044: value = null;
045: disposed = false;
046: labelBox = (LabelBoxPrinter) ((Box) owner.getModel())
047: .getBoxPrinter();
048: if (manager == null) {
049: BoxFigure nodeFigure = (BoxFigure) owner.getFigure();
050: manager = new LabelDirectEditManager(owner,
051: TextCellEditor.class, new LabelCellEditorLocator(
052: nodeFigure), nodeFigure) {
053:
054: private boolean committing;
055:
056: @Override
057: protected void initCellEditor() {
058: super .initCellEditor();
059: Text text = (Text) getCellEditor().getControl();
060: text.setText(labelBox.getText());
061: }
062:
063: @Override
064: protected void commit() {
065: if (committing)
066: return;
067: committing = true;
068: try {
069: eraseFeedback();
070: value = (String) getCellEditor().getValue();
071: } finally {
072: bringDown();
073: committing = false;
074: }
075:
076: }
077:
078: @Override
079: protected void bringDown() {
080: super .bringDown();
081: disposed = true;
082: }
083: };
084: }
085:
086: manager.show();
087: }
088:
089: public Command getCommand() {
090: if (value == null)
091: return null;
092: return new Command() {
093:
094: private String oldValue;
095: private String newValue;
096:
097: @Override
098: public boolean canExecute() {
099: return isDone();
100: }
101:
102: @Override
103: public void execute() {
104: oldValue = labelBox.getText();
105: newValue = value;
106: labelBox.setText(newValue);
107: }
108:
109: @Override
110: public void redo() {
111: labelBox.setText(newValue);
112: }
113:
114: @Override
115: public void undo() {
116: labelBox.setText(oldValue);
117: }
118: };
119: }
120:
121: public void init(BoxPart owner) {
122: this .owner = owner;
123: }
124:
125: public boolean isDone() {
126: return disposed;
127: }
128:
129: }
|