/*******************************************************************************
* 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.ide.graphiti.ui.palette;
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.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.part.CellEditorActionHandler;
/**
*
*/
public class RHGraphitiPaletteFilterEditManager extends DirectEditManager {
private IActionBars actionBars;
private CellEditorActionHandler actionHandler;
private IAction copy, cut, paste, undo, redo, find, selectAll, delete;
private RHGraphitiPaletteFilter filter;
public RHGraphitiPaletteFilterEditManager(RHGraphitiPaletteFilterEditPart source, RHGraphitiPaletteFilterCellEditorLocator locator, RHGraphitiPaletteFilter 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);
((RHGraphitiPaletteFilterFigure) ((RHGraphitiPaletteFilterEditPart) getEditPart()).getFigure()).setText(text);
}
});
return retVal;
}
/*
* @see org.eclipse.gef.tools.DirectEditManager#showFeedback()
*/
@Override
public void showFeedback() {
// DO nothing
}
protected void initCellEditor() {
// update text
RHGraphitiPaletteFilterFigure stickyNote = (RHGraphitiPaletteFilterFigure) 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.
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart activeEditor = page.getActiveEditor();
if (activeEditor == null) {
return;
}
IEditorSite site = activeEditor.getEditorSite();
actionBars = site.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());
}
}