[Title] common-eplugin: added error message functions in DynamicDialog
authorJihoon Song <jihoon80.song@samsung.com>
Mon, 19 Nov 2012 11:34:12 +0000 (20:34 +0900)
committerJihoon Song <jihoon80.song@samsung.com>
Wed, 21 Nov 2012 07:25:03 +0000 (16:25 +0900)
[Desc.]
[Issue]

Change-Id: Idd7299ed24428cd56198855684c4a451a1717a75

org.tizen.common.sign/src/org/tizen/common/sign/command/ReadSigningProfileFileCommand.java
org.tizen.common/src/org/tizen/common/ui/dialog/DynamicDialog.java

index 969e68f..d14bed6 100644 (file)
@@ -39,6 +39,7 @@ import org.slf4j.LoggerFactory;
 import org.tizen.common.core.command.AbstractCommand;
 import org.tizen.common.core.command.ExecutionContext;
 import org.tizen.common.core.command.Executor;
+import org.tizen.common.core.command.InputValidator;
 import org.tizen.common.core.command.UserField;
 import org.tizen.common.file.FileHandler;
 import org.tizen.common.file.FileHandler.Attribute;
@@ -47,6 +48,7 @@ import org.tizen.common.sign.model.SignatureConstants;
 import org.tizen.common.sign.preferences.SigningProfileItem;
 import org.tizen.common.sign.util.SigningProfileUtil;
 import org.tizen.common.util.IOUtil;
+import org.tizen.common.util.StringUtil;
 
 public class
 ReadSigningProfileFileCommand
@@ -117,6 +119,15 @@ extends AbstractCommand<Object>
             // Author password text
             UserField authorText = new UserField( SignatureConstants.AUTHOR, "Author password : ", char[].class );
             authorText.setValue( authorItem.getPassword() );
+            authorText.setValidator( new InputValidator() {
+                @Override
+                public String check(Object value) {
+                    if ( value instanceof String ) {
+                        return StringUtil.isEmpty( (String) value ) ? "Input author password." : null;
+                    }
+                    return null;
+                }
+            } );
             fieldList.add( authorText );
             
             // Savable author password button 
@@ -129,6 +140,15 @@ extends AbstractCommand<Object>
                 distributor2Text.setModify( false );
             } else {
                 distributor2Text.setValue( distributorItem.getPassword() );
+                distributor2Text.setValidator( new InputValidator() {
+                    @Override
+                    public String check(Object value) {
+                        if ( value instanceof String ) {
+                            return StringUtil.isEmpty( (String) value ) ? "Input distributor password." : null;
+                        }
+                        return null;
+                    }
+                } );
             }
             fieldList.add( distributor2Text );
             
index af4c35b..87af2d0 100644 (file)
@@ -31,8 +31,11 @@ import java.util.Map;
 import java.util.Map.Entry;
 
 import org.eclipse.jface.dialogs.Dialog;
+import org.eclipse.jface.dialogs.IDialogConstants;
 import org.eclipse.jface.window.IShellProvider;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.DisposeEvent;
+import org.eclipse.swt.events.DisposeListener;
 import org.eclipse.swt.events.ModifyEvent;
 import org.eclipse.swt.events.ModifyListener;
 import org.eclipse.swt.events.SelectionAdapter;
@@ -55,6 +58,7 @@ import org.tizen.common.core.command.UserField;
 import org.tizen.common.util.Assert;
 import org.tizen.common.util.CollectionUtil;
 import org.tizen.common.util.SWTUtil;
