|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Patrick Ziegler and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Patrick Ziegler - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | + |
| 14 | +package org.eclipse.gef.examples.shapes.handler; |
| 15 | + |
| 16 | +import org.eclipse.e4.core.contexts.Active; |
| 17 | +import org.eclipse.e4.core.di.annotations.CanExecute; |
| 18 | +import org.eclipse.e4.core.di.annotations.Execute; |
| 19 | +import org.eclipse.e4.ui.model.application.ui.basic.MPart; |
| 20 | +import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem; |
| 21 | +import org.eclipse.ui.IEditorPart; |
| 22 | + |
| 23 | +import org.eclipse.gef.GraphicalViewer; |
| 24 | +import org.eclipse.gef.ui.palette.PaletteViewer; |
| 25 | + |
| 26 | +import org.eclipse.gef.examples.shapes.ShapesEditor; |
| 27 | +import org.eclipse.gef.examples.shapes.palette.ShapesColorPalette; |
| 28 | + |
| 29 | +/** |
| 30 | + * This handler manages the {@code Use Custom Palette} menu item which switches |
| 31 | + * between the default and the custom palette theme. |
| 32 | + */ |
| 33 | +public class ColorPaletteHandler { |
| 34 | + @Execute |
| 35 | + @SuppressWarnings("static-method") |
| 36 | + public void execute(@Active MPart activePart, MMenuItem menuItem) { |
| 37 | + IEditorPart editorPart = activePart.getContext().get(IEditorPart.class); |
| 38 | + GraphicalViewer graphicalViewer = editorPart.getAdapter(GraphicalViewer.class); |
| 39 | + PaletteViewer paletteViewer = graphicalViewer.getEditDomain().getPaletteViewer(); |
| 40 | + if (menuItem.isSelected()) { |
| 41 | + paletteViewer.setColorPalette(new ShapesColorPalette()); |
| 42 | + } else { |
| 43 | + paletteViewer.setColorPalette(null); |
| 44 | + } |
| 45 | + paletteViewer.getControl().redraw(); |
| 46 | + } |
| 47 | + |
| 48 | + @CanExecute |
| 49 | + @SuppressWarnings("static-method") |
| 50 | + public boolean canExecute(@Active MPart activePart) { |
| 51 | + return activePart.getContext().get(IEditorPart.class) instanceof ShapesEditor; |
| 52 | + } |
| 53 | +} |
0 commit comments