Home » Eclipse Projects » Eclipse Scout » Colour picker
|
Re: Colour picker [message #1070659 is a reply to message #1067517] |
Thu, 18 July 2013 12:07 |
Eclipse User |
|
|
|
Hi Urs
So far we did not implement a color picker. To get there implement a custom control and use the
SWT: org.eclipse.swt.widgets.ColorDialog
Swing: javax.swing.JColorChooser
If you are able please create a patch/gerrit change.
Many thanks...
-Andreas
|
|
| | |
Re: Colour picker [message #1072732 is a reply to message #1072724] |
Tue, 23 July 2013 11:48 |
Urs Beeli Messages: 573 Registered: October 2012 Location: Bern, Switzerland |
Senior Member |
|
|
Attached you will find a ZIP file with all the necessary java files:
Client
IColorPickerField
import org.eclipse.scout.rt.client.ui.action.menu.IMenu;
import org.eclipse.scout.rt.client.ui.form.fields.IFormField;
public interface IColorPickerField extends IFormField {
public static final String PROP_COLOR = "Color";
public static final String PROP_CLEAR_CUSTOM_COLORS = "ClearCustomColors";
String getColor();
void setColor(String c);
boolean hasMenus();
IMenu[] getMenus();
IColorPickerUiFacade getUiFacade();
}
IColorPickerUiFacade
import org.eclipse.scout.rt.client.ui.action.menu.IMenu;
public interface IColorPickerUiFacade {
void setColorFromUI(String color);
IMenu[] firePopupFromUI();
}
AbstractColorPickerField
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.minicrm.shared.services.process.AbstractColorPickerFieldData;
import org.eclipse.scout.commons.ConfigurationUtility;
import org.eclipse.scout.commons.TypeCastUtility;
import org.eclipse.scout.commons.annotations.ConfigOperation;
import org.eclipse.scout.commons.annotations.ConfigProperty;
import org.eclipse.scout.commons.annotations.ConfigPropertyValue;
import org.eclipse.scout.commons.annotations.FormData;
import org.eclipse.scout.commons.annotations.FormData.DefaultSubtypeSdkCommand;
import org.eclipse.scout.commons.annotations.FormData.SdkCommand;
import org.eclipse.scout.commons.annotations.Order;
import org.eclipse.scout.commons.exception.ProcessingException;
import org.eclipse.scout.commons.logger.IScoutLogger;
import org.eclipse.scout.commons.logger.ScoutLogManager;
import org.eclipse.scout.commons.xmlparser.SimpleXmlElement;
import org.eclipse.scout.rt.client.ui.action.menu.AbstractMenu;
import org.eclipse.scout.rt.client.ui.action.menu.IMenu;
import org.eclipse.scout.rt.client.ui.form.fields.customfield.AbstractCustomField;
import org.eclipse.scout.rt.shared.TEXTS;
import org.eclipse.scout.rt.shared.services.common.exceptionhandler.IExceptionHandlerService;
import org.eclipse.scout.rt.shared.ui.UserAgentUtility;
import org.eclipse.scout.service.SERVICES;
@FormData(value = AbstractColorPickerFieldData.class, sdkCommand = SdkCommand.CREATE, defaultSubtypeSdkCommand = DefaultSubtypeSdkCommand.CREATE)
public abstract class AbstractColorPickerField extends AbstractCustomField implements IColorPickerField {
private static final IScoutLogger LOG = ScoutLogManager.getLogger(AbstractColorPickerField.class);
private IColorPickerUiFacade m_uiFacade;
private IMenu[] m_menus;
private String m_color;
// ---------------------------------------------------------------------------------
@Override
@FormData
public String getColor() {
return m_color;
}
@Override
@FormData
public void setColor(String color) {
m_color = color;
propertySupport.setProperty(PROP_COLOR, color);
execChanged();
}
@Override
public IColorPickerUiFacade getUiFacade() {
return m_uiFacade;
}
@ConfigOperation
@Order(190)
// override if needed
public void execChanged() {
}
public void clearCustomColors() {
String toggle = "Clear";
if (propertySupport.getProperty(PROP_CLEAR_CUSTOM_COLORS) != null) {
toggle = null;
}
propertySupport.setProperty(PROP_CLEAR_CUSTOM_COLORS, toggle);
}
// ---------------------------------------------------------------------------------
/*
* Configuration
*/
// ---------------------------------------------------------------------------------
@ConfigProperty(ConfigProperty.STRING)
@Order(500)
@ConfigPropertyValue("0")
protected String getConfiguredColor() {
return null;
}
@ConfigProperty(ConfigProperty.BOOLEAN)
@Order(501)
@ConfigPropertyValue("FALSE")
protected boolean getConfiguredDefaultMenus() {
return false;
}
@SuppressWarnings("unchecked")
private Class<? extends IMenu>[] getConfiguredMenus() {
Class[] dca = ConfigurationUtility.getDeclaredPublicClasses(getClass());
Class<IMenu>[] foca = ConfigurationUtility.sortFilteredClassesByOrderAnnotation(dca, IMenu.class);
if (!getConfiguredDefaultMenus()) {
List<Class<IMenu>> menus = new ArrayList<Class<IMenu>>();
for (int i=0; i<foca.length; ++i) {
boolean local = false;
for (Class iface : foca[i].getInterfaces()) {
if (iface.getName().equals(IDefaultColorPickerMenu.class.getName())) {
local = true;
}
}
if (!local) {
menus.add(foca[i]);
}
}
foca = menus.toArray(new Class[menus.size()]);
}
return ConfigurationUtility.removeReplacedClasses(foca);
}
@Override
protected void initConfig() {
m_uiFacade = new P_UiFacade();
super.initConfig();
setColor(getConfiguredColor());
// menus
ArrayList<IMenu> menuList = new ArrayList<IMenu>();
Class<? extends IMenu>[] menuArray = getConfiguredMenus();
for (int i = 0; i < menuArray.length; i++) {
IMenu menu;
try {
menu = ConfigurationUtility.newInnerInstance(this, menuArray[i]);
menuList.add(menu);
} catch (Throwable t) {
SERVICES.getService(IExceptionHandlerService.class).handleException(new ProcessingException("menu: " + menuArray[i].getName(), t));
}
}
try {
injectMenusInternal(menuList);
} catch (Exception e) {
LOG.error("error occured while dynamically contributing menus.", e);
}
m_menus = menuList.toArray(new IMenu[0]);
}
/**
* Override this internal method only in order to make use of dynamic menus<br>
* Used to manage menu list and add/remove menus
*
* @param menuList
* live and mutable list of configured menus
*/
protected void injectMenusInternal(List<IMenu> menuList) {
}
@Override
public boolean hasMenus() {
return m_menus.length > 0;
}
@Override
public IMenu[] getMenus() {
return m_menus;
}
private IMenu[] firePopup() {
List<IMenu> result = new ArrayList<IMenu>();
// single observer add our menus
IMenu[] a = getMenus();
for (int i = 0; i < a.length; i++) {
IMenu m = a[i];
m.prepareAction();
if (m.isVisible()) {
result.add(m);
}
}
return result.toArray(new IMenu[result.size()]);
}
// ---------------------------------------------------------------------------------
private class P_UiFacade implements IColorPickerUiFacade {
@Override
public void setColorFromUI(String color) {
setColor(color);
}
@Override
public IMenu[] firePopupFromUI() {
return firePopup();
}
}
// ---------------------------------------------------------------------------------
private String m_initColor;
public AbstractColorPickerField() {
this(true);
}
public AbstractColorPickerField(boolean callInitializer) {
super(callInitializer);
}
// ---------------------------------------------------------------------------------
/*
* XML i/o
*/
@Override
public void storeXML(SimpleXmlElement x) throws ProcessingException {
super.storeXML(x);
try {
x.setObjectAttribute(IColorPickerField.PROP_COLOR, getColor());
} catch (IOException e) {
if (LOG.isInfoEnabled()) {
LOG.info("not serializable value in field " + getClass().getName() + "/" + getLabel() + ": " + e);
}
}
}
@Override
public void loadXML(SimpleXmlElement x) throws ProcessingException {
super.loadXML(x);
try {
String color = TypeCastUtility.castValue(x.getObjectAttribute(IColorPickerField.PROP_COLOR, null), String.class);
setColor(color);
} catch (Exception e) {
// be lenient, maybe the field was changed
LOG.warn(null, e);
}
}
// ---------------------------------------------------------------------------------
public void setInitValue(String initColor) {
m_initColor = initColor;
}
public String getInitColor() {
return m_initColor;
}
@Override
protected boolean execIsSaveNeeded() throws ProcessingException {
String color = getColor();
String initColor = getInitColor();
if (color == null && initColor == null) {
return false;
} else if (color == null || initColor == null) {
return true;
} else {
return !color.equals(initColor);
}
}
@Override
protected void execMarkSaved() throws ProcessingException {
super.execMarkSaved();
setInitValue(getColor());
}
// ---------------------------------------------------------------------------------
private interface IDefaultColorPickerMenu extends IMenu {
}
@Order(1.0)
public class ClearColorContextMenu extends AbstractMenu implements IDefaultColorPickerMenu {
@Override
protected String getConfiguredText() {
return TEXTS.get("ClearColor");
}
@Override
protected void execPrepareAction() throws ProcessingException {
setEnabled(getColor() != null);
}
@Override
protected void execAction() throws ProcessingException {
setColor(null);
}
}
@Order(2.0)
public class ClearCustomColorsContextMenu extends AbstractMenu implements IDefaultColorPickerMenu {
@Override
protected String getConfiguredText() {
return TEXTS.get("ClearCustomColors");
}
@Override
protected void execAction() throws ProcessingException {
clearCustomColors();
}
@Override
protected void execPrepareAction() throws ProcessingException {
// when "recent color" support has been added to Swing, isSwtUi() can be replaced with isRichClient()
setVisible(UserAgentUtility.isSwtUi());
// setVisible(UserAgentUtility.isRichClient());
}
}
@Order(3.0)
public class DefaultSeparatorContextMenu extends AbstractMenu implements IDefaultColorPickerMenu {
@Override
protected boolean getConfiguredSeparator() {
return true;
}
}
}
SWT
SwtScoutColorPickerField
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.minicrm.client.ui.form.fields.ext.IColorPickerField;
import org.eclipse.scout.commons.RunnableWithData;
import org.eclipse.scout.commons.StringUtility;
import org.eclipse.scout.commons.job.JobEx;
import org.eclipse.scout.rt.client.ui.action.menu.IMenu;
import org.eclipse.scout.rt.shared.TEXTS;
import org.eclipse.scout.rt.ui.swt.LogicalGridLayout;
import org.eclipse.scout.rt.ui.swt.SwtMenuUtility;
import org.eclipse.scout.rt.ui.swt.ext.StatusLabelEx;
import org.eclipse.scout.rt.ui.swt.form.fields.SwtScoutFieldComposite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
public class SwtScoutColorPickerField extends SwtScoutFieldComposite<IColorPickerField> {
private P_MouseListener m_mouseListener;
private static RGB[] rgbs;
private Menu m_contextMenu;
private final static RGB WHITE = new RGB(255, 255, 255);
@Override
protected void initializeSwt(Composite parent) {
m_mouseListener = new P_MouseListener();
Composite container = getEnvironment().getFormToolkit().createComposite(parent);
StatusLabelEx label = getEnvironment().getFormToolkit().createStatusLabel(container, getEnvironment(), getScoutObject());
Canvas canvas = getEnvironment().getFormToolkit().createCanvas(container);
canvas.addPaintListener(new P_PaintListener());
canvas.addMouseListener(m_mouseListener);
// context menu
m_contextMenu = new Menu(canvas.getShell(), SWT.POP_UP);
m_contextMenu.addMenuListener(new P_ContextMenuListener());
canvas.setMenu(m_contextMenu);
setSwtContainer(container);
setSwtLabel(label);
setSwtField(canvas);
// layout
getSwtContainer().setLayout(new LogicalGridLayout(1, 0));
}
@Override
protected void attachScout() {
super.attachScout();
updateColorFromScout();
}
protected void updateColorFromScout() {
getSwtField().redraw();
}
protected void handleSwtPickColor() {
Shell shell = new Shell(getEnvironment().getDisplay());
ColorDialog colorDialog = new ColorDialog(shell);
colorDialog.setText(TEXTS.get("SelectColor"));
RGB rgb = getRgb(getScoutObject().getColor());
if (rgb != null) {
colorDialog.setRGB(rgb);
if (rgbs == null) {
rgbs = colorDialog.getRGBs();
}
if (rgbs != null) {
boolean found = false;
for(int i=0; i<rgbs.length; ++i) {
if (rgbs[i] != null && rgbs[i].equals(rgb)) {
found = true;
break;
}
}
if (!found) {
for (int i = 0; i < rgbs.length; ++i) {
if (WHITE.equals(rgbs[i])) {
rgbs[i] = rgb;
found = true;
break;
}
}
}
if (!found) {
rgbs[rgbs.length-1] = rgb;
}
} else {
rgbs = new RGB[] { rgb };
}
}
colorDialog.setRGBs(rgbs);
RGB selectedColor = colorDialog.open();
if (selectedColor != null) {
rgbs = colorDialog.getRGBs();
final String color = getColorString(selectedColor.red, selectedColor.green, selectedColor.blue).toUpperCase();
RunnableWithData t = new RunnableWithData() {
@Override
public void run() {
getScoutObject().getUiFacade().setColorFromUI(color);
}
};
getEnvironment().invokeScoutLater(t, 2345);
}
shell.dispose();
}
public String getColorString(int red, int green, int blue) {
int iRgb = ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | ((blue & 0xFF) << 0);
String hexString = Integer.toHexString(iRgb);
while (hexString.length() < 6) {
hexString = "0" + hexString;
}
return hexString;
}
public RGB getRgb(String color) {
if (!StringUtility.isNullOrEmpty(color) && color.length() >= 6) {
int red = Integer.parseInt(color.substring(0, 2), 16);
int green = Integer.parseInt(color.substring(2, 4), 16);
int blue = Integer.parseInt(color.substring(4, 6), 16);
return new RGB(red, green, blue);
}
return null;
}
@Override
protected void handleScoutPropertyChange(String name, Object newValue) {
if (name.equals(IColorPickerField.PROP_COLOR)) {
updateColorFromScout();
} else if (name.equals(IColorPickerField.PROP_CLEAR_CUSTOM_COLORS)) {
rgbs = new RGB[] { WHITE };
}
super.handleScoutPropertyChange(name, newValue);
}
private class P_PaintListener implements PaintListener {
private static final long serialVersionUID = 1L;
@Override
public void paintControl(PaintEvent e) {
if (getScoutObject().getColor() != null) {
e.gc.setBackground(getEnvironment().getColor(getScoutObject().getColor()));
e.gc.setForeground(getEnvironment().getColor("000000"));
} else {
e.gc.setBackground(getEnvironment().getColor("FFFFFF"));
e.gc.setForeground(getEnvironment().getColor("000000"));
}
Rectangle bounds = getSwtField().getBounds();
e.gc.fillRectangle(bounds);
e.gc.drawRectangle(bounds.x, bounds.y, bounds.width - bounds.x - 1, bounds.height - 1);
}
}
private class P_MouseListener implements MouseListener, MouseMoveListener {
@Override
public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) {
}
@Override
public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
if (e != null && e.button == 1) {
handleSwtPickColor();
}
}
@Override
public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
}
@Override
public void mouseMove(org.eclipse.swt.events.MouseEvent e) {
}
}
private class P_ContextMenuListener extends MenuAdapter {
@Override
public void menuShown(MenuEvent e) {
for (MenuItem item : m_contextMenu.getItems()) {
disposeMenuItem(item);
}
final AtomicReference<IMenu[]> scoutMenusRef = new AtomicReference<IMenu[]>();
Runnable t = new Runnable() {
@Override
public void run() {
IMenu[] scoutMenus = getScoutObject().getUiFacade().firePopupFromUI();
scoutMenusRef.set(scoutMenus);
}
};
JobEx job = getEnvironment().invokeScoutLater(t, 1200);
try {
job.join(1200);
} catch (InterruptedException ex) {
// nop
}
// grab the actions out of the job, when the actions are providden within
// the scheduled time the popup will be handled.
if (scoutMenusRef.get() != null) {
SwtMenuUtility.fillContextMenu(scoutMenusRef.get(), m_contextMenu, getEnvironment());
}
}
private void disposeMenuItem(MenuItem item) {
Menu menu = item.getMenu();
if (menu != null) {
for (MenuItem childItem : menu.getItems()) {
disposeMenuItem(childItem);
}
menu.dispose();
}
item.dispose();
}
} // end class P_ContextMenuListener
}
plugin.xml
<extension
point="org.eclipse.scout.rt.ui.swt.formfields">
<formField
active="true"
modelClass="org.eclipse.minicrm.client.ui.form.fields.ext.IColorPickerField"
name="Color Picker Field"
scope="default">
<uiClass
class="org.eclipse.minicrm.ui.swt.form.fields.ext.SwtScoutColorPickerField">
</uiClass>
</formField>
</extension>
Swing
SwingScoutColorPickerField
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import org.eclipse.minicrm.client.ui.form.fields.ext.IColorPickerField;
import org.eclipse.scout.commons.RunnableWithData;
import org.eclipse.scout.commons.StringUtility;
import org.eclipse.scout.rt.client.ui.action.menu.IMenu;
import org.eclipse.scout.rt.shared.TEXTS;
import org.eclipse.scout.rt.ui.swing.LogicalGridLayout;
import org.eclipse.scout.rt.ui.swing.SwingPopupWorker;
import org.eclipse.scout.rt.ui.swing.SwingUtility;
import org.eclipse.scout.rt.ui.swing.ext.BorderLayoutEx;
import org.eclipse.scout.rt.ui.swing.ext.JPanelEx;
import org.eclipse.scout.rt.ui.swing.ext.JStatusLabelEx;
import org.eclipse.scout.rt.ui.swing.form.fields.SwingScoutFieldComposite;
public class SwingScoutColorPickerField extends SwingScoutFieldComposite<IColorPickerField> {
private P_MouseListener m_mouseListener;
@Override
protected void initializeSwing() {
m_mouseListener = new P_MouseListener();
JPanelEx container = new JPanelEx(new BorderLayoutEx());
container.setOpaque(false);
JStatusLabelEx label = new JStatusLabelEx();
container.add(label, BorderLayoutEx.CENTER);
MyComponent field = new MyComponent();
SwingUtility.installDefaultFocusHandling(field);
container.add(field);
field.addMouseListener(m_mouseListener);
setSwingContainer(container);
setSwingLabel(label);
setSwingField(field);
getSwingContainer().setLayout(new LogicalGridLayout(getSwingEnvironment(), 1, 0));
}
@Override
protected void attachScout() {
super.attachScout();
updateColorFromScout();
}
protected void updateColorFromScout() {
getSwingField().repaint();
}
protected void handleSwtPickColor() {
Color color = getColor(getScoutObject().getColor());
// TODO: add recent colour handling : see http://stackoverflow.com/questions/10793916/jcolorchooser-save-restore-recent-colors-in-swatches-panel
Color selectedColor = JColorChooser.showDialog(getSwingField(), TEXTS.get("SelectColor"), color);
if (selectedColor != null) {
final String colorString = getColorString(selectedColor.getRed(), selectedColor.getGreen(), selectedColor.getBlue()).toUpperCase();
RunnableWithData t = new RunnableWithData() {
@Override
public void run() {
getScoutObject().getUiFacade().setColorFromUI(colorString);
}
};
getSwingEnvironment().invokeScoutLater(t, 2345);
}
}
public String getColorString(int red, int green, int blue) {
int iRgb = ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | ((blue & 0xFF) << 0);
String hexString = Integer.toHexString(iRgb);
while (hexString.length() < 6) {
hexString = "0" + hexString;
}
return hexString;
}
public Color getColor(String color) {
if (!StringUtility.isNullOrEmpty(color) && color.length() >= 6) {
int red = Integer.parseInt(color.substring(0, 2), 16);
int green = Integer.parseInt(color.substring(2, 4), 16);
int blue = Integer.parseInt(color.substring(4, 6), 16);
return new Color(red, green, blue);
}
return null;
}
@Override
protected void handleScoutPropertyChange(String name, Object newValue) {
if (name.equals(IColorPickerField.PROP_COLOR)) {
updateColorFromScout();
} else if (name.equals(IColorPickerField.PROP_CLEAR_CUSTOM_COLORS)) {
// TODO: clear recent
}
super.handleScoutPropertyChange(name, newValue);
}
private class MyComponent extends JComponent {
private static final long serialVersionUID = 1L;
@Override
public void paint(Graphics g) {
super.paint(g);
Rectangle bounds = getSwingField().getBounds();
if (getScoutObject().getColor() != null) {
g.setColor(getColor(getScoutObject().getColor()));
} else {
g.setColor(getColor("FFFFFF"));
}
g.fillRect(bounds.x, bounds.y, bounds.width - bounds.x - 1, bounds.height - 1);
if (getScoutObject().getColor() != null) {
g.setColor(getColor(getScoutObject().getColor()));
} else {
g.setColor(getColor("000000"));
}
g.drawRect(bounds.x, bounds.y, bounds.width - bounds.x - 1, bounds.height - 1);
}
}
private class P_MouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
if (e != null) {
if (e.getButton() == MouseEvent.BUTTON1) {
handleSwtPickColor();
}
if (e.isPopupTrigger() && e.getComponent().isEnabled()) {
handleSwingPopup(e);
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e != null && e.isPopupTrigger() && e.getComponent().isEnabled()) {
handleSwingPopup(e);
}
}
@Override
public void mouseDragged(MouseEvent e) {
}
}
protected void handleSwingPopup(final MouseEvent e) {
// notify Scout
Runnable t = new Runnable() {
@Override
public void run() {
IMenu[] a = getScoutObject().getUiFacade().firePopupFromUI();
// call swing menu
new SwingPopupWorker(getSwingEnvironment(), e.getComponent(), e.getPoint(), a).enqueue();
}
};
getSwingEnvironment().invokeScoutLater(t, 5678);
// end notify
}
}
plugin.xml
<extension
point="org.eclipse.scout.rt.ui.swing.formfields">
<formField
active="true"
modelClass="org.eclipse.minicrm.client.ui.form.fields.ext.IColorPickerField"
name="Color Picker Field"
scope="default">
<uiClass
class="org.eclipse.minicrm.ui.swing.form.fields.ext.SwingScoutColorPickerField">
</uiClass>
</extension>
RAP
RwtScoutColorPickerField
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.minicrm.client.ui.form.fields.ext.IColorPickerField;
import org.eclipse.scout.commons.RunnableWithData;
import org.eclipse.scout.commons.StringUtility;
import org.eclipse.scout.commons.job.JobEx;
import org.eclipse.scout.rt.client.ui.action.menu.IMenu;
import org.eclipse.scout.rt.shared.TEXTS;
import org.eclipse.scout.rt.ui.rap.LogicalGridLayout;
import org.eclipse.scout.rt.ui.rap.RwtMenuUtility;
import org.eclipse.scout.rt.ui.rap.ext.StatusLabelEx;
import org.eclipse.scout.rt.ui.rap.form.fields.RwtScoutFieldComposite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
public class RwtScoutColorPickerField extends RwtScoutFieldComposite<IColorPickerField> {
private P_MouseListener m_mouseListener;
private Menu m_contextMenu;
@Override
protected void initializeUi(Composite parent) {
m_mouseListener = new P_MouseListener();
Composite container = getUiEnvironment().getFormToolkit().createComposite(parent);
StatusLabelEx label = getUiEnvironment().getFormToolkit().createStatusLabel(container, getScoutObject());
Canvas canvas = getUiEnvironment().getFormToolkit().createCanvas(container);
canvas.addPaintListener(new P_PaintListener());
canvas.addMouseListener(m_mouseListener);
// context menu
m_contextMenu = new Menu(canvas.getShell(), SWT.POP_UP);
m_contextMenu.addMenuListener(new P_ContextMenuListener());
canvas.setMenu(m_contextMenu);
setUiContainer(container);
setUiLabel(label);
setUiField(canvas);
// layout
getUiContainer().setLayout(new LogicalGridLayout(1, 0));
}
@Override
protected void attachScout() {
super.attachScout();
updateColorFromScout();
}
protected void updateColorFromScout() {
getUiField().redraw();
}
protected void handleSwtPickColor() {
Shell shell = new Shell(getUiEnvironment().getDisplay());
ColorDialog colorDialog = new ColorDialog(shell);
colorDialog.setText(TEXTS.get("SelectColor"));
RGB rgb = getRgb(getScoutObject().getColor());
if (rgb != null) {
colorDialog.setRGB(rgb);
}
RGB selectedColor = colorDialog.open();
if (selectedColor != null) {
final String color = getColorString(selectedColor.red, selectedColor.green, selectedColor.blue).toUpperCase();
RunnableWithData t = new RunnableWithData() {
@Override
public void run() {
getScoutObject().getUiFacade().setColorFromUI(color);
}
};
getUiEnvironment().invokeScoutLater(t, 2345);
}
shell.dispose();
}
public String getColorString(int red, int green, int blue) {
int iRgb = ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | ((blue & 0xFF) << 0);
String hexString = Integer.toHexString(iRgb);
while (hexString.length() < 6) {
hexString = "0" + hexString;
}
return hexString;
}
public RGB getRgb(String color) {
if (!StringUtility.isNullOrEmpty(color) && color.length() >= 6) {
int red = Integer.parseInt(color.substring(0, 2), 16);
int green = Integer.parseInt(color.substring(2, 4), 16);
int blue = Integer.parseInt(color.substring(4, 6), 16);
return new RGB(red, green, blue);
}
return null;
}
@Override
protected void handleScoutPropertyChange(String name, Object newValue) {
if (name.equals(IColorPickerField.PROP_COLOR)) {
updateColorFromScout();
}
super.handleScoutPropertyChange(name, newValue);
}
private class P_PaintListener implements PaintListener {
private static final long serialVersionUID = 1L;
@Override
public void paintControl(PaintEvent e) {
if (getScoutObject().getColor() != null) {
e.gc.setBackground(getUiEnvironment().getColor(getScoutObject().getColor()));
e.gc.setForeground(getUiEnvironment().getColor("000000"));
} else {
e.gc.setBackground(getUiEnvironment().getColor("FFFFFF"));
e.gc.setForeground(getUiEnvironment().getColor("000000"));
}
Rectangle bounds = getUiField().getBounds();
e.gc.fillRectangle(bounds);
e.gc.drawRectangle(bounds.x, bounds.y, bounds.width - bounds.x - 1, bounds.height - 1);
}
}
private class P_MouseListener implements MouseListener, MouseMoveListener {
private static final long serialVersionUID = 1L;
private boolean m_mouseDown = false;
@Override
public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) {
}
@Override
public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
if (e != null && e.button == 1) {
m_mouseDown = true;
handleSwtPickColor();
}
}
@Override
public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
}
@Override
public void mouseMove(org.eclipse.swt.events.MouseEvent e) {
}
}
private class P_ContextMenuListener extends MenuAdapter {
private static final long serialVersionUID = 1L;
@Override
public void menuShown(MenuEvent e) {
for (MenuItem item : m_contextMenu.getItems()) {
disposeMenuItem(item);
}
final AtomicReference<IMenu[]> scoutMenusRef = new AtomicReference<IMenu[]>();
Runnable t = new Runnable() {
@Override
public void run() {
IMenu[] scoutMenus = getScoutObject().getUiFacade().firePopupFromUI();
scoutMenusRef.set(scoutMenus);
}
};
JobEx job = getUiEnvironment().invokeScoutLater(t, 1200);
try {
job.join(1200);
} catch (InterruptedException ex) {
// nop
}
// grab the actions out of the job, when the actions are providden within
// the scheduled time the popup will be handled.
if (scoutMenusRef.get() != null) {
RwtMenuUtility.fillContextMenu(scoutMenusRef.get(), getUiEnvironment(), m_contextMenu);
}
}
private void disposeMenuItem(MenuItem item) {
Menu menu = item.getMenu();
if (menu != null) {
for (MenuItem childItem : menu.getItems()) {
disposeMenuItem(childItem);
}
menu.dispose();
}
item.dispose();
}
} // end class P_ContextMenuListener
}
plugin.xml
<extension
point="org.eclipse.scout.rt.ui.rap.formfields">
<formField
active="true"
modelClass="org.eclipse.minicrm.client.ui.form.fields.ext.IColorPickerField"
name="Color Picker Field"
scope="default">
<uiClass
class="org.eclipse.minicrm.ui.rap.form.fields.ext.RwtScoutColorPickerField">
</uiClass>
</formField>
</extension>
|
|
| |
Re: Colour picker [message #1073116 is a reply to message #1072759] |
Wed, 24 July 2013 07:06 |
Urs Beeli Messages: 573 Registered: October 2012 Location: Bern, Switzerland |
Senior Member |
|
|
A small correction, it seems there is something funny about the values getBounds() returns in all of the renderers. In order to correctly draw the fields in the currently selected colour, some code must be adjusted:
SWT & RAP
In paintControl() replace
e.gc.fillRectangle(bounds);
e.gc.drawRectangle(bounds.x, bounds.y, bounds.width - bounds.x - 1, bounds.height - 1);
with
e.gc.fillRectangle(0, 0, bounds.width - 1, bounds.height - 1);
e.gc.drawRectangle(0, 0, bounds.width - 1, bounds.height - 1);
Swing
In MyComponent.paint() Replace
g.fillRect(bounds.x, bounds.y, bounds.width - bounds.x - 1, bounds.height - 1);
if (getScoutObject().getColor() != null) {
g.setColor(getColor(getScoutObject().getColor()));
} else {
g.setColor(getColor("000000"));
}
g.drawRect(bounds.x, bounds.y, bounds.width - bounds.x - 1, bounds.height - 1);
with
g.fillRect(0, 0, bounds.width - 1, bounds.height - 1);
g.setColor(getColor("000000"));
g.drawRect(0, 0, bounds.width - 1, bounds.height - 1);
[Updated on: Wed, 24 July 2013 07:06] Report message to a moderator
|
|
| | | | | | |
Goto Forum:
Current Time: Mon Sep 23 11:07:14 GMT 2024
Powered by FUDForum. Page generated in 0.05921 seconds
|