| /***************************************************************************** |
| * Copyright (c) 2018 CEA LIST |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * David Lopez david.lopez@cea.fr(CEA LIST) |
| * |
| *****************************************************************************/ |
| package org.eclipse.papyrus.aceeditor; |
| |
| import java.util.HashSet; |
| import java.util.Set; |
| |
| import org.eclipse.core.databinding.observable.value.AbstractObservableValue; |
| import org.eclipse.core.databinding.observable.value.IObservableValue; |
| import org.eclipse.papyrus.infra.properties.ui.modelelement.ModelElement; |
| import org.eclipse.papyrus.infra.widgets.editors.AbstractEditor; |
| import org.eclipse.papyrus.infra.widgets.editors.ICommitListener; |
| import org.eclipse.papyrus.uml.properties.widgets.BodyEditor; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Event; |
| import org.eclipse.swt.widgets.Listener; |
| |
| public class AceBodyEditor implements BodyEditor, AceCodeEditorListener { |
| |
| protected AceCodeEditor editor; |
| private final Set<Listener> changeListeners = new HashSet<Listener>(); |
| private String currentValue; |
| protected ModelElement context; |
| |
| @Override |
| public void createWidget(Composite parent, int style) { |
| editor = new AceCodeEditor(parent, style); |
| editor.addCommitListener(new ICommitListener() { |
| |
| @Override |
| public void commit(AbstractEditor absEditor) { |
| // System.out.println("AceCodeEditor.commit"); |
| Event event = new Event(); |
| currentValue = (String) editor.getValue(); |
| event.text = currentValue; |
| |
| synchronized (changeListeners) { |
| changeListeners.forEach( l -> l.handleEvent(event) ); |
| } |
| } |
| }); |
| |
| editor.addAceCodeEditorListener(this); |
| editor.layout(); |
| } |
| |
| @Override |
| public void setInput(String value) { |
| currentValue = value; |
| |
| IObservableValue obs = new AbstractObservableValue() { |
| |
| @Override |
| public Object getValueType() { |
| return String.class; |
| } |
| |
| @Override |
| protected Object doGetValue() { |
| return currentValue; |
| } |
| |
| @Override |
| protected void doSetValue(Object value) { |
| if( value instanceof String ) { |
| currentValue = (String)value; |
| } |
| } |
| }; |
| |
| editor.setModelObservable(obs); |
| editor.setValue(currentValue); |
| } |
| |
| @Override |
| public void dispose() { |
| editor.dispose(); |
| // System.out.println("Disposing ..."); |
| |
| synchronized (changeListeners) { |
| changeListeners.clear(); |
| } |
| } |
| |
| public void addChangeListener(Listener listener) { |
| changeListeners.add(listener); |
| } |
| |
| public void removeChangeListener(Listener listener) { |
| synchronized (changeListeners) { |
| changeListeners.remove(listener); |
| } |
| } |
| |
| public String getValue() { |
| return currentValue; |
| } |
| |
| @Override |
| public void setReadOnly(boolean readOnly) { |
| editor.setReadOnly(readOnly); |
| } |
| |
| @Override |
| public void setContext(ModelElement context) { |
| this.context = context; |
| } |
| |
| public AceCodeEditorConfiguration getEditorConfiguration() { |
| return editor.getConfiguration(); |
| } |
| |
| public void refreshEditor() { |
| editor.refresh(); |
| } |
| |
| @Override |
| public void onChange(String newValue) { |
| } |
| } |