/**
* This file is protected by Copyright.
* Please refer to the COPYRIGHT file distributed with this source distribution.
*
* This file is part of REDHAWK IDE.
*
* 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.
*
*/
package gov.redhawk.sca.sad.diagram.palette;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.gef.tools.DirectEditManager;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.part.CellEditorActionHandler;
/**
* @since 3.2
*/
public class LabelEditManager extends DirectEditManager {
private IActionBars actionBars;
private CellEditorActionHandler actionHandler;
private IAction copy, cut, paste, undo, redo, find, selectAll, delete;
private PaletteFilter filter;
public LabelEditManager(LabelEditPart source, CellEditorLocator locator, PaletteFilter filter) {
super(source, null, locator);
this.filter = filter;
}
/**
* @see org.eclipse.gef.tools.DirectEditManager#bringDown()
*/
protected void bringDown() {
if (actionHandler != null) {
actionHandler.dispose();
actionHandler = null;
}
if (actionBars != null) {
restoreSavedActions(actionBars);
actionBars.updateActionBars();
actionBars = null;
}
super.bringDown();
}
protected CellEditor createCellEditorOn(Composite composite) {
final TextCellEditor retVal = new TextCellEditor(composite, SWT.SINGLE);
retVal.getControl().setForeground(composite.getDisplay().getSystemColor(SWT.COLOR_BLACK));
retVal.getControl().setBackground(composite.getDisplay().getSystemColor(SWT.COLOR_WHITE));
final Text textControl = ((Text) retVal.getControl());
textControl.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
String text = textControl.getText().trim();
filter.setFilter(text);
}
});
textControl.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetDefaultSelected(SelectionEvent e) {
commit();
}
});
return retVal;
}
/*
* @see org.eclipse.gef.tools.DirectEditManager#showFeedback()
*/
@Override
public void showFeedback() {
// DO nothing
}
protected void initCellEditor() {
// update text
LabelFigure stickyNote = (LabelFigure) getEditPart().getFigure();
String text = filter.getFilter();
if (text == null) {
text = "";
}
getCellEditor().setValue(text);
// update font
getCellEditor().getControl().setFont(stickyNote.getFont());
// Hook the cell editor's copy/paste actions to the actionBars so that
// they can
// be invoked via keyboard shortcuts.
actionBars = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getEditorSite().getActionBars();
saveCurrentActions(actionBars);
actionHandler = new CellEditorActionHandler(actionBars);
actionHandler.addCellEditor(getCellEditor());
actionBars.updateActionBars();
}
private void restoreSavedActions(IActionBars actionBars) {
actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), copy);
actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), paste);
actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), delete);
actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), selectAll);
actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(), cut);
actionBars.setGlobalActionHandler(ActionFactory.FIND.getId(), find);
actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), undo);
actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), redo);
}
private void saveCurrentActions(IActionBars actionBars) {
copy = actionBars.getGlobalActionHandler(ActionFactory.COPY.getId());
paste = actionBars.getGlobalActionHandler(ActionFactory.PASTE.getId());
delete = actionBars.getGlobalActionHandler(ActionFactory.DELETE.getId());
selectAll = actionBars.getGlobalActionHandler(ActionFactory.SELECT_ALL.getId());
cut = actionBars.getGlobalActionHandler(ActionFactory.CUT.getId());
find = actionBars.getGlobalActionHandler(ActionFactory.FIND.getId());
undo = actionBars.getGlobalActionHandler(ActionFactory.UNDO.getId());
redo = actionBars.getGlobalActionHandler(ActionFactory.REDO.getId());
}
}