Example
29. December 2006
richhtml4eclipse provides different possibilities to manipulate content and informs via Event-Handling its listeners about changes. On the following page is described how to create an instance of the widget and to implement a Bold-Action. For a complete example see the SourceForge-Download Page.
Creating the widget
JAVA:
-
final HtmlComposer composer = new HtmlComposer(comp, SWT.BORDER);
-
composer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
The Bold Action
JAVA:
-
import org.eclipse.jface.action.Action;
-
import org.eclipse.jface.action.IAction;
-
import org.eclipse.swt.widgets.Event;
-
import org.eclipse.swt.widgets.Listener;
-
import org.eclipse.ui.plugin.AbstractUIPlugin;
-
import de.spiritlink.richhtml4eclipse.widgets.AllActionConstants;
-
import de.spiritlink.richhtml4eclipse.widgets.ComposerStatus;
-
import de.spiritlink.richhtml4eclipse.widgets.EventConstants;
-
import de.spiritlink.richhtml4eclipse.widgets.HtmlComposer;
-
import de.spiritlink.richhtml4eclipse.widgets.JavaScriptCommands;
-
/**
-
* A JFace-Action for switching the bold status of a selection
-
* or the current cursor. Bold has toggle behavior, therefore
-
* the action has the {@link IAction#AS_CHECK_BOX} flag.
-
* @author Tom Seidel <tom.seidel@spiritlink.de>
-
*
-
*/
-
private HtmlComposer composer = null;
-
public BoldAction(HtmlComposer composer) {
-
super("", IAction.AS_CHECK_BOX); //$NON-NLS-1$
-
setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin("de.spiritlink.richhtml4eclipse", //$NON-NLS-1$
-
"tiny_mce/jscripts/tiny_mce/themes/advanced/images/bold.gif")); //$NON-NLS-1$
-
this.composer = composer;
-
// adds a listener to the widget for "bold-events"
-
this.composer.addListener(EventConstants.BOLD, this);
-
}
-
/* (non-Javadoc)
-
* @see org.eclipse.jface.action.Action#run()
-
*/
-
@Override
-
public void run() {
-
// Executes the command for bold to the composer
-
this.composer.execute(JavaScriptCommands.BOLD);
-
}
-
/* (non-Javadoc)
-
* @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
-
*/
-
// callback for the status of the current selection/cusor
-
if (ComposerStatus.DISABLED.equals(event.text)) {
-
setChecked(false);
-
setEnabled(false);
-
} else if (ComposerStatus.SELECTED.equals(event.text)) {
-
// current selection/cursor is bold --> set the action checked
-
setEnabled(true);
-
setChecked(true);
-
} else if (ComposerStatus.NORMAL.equals(event.text)) {
-
setEnabled(true);
-
setChecked(false);
-
} else if (event.type == EventConstants.ALL && event.text.equals(AllActionConstants.RESET_ALL)) {
-
// callback if the cursor changed, reset the state.
-
setEnabled(true);
-
setChecked(false);
-
}
-
}
-
}


When I add a Listener all the events I get are of type 0 with text=null and no other information except a Properties in event.data containing {command=resetAll}.
Comment by Martin Kristensen — 5. November 2009 @ 13:49