KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > AbstractPaletteFactory


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

27
28 package org.nightlabs.editor2d;
29
30 import org.eclipse.gef.palette.MarqueeToolEntry;
31 import org.eclipse.gef.palette.PaletteContainer;
32 import org.eclipse.gef.palette.PaletteDrawer;
33 import org.eclipse.gef.palette.PaletteGroup;
34 import org.eclipse.gef.palette.PaletteRoot;
35 import org.eclipse.gef.palette.PaletteSeparator;
36 import org.eclipse.gef.palette.ToolEntry;
37 import org.eclipse.gef.requests.CreationFactory;
38 import org.eclipse.gef.ui.palette.FlyoutPaletteComposite.FlyoutPreferences;
39 import org.eclipse.jface.preference.IPreferenceStore;
40 import org.eclipse.jface.resource.ImageDescriptor;
41
42 import org.nightlabs.base.resource.SharedImages;
43 import org.nightlabs.base.resource.SharedImages.ImageDimension;
44 import org.nightlabs.base.resource.SharedImages.ImageFormat;
45 import org.nightlabs.editor2d.EllipseDrawComponent;
46 import org.nightlabs.editor2d.ImageDrawComponent;
47 import org.nightlabs.editor2d.LineDrawComponent;
48 import org.nightlabs.editor2d.RectangleDrawComponent;
49 import org.nightlabs.editor2d.TextDrawComponent;
50 import org.nightlabs.editor2d.model.ModelCreationFactory;
51 import org.nightlabs.editor2d.tools.EditorSelectionToolEntry;
52 import org.nightlabs.editor2d.tools.EllipseToolEntry;
53 import org.nightlabs.editor2d.tools.ImageToolEntry;
54 import org.nightlabs.editor2d.tools.LineToolEntry;
55 import org.nightlabs.editor2d.tools.RectangleToolEntry;
56 import org.nightlabs.editor2d.tools.TextToolEntry;
57 import org.nightlabs.editor2d.tools.ZoomToolEntry;
58
59 public abstract class AbstractPaletteFactory
60 {
61   public AbstractPaletteFactory() {
62     super();
63   }
64     
65   /** Default palette size. */
66   protected static final int DEFAULT_PALETTE_SIZE = 125;
67   /** Preference ID used to persist the palette location. */
68   protected static final String JavaDoc PALETTE_DOCK_LOCATION = "PaletteFactory.Location";
69   /** Preference ID used to persist the palette size. */
70   protected static final String JavaDoc PALETTE_SIZE = "PaletteFactory.Size";
71   /** Preference ID used to persist the flyout palette's state. */
72   protected static final String JavaDoc PALETTE_STATE = "PaletteFactory.State";
73   
74   public abstract CreationFactory getCreationFactory(Class JavaDoc targetClass);
75   
76   /**
77    * Return a FlyoutPreferences instance used to save/load the preferences of a flyout palette.
78    */

79   public static FlyoutPreferences createPalettePreferences()
80   {
81     // set default flyout palette preference values, in case the preference store
82
// does not hold stored values for the given preferences
83
getPreferenceStore().setDefault(PALETTE_DOCK_LOCATION, -1);
84     getPreferenceStore().setDefault(PALETTE_STATE, -1);
85     getPreferenceStore().setDefault(PALETTE_SIZE, DEFAULT_PALETTE_SIZE);
86     
87     return new FlyoutPreferences() {
88       public int getDockLocation() {
89         return getPreferenceStore().getInt(PALETTE_DOCK_LOCATION);
90       }
91       public int getPaletteState() {
92         return getPreferenceStore().getInt(PALETTE_STATE);
93       }
94       public int getPaletteWidth() {
95         return getPreferenceStore().getInt(PALETTE_SIZE);
96       }
97       public void setDockLocation(int location) {
98         getPreferenceStore().setValue(PALETTE_DOCK_LOCATION, location);
99       }
100       public void setPaletteState(int state) {
101         getPreferenceStore().setValue(PALETTE_STATE, state);
102       }
103       public void setPaletteWidth(int width) {
104         getPreferenceStore().setValue(PALETTE_SIZE, width);
105       }
106     };
107   }
108     
109   /*
110   * Returns the preference store for the EditorPlugin.
111   * @see org.eclipse.ui.plugin.AbstractUIPlugin#getPreferenceStore()
112   */

113   protected static IPreferenceStore getPreferenceStore()
114  {
115     return EditorPlugin.getDefault().getPreferenceStore();
116  }
117  
118  /** Create the "Shapes" drawer. */
119  protected static PaletteContainer createShapesDrawer()
120  {
121   PaletteDrawer componentsDrawer = new PaletteDrawer(EditorPlugin.getResourceString("palette.group.shapes"));
122
123   // add Rectangle Tool
124
ToolEntry component = new RectangleToolEntry
125   (
126     EditorPlugin.getResourceString("palette.rectangle.label"),
127     EditorPlugin.getResourceString("palette.rectangle.shortdesc"),
128     RectangleDrawComponent.class,
129     new ModelCreationFactory(RectangleDrawComponent.class),
130 // ImageDescriptor.createFromFile(EditorPlugin.class, "icons/rectangle16.gif"),
131
// ImageDescriptor.createFromFile(EditorPlugin.class, "icons/rectangle24.gif")
132
SharedImages.getSharedImageDescriptor(EditorPlugin.getDefault(), AbstractPaletteFactory.class, "Rectangle"),
133     SharedImages.getSharedImageDescriptor(EditorPlugin.getDefault(), AbstractPaletteFactory.class, "Rectangle",
134             ImageDimension._24x24, ImageFormat.png)
135   );
136   componentsDrawer.add(component);
137
138   // add Ellipse Tool
139
component = new EllipseToolEntry
140   (
141     EditorPlugin.getResourceString("palette.ellipse.label"),
142     EditorPlugin.getResourceString("palette.ellipse.shortdesc"),
143     EllipseDrawComponent.class,
144     new ModelCreationFactory(EllipseDrawComponent.class),
145 // ImageDescriptor.createFromFile(EditorPlugin.class, "icons/ellipse16.gif"),
146
// ImageDescriptor.createFromFile(EditorPlugin.class, "icons/ellipse24.gif")
147
SharedImages.getSharedImageDescriptor(EditorPlugin.getDefault(), AbstractPaletteFactory.class, "Ellipse"),
148     SharedImages.getSharedImageDescriptor(EditorPlugin.getDefault(), AbstractPaletteFactory.class, "Ellipse",
149             ImageDimension._24x24, ImageFormat.png)
150   );
151   componentsDrawer.add(component);
152
153   // add Line Tool
154
component = new LineToolEntry
155   (
156     EditorPlugin.getResourceString("palette.line.label"),
157     EditorPlugin.getResourceString("palette.line.shortdesc"),
158     LineDrawComponent.class,
159     new ModelCreationFactory(LineDrawComponent.class),
160 // ImageDescriptor.createFromFile(EditorPlugin.class, "icons/line16.gif"),
161
// ImageDescriptor.createFromFile(EditorPlugin.class, "icons/line24.gif")
162
SharedImages.getSharedImageDescriptor(EditorPlugin.getDefault(), AbstractPaletteFactory.class, "Line"),
163     SharedImages.getSharedImageDescriptor(EditorPlugin.getDefault(), AbstractPaletteFactory.class, "Line",
164             ImageDimension._24x24, ImageFormat.png)
165   );
166   componentsDrawer.add(component);
167   
168   // add Text Tool
169
component = new TextToolEntry
170   (
171     EditorPlugin.getResourceString("palette.text.label"),
172     EditorPlugin.getResourceString("palette.text.shortdesc"),
173     TextDrawComponent.class,
174     new ModelCreationFactory(TextDrawComponent.class),
175 // ImageDescriptor.createFromFile(EditorPlugin.class, "icons/text16.gif"),
176
// ImageDescriptor.createFromFile(EditorPlugin.class, "icons/text24.gif")
177
SharedImages.getSharedImageDescriptor(EditorPlugin.getDefault(), AbstractPaletteFactory.class, "Text"),
178     SharedImages.getSharedImageDescriptor(EditorPlugin.getDefault(), AbstractPaletteFactory.class, "Text",
179             ImageDimension._24x24, ImageFormat.png)
180   );
181   componentsDrawer.add(component);
182
183   // add Image Tool
184
component = new ImageToolEntry
185   (
186     EditorPlugin.getResourceString("palette.image.label"),
187     EditorPlugin.getResourceString("palette.image.shortdesc"),
188     ImageDrawComponent.class,
189     new ModelCreationFactory(ImageDrawComponent.class),
190 // ImageDescriptor.createFromFile(EditorPlugin.class, "icons/image16.gif"),
191
// ImageDescriptor.createFromFile(EditorPlugin.class, "icons/image24.gif")
192
SharedImages.getSharedImageDescriptor(EditorPlugin.getDefault(), AbstractPaletteFactory.class, "Image"),
193     SharedImages.getSharedImageDescriptor(EditorPlugin.getDefault(), AbstractPaletteFactory.class, "Image",
194             ImageDimension._24x24, ImageFormat.png)
195   );
196   componentsDrawer.add(component);
197   
198   return componentsDrawer;
199  }
200   
201  /**
202   * Creates the PaletteRoot and adds all palette elements.
203   * Use this factory method to create a new palette for your graphical editor.
204   * @return a new PaletteRoot
205   */

206  protected static PaletteRoot createPalette()
207  {
208   PaletteRoot palette = new PaletteRoot();
209   palette.add(createToolsGroup(palette));
210   palette.add(createShapesDrawer());
211   return palette;
212  }
213  
214  /** Create the "Tools" group. */
215  protected static PaletteContainer createToolsGroup(PaletteRoot palette)
216  {
217   PaletteGroup toolGroup = new PaletteGroup(EditorPlugin.getResourceString("palette.group.tools"));
218
219   // Add a selection tool to the group
220
ToolEntry tool = new EditorSelectionToolEntry();
221   toolGroup.add(tool);
222   palette.setDefaultEntry(tool);
223   
224   // Add a marquee tool to the group
225
toolGroup.add(new MarqueeToolEntry());
226
227   // Add a zoom tool to the group
228
toolGroup.add(new ZoomToolEntry(EditorPlugin.getResourceString("palette.zoom.label"),
229     EditorPlugin.getResourceString("palette.zoom.shortdesc"),
230 // ImageDescriptor.createFromFile(EditorPlugin.class, "icons/zoom16.gif"),
231
// ImageDescriptor.createFromFile(EditorPlugin.class, "icons/zoom24.gif")
232
SharedImages.getSharedImageDescriptor(EditorPlugin.getDefault(), AbstractPaletteFactory.class, "Zoom"),
233     SharedImages.getSharedImageDescriptor(EditorPlugin.getDefault(), AbstractPaletteFactory.class, "Zoom",
234             ImageDimension._24x24, ImageFormat.png)
235   ));
236   
237   // Add a (unnamed) separator to the group
238
toolGroup.add(new PaletteSeparator());
239
240   return toolGroup;
241  }
242  
243 }
244
Popular Tags