1 26 package org.nightlabs.editor2d.composite; 27 28 import java.util.HashMap ; 29 import java.util.Map ; 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 64 public class QuickOptionsComposite 65 extends XComposite 66 { 67 68 72 public QuickOptionsComposite(Composite parent, int style) 73 { 74 super(parent, style); 75 init(); 76 } 77 78 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 (e); 124 } 125 } 126 127 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 <Spinner, Integer > spinner2Identifier = new HashMap <Spinner, Integer >(); 198 199 protected void createEntry(Composite parent, String 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 (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 ) 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 } 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 |