+import org.tizen.common.util.StringUtil;
 
 
 /**
@@ -70,6 +74,9 @@ public class DynamicDialog extends Dialog {
     protected String title = "Dynamic Dialog"; // default. If you want to change, use setter.
     protected int width = 500; // default. This is fixed currently. 
     
+    protected Text errorMessageText;
+    protected String errorMessage; 
+    
     // inputted fields
     protected Collection<UserField> fields;
     
@@ -127,6 +134,11 @@ public class DynamicDialog extends Dialog {
     }
 
     @Override
+    protected int getShellStyle() {
+        return super.getShellStyle() | SWT.APPLICATION_MODAL;
+    }
+
+    @Override
     protected Point getInitialSize() {
         Point initialSize = super.getInitialSize();
         
@@ -135,6 +147,76 @@ public class DynamicDialog extends Dialog {
         return initialSize;
     }
 
+    protected void setErrorMessage(String errorMessage) {
+        this.errorMessage = errorMessage;
+        
+        if ( this.errorMessageText != null && !this.errorMessageText.isDisposed()) {
+            this.errorMessageText.setText( StringUtil.nvl( errorMessage ) ); //$NON-NLS-1$
+            
+            boolean hasError = StringUtil.hasLength( errorMessage );
+            this.errorMessageText.setEnabled( hasError );
+            this.errorMessageText.setVisible( hasError );
+            this.errorMessageText.getParent().update();
+            
+            Control button = getButton( IDialogConstants.OK_ID );
+            if (button != null) {
+                button.setEnabled( !hasError );
+            }
+        }
+    }
+    
+    protected Text createErrorMessageText(Composite parent) {
+        errorMessageText = new Text( parent, SWT.READ_ONLY | SWT.WRAP );
+        errorMessageText.setLayoutData( new GridData( GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL ) );
+        errorMessageText.setBackground( errorMessageText.getDisplay().getSystemColor( SWT.COLOR_WIDGET_BACKGROUND ) );
+        
+        setErrorMessage(errorMessage);
+        
+        return errorMessageText;
+    }
+    
+    private String validateInput(UserField userField) {
+        // validate this
+        InputValidator validator = userField.getValidator();
+        if ( validator != null ) {
+            Control control = this.controlMap.get( userField.getId() );
+            if ( control instanceof Text && control.isEnabled()) {
+                String text = ((Text) control).getText();
+                String errorMsg = validator.check( text );
+                if ( StringUtil.hasText( errorMsg ) ) {
+                    return errorMsg;
+                }
+            }
+        }
+        
+        // validate childs
+        Collection<UserField> children = userField.getChildren();
+        if ( children != null ) {
+            for ( UserField child : children ) {
+                String errorMsg = validateInput( child );
+                if ( StringUtil.hasText( errorMsg ) ) {
+                    return errorMsg;
+                }
+            }
+        }
+        
+        return StringUtil.EMPTY_STRING;
+    }
+    
+    protected void validateInput() {
+        String errorMessage = null;
+        
+        // validate
+        for ( UserField userField : this.fields ) {
+            errorMessage = validateInput( userField );
+            if ( StringUtil.hasText( errorMessage ) ) {
+                break;
+            }
+        }
+        
+        setErrorMessage( errorMessage );
+    }
+    
     /* (non-Javadoc)
      * @see org.eclipse.jface.dialogs.Dialog#createContents(org.eclipse.swt.widgets.Composite)
      */
