下面列出了org.eclipse.swt.widgets.ColorDialog#open ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void openWindow(final PTWidget widget, final Item item, final PTProperty property) {
final ColorDialog dialog = new ColorDialog(widget.getWidget().getShell());
final RGB result = dialog.open();
if (result != null) {
property.setValue(result);
final Color bgColor = getBackgroundColor(property);
if (bgColor != null) {
if (item instanceof TableItem) {
((TableItem) item).setBackground(1, bgColor);
}
if (item instanceof TreeItem) {
((TreeItem) item).setBackground(1, bgColor);
}
SWTGraphicUtil.addDisposer(item, bgColor);
}
if (item instanceof TableItem) {
((TableItem) item).setText(1, getTextFor(property));
} else {
((TreeItem) item).setText(1, getTextFor(property));
}
}
}
public void changeBackgroundColor() {
ColorDialog cd = new ColorDialog(getShell());
cd.setRGB(text.getBackground().getRGB());
cd.setText("Choose a color");
RGB newColor = cd.open();
if (newColor != null)
Config.getInstance().setValue(Config.BACKGROUND_COLOR,
new Color(Display.getCurrent(), newColor));
updateFont();
}
public void changeFontColor() {
ColorDialog cd = new ColorDialog(getShell());
cd.setRGB(text.getBackground().getRGB());
cd.setText("Choose a color");
RGB newColor = cd.open();
if (newColor != null)
Config.getInstance().setValue(Config.FONT_COLOR,
new Color(Display.getCurrent(), newColor));
updateFont();
}
/**
* Opens a {@link ColorDialog} to let the user pick a {@link Color}. Returns
* the picked {@link Color}, or <code>null</code> if no color was picked.
*
* @param shell
* The {@link Shell} which serves as the parent for the
* {@link ColorDialog}.
* @param initial
* The initial {@link Color} to display in the
* {@link ColorDialog}.
* @return The picked {@link Color}, or <code>null</code>.
*/
protected static Color pickColor(Shell shell, Color initial) {
ColorDialog cd = new ColorDialog(shell);
RGB rgb = new RGB((int) (255 * initial.getRed()),
(int) (255 * initial.getGreen()),
(int) (255 * initial.getBlue()));
cd.setRGB(rgb);
RGB newRgb = cd.open();
if (newRgb != null) {
return Color.rgb(newRgb.red, newRgb.green, newRgb.blue);
}
return null;
}
public void choose(UIColorChooserHandler selectionHandler) {
ColorDialog dlg = new ColorDialog(this.window.getControl());
if( this.text != null ) {
dlg.setText(this.text);
}
if( this.defaultModel != null ) {
dlg.setRGB(new RGB(this.defaultModel.getRed(), this.defaultModel.getGreen(), this.defaultModel.getBlue()));
}
RGB rgb = dlg.open();
selectionHandler.onSelectColor(rgb != null ? new UIColorModel(rgb.red, rgb.green, rgb.blue) : null);
}
@Override
public void mouseDoubleClick(MouseEvent e) {
final TreeItem selection;
if (tree.getSelectionCount() == 1 && (selection = tree.getSelection()[0]).getData() != null) {
ColorDialog dlg = new ColorDialog(getShell());
// Change the title bar text
dlg.setText(selection.getText(0));
dlg.setRGB(selection.getParent().getMapInv().get(selection).getBackground(1).getRGB());
// Open the dialog and retrieve the selected color
RGB rgb = dlg.open();
if (rgb != null) {
GColour refCol = new GColour(-1, rgb.red / 255f, rgb.green / 255f, rgb.blue / 255f, 1f);
tree.getMapInv().get(selection).setBackground(1, SWTResourceManager.getColor(rgb));
Object[] colourObj = (Object[]) selection.getData();
ColourType type = (ColourType) colourObj[0];
switch (type) {
case OPENGL_COLOUR:
((float[]) ((Object[]) colourObj[1])[0])[0] = refCol.getR();
((float[]) ((Object[]) colourObj[1])[1])[0] = refCol.getG();
((float[]) ((Object[]) colourObj[1])[2])[0] = refCol.getB();
break;
case SWT_COLOUR:
((Color[]) colourObj[1])[0] = SWTResourceManager.getColor(rgb) ;
break;
default:
break;
}
for (EditorTextWindow w : Project.getOpenTextWindows()) {
for (CTabItem t : w.getTabFolder().getItems()) {
((CompositeTab) t).updateColours();
}
}
tree.build();
tree.update();
}
}
}
public static void openView(final IColorRunnable runnable, final RGB initial) {
final Shell shell = new Shell(WorkbenchHelper.getDisplay(), SWT.MODELESS);
final ColorDialog dlg = new ColorDialog(shell, SWT.MODELESS);
dlg.setText("Choose a custom color");
dlg.setRGB(initial);
final RGB rgb = dlg.open();
// final int a = StringUtils.INDEX_NOT_FOUND;
if (rgb != null) {
if (runnable != null) {
runnable.run(rgb.red, rgb.green, rgb.blue);
}
}
}
protected static RGB openAndGetColor(Composite parent, Label label) {
ColorDialog dlg = new ColorDialog(parent.getShell());
dlg.setRGB(label.getBackground().getRGB());
dlg.setText("Choose a Color");
RGB rgb = dlg.open();
label.setBackground(new Color(parent.getShell().getDisplay(), rgb));
return rgb;
}
protected Object openDialogBox( Control cellEditorWindow )
{
Shell shell = new Shell( Display.getCurrent( ), SWT.SHELL_TRIM );
shell.setLocation( cellEditorWindow.toDisplay( 0, 0 ).x
+ cellEditorWindow.getBounds( ).width,
cellEditorWindow.toDisplay( 0, 0 ).y
- cellEditorWindow.getBounds( ).height );
ColorDialog dialog = new ColorDialog( shell, SWT.APPLICATION_MODAL );
RGB[] rgbs = ReportPlugin.getDefault( ).getCustomColorsPreference( );
if ( rgbs != null )
{
dialog.setRGBs( rgbs );
}
Object value = getValue( );
try
{
int color;
if ( value instanceof String )
{
color = ColorUtil.parseColor( (String) value );
}
else
{
color = ( (Integer) value ).intValue( );
}
dialog.setRGB( DEUtil.getRGBValue( color ) );
}
catch ( Exception e )
{
// ignore.
}
value = dialog.open( );
ReportPlugin.getDefault( )
.setCustomColorsPreference( dialog.getRGBs( ) );
if ( value != null && dialog.getRGB( ) != null )
{
deactivate( );
return ColorUtil.format( ColorUtil.formRGB( dialog.getRGB( ).red,
dialog.getRGB( ).green,
dialog.getRGB( ).blue ), ColorUtil.HTML_FORMAT );
}
comboBox.setFocus( );
shell.dispose( );
return value;
}