KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > viewer > action > BackgroundContributionItem


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26
27 package org.nightlabs.editor2d.viewer.action;
28
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.events.DisposeEvent;
31 import org.eclipse.swt.events.DisposeListener;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.events.SelectionListener;
34 import org.eclipse.swt.graphics.Color;
35 import org.eclipse.swt.graphics.RGB;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.ColorDialog;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Display;
41 import org.nightlabs.base.action.AbstractContributionItem;
42 import org.nightlabs.editor2d.viewer.IViewer;
43 import org.nightlabs.editor2d.viewer.ViewerPlugin;
44
45 public class BackgroundContributionItem
46 extends AbstractContributionItem
47 {
48     public static String JavaDoc ID = BackgroundContributionItem.class.getName();
49     
50     public BackgroundContributionItem(IViewer viewer)
51     {
52         super(ID, ViewerPlugin.getResourceString("contribution.background.text"));
53         this.viewer = viewer;
54         bgColor = viewer.getBgColor();
55     }
56     
57     protected IViewer viewer = null;
58     protected Button colorButton = null;
59     protected Color bgColor = null;
60     protected Control createControl(Composite parent)
61     {
62         colorButton = new Button(parent, SWT.NONE);
63         colorButton.setText(getName());
64         colorButton.setBackground(bgColor);
65         colorButton.addSelectionListener(selectionListener);
66         colorButton.addDisposeListener(disposeListener);
67         return colorButton;
68     }
69     
70     protected SelectionListener selectionListener = new SelectionListener()
71     {
72         public void widgetDefaultSelected(SelectionEvent e) {
73             widgetSelected(e);
74         }
75         public void widgetSelected(SelectionEvent e)
76         {
77             ColorDialog colorDialog = new ColorDialog(Display.getCurrent().getActiveShell());
78             RGB rgb = colorDialog.open();
79             if (rgb != null) {
80                 bgColor.dispose();
81                 bgColor = new Color(Display.getCurrent(), rgb);
82                 colorButton.setBackground(bgColor);
83                 viewer.setBgColor(bgColor);
84                 viewer.updateCanvas();
85             }
86         }
87     };
88     
89     protected DisposeListener disposeListener = new DisposeListener()
90     {
91         public void widgetDisposed(DisposeEvent e) {
92             bgColor.dispose();
93             colorButton.removeSelectionListener(selectionListener);
94         }
95     };
96
97 // protected String initID() {
98
// return ID;
99
// }
100

101 // protected String initName() {
102
// return ViewerPlugin.getResourceString("contribution.background.text");
103
// }
104

105 }
106
Popular Tags