| /******************************************************************************* |
| * Copyright (c) 2004-2008 Istvan Rath and Daniel Varro |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Istvan Rath - initial API and implementation |
| *******************************************************************************/ |
| |
| package org.eclipse.viatra2.treeeditor.providers;
|
|
|
|
|
| import org.eclipse.swt.SWT;
|
| import org.eclipse.swt.custom.TreeEditor;
|
| import org.eclipse.swt.events.FocusEvent;
|
| import org.eclipse.swt.events.FocusListener;
|
| import org.eclipse.swt.events.KeyEvent;
|
| import org.eclipse.swt.events.KeyListener;
|
| import org.eclipse.swt.events.ModifyEvent;
|
| import org.eclipse.swt.events.ModifyListener;
|
| import org.eclipse.swt.widgets.Control;
|
| import org.eclipse.swt.widgets.Text;
|
| import org.eclipse.viatra2.core.IModelElement;
|
| import org.eclipse.viatra2.treeeditor.ViatraTreeEditor;
|
| import org.eclipse.viatra2.treeeditor.commands.ChangeElementNameCommand;
|
| import org.eclipse.viatra2.treeeditor.commands.ChangePropertyCommand; |
| import org.eclipse.viatra2.treeeditor.properties.VPMProperties; |
|
|
| public class ViatraTreeEventListener implements ModifyListener, FocusListener,
|
| KeyListener {
|
|
|
| /**
|
| * Reference to the cell editor for the Tree widget.
|
| */
|
| protected TreeEditor iEditor;
|
|
|
| protected IModelElement iME;
|
|
|
| protected ViatraTreeEditor iVTE;
|
|
|
| public ViatraTreeEventListener(IModelElement me, TreeEditor te, ViatraTreeEditor vte) {
|
| iEditor = te;
|
| iME = me;
|
| iVTE = vte;
|
| }
|
|
|
|
|
| public void modifyText(ModifyEvent e) {
|
| // don't care
|
|
|
| }
|
|
|
| public void focusGained(FocusEvent e) {
|
| // don't care
|
| }
|
|
|
| public void focusLost(FocusEvent e) {
|
| cancelEditing();
|
| }
|
|
|
| public void keyPressed(KeyEvent e) {
|
| if (e.keyCode == SWT.ESC)
|
| cancelEditing();
|
| else if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR)
|
| submitEditing();
|
| }
|
|
|
| public void keyReleased(KeyEvent e) {
|
| // don't care
|
| }
|
|
|
| /**
|
| * Cancels the editing of the tree item.
|
| */
|
| public void cancelEditing() {
|
| Control ctrl = iEditor.getEditor();
|
| if (ctrl != null && !ctrl.isDisposed()) {
|
| ctrl.removeFocusListener(this);
|
| ctrl.removeKeyListener(this);
|
| ((Text) ctrl).removeModifyListener(this);
|
| ctrl.dispose();
|
| }
|
| iEditor.dispose();
|
| }
|
|
|
| /**
|
| * Submits the editing of the tree item.
|
| */
|
| public void submitEditing() { |
| String text = ((Text)iEditor.getEditor()).getText(); |
| String name = ""; |
| String value = ""; |
| int braceStart = text.indexOf('{'); |
| int braceEnd = text.indexOf('}'); |
| if (braceStart>-1 && braceEnd>braceStart) { |
| value = text.substring(braceStart+1, braceEnd); |
| name = text.replaceAll(value, ""); |
| name = name.replaceAll("\\{\\}", "").trim(); |
| } |
| else { |
| name = text; |
| }
|
| ChangeElementNameCommand cmd = new ChangeElementNameCommand();
|
| cmd.setTargetElem(iME);
|
| cmd.setNewElemName(name);
|
| iVTE.getCommandStack().execute(cmd); |
| if (value.length()>0) { |
| ChangePropertyCommand c1 = new ChangePropertyCommand(); |
| c1.setKind(VPMProperties.VALUE); |
| c1.setNewValue(value); |
| c1.setTarget(iME); |
| iVTE.getCommandStack().execute(c1); |
| }
|
| cancelEditing();
|
| }
|
|
|
| }
|