rich client 2.0

Example


29. December 2006
Tom Seidel @ 23:43

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:
  1. final HtmlComposer composer = new HtmlComposer(comp, SWT.BORDER);
  2. composer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

The Bold Action

JAVA:
  1. import org.eclipse.jface.action.Action;
  2. import org.eclipse.jface.action.IAction;
  3. import org.eclipse.swt.widgets.Event;
  4. import org.eclipse.swt.widgets.Listener;
  5. import org.eclipse.ui.plugin.AbstractUIPlugin;
  6. import de.spiritlink.richhtml4eclipse.widgets.AllActionConstants;
  7. import de.spiritlink.richhtml4eclipse.widgets.ComposerStatus;
  8. import de.spiritlink.richhtml4eclipse.widgets.EventConstants;
  9. import de.spiritlink.richhtml4eclipse.widgets.HtmlComposer;
  10. import de.spiritlink.richhtml4eclipse.widgets.JavaScriptCommands;
  11. /**
  12. * A JFace-Action for switching the bold status of a selection
  13. * or the current cursor. Bold has toggle behavior, therefore
  14. * the action has the {@link IAction#AS_CHECK_BOX} flag.
  15. * @author Tom Seidel <tom.seidel@spiritlink.de>
  16. *
  17. */
  18. public class BoldAction extends Action implements Listener {
  19.     private HtmlComposer composer = null;
  20.     public BoldAction(HtmlComposer composer) {
  21.         super("", IAction.AS_CHECK_BOX); //$NON-NLS-1$
  22.         setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin("de.spiritlink.richhtml4eclipse", //$NON-NLS-1$
  23.                 "tiny_mce/jscripts/tiny_mce/themes/advanced/images/bold.gif")); //$NON-NLS-1$
  24.         this.composer = composer;
  25.         // adds a listener to the widget for "bold-events"
  26.         this.composer.addListener(EventConstants.BOLD, this);
  27.     }
  28.     /* (non-Javadoc)
  29.      * @see org.eclipse.jface.action.Action#run()
  30.      */
  31.     @Override
  32.     public void run() {
  33.         // Executes the command for bold to the composer
  34.         this.composer.execute(JavaScriptCommands.BOLD);
  35.     }
  36.     /* (non-Javadoc)
  37.      * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
  38.      */
  39.     public void handleEvent(Event event) {
  40.         // callback for the status of the current selection/cusor
  41.         if (ComposerStatus.DISABLED.equals(event.text)) {
  42.             setChecked(false);
  43.             setEnabled(false);
  44.         } else if (ComposerStatus.SELECTED.equals(event.text)) {
  45.             // current selection/cursor is bold --> set the action checked
  46.             setEnabled(true);
  47.             setChecked(true);
  48.         } else if (ComposerStatus.NORMAL.equals(event.text)) {
  49.             setEnabled(true);
  50.             setChecked(false);
  51.         } else if (event.type == EventConstants.ALL && event.text.equals(AllActionConstants.RESET_ALL)) {
  52.             // callback if the cursor changed, reset the state.
  53.             setEnabled(true);
  54.             setChecked(false);
  55.         }
  56.     }
  57. }

1 Comment »

  1. 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

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress