KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > views > QuickOptionsView


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.views;
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.Label;
42 import org.eclipse.swt.widgets.Spinner;
43 import org.eclipse.ui.forms.events.ExpansionAdapter;
44 import org.eclipse.ui.forms.events.ExpansionEvent;
45 import org.eclipse.ui.forms.widgets.FormToolkit;
46 import org.eclipse.ui.forms.widgets.ScrolledForm;
47 import org.eclipse.ui.forms.widgets.Section;
48 import org.eclipse.ui.forms.widgets.TableWrapData;
49 import org.eclipse.ui.forms.widgets.TableWrapLayout;
50 import org.eclipse.ui.part.ViewPart;
51 import org.nightlabs.base.form.XFormToolkit;
52 import org.nightlabs.config.Config;
53 import org.nightlabs.config.ConfigException;
54 import org.nightlabs.editor2d.EditorPlugin;
55 import org.nightlabs.editor2d.config.QuickOptionsConfigModule;
56
57 /**
58  * <p> Author: Daniel.Mazurek[AT]NightLabs[DOT]de </p>
59  */

60 public class QuickOptionsView
61 extends ViewPart
62 {
63     public static final String JavaDoc ID = QuickOptionsView.class.getName();
64     
65     public QuickOptionsView()
66     {
67         super();
68     }
69
70     protected void init()
71     {
72         setPartName(EditorPlugin.getResourceString("quickOptionsView.name"));
73         initConfigModule();
74     }
75     
76     protected QuickOptionsConfigModule confMod = null;
77     protected QuickOptionsConfigModule getConfigModule()
78     {
79         if (confMod == null)
80             initConfigModule();
81         
82         return confMod;
83     }
84     
85     protected void initConfigModule()
86     {
87         try {
88             confMod = (QuickOptionsConfigModule) Config.sharedInstance().createConfigModule(QuickOptionsConfigModule.class);
89         } catch (ConfigException e) {
90             throw new RuntimeException JavaDoc(e);
91         }
92     }
93     
94     private FormToolkit toolkit = null;
95     private ScrolledForm form = null;
96     
97     public void createPartControl(Composite parent)
98     {
99         toolkit = new XFormToolkit(parent.getDisplay());
100         form = toolkit.createScrolledForm(parent);
101         TableWrapLayout layout = new TableWrapLayout();
102 // ColumnLayout layout = new ColumnLayout();
103
form.getBody().setLayout(layout);
104         form.getBody().setLayoutData(new GridData(GridData.FILL_BOTH));
105 // form.setText(EditorPlugin.getResourceString("quickOptionsView.name"));
106

107         Section sectionDuplicate = toolkit.createSection(form.getBody(),
108               Section.DESCRIPTION|Section.TITLE_BAR|
109               Section.TWISTIE|Section.EXPANDED);
110         TableWrapData td = new TableWrapData(TableWrapData.FILL_GRAB);
111         sectionDuplicate.setLayoutData(td);
112         
113         sectionDuplicate.addExpansionListener(new ExpansionAdapter() {
114             public void expansionStateChanged(ExpansionEvent e) {
115                 form.reflow(true);
116           }
117         });
118         sectionDuplicate.setText(EditorPlugin.getResourceString("quickOptions.section.clone.text"));
119         sectionDuplicate.setDescription(EditorPlugin.getResourceString("quickOptions.section.clone.description"));
120         Composite sectionClientDuplicate = toolkit.createComposite(sectionDuplicate);
121         sectionClientDuplicate.setLayout(new GridLayout(2, false));
122         createEntry(sectionClientDuplicate, EditorPlugin.getResourceString("quickOptions.distanceCloneX.label"), CLONE_X);
123         createEntry(sectionClientDuplicate, EditorPlugin.getResourceString("quickOptions.distanceCloneY.label"), CLONE_Y);
124         sectionDuplicate.setClient(sectionClientDuplicate);
125         
126         Section sectionMove = toolkit.createSection(form.getBody(),
127               Section.DESCRIPTION|Section.TITLE_BAR|
128               Section.TWISTIE|Section.EXPANDED);
129         TableWrapData tdMove = new TableWrapData(TableWrapData.FILL_GRAB);
130         sectionMove.setLayoutData(tdMove);
131         
132         sectionMove.addExpansionListener(new ExpansionAdapter() {
133             public void expansionStateChanged(ExpansionEvent e) {
134                 form.reflow(true);
135           }
136         });
137         sectionMove.setText(EditorPlugin.getResourceString("quickOptions.section.move.text"));
138         sectionMove.setDescription(EditorPlugin.getResourceString("quickOptions.section.move.description"));
139         Composite sectionClientMove = toolkit.createComposite(sectionMove);
140         sectionClientMove.setLayout(new GridLayout(2, false));
141         createEntry(sectionClientMove, EditorPlugin.getResourceString("quickOptions.distanceMoveX.label"), MOVE_X);
142         createEntry(sectionClientMove, EditorPlugin.getResourceString("quickOptions.distanceMoveY.label"), MOVE_Y);
143         sectionMove.setClient(sectionClientMove);
144     }
145
146     protected static final int CLONE_X = 1;
147     protected static final int CLONE_Y = 2;
148     protected static final int MOVE_X = 3;
149     protected static final int MOVE_Y = 4;
150     
151     protected void createEntry(Composite parent, String JavaDoc labelText, int identifier)
152     {
153         toolkit.paintBordersFor(parent);
154         
155         Label l = toolkit.createLabel(parent, labelText);
156         Spinner spinner = new Spinner(parent, SWT.NONE);
157         spinner.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER);
158 // spinner.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
159
spinner.setSelection(getValue(identifier));
160         spinner.addSelectionListener(textListener);
161         spinner.addFocusListener(focusListener);
162         spinner.addDisposeListener(disposeListener);
163         
164         spinner2Identifier.put(spinner, new Integer JavaDoc(identifier));
165     }
166     
167     public void setFocus()
168     {
169         form.setFocus();
170     }
171
172     protected Map JavaDoc<Spinner, Integer JavaDoc> spinner2Identifier = new HashMap JavaDoc<Spinner, Integer JavaDoc>();
173     
174     protected SelectionListener textListener = new SelectionListener()
175     {
176         public void widgetDefaultSelected(SelectionEvent e) {
177             widgetSelected(e);
178         }
179         public void widgetSelected(SelectionEvent e)
180         {
181             Spinner t = (Spinner) e.getSource();
182             int identifier = ((Integer JavaDoc) spinner2Identifier.get(t)).intValue();
183             int value = t.getSelection();
184             setValue(identifier, value);
185         }
186     };
187         
188     protected void setValue(int identifier, int value)
189     {
190         switch (identifier)
191         {
192             case (CLONE_X):
193                 getConfigModule().setCloneDistanceX(value);
194                 break;
195             case (CLONE_Y):
196                 getConfigModule().setCloneDistanceY(value);
197                 break;
198             case (MOVE_X):
199                 getConfigModule().setMoveTranslationX(value);
200                 break;
201             case (MOVE_Y):
202                 getConfigModule().setMoveTranslationY(value);
203                 break;
204         }
205     }
206     
207     protected int getValue(int identifier)
208     {
209         switch (identifier)
210         {
211             case (CLONE_X):
212                 return getConfigModule().getCloneDistanceX();
213             case (CLONE_Y):
214                 return getConfigModule().getCloneDistanceY();
215             case (MOVE_X):
216                 return getConfigModule().getMoveTranslationX();
217             case (MOVE_Y):
218                 return getConfigModule().getMoveTranslationY();
219         }
220         return 0;
221     }
222     
223     protected FocusListener focusListener = new FocusListener()
224     {
225         public void focusLost(FocusEvent e)
226         {
227 // Spinner t = (Spinner) e.getSource();
228
// int identifier = ((Integer) spinner2Identifier.get(t)).intValue();
229
// int value = t.getSelection();
230
// setValue(identifier, value);
231
}
232         public void focusGained(FocusEvent e) {
233             
234         }
235     };
236     
237     protected DisposeListener disposeListener = new DisposeListener()
238     {
239         public void widgetDisposed(DisposeEvent e)
240         {
241             Spinner t = (Spinner) e.getSource();
242             t.removeSelectionListener(textListener);
243             t.removeFocusListener(focusListener);
244         }
245     };
246     
247 }
248
Popular Tags