@@ -143,11 +225,14 @@ public class DynamicDialog extends Dialog {
         // create super dialog control
         Composite superContents = (Composite) super.createContents( parent );
         
-        Control dialogArea = this.getDialogArea();
+        Composite dialogArea = (Composite) this.getDialogArea();
+        
+        this.createErrorMessageText( dialogArea );
+        
         boolean firstControl = true;
         for ( UserField field : this.fields ) {
             // create dynamic dialog control
-            Control control = createDynamicControl( (Composite) dialogArea, field );
+            Control control = createDynamicControl( dialogArea, field );
             
             // set focus to first control
             if ( firstControl && control != null ) {
@@ -276,13 +361,21 @@ public class DynamicDialog extends Dialog {
         Composite composite = createComposite( parent, new GridLayout( 2, false ), new GridData( GridData.FILL_BOTH ) );
         
         createLabel( composite, new GridData( GridData.HORIZONTAL_ALIGN_END ), field.getMessage(), field.canModify() );
-        Text text = createText( composite, new GridData( GridData.FILL_BOTH ), field.getValue(), field.canModify(), field.getValidator() );
-        text.addModifyListener( new ModifyListener() {
+        final Text text = createText( composite, new GridData( GridData.FILL_BOTH ), field.getValue(), field.canModify(), field.getValidator() );
+        final ModifyListener modifyListener = new ModifyListener() {
             @Override
             public void modifyText(ModifyEvent e) {
                 Text modifiedText = ((Text) e.widget);
                 field.setValue( modifiedText.getText() ); // synchronize model
             }
+        };
+        text.addModifyListener( modifyListener );
+        text.addDisposeListener( new DisposeListener() {
+            @Override
+            public void widgetDisposed(DisposeEvent e) {
+                text.removeModifyListener( modifyListener );
+                text.removeDisposeListener( this );
+            }
         } );
         
         this.controlMap.put( field.getId(), text );
@@ -294,13 +387,22 @@ public class DynamicDialog extends Dialog {
         Composite composite = createComposite( parent, new GridLayout( 2, false ), new GridData( GridData.FILL_BOTH ) );
         
         createLabel( composite, new GridData( GridData.HORIZONTAL_ALIGN_END ), field.getMessage(), field.canModify() );
-        Text text = createPasswordText( composite, new GridData( GridData.FILL_BOTH ), field.getValue(), field.canModify(), field.getValidator() );
-        text.addModifyListener( new ModifyListener() {
+        final Text text = createPasswordText( composite, new GridData( GridData.FILL_BOTH ), field.getValue(), field.canModify(), field.getValidator() );
+        
+        final ModifyListener modifyListener = new ModifyListener() {
             @Override
             public void modifyText(ModifyEvent e) {
                 Text modifiedText = ((Text) e.widget);
                 field.setValue( modifiedText.getText().toCharArray() ); // synchronize model
             }
+        };
+        text.addModifyListener( modifyListener );
+        text.addDisposeListener( new DisposeListener() {
+            @Override
+            public void widgetDisposed(DisposeEvent e) {
+                text.removeModifyListener( modifyListener );
+                text.removeDisposeListener( this );
+            }
         } );
         
         this.controlMap.put( field.getId(), text );
@@ -309,13 +411,21 @@ public class DynamicDialog extends Dialog {
     }
     
     protected Button createBooleanTypeControl(Composite parent, final UserField field) {
-        Button button = createCheckButton( parent, new GridData( GridData.CENTER ), field.getId(), field.getMessage(), field.getValue(), field.canModify() );
-        button.addSelectionListener( new SelectionAdapter() {
+        final Button button = createCheckButton( parent, new GridData( GridData.CENTER ), field.getId(), field.getMessage(), field.getValue(), field.canModify() );
+        final SelectionAdapter selectionAdapter = new SelectionAdapter() {
             @Override
             public void widgetSelected(SelectionEvent e) {
                 Button modifiedButton = ((Button) e.widget);
                 field.setValue( modifiedButton.getSelection() ); // synchronize model
             }
+        };
+        button.addSelectionListener( selectionAdapter );
+        button.addDisposeListener( new DisposeListener() {
+            @Override
+            public void widgetDisposed(DisposeEvent e) {
+                button.removeSelectionListener( selectionAdapter );
+                button.removeDisposeListener( this );
+            }
         } );
         
         this.controlMap.put( field.getId(), button );
@@ -362,42 +472,88 @@ public class DynamicDialog extends Dialog {
     }
     
     private Text createText(Composite parent, Object layoutData, Object defaultText, boolean enabled, final InputValidator validator) {
-        Text text = new Text( parent, getTextStyle() );
+        final Text text = new Text( parent, getTextStyle() );
         text.setLayoutData( layoutData );
+        
+        final ModifyListener textModifyListener = new ModifyListener() {
+            @Override
+            public void modifyText(ModifyEvent e) {
+                validateInput();
+            }
+        };
+        text.addModifyListener( textModifyListener );
+        text.addDisposeListener( new DisposeListener() {
+            @Override
+            public void widgetDisposed(DisposeEvent e) {
+                text.removeModifyListener( textModifyListener );
+                text.removeDisposeListener( this );
+            }
+        } );
+        
         if ( defaultText instanceof String ) {
             text.setText( (String) defaultText );
+        } else {
+            text.setText( StringUtil.EMPTY_STRING );
         }
         text.setEnabled( enabled );
-        // TODO : add validation logic
         
         return text;
     }
     
     private Text createPasswordText(Composite parent, Object layoutData, Object defaultText, boolean enabled, final InputValidator validator) {
-        Text text = new Text( parent, getPasswordTextStyle() );
+        final Text text = new Text( parent, getPasswordTextStyle() );
         text.setLayoutData( layoutData );
+        
+        final ModifyListener textModifyListener = new ModifyListener() {
+            @Override
+            public void modifyText(ModifyEvent e) {
+                validateInput();
+            }
+        };
+        text.addModifyListener( textModifyListener );
+        text.addDisposeListener( new DisposeListener() {
+            @Override
+            public void widgetDisposed(DisposeEvent e) {
+                text.removeModifyListener( textModifyListener );
+                text.removeDisposeListener( this );
+            }
+        } );
+        
         if ( defaultText instanceof char[] ) {
             text.setText( new String( (char[]) defaultText ) );
         } else if ( defaultText instanceof Character[] ) {
             text.setText( ((Character[]) defaultText).toString() );
+        } else {
+            text.setText( StringUtil.EMPTY_STRING );
         }
+        
         text.setEnabled( enabled );
-        // TODO : add validation logic
         
         return text;
     }
     
     private Button createCheckButton(Composite parent, Object layoutData, final String id, String msg, Object value, boolean enabled) {
-        Button button = new Button( parent, getCheckButtonStyle() );
+        final Button button = new Button( parent, getCheckButtonStyle() );
         button.setLayoutData( layoutData );
-        button.setText( msg );
-        button.setEnabled( enabled );
-        button.addSelectionListener( new SelectionAdapter() {
+        
+        final SelectionAdapter selectionAdapter = new SelectionAdapter() {
             @Override
             public void widgetSelected(SelectionEvent e) {
                 enableChildControls( id, ((Button) e.widget).getSelection() );
             }
-        });
+        };
+        button.addSelectionListener( selectionAdapter );
+        button.addDisposeListener( new DisposeListener() {
+            @Override
+            public void widgetDisposed(DisposeEvent e) {
+                button.removeSelectionListener( selectionAdapter );
+                button.removeDisposeListener( this );
+            }
+        } );
+        
+        button.setText( msg );
+        button.setEnabled( enabled );
+        
         button.setSelection( getBoolean( value ) );
         
         return button;