directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pamarce...@apache.org
Subject svn commit: r1397462 - /directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/integer/IntegerDialog.java
Date Fri, 12 Oct 2012 08:00:24 GMT
Author: pamarcelot
Date: Fri Oct 12 08:00:23 2012
New Revision: 1397462

URL: http://svn.apache.org/viewvc?rev=1397462&view=rev
Log:
Fix for DIRSTUDIO-823 (Integer Value Editor only selects values of Java Integer magnitude).

Modified:
    directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/integer/IntegerDialog.java

Modified: directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/integer/IntegerDialog.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/integer/IntegerDialog.java?rev=1397462&r1=1397461&r2=1397462&view=diff
==============================================================================
--- directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/integer/IntegerDialog.java
(original)
+++ directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/integer/IntegerDialog.java
Fri Oct 12 08:00:23 2012
@@ -21,17 +21,29 @@
 package org.apache.directory.studio.valueeditors.integer;
 
 
+import java.math.BigDecimal;
+
 import org.apache.directory.studio.valueeditors.ValueEditorsActivator;
 import org.apache.directory.studio.valueeditors.ValueEditorsConstants;
 import org.eclipse.jface.dialogs.Dialog;
 import org.eclipse.jface.dialogs.IDialogConstants;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.KeyAdapter;
+import org.eclipse.swt.events.KeyEvent;
+import org.eclipse.swt.events.KeyListener;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.VerifyEvent;
+import org.eclipse.swt.events.VerifyListener;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Shell;
-import org.eclipse.swt.widgets.Spinner;
+import org.eclipse.swt.widgets.Text;
 
 
 /**
@@ -41,15 +53,14 @@ import org.eclipse.swt.widgets.Spinner;
  */
 public class IntegerDialog extends Dialog
 {
+    /** The initial value */
+    private BigDecimal initialValue;
 
-    /** The initial value. */
-    private int initialValue;
-
-    /** The return value. */
-    private int returnValue;
+    /** The value */
+    private BigDecimal value;
 
-    /** The spinner to select an integer */
-    private Spinner spinner = null;
+    /** The text */
+    private Text text;
 
 
     /**
@@ -58,12 +69,12 @@ public class IntegerDialog extends Dialo
      * @param parentShell the parent shell
      * @param initialValue the initial value
      */
-    public IntegerDialog( Shell parentShell, int initialValue )
+    public IntegerDialog( Shell parentShell, BigDecimal initialValue )
     {
         super( parentShell );
         super.setShellStyle( super.getShellStyle() | SWT.RESIZE );
         this.initialValue = initialValue;
-        this.returnValue = -1;
+        this.value = new BigDecimal( initialValue.toString() );
     }
 
 
@@ -93,7 +104,7 @@ public class IntegerDialog extends Dialo
      */
     protected void okPressed()
     {
-        returnValue = spinner.getSelection();
+        //        returnValue = spinner.getSelection();
         super.okPressed();
     }
 
@@ -103,20 +114,89 @@ public class IntegerDialog extends Dialo
      */
     protected Control createDialogArea( Composite parent )
     {
-        // create composite
+        // Composite
         Composite composite = ( Composite ) super.createDialogArea( parent );
-        composite.setLayout( new GridLayout() );
+        composite.setLayout( new GridLayout( 3, false ) );
         GridData gd = new GridData( GridData.FILL_BOTH );
         composite.setLayoutData( gd );
 
-        spinner = new Spinner( composite, SWT.BORDER );
-        spinner.setMinimum( Integer.MIN_VALUE );
-        spinner.setMaximum( Integer.MAX_VALUE );
-        spinner.setDigits( 0 );
-        spinner.setIncrement( 1 );
-        spinner.setPageIncrement( 100 );
-        spinner.setSelection( initialValue );
-        spinner.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
+        // - Button
+        Button minusButton = new Button( composite, SWT.PUSH );
+        minusButton.setText( "-" );
+        minusButton.addSelectionListener( new SelectionAdapter()
+        {
+            public void widgetSelected( SelectionEvent e )
+            {
+                addToValue( -1 );
+                text.selectAll();
+            }
+        } );
+
+        // Text
+        text = new Text( composite, SWT.BORDER );
+        text.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
+        text.addModifyListener( new ModifyListener()
+        {
+            public void modifyText( ModifyEvent e )
+            {
+                updateValueFromText();
+            }
+        } );
+        text.addVerifyListener( new VerifyListener()
+        {
+            public void verifyText( VerifyEvent e )
+            {
+                // Prevent the user from entering anything but an integer
+                if ( !e.text.matches( "(-)?([0-9])*" ) ) //$NON-NLS-1$
+                {
+                    e.doit = false;
+                }
+            }
+        } );
+        text.addKeyListener( new KeyAdapter()
+        {
+            public void keyPressed( KeyEvent e )
+            {
+                if ( e.keyCode == SWT.ARROW_UP )
+                {
+                    addToValue( 1 );
+                    e.doit = false;
+                    text.selectAll();
+                }
+                else if ( e.keyCode == SWT.ARROW_DOWN )
+                {
+                    addToValue( -1 );
+                    e.doit = false;
+                    text.selectAll();
+                }
+                else if ( e.keyCode == SWT.PAGE_UP )
+                {
+                    addToValue( 100 );
+                    e.doit = false;
+                    text.selectAll();
+                }
+                else if ( e.keyCode == SWT.PAGE_DOWN )
+                {
+                    addToValue( -100 );
+                    e.doit = false;
+                    text.selectAll();
+                }
+            }
+        } );
+
+        // + Button
+        Button plusButton = new Button( composite, SWT.PUSH );
+        plusButton.setText( "+" );
+        plusButton.addSelectionListener( new SelectionAdapter()
+        {
+            public void widgetSelected( SelectionEvent e )
+            {
+                addToValue( 1 );
+                text.selectAll();
+            }
+        } );
+
+        updateTextValue();
 
         applyDialogFont( composite );
         return composite;
@@ -124,13 +204,52 @@ public class IntegerDialog extends Dialo
 
 
     /**
+     * Updates the text value.
+     */
+    private void updateTextValue()
+    {
+        text.setText( value.toString() );
+    }
+
+
+    /**
+     * Adds the given integer to the value.
+     *
+     * @param i the integer
+     */
+    private void addToValue( int i )
+    {
+        value = value.add( new BigDecimal( i ) );
+
+        updateTextValue();
+    }
+
+
+    /**
+     * Updates the value from the text's value.
+     */
+    private void updateValueFromText()
+    {
+        try
+        {
+            BigDecimal newValue = new BigDecimal( text.getText() );
+            value = newValue;
+        }
+        catch ( NumberFormatException e )
+        {
+            // Nothing to do
+        }
+    }
+
+
+    /**
      * Gets the integer.
      * 
      * @return the integer
      */
-    public int getInteger()
+    public BigDecimal getInteger()
     {
-        return returnValue;
+        return value;
     }
 
 
@@ -143,6 +262,6 @@ public class IntegerDialog extends Dialo
      */
     public boolean isDirty()
     {
-        return initialValue != returnValue;
+        return !initialValue.equals( value );
     }
 }



Mime
View raw message