Permalink
Browse files

Update drawNumbers state when model is changed

Makes sure that the menu item toggle state is synced with the RendererModel

Sovles bug 1953
  • Loading branch information...
1 parent ce67999 commit 665758195246751b4e295260fd813d617dae6a57 Arvid Berg committed with jonalv Jul 1, 2010
@@ -14,14 +14,17 @@
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.EventObject;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import javax.vecmath.Vector2d;
import net.bioclipse.cdk.domain.ICDKMolecule;
+import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
@@ -37,6 +40,10 @@
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.activities.WorkbenchActivityHelper;
+import org.eclipse.ui.commands.ICommandService;
+import org.eclipse.ui.services.IServiceScopes;
import org.openscience.cdk.event.ICDKChangeListener;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IChemModel;
@@ -133,7 +140,7 @@ public void widgetDisposed( DisposeEvent e ) {
renderer = new Renderer(generators, fontManager);
rendererModel = renderer.getRenderer2DModel();
setupPaintListener();
- //setupPreferenceListener( renderer );
+ setupPreferenceListener( renderer );
drawNumbers.setUse(false);
setAtomNumberColors( drawNumbers );
}
@@ -151,9 +158,10 @@ private void setupPreferenceListener(IRenderer renderer) {
public void stateChanged( EventObject event ) {
if(event.getSource() instanceof RendererModel) {
- drawNumbers.setUse(
- JChemPaintWidget.this.renderer.getRenderer2DModel().
- getParameter(WillDrawAtomNumbers.class).getValue());
+ ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
+ Map filter = new HashMap();
+ filter.put(IServiceScopes.WINDOW_SCOPE, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
+ service.refreshElements("net.bioclipse.cdk.jchempaint.preference.atomNumbers", filter);
}
}
});
@@ -14,7 +14,9 @@
package net.bioclipse.cdk.jchempaint.business;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import net.bioclipse.cdk.jchempaint.Activator;
import net.bioclipse.cdk.jchempaint.editor.JChemPaintEditor;
@@ -28,6 +30,8 @@
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.commands.ICommandService;
+import org.eclipse.ui.services.IServiceScopes;
import org.openscience.cdk.renderer.RendererModel;
import org.openscience.cdk.renderer.generators.IGenerator;
import org.openscience.cdk.renderer.generators.IGeneratorParameter;
@@ -116,6 +120,11 @@ public void applyProperties(RendererModel model) throws BioclipseException {
applyProperty(model, WedgeWidth.class, getWedgeWidth());
applyProperty(model, ShowImplicitHydrogens.class, getShowImplicitHydrogens());
applyProperty(model, WillDrawAtomNumbers.class, getShowNumbers());
+
+ ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
+ Map filter = new HashMap();
+ filter.put(IServiceScopes.WINDOW_SCOPE, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
+ service.refreshElements("net.bioclipse.cdk.jchempaint.preference.atomNumbers", filter);
}
public void applyGlobalProperties() throws BioclipseException {
@@ -26,14 +26,10 @@
import net.bioclipse.cdk.business.Activator;
import net.bioclipse.cdk.business.ICDKManager;
import net.bioclipse.cdk.domain.CDKChemObject;
-import net.bioclipse.cdk.domain.CDKMoleculeUtils;
import net.bioclipse.cdk.domain.ICDKMolecule;
import net.bioclipse.cdk.domain.ISubStructure;
-import net.bioclipse.cdk.domain.CDKMoleculeUtils.MolProperty;
import net.bioclipse.cdk.jchempaint.generators.SubStructureGenerator;
import net.bioclipse.cdk.jchempaint.handlers.ModuleState;
-import net.bioclipse.cdk.jchempaint.handlers.RedoHandler;
-import net.bioclipse.cdk.jchempaint.handlers.UndoHandler;
import net.bioclipse.cdk.jchempaint.outline.JCPOutlinePage;
import net.bioclipse.cdk.jchempaint.view.JChemPaintWidget;
import net.bioclipse.cdk.jchempaint.view.JChemPaintWidget.Message;
@@ -45,7 +41,9 @@
import net.bioclipse.ui.dialogs.SaveAsDialog;
import org.apache.log4j.Logger;
+import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.commands.State;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
@@ -77,7 +75,6 @@
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
-import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
@@ -88,13 +85,13 @@
import org.eclipse.ui.IWorkbenchPartConstants;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.PartInitException;
-import org.eclipse.ui.actions.ActionFactory;
+import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.contexts.IContextService;
-import org.eclipse.ui.operations.UndoActionHandler;
import org.eclipse.ui.operations.UndoRedoActionGroup;
import org.eclipse.ui.part.EditorPart;
import org.eclipse.ui.part.FileEditorInput;
+import org.eclipse.ui.services.IServiceScopes;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
import org.openscience.cdk.controller.ControllerHub;
import org.openscience.cdk.controller.IChemModelRelay;
@@ -111,6 +108,7 @@
import org.openscience.cdk.io.formats.MDLV2000Format;
import org.openscience.cdk.renderer.RendererModel;
import org.openscience.cdk.renderer.generators.IGenerator;
+import org.openscience.cdk.renderer.generators.AtomNumberGenerator.WillDrawAtomNumbers;
import org.openscience.cdk.renderer.selection.AbstractSelection;
import org.openscience.cdk.renderer.selection.IChemObjectSelection;
import org.openscience.cdk.renderer.selection.MultiSelection;
@@ -302,8 +300,8 @@ public boolean isSaveAsAllowed() {
public void createPartControl( Composite parent ) {
createWidget(parent);
-
createMenu();
+ updateMenu(getWidget().getRenderer2DModel());
getSite().getPage().addSelectionListener( this );
@@ -362,6 +360,10 @@ public void widgetDisposed( DisposeEvent e ) {
contextService.activateContext( "net.bioclipse.ui.contexts.JChemPaint" );
createUndoRedoHandler();
+ Map filter = new HashMap();
+ filter.put(IServiceScopes.WINDOW_SCOPE, getSite().getPage().getWorkbenchWindow());
+ ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
+ service.refreshElements("net.bioclipse.cdk.jchempaint.preference.atomNumbers", filter);
}
private void createPartListener() {
@@ -419,6 +421,15 @@ private void createUndoRedoHandler() {
actionGroup.fillActionBars( site.getActionBars() );
}
+ public void updateMenu(RendererModel model) {
+ Boolean value = model.get(WillDrawAtomNumbers.class);
+ ICommandService service =
+ (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
+ Command command = service.getCommand("net.bioclipse.cdk.jchempaint.preference.atomNumbers");
+ State state = command.getState("org.eclipse.ui.commands.toggleState");
+ state.setValue(value);
+ }
+
private void createMenu() {
MenuManager menuMgr = new MenuManager();
@@ -12,26 +12,83 @@
******************************************************************************/
package net.bioclipse.cdk.jchempaint.handlers;
-import net.bioclipse.cdk.jchempaint.Activator;
-import net.bioclipse.cdk.jchempaint.business.IJChemPaintManager;
+import java.util.HashMap;
+import java.util.Map;
+
+import net.bioclipse.cdk.jchempaint.editor.JChemPaintEditor;
+import net.bioclipse.cdk.jchempaint.view.JChemPaintWidget;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.IWorkbenchPartSite;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.commands.ICommandService;
+import org.eclipse.ui.commands.IElementUpdater;
import org.eclipse.ui.handlers.HandlerUtil;
+import org.eclipse.ui.menus.UIElement;
+import org.eclipse.ui.services.IServiceScopes;
+import org.openscience.cdk.renderer.RendererModel;
+import org.openscience.cdk.renderer.generators.AtomNumberGenerator.WillDrawAtomNumbers;
+
+public class TogglePrefHandler extends AbstractHandler implements IElementUpdater{
-public class TogglePrefHandler extends AbstractHandler {
+ boolean isSelected = false;
public Object execute( ExecutionEvent event ) throws ExecutionException {
Command command = event.getCommand();
- boolean oldValue = HandlerUtil.toggleCommandState( command );
-
- IJChemPaintManager jcp = Activator.getDefault().getJavaManager();
-
- jcp.setDrawNumbers( !oldValue );
-
+ // boolean oldValue = HandlerUtil.toggleCommandState( command );
+ boolean oldValue = false;
+ IEditorPart jcpPart = HandlerUtil.getActiveEditor(event);
+
+ JChemPaintWidget jcpWidget = getWidget(jcpPart);
+ if(jcpWidget != null)
+ updateModel(jcpWidget, oldValue);
+
+ IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
+ Map filter = new HashMap();
+ filter.put(IServiceScopes.WINDOW_SCOPE, window);
+ ICommandService service = (ICommandService) window.getService(ICommandService.class);
+ service.refreshElements("net.bioclipse.cdk.jchempaint.preference.atomNumbers", filter);
+ return null;
+ }
+
+ private JChemPaintWidget getWidget(IWorkbenchPart part) {
+ if(part instanceof JChemPaintEditor) {
+ return ((JChemPaintEditor) part).getWidget();
+ } else {
+ JChemPaintEditor editor = (JChemPaintEditor) part.getAdapter(JChemPaintEditor.class);
+ if(editor != null) return editor.getWidget();
+ }
return null;
}
+
+ private void updateModel(JChemPaintWidget widget, boolean oldValue) {
+ RendererModel model = widget.getRenderer2DModel();
+ boolean value = model.get(WillDrawAtomNumbers.class);
+ model.set(WillDrawAtomNumbers.class, !value);
+ isSelected = !value;
+ widget.redraw();
+
+ }
+
+ @Override
+ public void updateElement(UIElement element, Map parameters) {
+ boolean state = isSelected;
+ Object value = parameters.get("org.eclipse.ui.part.IWorkbenchPartSite");
+ if( value instanceof IWorkbenchPartSite) {
+ IWorkbenchPartSite site = (IWorkbenchPartSite) value;
+ IWorkbenchPart part = site.getPart();
+ JChemPaintWidget widget = getWidget(part);
+ if(widget!=null) {
+ RendererModel model = widget.getRenderer2DModel();
+ state = model.get(WillDrawAtomNumbers.class);
+ }
+ }
+ element.setChecked(state);
+ }
}

0 comments on commit 6657581

Please sign in to comment.