KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * <p> Project: com.nightlabs.editor2d </p>
3  * <p> Copyright: Copyright (c) 2004 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 01.03.2005 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.editor2d;
9
10 import org.eclipse.gef.palette.MarqueeToolEntry;
11 import org.eclipse.gef.palette.PaletteContainer;
12 import org.eclipse.gef.palette.PaletteDrawer;
13 import org.eclipse.gef.palette.PaletteGroup;
14 import org.eclipse.gef.palette.PaletteRoot;
15 import org.eclipse.gef.palette.PaletteSeparator;
16 import org.eclipse.gef.palette.ToolEntry;
17 import org.eclipse.gef.requests.CreationFactory;
18 import org.eclipse.gef.ui.palette.FlyoutPaletteComposite.FlyoutPreferences;
19 import org.eclipse.jface.preference.IPreferenceStore;
20 import org.eclipse.jface.resource.ImageDescriptor;
21
22 import com.nightlabs.editor2d.EllipseDrawComponent;
23 import com.nightlabs.editor2d.ImageDrawComponent;
24 import com.nightlabs.editor2d.LineDrawComponent;
25 import com.nightlabs.editor2d.RectangleDrawComponent;
26 import com.nightlabs.editor2d.TextDrawComponent;
27 import com.nightlabs.editor2d.model.ModelCreationFactory;
28 import com.nightlabs.editor2d.tools.EditorSelectionToolEntry;
29 import com.nightlabs.editor2d.tools.EllipseToolEntry;
30 import com.nightlabs.editor2d.tools.ImageToolEntry;
31 import com.nightlabs.editor2d.tools.LineToolEntry;
32 import com.nightlabs.editor2d.tools.RectangleToolEntry;
33 import com.nightlabs.editor2d.tools.TextToolEntry;
34 import com.nightlabs.editor2d.tools.ZoomToolEntry;
35
36 public abstract class AbstractPaletteFactory
37 {
38   public AbstractPaletteFactory() {
39     super();
40   }
41     
42   /** Default palette size. */
43   protected static final int DEFAULT_PALETTE_SIZE = 125;
44   /** Preference ID used to persist the palette location. */
45   protected static final String JavaDoc PALETTE_DOCK_LOCATION = "PaletteFactory.Location";
46   /** Preference ID used to persist the palette size. */
47   protected static final String JavaDoc PALETTE_SIZE = "PaletteFactory.Size";
48   /** Preference ID used to persist the flyout palette's state. */
49   protected static final String JavaDoc PALETTE_STATE = "PaletteFactory.State";
50   
51   public abstract CreationFactory getCreationFactory(Class JavaDoc targetClass);
52   
53   /**
54    * Return a FlyoutPreferences instance used to save/load the preferences of a flyout palette.
55    */

56   public static FlyoutPreferences createPalettePreferences()
57   {
58     // set default flyout palette preference values, in case the preference store
59
// does not hold stored values for the given preferences
60
getPreferenceStore().setDefault(PALETTE_DOCK_LOCATION, -1);
61     getPreferenceStore().setDefault(PALETTE_STATE, -1);
62     getPreferenceStore().setDefault(PALETTE_SIZE, DEFAULT_PALETTE_SIZE);
63     
64     return new FlyoutPreferences() {
65       public int getDockLocation() {
66         return getPreferenceStore().getInt(PALETTE_DOCK_LOCATION);
67       }
68       public int getPaletteState() {
69         return getPreferenceStore().getInt(PALETTE_STATE);
70       }
71       public int getPaletteWidth() {
72         return getPreferenceStore().getInt(PALETTE_SIZE);
73       }
74       public void setDockLocation(int location) {
75         getPreferenceStore().setValue(PALETTE_DOCK_LOCATION, location);
76       }
77       public void setPaletteState(int state) {
78         getPreferenceStore().setValue(PALETTE_STATE, state);
79       }
80       public void setPaletteWidth(int width) {
81         getPreferenceStore().setValue(PALETTE_SIZE, width);
82       }
83     };
84   }
85     
86   /*
87   * Returns the preference store for the EditorPlugin.
88   * @see org.eclipse.ui.plugin.AbstractUIPlugin#getPreferenceStore()
89   */

90   protected static IPreferenceStore getPreferenceStore()
91  {
92     return EditorPlugin.getDefault().getPreferenceStore();
93  }
94  
95  /** Create the "Shapes" drawer. */
96  protected static PaletteContainer createShapesDrawer()
97  {
98   PaletteDrawer componentsDrawer = new PaletteDrawer(EditorPlugin.getResourceString("palette_group_shapes"));
99
100   // add Rectangle Tool
101
// ToolEntry component = new CombinedTemplateCreationEntry
102
ToolEntry component = new RectangleToolEntry
103   (
104     EditorPlugin.getResourceString("palette_rectangle_label"),
105     EditorPlugin.getResourceString("palette_rectangle_shortdesc"),
106     RectangleDrawComponent.class,
107     new ModelCreationFactory(RectangleDrawComponent.class),
108     ImageDescriptor.createFromFile(EditorPlugin.class, "icons/rectangle16.gif"),
109     ImageDescriptor.createFromFile(EditorPlugin.class, "icons/rectangle24.gif")
110   );
111   componentsDrawer.add(component);
112
113   // add Ellipse Tool
114
// component = new CombinedTemplateCreationEntry
115
component = new EllipseToolEntry
116   (
117     EditorPlugin.getResourceString("palette_ellipse_label"),
118     EditorPlugin.getResourceString("palette_ellipse_shortdesc"),
119     EllipseDrawComponent.class,
120     new ModelCreationFactory(EllipseDrawComponent.class),
121     ImageDescriptor.createFromFile(EditorPlugin.class, "icons/ellipse16.gif"),
122     ImageDescriptor.createFromFile(EditorPlugin.class, "icons/ellipse24.gif")
123   );
124   componentsDrawer.add(component);
125
126   // add Line Tool
127
component = new LineToolEntry
128   (
129     EditorPlugin.getResourceString("palette_line_label"),
130     EditorPlugin.getResourceString("palette_line_shortdesc"),
131     LineDrawComponent.class,
132     new ModelCreationFactory(LineDrawComponent.class),
133     ImageDescriptor.createFromFile(EditorPlugin.class, "icons/line16.gif"),
134     ImageDescriptor.createFromFile(EditorPlugin.class, "icons/line24.gif")
135   );
136   componentsDrawer.add(component);
137   
138   // add Text Tool
139
component = new TextToolEntry
140   (
141     EditorPlugin.getResourceString("palette.text.label"),
142     EditorPlugin.getResourceString("palette.text.shortdesc"),
143     TextDrawComponent.class,
144     new ModelCreationFactory(TextDrawComponent.class),
145     ImageDescriptor.createFromFile(EditorPlugin.class, "icons/text16.gif"),
146     ImageDescriptor.createFromFile(EditorPlugin.class, "icons/text24.gif")
147   );
148   componentsDrawer.add(component);
149
150   // add Image Tool
151
component = new ImageToolEntry
152   (
153     EditorPlugin.getResourceString("palette.image.label"),
154     EditorPlugin.getResourceString("palette.image.shortdesc"),
155     ImageDrawComponent.class,
156     new ModelCreationFactory(ImageDrawComponent.class),
157     ImageDescriptor.createFromFile(EditorPlugin.class, "icons/image16.gif"),
158     ImageDescriptor.createFromFile(EditorPlugin.class, "icons/image24.gif")
159   );
160   componentsDrawer.add(component);
161   
162   return componentsDrawer;
163  }
164   
165  /**
166   * Creates the PaletteRoot and adds all palette elements.
167   * Use this factory method to create a new palette for your graphical editor.
168   * @return a new PaletteRoot
169   */

170  protected static PaletteRoot createPalette()
171  {
172   PaletteRoot palette = new PaletteRoot();
173   palette.add(createToolsGroup(palette));
174   palette.add(createShapesDrawer());
175   return palette;
176  }
177  
178  /** Create the "Tools" group. */
179  protected static PaletteContainer createToolsGroup(PaletteRoot palette)
180  {
181   PaletteGroup toolGroup = new PaletteGroup(EditorPlugin.getResourceString("palette_group_tools"));
182
183 // // Add a selection tool to the group
184
// ToolEntry tool = new PanningSelectionToolEntry();
185
// toolGroup.add(tool);
186
// palette.setDefaultEntry(tool);
187
ToolEntry tool = new EditorSelectionToolEntry();
188   toolGroup.add(tool);
189   palette.setDefaultEntry(tool);
190   
191   // Add a marquee tool to the group
192
toolGroup.add(new MarqueeToolEntry());
193
194   // Add a zoom tool to the group
195
toolGroup.add(new ZoomToolEntry(EditorPlugin.getResourceString("palette.zoom.label"),
196     EditorPlugin.getResourceString("palette.zoom.shortdesc"),
197     ImageDescriptor.createFromFile(EditorPlugin.class, "icons/zoom16.gif"),
198     ImageDescriptor.createFromFile(EditorPlugin.class, "icons/zoom24.gif")
199   ));
200   
201   // Add a (unnamed) separator to the group
202
toolGroup.add(new PaletteSeparator());
203
204   return toolGroup;
205  }
206  
207 }
208
Popular Tags