KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > composite > QuickOptionsComposite


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 package org.nightlabs.editor2d.composite;
27
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.events.DisposeEvent;
33 import org.eclipse.swt.events.DisposeListener;
34 import org.eclipse.swt.events.FocusEvent;
35 import org.eclipse.swt.events.FocusListener;
36 import org.eclipse.swt.events.SelectionEvent;
37 import org.eclipse.swt.events.SelectionListener;
38 import org.eclipse.swt.layout.GridData;
39 import org.eclipse.swt.layout.GridLayout;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Group;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.Layout;
44 import org.eclipse.swt.widgets.Spinner;
45 import org.eclipse.swt.widgets.Text;
46 import org.eclipse.ui.forms.events.ExpansionAdapter;
47 import org.eclipse.ui.forms.events.ExpansionEvent;
48 import org.eclipse.ui.forms.widgets.FormToolkit;
49 import org.eclipse.ui.forms.widgets.ScrolledForm;
50 import org.eclipse.ui.forms.widgets.Section;
51 import org.eclipse.ui.forms.widgets.TableWrapData;
52 import org.eclipse.ui.forms.widgets.TableWrapLayout;
53 import org.nightlabs.base.composite.XComposite;
54 import org.nightlabs.base.composite.XComposite.LayoutMode;
55 import org.nightlabs.base.form.XFormToolkit;
56 import org.nightlabs.config.Config;
57 import org.nightlabs.config.ConfigException;
58 import org.nightlabs.editor2d.EditorPlugin;
59 import org.nightlabs.editor2d.config.QuickOptionsConfigModule;
60
61 /**
62  * <p> Author: Daniel.Mazurek[AT]NightLabs[DOT]de </p>
63  */

