blob: 93c19cd872eeafdf621891bdc3a97cabf1367d0a [file] [log] [blame]
kmoore8eebfc62011-02-06 02:26:37 +00001/*******************************************************************************
2 * Copyright (c) 2007, 2010 Oracle. All rights reserved.
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v1.0, which accompanies this distribution
5 * and is available at http://www.eclipse.org/legal/epl-v10.html.
6 *
7 * Contributors:
8 * Oracle - initial API and implementation
9 ******************************************************************************/
10
11package org.eclipse.jpt.jpa.ui.internal.wizards.gen;
12
13import org.eclipse.core.runtime.IStatus;
14import org.eclipse.jdt.core.JavaConventions;
15import org.eclipse.jdt.core.JavaCore;
16import org.eclipse.jface.wizard.WizardPage;
17import org.eclipse.jpt.jpa.gen.internal.ORMGenColumn;
18import org.eclipse.jpt.jpa.gen.internal.ORMGenCustomizer;
19import org.eclipse.jpt.jpa.ui.JptJpaUiPlugin;
20import org.eclipse.swt.SWT;
21import org.eclipse.swt.events.ModifyEvent;
22import org.eclipse.swt.events.ModifyListener;
23import org.eclipse.swt.events.SelectionEvent;
24import org.eclipse.swt.events.SelectionListener;
25import org.eclipse.swt.layout.GridData;
26import org.eclipse.swt.layout.GridLayout;
27import org.eclipse.swt.widgets.Button;
28import org.eclipse.swt.widgets.Combo;
29import org.eclipse.swt.widgets.Composite;
30import org.eclipse.swt.widgets.Control;
31import org.eclipse.swt.widgets.Group;
32import org.eclipse.swt.widgets.Text;
33
34/**
35 * The panel used in the <code>TablesAndColumnsPage</code> wizard page
36 * to edit the column generation properties.
37 * An instance of this class is created by the <code>ORMGenWizard</code>
38 * implementation.
39
40 */
41public class ColumnGenPanel
42{
43 WizardPage wizardPage ;
44 Composite parent; //parent control with grid layout
45 int columns; //total columns in the parent composite
46
47 ORMGenCustomizer customizer;
48
49 private ORMGenColumn mColumn;
50 private boolean mPanelInited;
51 private boolean mIsUpdatingControls;
52
53 private Group columnMappingGroup;
54 private Button mGeneratedCheckbox;
55 private Text mPropNameField;
56 private Combo mMappingKindCombo;
57 private Combo mPropTypeCombo;
58 private Button mUpdateableCheckBox;
59 private Button mInsertableCheckBox;
60
61 private Group domainClassGroup ;
62 private ScopePanel mPropGetScopePanel;
63 private ScopePanel mPropSetScopePanel;
64
65 public ColumnGenPanel(Composite parent, int columns, ORMGenCustomizer customizer, WizardPage wizardPage ) {
66 this.wizardPage = wizardPage;
67 this.customizer = customizer;
68 this.parent =parent;
69 this.columns = columns;
70
71 initPanel();
72 }
73 /**
74 * Changes the table edited by the panel.
75 * This is supposed to update the panel editing controls
76 * using the column values.
77 */
78 public void setColumn(ORMGenColumn column) {
79 mColumn = column;
80
81 /*lazy init panel because it uses mColumn*/
82 if (!mPanelInited) {
83 initPanel();
84 mPanelInited = true;
85 }
86
87 updateControls();
88 }
89 private void updateControls() {
90 if (mIsUpdatingControls) {
91 return;
92 }
93
94 mIsUpdatingControls = true;
95 boolean isGenerated = mColumn.isGenerated();
96 mGeneratedCheckbox.setSelection( isGenerated);
97 mGeneratedCheckbox.setEnabled(true);
98 enableControls(isGenerated);
99 try {
100 mPropNameField.setText(mColumn.getPropertyName());
101
102 mPropTypeCombo.setText( mColumn.getPropertyType());
103
104 mMappingKindCombo.setText( mColumn.getMappingKind());
105
106 mUpdateableCheckBox.setSelection( mColumn.isUpdateable());
107
108 mInsertableCheckBox.setSelection(mColumn.isInsertable());
109
110 mPropGetScopePanel.enableComponents(isGenerated);
111 mPropGetScopePanel.setScope(mColumn.getPropertyGetScope());
112
113 mPropSetScopePanel.enableComponents( isGenerated );
114 mPropSetScopePanel.setScope(mColumn.getPropertySetScope());
115
116 if( mColumn.isPartOfCompositePrimaryKey()){
117 enableControls(false);
118 mPropNameField.setEnabled(true);
119 mGeneratedCheckbox.setEnabled(false);
120 }
121
122 } catch (Exception e) {
123 JptJpaUiPlugin.log(e);
124 }
125
126 mIsUpdatingControls = false;
127 }
128 private void enableControls(boolean isGenerated) {
129 Control[] controls = this.domainClassGroup.getChildren();
130 for( Control c: controls){
131 c.setEnabled( isGenerated );
132 }
133
134 controls = this.columnMappingGroup.getChildren();
135 for( Control c: controls){
136 c.setEnabled( isGenerated );
137 }
138 }
139 /**
140 * Initializes the panel by adding the editing controls.
141 * @param columns
142 * @param parent
143 */
144 protected void initPanel() {
145 createControls(parent, columns);
146 this.mPanelInited = true;
147 }
148
149 //-------------------------------------------
150 //----- ScopePanel class --------------------
151 //-------------------------------------------
152 /**
153 * A panel containing 3 radios (public, protected, private)
154 */
155 private class ScopePanel
156 {
157 private Button mPublicRadio;
158 private Button mProtectedRadio;
159 private Button mPrivateRadio;
160
161 public ScopePanel(Composite comp, SelectionListener listener) {
162 //super(3, 20/*hspacing*/, 0/*vspacing*/);
163
164 Composite radioGroup = new Composite( comp, SWT.NONE);
165 radioGroup.setLayout(new GridLayout(3, true));
166 GridData gd = new GridData();
167 gd.horizontalSpan = 3;
168 radioGroup.setLayoutData(gd);
169
170 /*string not localized intentionally, they are used as the actual
171 * scope value (see getText() usage)*/
172 mPublicRadio = new Button( radioGroup, SWT.RADIO );
173 mPublicRadio.setText( "public");
174 mPublicRadio.setLayoutData(new GridData());
175 mProtectedRadio = new Button( radioGroup, SWT.RADIO );
176 mProtectedRadio.setText("protected");
177 mProtectedRadio.setLayoutData(new GridData());
178 mPrivateRadio = new Button(radioGroup, SWT.RADIO );
179 mPrivateRadio.setText( "private");
180 mPrivateRadio.setLayoutData(new GridData());
181
182 mPublicRadio.addSelectionListener(listener);
183 mProtectedRadio.addSelectionListener(listener);
184 mPrivateRadio.addSelectionListener(listener);
185
186 }
187 public void enableComponents(boolean b) {
188 mPublicRadio.setEnabled(b);
189 mProtectedRadio.setEnabled(b);
190 mPrivateRadio.setEnabled(b);
191 }
192 /**
193 * Returns the currently selected scope.
194 */
195 public String getScope() {
196 Button radio = null;
197 if (mPublicRadio.getSelection()) {
198 radio = mPublicRadio;
199 } else if (mProtectedRadio.getSelection() ) {
200 radio = mProtectedRadio;
201 } else if (mPrivateRadio.getSelection() ) {
202 radio = mPrivateRadio;
203 }
204 return radio != null ? radio.getText() : null;
205 }
206 public void setScope(String scope) {
207 mPublicRadio.setSelection(false);
208 mProtectedRadio.setSelection(false);
209 mPrivateRadio.setSelection(false);
210 if( scope == null )
211 return;
212 if (scope.equals(ORMGenColumn.PUBLIC_SCOPE)) {
213 mPublicRadio.setSelection(true);
214 } else if (scope.equals(ORMGenColumn.PROTECTED_SCOPE)) {
215 mProtectedRadio.setSelection(true);
216 } else if (scope.equals(ORMGenColumn.PRIVATE_SCOPE)) {
217 mPrivateRadio.setSelection(true);
218 }
219 }
220 }
221
222 //-------------------------------------------
223 //----- private methods ---------------------
224 //-------------------------------------------
225 private void createControls(Composite composite, int columns) {
226 mGeneratedCheckbox = new Button(composite, SWT.CHECK);
227 mGeneratedCheckbox.setText( JptUiEntityGenMessages.GenerateEntitiesWizard_colPanel_genProp);
228 mGeneratedCheckbox.addSelectionListener(new SelectionListener() {
229 public void widgetDefaultSelected(SelectionEvent e) {}
230 public void widgetSelected(SelectionEvent e) {
231 if (!mIsUpdatingControls) {
232 mColumn.setGenerated(mGeneratedCheckbox.getSelection() );
233 updateControls();
234 }
235 }});
236 SWTUtil.fillColumns(mGeneratedCheckbox, columns);
237
238 columnMappingGroup = new Group( composite, SWT.NONE);
239 columnMappingGroup.setText( JptUiEntityGenMessages.GenerateEntitiesWizard_colPanel_colMapping);
240 columnMappingGroup.setLayout(new GridLayout(columns, false));
241 GridData layoutData = new GridData();
242 layoutData.horizontalSpan = columns;
243 layoutData.verticalAlignment = SWT.FILL;
244 layoutData.horizontalAlignment = SWT.FILL;
245 layoutData.grabExcessHorizontalSpace = true;
246 layoutData.grabExcessVerticalSpace = false;
247 layoutData.horizontalIndent = 20 ;
248 columnMappingGroup.setLayoutData(layoutData);
249
250 SWTUtil.createLabel(columnMappingGroup, 1, JptUiEntityGenMessages.GenerateEntitiesWizard_colPanel_propName );
251 mPropNameField = new Text(columnMappingGroup, SWT.BORDER | SWT.SINGLE );
252 mPropNameField.addModifyListener(new ModifyListener(){
253 @SuppressWarnings("restriction")
254 public void modifyText(ModifyEvent e) {
255 if (!mIsUpdatingControls) {
256 String fldName = mPropNameField.getText();
257 IStatus status = JavaConventions.validateIdentifier( fldName,
258 JavaCore.VERSION_1_3, JavaCore.VERSION_1_3 );
259 if( !status.matches(IStatus.ERROR)){
260 mColumn.setPropertyName(fldName);
261 wizardPage.setErrorMessage(null);
262 }else{
263 wizardPage.setErrorMessage(status.getMessage());
264 }
265 }
266 }
267 });
268 SWTUtil.fillColumns(mPropNameField ,3);
269
270 SWTUtil.createLabel(columnMappingGroup, 1, JptUiEntityGenMessages.GenerateEntitiesWizard_colPanel_propType );
271 mPropTypeCombo = new Combo(columnMappingGroup, SWT.SINGLE | SWT.READ_ONLY);
272 mPropTypeCombo.setItems( this.customizer.getAllPropertyTypes());
273 mPropTypeCombo.addModifyListener( new ModifyListener(){
274 public void modifyText(ModifyEvent e) {
275 if (!mIsUpdatingControls) {
276 mColumn.setPropertyType(mPropTypeCombo.getText());
277 }
278 }
279 });
280 SWTUtil.fillColumns(mPropTypeCombo,3);
281
282 SWTUtil.createLabel(columnMappingGroup, 1, JptUiEntityGenMessages.GenerateEntitiesWizard_colPanel_mapKind );
283 mMappingKindCombo = new Combo(columnMappingGroup, SWT.SINGLE | SWT.READ_ONLY);
284 mMappingKindCombo.setItems( this.customizer.getAllMappingKinds());
285 mMappingKindCombo.addSelectionListener(new SelectionListener() {
286 public void widgetDefaultSelected(SelectionEvent e) {}
287 public void widgetSelected(SelectionEvent e) {
288 if (!mIsUpdatingControls) {
289 mColumn.setMappingKind((String)mMappingKindCombo.getText());
290 }
291
292 }});
293 SWTUtil.fillColumns(mMappingKindCombo ,3);
294
295 mUpdateableCheckBox = new Button(columnMappingGroup, SWT.CHECK);
296 mUpdateableCheckBox.setText( JptUiEntityGenMessages.GenerateEntitiesWizard_colPanel_colUpdateable);
297 mUpdateableCheckBox.addSelectionListener(new SelectionListener() {
298 public void widgetDefaultSelected(SelectionEvent e) {}
299 public void widgetSelected(SelectionEvent e) {
300 if (!mIsUpdatingControls) {
301 mColumn.setUpdateable(mUpdateableCheckBox.getSelection() );
302 }
303 }});
304 SWTUtil.fillColumns(mUpdateableCheckBox ,4);
305
306 mInsertableCheckBox = new Button(columnMappingGroup, SWT.CHECK);
307 mInsertableCheckBox.setText( JptUiEntityGenMessages.GenerateEntitiesWizard_colPanel_colInsertable);
308 mInsertableCheckBox.addSelectionListener(new SelectionListener() {
309 public void widgetDefaultSelected(SelectionEvent e) {}
310 public void widgetSelected(SelectionEvent e) {
311 if (!mIsUpdatingControls) {
312 mColumn.setInsertable(mInsertableCheckBox.getSelection());
313 }
314 }});
315 SWTUtil.fillColumns(mInsertableCheckBox ,4);
316
317 SWTUtil.createLabel(composite, 4,"");
318
319 createJavaBeanPropertyControls(composite, columns);
320 }
321
322 void createJavaBeanPropertyControls(Composite composite, int columns){
323 //Java class generation properties
324 domainClassGroup = new Group(composite, SWT.NONE);
325 domainClassGroup.setText( JptUiEntityGenMessages.GenerateEntitiesWizard_colPanel_beanProp );
326 domainClassGroup.setLayout(new GridLayout(columns, false));
327 GridData layoutData = new GridData();
328 layoutData.horizontalSpan = columns;
329 layoutData.verticalAlignment = SWT.FILL;
330 layoutData.horizontalAlignment = SWT.FILL;
331 layoutData.grabExcessHorizontalSpace = true;
332 layoutData.grabExcessVerticalSpace = false;
333 layoutData.horizontalIndent = 20;
334 domainClassGroup.setLayoutData(layoutData);
335
336 SWTUtil.createLabel(domainClassGroup, 1, JptUiEntityGenMessages.GenerateEntitiesWizard_colPanel_getterScope );
337 mPropGetScopePanel = new ScopePanel(domainClassGroup, new SelectionListener() {
338 public void widgetDefaultSelected(SelectionEvent e) {}
339
340 public void widgetSelected(SelectionEvent e) {
341 if (!mIsUpdatingControls) {
342 if( ((Button)e.getSource()).getSelection() )
343 mColumn.setPropertyGetScope(mPropGetScopePanel.getScope());
344 }
345
346 }});
347
348 SWTUtil.createLabel(domainClassGroup, 1, JptUiEntityGenMessages.GenerateEntitiesWizard_colPanel_setterScope );
349 mPropSetScopePanel = new ScopePanel(domainClassGroup, new SelectionListener() {
350 public void widgetDefaultSelected(SelectionEvent e) {}
351
352 public void widgetSelected(SelectionEvent e) {
353 if (!mIsUpdatingControls) {
354 if( ((Button)e.getSource()).getSelection() )
355 mColumn.setPropertySetScope(mPropSetScopePanel.getScope());
356 }
357 }});
358
359 }
360}