64 public class QuickOptionsComposite
65 extends XComposite
66 {
67     
68     /**
69      * @param parent
70      * @param style
71      */

72     public QuickOptionsComposite(Composite parent, int style)
73     {
74         super(parent, style);
75         init();
76     }
77
78     /**
79      * @param parent
80      * @param style
81      * @param layoutMode
82      * @param layoutDataMode
83      */

84     public QuickOptionsComposite(Composite parent, int style,
85             LayoutMode layoutMode, LayoutDataMode layoutDataMode)
86     {
87         super(parent, style, layoutMode, layoutDataMode);
88         init();
89     }
90
91     protected void init()
92     {
93         initConfigModule();
94         initLayout();
95         createComposite(this);
96     }
97         
98     protected void initLayout()
99     {
100         GridLayout layout = new GridLayout();
101         layout.marginBottom = 0;
102         layout.marginTop = 0;
103         layout.marginLeft = 0;
104         layout.marginRight = 0;
105         setLayout(layout);
106         setLayoutData(new GridData(GridData.FILL_BOTH));
107     }
108     
109     protected QuickOptionsConfigModule confMod = null;
110     protected QuickOptionsConfigModule getConfigModule()
111     {
112         if (confMod == null)
113             initConfigModule();
114         
115         return confMod;
116     }
117     
118     protected void initConfigModule()
119     {
120         try {
121             confMod = (QuickOptionsConfigModule) Config.sharedInstance().createConfigModule(QuickOptionsConfigModule.class);
122         } catch (ConfigException e) {
123             throw new RuntimeException JavaDoc(e);
124         }
125     }
126     
127 // protected void createComposite(Composite parent)
128
// {
129
// Group cloneGroup = new Group(parent, SWT.NONE);
130
// cloneGroup.setText(EditorPlugin.getResourceString("quickOptionsView.group.clone.name"));
131
// cloneGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
132
// cloneGroup.setLayout(new GridLayout());
133
// createEntry(cloneGroup, EditorPlugin.getResourceString("quickOptions.distanceCloneX.label"), CLONE_X);
134
// createEntry(cloneGroup, EditorPlugin.getResourceString("quickOptions.distanceCloneY.label"), CLONE_Y);
135
//
136
// Group moveGroup = new Group(parent, SWT.NONE);
137
// moveGroup.setText(EditorPlugin.getResourceString("quickOptionsView.group.move.name"));
138
// moveGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
139
// moveGroup.setLayout(new GridLayout());
140
// createEntry(moveGroup, EditorPlugin.getResourceString("quickOptions.distanceMoveX.label"), MOVE_X);
141
// createEntry(moveGroup, EditorPlugin.getResourceString("quickOptions.distanceMoveY.label"), MOVE_Y);
142
// }
143
private FormToolkit toolkit = null;
144     private ScrolledForm form = null;
145     protected void createComposite(Composite parent)
146     {
147         toolkit = new XFormToolkit(parent.getDisplay());
148         form = toolkit.createScrolledForm(parent);
149         TableWrapLayout layout = new TableWrapLayout();
150         form.getBody().setLayout(layout);
151         form.getBody().setLayoutData(new GridData(GridData.FILL_BOTH));
152                 
153         Section sectionDuplicate = toolkit.createSection(form.getBody(),
154               Section.DESCRIPTION|Section.TITLE_BAR|
155               Section.TWISTIE|Section.EXPANDED);
156         TableWrapData td = new TableWrapData(TableWrapData.FILL_GRAB);
157         sectionDuplicate.setLayoutData(td);
158         
159         sectionDuplicate.addExpansionListener(new ExpansionAdapter() {
160             public void expansionStateChanged(ExpansionEvent e) {
161                 form.reflow(true);
162           }
163         });
164         sectionDuplicate.setText(EditorPlugin.getResourceString("quickOptions.section.clone.text"));
165         sectionDuplicate.setDescription(EditorPlugin.getResourceString("quickOptions.section.clone.description"));
166         Composite sectionClientDuplicate = toolkit.createComposite(sectionDuplicate);
167         sectionClientDuplicate.setLayout(new GridLayout(2, false));
168         createEntry(sectionClientDuplicate, EditorPlugin.getResourceString("quickOptions.distanceCloneX.label"), CLONE_X);
169         createEntry(sectionClientDuplicate, EditorPlugin.getResourceString("quickOptions.distanceCloneY.label"), CLONE_Y);
170         sectionDuplicate.setClient(sectionClientDuplicate);
171         
172         Section sectionMove = toolkit.createSection(form.getBody(),
173               Section.DESCRIPTION|Section.TITLE_BAR|
174               Section.TWISTIE|Section.EXPANDED);
175         TableWrapData tdMove = new TableWrapData(TableWrapData.FILL_GRAB);
176         sectionMove.setLayoutData(tdMove);
177         
178         sectionMove.addExpansionListener(new ExpansionAdapter() {
179             public void expansionStateChanged(ExpansionEvent e) {
180                 form.reflow(true);
181           }
182         });
183         sectionMove.setText(EditorPlugin.getResourceString("quickOptions.section.move.text"));
184         sectionMove.setDescription(EditorPlugin.getResourceString("quickOptions.section.move.description"));
185         Composite sectionClientMove = toolkit.createComposite(sectionMove);
186         sectionClientMove.setLayout(new GridLayout(2, false));
187         createEntry(sectionClientMove, EditorPlugin.getResourceString("quickOptions.distanceMoveX.label"), MOVE_X);
188         createEntry(sectionClientMove, EditorPlugin.getResourceString("quickOptions.distanceMoveY.label"), MOVE_Y);
189         sectionMove.setClient(sectionClientMove);
190     }
191     
192     protected static final int CLONE_X = 1;
193     protected static final int CLONE_Y = 2;
194     protected static final int MOVE_X = 3;
195     protected static final int MOVE_Y = 4;
196     
197     protected Map JavaDoc<Spinner, Integer JavaDoc> spinner2Identifier = new HashMap JavaDoc<Spinner, Integer JavaDoc>();
198     
199 // protected void createEntry(Composite parent, String labelText, int identifier)
200
// {
201
// XComposite comp = new XComposite(parent, SWT.NONE, LayoutMode.TIGHT_WRAPPER);
202
// GridLayout layout = new GridLayout(2, true);
203
// comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
204
//
205
// Label l = new Label(comp, SWT.NONE);
206
// l.setText(labelText);
207
// Spinner spinner = new Spinner(comp, SWT.BORDER);
208
// spinner.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
209
//
210
// spinner.setSelection(getValue(identifier));
211
//
212
// spinner.addSelectionListener(textListener);
213
// spinner.addFocusListener(focusListener);
214
// spinner.addDisposeListener(disposeListener);
215
//
216
// spinner2Identifier.put(spinner, new Integer(identifier));
217
// }
218
protected void createEntry(Composite parent, String JavaDoc labelText, int identifier)
219     {
220         toolkit.paintBordersFor(parent);
221         
222         Label l = toolkit.createLabel(parent, labelText);
223         Spinner spinner = new Spinner(parent, SWT.NONE);
224         spinner.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER);
225         spinner.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
226         spinner.setSelection(getValue(identifier));
227         spinner.addSelectionListener(textListener);
228         spinner.addFocusListener(focusListener);
229         spinner.addDisposeListener(disposeListener);
230         
231         spinner2Identifier.put(spinner, new Integer JavaDoc(identifier));
232     }
233     
234     protected SelectionListener textListener = new SelectionListener()
235     {
236         public void widgetDefaultSelected(SelectionEvent e) {
237             widgetSelected(e);
238         }
239         public void widgetSelected(SelectionEvent e)
240         {
241             Spinner t = (Spinner) e.getSource();
242             int identifier = ((Integer JavaDoc) spinner2Identifier.get(t)).intValue();
243             int value = t.getSelection();
244             setValue(identifier, value);
245         }
246     };
247         
248     protected void setValue(int identifier, int value)
249     {
250         switch (identifier)
251         {
252             case (CLONE_X):
253                 getConfigModule().setCloneDistanceX(value);
254                 break;
255             case (CLONE_Y):
256                 getConfigModule().setCloneDistanceY(value);
257                 break;
258             case (MOVE_X):
259                 getConfigModule().setMoveTranslationX(value);
260                 break;
261             case (MOVE_Y):
262                 getConfigModule().setMoveTranslationY(value);
263                 break;
264         }
265     }
266     
267     protected int getValue(int identifier)
268     {
269         switch (identifier)
270         {
271             case (CLONE_X):
272                 return getConfigModule().getCloneDistanceX();
273             case (CLONE_Y):
274                 return getConfigModule().getCloneDistanceY();
275             case (MOVE_X):
276                 return getConfigModule().getMoveTranslationX();
277             case (MOVE_Y):
278                 return getConfigModule().getMoveTranslationY();
279         }
280         return 0;
281     }
282     
283     protected FocusListener focusListener = new FocusListener()
284     {
285         public void focusLost(FocusEvent e)
286         {
287 // Spinner t = (Spinner) e.getSource();
288
// int identifier = ((Integer) spinner2Identifier.get(t)).intValue();
289
// int value = t.getSelection();
290
// setValue(identifier, value);
291
}
292         public void focusGained(FocusEvent e) {
293             
294         }
295     };
296     
297     protected DisposeListener disposeListener = new DisposeListener()
298     {
299         public void widgetDisposed(DisposeEvent e)
300         {
301             Spinner t = (Spinner) e.getSource();
302             t.removeSelectionListener(textListener);
303             t.removeFocusListener(focusListener);
304         }
305     };
306     
307 }
Popular Tags