r1382 - in jaxx/trunk: jaxx-example jaxx-example/src/main/java/jaxx/demo jaxx-example/src/main/resources jaxx-example/src/main/resources/i18n jaxx-runtime-swing-widget jaxx-runtime-swing-widget/src/main/java/jaxx/runtime/swing jaxx-runtime-swing-widget/src/main/java/jaxx/runtime/swing/editor jaxx-runtime-swing-widget/src/main/resources jaxx-runtime-swing-widget/src/main/resources/i18n jaxx-runtime-swing-widget/src/main/resources/icons
Author: tchemit Date: 2009-05-04 19:39:58 +0000 (Mon, 04 May 2009) New Revision: 1382 Added: jaxx/trunk/jaxx-example/src/main/java/jaxx/demo/NumberEditorDemo.jaxx jaxx/trunk/jaxx-example/src/main/java/jaxx/demo/NumberEditorDemoModel.java jaxx/trunk/jaxx-runtime-swing-widget/src/main/java/jaxx/runtime/swing/editor/ jaxx/trunk/jaxx-runtime-swing-widget/src/main/java/jaxx/runtime/swing/editor/NumberEditor.jaxx jaxx/trunk/jaxx-runtime-swing-widget/src/main/java/jaxx/runtime/swing/editor/NumberEditorHandler.java jaxx/trunk/jaxx-runtime-swing-widget/src/main/java/jaxx/runtime/swing/editor/NumberEditorPopup.css jaxx/trunk/jaxx-runtime-swing-widget/src/main/resources/icons/ jaxx/trunk/jaxx-runtime-swing-widget/src/main/resources/icons/action-numbereditor-calculator.png jaxx/trunk/jaxx-runtime-swing-widget/src/main/resources/icons/action-numbereditor-reset.png jaxx/trunk/jaxx-runtime-swing-widget/src/main/resources/icons/action-numbereditor-validate.png Modified: jaxx/trunk/jaxx-example/pom.xml jaxx/trunk/jaxx-example/src/main/java/jaxx/demo/JAXXDemo.jaxx jaxx/trunk/jaxx-example/src/main/resources/i18n/jaxx-example-en_GB.properties jaxx/trunk/jaxx-example/src/main/resources/i18n/jaxx-example-fr_FR.properties jaxx/trunk/jaxx-example/src/main/resources/log4j.properties jaxx/trunk/jaxx-runtime-swing-widget/pom.xml jaxx/trunk/jaxx-runtime-swing-widget/src/main/resources/i18n/jaxx-runtime-swing-widget-en_GB.properties jaxx/trunk/jaxx-runtime-swing-widget/src/main/resources/i18n/jaxx-runtime-swing-widget-fr_FR.properties Log: add NumberEditor + do example Modified: jaxx/trunk/jaxx-example/pom.xml =================================================================== --- jaxx/trunk/jaxx-example/pom.xml 2009-05-04 16:50:00 UTC (rev 1381) +++ jaxx/trunk/jaxx-example/pom.xml 2009-05-04 19:39:58 UTC (rev 1382) @@ -1,3 +1,4 @@ + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> @@ -54,6 +55,9 @@ <maven.jar.main.class>jaxx.demo.JAXXDemo</maven.jar.main.class> + <jaxx.addProjectClassPath>true</jaxx.addProjectClassPath> + <jaxx.addSourcesToClassPath>true</jaxx.addSourcesToClassPath> + <!-- jnlp --> <keystorepath>${codelutin.keystorepath}</keystorepath> <keystorealias>CodeLutin</keystorealias> Modified: jaxx/trunk/jaxx-example/src/main/java/jaxx/demo/JAXXDemo.jaxx =================================================================== --- jaxx/trunk/jaxx-example/src/main/java/jaxx/demo/JAXXDemo.jaxx 2009-05-04 16:50:00 UTC (rev 1381) +++ jaxx/trunk/jaxx-example/src/main/java/jaxx/demo/JAXXDemo.jaxx 2009-05-04 19:39:58 UTC (rev 1382) @@ -1,9 +1,13 @@ <Application title="JAXX Demo" width='1024' height='800' defaultCloseOperation='exit_on_close'> <script><![CDATA[ -org.codelutin.i18n.I18n.init(); +static { +org.codelutin.i18n.I18n.init("fr", "FR"); +} void $afterCompleteSetup() { + + try { jaxx.runtime.SwingUtil.initNimbusLoookAndFeel(); } catch (Exception e) { log.error(e.getMessage(), e); } SwingUtilities.invokeLater(new Runnable() { @Override @@ -81,6 +85,9 @@ </item> <item value='{dataBindingDemo}'/> <item value='{boxedDecoratorDemo}'/> + <item value='Editors'> + <item value='{numberEditorDemo}'/> + </item> </item> </JTree> </JScrollPane> @@ -113,6 +120,7 @@ <CounterDemo id='counterDemo' constraints='counterDemo.getLabel()'/> <CalculatorDemo id='calculatorDemo' constraints='calculatorDemo.getLabel()'/> <BoxedDecoratorDemo id='boxedDecoratorDemo' constraints='boxedDecoratorDemo.getLabel()'/> + <NumberEditorDemo id='numberEditorDemo' constraints='numberEditorDemo.getLabel()'/> </JPanel> </JSplitPane> </Application> \ No newline at end of file Added: jaxx/trunk/jaxx-example/src/main/java/jaxx/demo/NumberEditorDemo.jaxx =================================================================== --- jaxx/trunk/jaxx-example/src/main/java/jaxx/demo/NumberEditorDemo.jaxx (rev 0) +++ jaxx/trunk/jaxx-example/src/main/java/jaxx/demo/NumberEditorDemo.jaxx 2009-05-04 19:39:58 UTC (rev 1382) @@ -0,0 +1,166 @@ + +<DemoPanel> + <script><![CDATA[ +import jaxx.runtime.swing.editor.NumberEditor; + +void $afterCompleteSetup() { + positifIntegerEditor.init(); + positifIntegerEditor2.init(); + normalIntegerEditor.init(); + normalIntegerEditor2.init(); + positifFloatEditor.init(); + positifFloatEditor2.init(); + normalFloatEditor.init(); + normalFloatEditor2.init(); +} +]]> + </script> + + <!-- model --> + <NumberEditorDemoModel id='demoModel'/> + + <Table id='demoPanel' insets='0' fill='both'> + <row> + <cell weightx='0.5'> + <JLabel horizontalAlignment='center' text='numbereditor.type'/> + </cell> + <cell weightx='0.25'> + <JLabel horizontalAlignment='center' text='numbereditor.without.auto.popup'/> + </cell> + <cell weightx='0.25'> + <JLabel horizontalAlignment='center' text='numbereditor.with.auto.popup'/> + </cell> + </row> + <row> + <cell> + <JLabel text='{_("numbereditor.positive.int", demoModel.getPositifInteger())}'/> + </cell> + <cell> + <NumberEditor id='positifIntegerEditor' + property='positifInteger' + constructorParams='this' + bean='{demoModel}' + model='{demoModel.getPositifInteger()}' + autoPopup='false' + showPopupButton='true' + showReset='true'/> + </cell> + <cell> + <NumberEditor id='positifIntegerEditor2' + property='positifInteger' + constructorParams='this' + bean='{demoModel}' + model='{demoModel.getPositifInteger()}' + autoPopup='true' + showPopupButton='true' + showReset='true'/> + </cell> + </row> + <row> + <cell> + <JLabel text='{_("numbereditor.normal.int", demoModel.getNormalInteger())}'/> + </cell> + <cell> + <NumberEditor id='normalIntegerEditor' + property='normalInteger' + constructorParams='this' + bean='{demoModel}' + model='{demoModel.getNormalInteger()}' + autoPopup='false' + showPopupButton='true' + showReset='true' + useSign='true'/> + </cell> + <cell> + <NumberEditor id='normalIntegerEditor2' + property='normalInteger' + constructorParams='this' + bean='{demoModel}' + model='{demoModel.getNormalInteger()}' + autoPopup='true' + showPopupButton='true' + showReset='true' + useSign='true'/> + </cell> + </row> + <row> + <cell> + <JLabel text='{_("numbereditor.positive.float", demoModel.getPositifFloat())}'/> + </cell> + <cell> + <NumberEditor id='positifFloatEditor' + property='positifFloat' + constructorParams='this' + bean='{demoModel}' + model='{demoModel.getPositifFloat()}' + autoPopup='false' + showPopupButton='true' + showReset='true' + useFloat='true'/> + </cell> + <cell> + <NumberEditor id='positifFloatEditor2' + property='positifFloat' + constructorParams='this' + bean='{demoModel}' + model='{demoModel.getPositifFloat()}' + autoPopup='true' + showPopupButton='true' + showReset='true' + useFloat='true'/> + </cell> + </row> + <row> + <cell> + <JLabel text='{_("numbereditor.normal.float", demoModel.getNormalFloat())}'/> + </cell> + <cell> + <NumberEditor id='normalFloatEditor' + property='normalFloat' + constructorParams='this' + bean='{demoModel}' + model='{demoModel.getNormalFloat()}' + autoPopup='false' + showPopupButton='true' + showReset='true' + useSign='true' + useFloat='true'/> + </cell> + <cell> + <NumberEditor id='normalFloatEditor2' + property='normalFloat' + constructorParams='this' + bean='{demoModel}' + model='{demoModel.getNormalFloat()}' + autoPopup='true' + showPopupButton='true' + showReset='true' + useSign='true' + useFloat='true'/> + </cell> + </row> + <row> + <cell> + + </cell> + <cell> + + </cell> + <cell> + + </cell> + </row> + <row> + <cell> + + </cell> + <cell> + + </cell> + <cell> + + </cell> + </row> + </Table> + +</DemoPanel> Added: jaxx/trunk/jaxx-example/src/main/java/jaxx/demo/NumberEditorDemoModel.java =================================================================== --- jaxx/trunk/jaxx-example/src/main/java/jaxx/demo/NumberEditorDemoModel.java (rev 0) +++ jaxx/trunk/jaxx-example/src/main/java/jaxx/demo/NumberEditorDemoModel.java 2009-05-04 19:39:58 UTC (rev 1382) @@ -0,0 +1,78 @@ +package jaxx.demo; + +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; + +/** + * + * @author chemit + * @since 1.5 + */ +public class NumberEditorDemoModel { + + protected PropertyChangeSupport p; + protected int positifInteger; + protected int normalInteger; + protected float positifFloat; + protected float normalFloat; + + public NumberEditorDemoModel() { + p = new PropertyChangeSupport(this); + } + + public float getNormalFloat() { + return normalFloat; + } + + public int getNormalInteger() { + return normalInteger; + } + + public float getPositifFloat() { + return positifFloat; + } + + public int getPositifInteger() { + return positifInteger; + } + + public void setNormalFloat(float normalFloat) { + float old = this.normalFloat; + this.normalFloat = normalFloat; + p.firePropertyChange("normalFloat", old, normalFloat); + } + + public void setNormalInteger(int normalInteger) { + int old = this.normalInteger; + this.normalInteger = normalInteger; + p.firePropertyChange("normalInteger", old, normalInteger); + } + + public void setPositifFloat(float positifFloat) { + float old = this.positifFloat; + this.positifFloat = positifFloat; + p.firePropertyChange("positifFloat", old, positifFloat); + } + + public void setPositifInteger(int positifInteger) { + int old = this.positifInteger; + this.positifInteger = positifInteger; + p.firePropertyChange("positifInteger", old, positifInteger); + } + + public void addPropertyChangeListener(PropertyChangeListener listener) { + p.addPropertyChangeListener(listener); + } + + public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { + p.addPropertyChangeListener(propertyName, listener); + } + + public void removePropertyChangeListener(PropertyChangeListener listener) { + p.removePropertyChangeListener(listener); + } + + public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { + p.removePropertyChangeListener(propertyName, listener); + } +} Modified: jaxx/trunk/jaxx-example/src/main/resources/i18n/jaxx-example-en_GB.properties =================================================================== --- jaxx/trunk/jaxx-example/src/main/resources/i18n/jaxx-example-en_GB.properties 2009-05-04 16:50:00 UTC (rev 1381) +++ jaxx/trunk/jaxx-example/src/main/resources/i18n/jaxx-example-en_GB.properties 2009-05-04 19:39:58 UTC (rev 1382) @@ -106,6 +106,18 @@ form2.text=Form2 \: text form2.text2=Form2 \: text2 no\ layer= +numbereditor.normal.float= +numbereditor.normal.float.value= +numbereditor.normal.int= +numbereditor.normal.int.value= +numbereditor.positive.float= +numbereditor.positive.float.value= +numbereditor.positive.int= +numbereditor.positive.int.value= +numbereditor.positive.integer=Simple positive integer editor +numbereditor.type= +numbereditor.with.auto.popup= +numbereditor.without.auto.popup= valid= validator.field=Champ validator.field.header.tip= Modified: jaxx/trunk/jaxx-example/src/main/resources/i18n/jaxx-example-fr_FR.properties =================================================================== --- jaxx/trunk/jaxx-example/src/main/resources/i18n/jaxx-example-fr_FR.properties 2009-05-04 16:50:00 UTC (rev 1381) +++ jaxx/trunk/jaxx-example/src/main/resources/i18n/jaxx-example-fr_FR.properties 2009-05-04 19:39:58 UTC (rev 1382) @@ -106,6 +106,13 @@ form2.text=Form2 \: text form2.text2=Form2 \: text2 no\ layer= +numbereditor.normal.float=D\u00E9cimal primitif \: [%1$s] +numbereditor.normal.int=Entier primitif \: [%1$s] +numbereditor.positive.float=D\u00E9cimal primitif positif \: [%1$s] +numbereditor.positive.int=Entier primitif positif \: [%1$s] +numbereditor.type=Type d'\u00E9diteur +numbereditor.with.auto.popup=Avec popup auto +numbereditor.without.auto.popup=Sans popup auto valid= validator.field= validator.field.header.tip= Modified: jaxx/trunk/jaxx-example/src/main/resources/log4j.properties =================================================================== --- jaxx/trunk/jaxx-example/src/main/resources/log4j.properties 2009-05-04 16:50:00 UTC (rev 1381) +++ jaxx/trunk/jaxx-example/src/main/resources/log4j.properties 2009-05-04 19:39:58 UTC (rev 1382) @@ -5,5 +5,6 @@ log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) %M - %m%n -log4j.logger.examples=DEBUG +log4j.logger.demo=DEBUG log4j.logger.jaxx=INFO +log4j.logger.org.codelutin=INFO Modified: jaxx/trunk/jaxx-runtime-swing-widget/pom.xml =================================================================== --- jaxx/trunk/jaxx-runtime-swing-widget/pom.xml 2009-05-04 16:50:00 UTC (rev 1381) +++ jaxx/trunk/jaxx-runtime-swing-widget/pom.xml 2009-05-04 19:39:58 UTC (rev 1382) @@ -32,7 +32,7 @@ <!-- ************************************************************* --> <name>${project.artifactId}</name> - <description>Jaxx Examples</description> + <description>Jaxx runtime swing widgets</description> <!-- ************************************************************* --> <!-- *** Build Settings ****************************************** --> @@ -57,25 +57,6 @@ </resource> </resources> - <pluginManagement> - <plugins> - - <plugin> - <artifactId>maven-jar-plugin</artifactId> - <configuration> - <archive> - <manifest> - <addClasspath>true</addClasspath> - <classpathPrefix>./lib/</classpathPrefix> - </manifest> - </archive> - </configuration> - </plugin> - - </plugins> - - </pluginManagement> - <plugins> <plugin> @@ -112,15 +93,8 @@ </goals> </execution> </executions> - </plugin> + </plugin> - <plugin> - <artifactId>maven-dependency-plugin</artifactId> - <configuration> - <outputDirectory>${project.build.directory}/lib</outputDirectory> - </configuration> - </plugin> - </plugins> </build> Added: jaxx/trunk/jaxx-runtime-swing-widget/src/main/java/jaxx/runtime/swing/editor/NumberEditor.jaxx =================================================================== --- jaxx/trunk/jaxx-runtime-swing-widget/src/main/java/jaxx/runtime/swing/editor/NumberEditor.jaxx (rev 0) +++ jaxx/trunk/jaxx-runtime-swing-widget/src/main/java/jaxx/runtime/swing/editor/NumberEditor.jaxx 2009-05-04 19:39:58 UTC (rev 1382) @@ -0,0 +1,152 @@ + +<JPanel layout='{new BorderLayout()}' + onFocusGained='textField.requestFocus()' + onFocusLost='setPopupVisible(false);popup.setVisible(false);' + onMouseExited='setPopupVisible(false);popup.setVisible(false);'> + + <!-- onFocusGained='if (autoPopup) setPopupVisible(true); textField.requestFocus();'--> + + <!-- bean property associated with the editing value --> + <String id='property' javaBean='""'/> + + <!-- bean property --> + <Object id='bean' javaBean='null'/> + + <!-- editor model --> + <Number id="model" javaBean='null'/> + + <!-- useFloat property --> + <Boolean id='useFloat' javaBean='false'/> + + <!-- useSign property --> + <Boolean id='useSign' javaBean='false'/> + + <!-- autoPopup property --> + <Boolean id='autoPopup' javaBean='false'/> + + <!-- showPopupButton property --> + <Boolean id='showPopupButton' javaBean='true'/> + + <!-- show reset property --> + <Boolean id='showReset' javaBean='false'/> + + <!-- internal editor model as text --> + <String id="modelText" javaBean='""'/> + + <!-- internal state --> + <Boolean id='popupVisible' javaBean='false'/> + + <!-- ui handler --> + <NumberEditorHandler id='handler' constructorParams='this'/> + + <!-- popup digital number editor --> + <JPopupMenu id='popup' + onPopupMenuWillBecomeVisible='button.setSelected(true)' + onPopupMenuWillBecomeInvisible='button.setSelected(false)' + onPopupMenuCanceled='button.setSelected(false)'> + <style source='NumberEditorPopup.css'/> + <JPanel layout='{new GridLayout(4,4)}' + border='{BorderFactory.createEmptyBorder(4, 4, 4, 4)}' + background='{Color.WHITE}'> + <JButton text='7' onActionPerformed='addChar(event)' styleClass='digit'/> + <JButton text='8' onActionPerformed='addChar(event)' styleClass='digit'/> + <JButton text='9' onActionPerformed='addChar(event)' styleClass='digit'/> + <JButton text='numbereditor.clearAll' onActionPerformed='setModel(null)' styleClass='clear' + enabled='{!getModelText().isEmpty()}'/> + + + <JButton text='4' onActionPerformed='addChar(event)' styleClass='digit'/> + <JButton text='5' onActionPerformed='addChar(event)' styleClass='digit'/> + <JButton text='6' onActionPerformed='addChar(event)' styleClass='digit'/> + <JButton text='numbereditor.clearOne' onActionPerformed='getHandler().removeChar()' styleClass='clear' + enabled='{!(getModelText().isEmpty() || textField.getCaretPosition() ==0 )}'/> + + <JButton text='1' onActionPerformed='addChar(event)' styleClass='digit'/> + <JButton text='2' onActionPerformed='addChar(event)' styleClass='digit'/> + <JButton text='3' onActionPerformed='addChar(event)' styleClass='digit'/> + + <JButton enabled="false"/> + + <JButton text='0' onActionPerformed='addChar(event)' styleClass='digit' + enabled='{!getEditor().getModelText().equals("0")}'/> + + <JButton id='toggleSign' text='numbereditor.toggleSign' styleClass='operator' + onActionPerformed='getHandler().toggleSign()' + enabled='{isUseSign() && !getEditor().getModelText().isEmpty()}'/> + + <JButton id='dot' text='.' styleClass='operator' + onActionPerformed='addChar(event)' + enabled='{isUseFloat() && getEditor().getModelText().indexOf(".") == -1 }'/> + + <JButton actionIcon='numbereditor-validate' onActionPerformed="getHandler().validate()"/> + </JPanel> + </JPopupMenu> + <script><![CDATA[ +public void init() { + handler.init(); +} + +public NumberEditor getEditor() { + return this; +} + +public void addChar(ActionEvent event) { + getHandler().addChar(((JButton)event.getSource()).getText()); +} + +void showPopup() { + if ( popupVisible || autoPopup ) { + if (!popupVisible) { + setPopupVisible(true); + } else if (!getPopup().isVisible()) { + getHandler().setPopupVisible(true); + } + } +} + +]]> + </script> + + <JToolBar floatable='false' + borderPainted='false' + visible='{isShowReset()}' + constraints='BorderLayout.WEST'> + <JButton id='resetButton' + actionIcon='numbereditor-reset' + toolTipText='numbereditor.action.reset.tip' + focusable='false' + focusPainted='false' + enabled='{isEnabled()}' + onActionPerformed='setModel(null)'/> + </JToolBar> + + <JTextField id='textField' + constraints='BorderLayout.CENTER' + text='{getModelText()}' + enabled='{isEnabled()}' + onKeyReleased='getHandler().setModel(textField.getText())' + onFocusGained='showPopup()'/> + + <!--JFormattedTextField id='textField' constraints='BorderLayout.CENTER' + text='{getModelText()}' + enabled='{isEnabled()}' + onKeyReleased='getHandler().setModel(textField.getText())' + focusLostBehavior='0' + onFocusGained='showPopup()'/--> + + <JToolBar constraints='BorderLayout.EAST' + floatable='false' + opaque='false' + borderPainted='false' + visible='{isShowPopupButton()}' + maximumSize='{new Dimension(24,24)}'> + <JToggleButton id='button' + focusable='false' + focusPainted='false' + actionIcon='numbereditor-calculator' + toolTipText='numbereditor.action.show.tip' + enabled='{isEnabled()}' + onActionPerformed='getHandler().setPopupVisible(!popup.isVisible())'/> + </JToolBar> + +</JPanel> \ No newline at end of file Added: jaxx/trunk/jaxx-runtime-swing-widget/src/main/java/jaxx/runtime/swing/editor/NumberEditorHandler.java =================================================================== --- jaxx/trunk/jaxx-runtime-swing-widget/src/main/java/jaxx/runtime/swing/editor/NumberEditorHandler.java (rev 0) +++ jaxx/trunk/jaxx-runtime-swing-widget/src/main/java/jaxx/runtime/swing/editor/NumberEditorHandler.java 2009-05-04 19:39:58 UTC (rev 1382) @@ -0,0 +1,394 @@ +package jaxx.runtime.swing.editor; + +import java.awt.Dimension; +import javax.swing.JTextField; +import org.apache.commons.beanutils.PropertyUtils; +import org.apache.commons.lang.math.NumberUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.swing.SwingUtilities; +import javax.swing.text.BadLocationException; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.beans.PropertyDescriptor; +import java.lang.reflect.Method; +import javax.swing.JToggleButton; + +/** + * Le handler de l'éditeur graphique de nombres. + * <p/> + * Note: Ce handler n'est pas staless, et chaque ui possède le sien. + * + * @author chemit + * @see NumberEditor + */ +public class NumberEditorHandler { + + public static final Log log = LogFactory.getLog(NumberEditorHandler.class); + public static final String BEAN_PROPERTY = "bean"; + public static final String PROPERTY_PROPERTY = "property"; + public static final String MODEL_PROPERTY = "model"; + public static final String AUTO_POPUP_PROPERTY = "autoPopup"; + public static final String POPUP_VISIBLE_PROPERTY = "popupVisible"; + public static final String USE_FLOAT_PROPERTY = "useFloat"; + public static final String USE_SIGN_PROPERTY = "useSign"; + public static final String VALIDATE_PROPERTY = "validate"; + /** editor ui */ + protected NumberEditor editor; + /** the mutator method on the property of boxed bean in the editor */ + protected Method mutator; + /** a flag to known if mutator accept null value */ + protected Boolean acceptNull; + + public NumberEditorHandler(NumberEditor ui) { + this.editor = ui; + } + + /** initialise l'ui et les listeners d'évènements. */ + public void init() { + + if (editor.getBean() == null) { + throw new NullPointerException("can not have a null bean in ui " + editor); + } + + editor.addPropertyChangeListener(MODEL_PROPERTY, new PropertyChangeListener() { + + @Override + public void propertyChange(PropertyChangeEvent evt) { + if (log.isDebugEnabled()) { + log.debug("set new model " + evt.getNewValue() + " for " + editor.getProperty()); + } + setModel((Number) evt.getOldValue(), (Number) evt.getNewValue()); + } + }); + editor.addPropertyChangeListener(POPUP_VISIBLE_PROPERTY, new PropertyChangeListener() { + + @Override + public void propertyChange(PropertyChangeEvent evt) { + setPopupVisible((Boolean) evt.getNewValue()); + } + }); + editor.getTextField().addMouseListener(new PopupListener()); + /*if (editor.getResetButton().getIcon() == null) { + editor.getResetButton().setIcon(SwingUtil.createActionIcon("numbereditor-reset")); + } + if (editor.getButton().getIcon() == null) { + editor.getButton().setIcon(SwingUtil.createActionIcon("numbereditor-calculator")); + }*/ + } + + /** + * Affiche ou cache la popup. + * + * @param newValue la nouvelle valeur de visibilité de la popup. + */ + public void setPopupVisible(Boolean newValue) { + + if (log.isTraceEnabled()) { + log.trace(newValue); + } + + if (newValue == null || !newValue) { + editor.getPopup().setVisible(false); + return; + } + SwingUtilities.invokeLater(new Runnable() { + + @Override + public void run() { + JToggleButton invoker = editor.getButton(); + Dimension dim = editor.getPopup().getPreferredSize(); + editor.getPopup().show(invoker, (int) (invoker.getPreferredSize().getWidth() - dim.getWidth()), invoker.getHeight()); + editor.getTextField().requestFocus(); + } + }); + } + + /** + * Modifie le modèle de la donnée à éditer à partir d'un evenement clavier + * + * TODO utiliser une filtre sur les donnes en entrees pour ne pas a avoir + * faire les tests ici. + * + * @param s la nouvelle valeur du modèle + */ + public void setModel(String s) { + + String text = editor.getModelText(); + if (text.equals(s)) { + // le modeèle n'a pas changé, rien a faire sur le modèle + if (log.isDebugEnabled()) { + log.debug("modelText is the same, skip !"); + } + return; + } + + boolean canApply = false; + + boolean endWithDot = false; + + Number newValue = null; + + if (s.trim().isEmpty()) { + // le champ est vide donc c'est la valeur null a reaffecter + s = null; + canApply = true; + } else if (s.endsWith(".")) { + s += "0"; + endWithDot = true; + } + + if (s != null && NumberUtils.isNumber(s)) { + + // on a un nombre valide + + try { + Float f = Float.parseFloat(s); + if (!editor.isUseSign() && (s.startsWith("-"))) { + if (log.isDebugEnabled()) { + log.debug("will skip since can not allow sign on this editor but was " + f); + } + } else { + + if (!editor.isUseFloat() && s.contains(".")) { + if (log.isDebugEnabled()) { + log.debug("will skip since can not allow float on this editor but was " + f); + } + } else { + // ok on peut utilise ce modele + if (editor.isUseFloat()) { + newValue = f; + } else { + newValue = f.intValue(); + } + canApply = true; + } + } + + } catch (NumberFormatException e) { + // rien a faire + log.debug(e); + } + } + JTextField field = editor.getTextField(); + + int oldPosition = field.getCaretPosition(); + + if (canApply) { + if (log.isDebugEnabled()) { + log.debug("can apply new model value : " + newValue); + } + // on peut mettre a jour le model + editor.setModel(newValue); + if (endWithDot) { + editor.setModelText(s.substring(0, s.length() - 1)); + field.setCaretPosition(oldPosition); + } + return; + } + + // on ne peut pas appliquer, on repositionne l'ancien texte + // dans l'éditeur + + if (log.isDebugEnabled()) { + log.debug("invalid text " + s + " reput old text " + text); + } + + if (oldPosition > 0) { + oldPosition--; + } + field.setText(text); + field.setCaretPosition(oldPosition); + } + + /** + * Ajoute le caractère donné à l'endroit où est le curseur dans la zone de + * saisie et met à jour le modèle. + * + * @param s le caractère à ajouter. + */ + public void addChar(String s) { + char c = s.charAt(0); + try { + editor.getTextField().getDocument().insertString(editor.getTextField().getCaretPosition(), c + "", null); + setModel(editor.getTextField().getText()); + //setModel(editor.getModelText() + c); + + } catch (BadLocationException e) { + log.warn(e); + } + } + + /** + * Supprime le caractère juste avant le curseur du modèle (textuel) et + * met à jour la zone de saisie. + */ + public void removeChar() { + String s = editor.getModelText(); + int position = editor.getTextField().getCaretPosition(); + if (position < 1 || s.isEmpty()) { + if (log.isDebugEnabled()) { + log.debug("cannot remove when caret on first position or text empty"); + } + // on est au debut du doc, donc rien a faire + return; + } + try { + editor.getTextField().getDocument().remove(position - 1, 1); + } catch (BadLocationException ex) { + // ne devrait jamais arrive vu qu'on a fait le controle... + log.debug(ex); + return; + } + String newText = editor.getTextField().getText(); + if (log.isDebugEnabled()) { + log.debug("text updated : " + newText); + } + position--; + editor.getTextField().setCaretPosition(position); + setModel(newText); + } + + /** + * Permute le signe dans la zone de saisie et + * dans le modèle. + */ + public void toggleSign() { + String newValue = editor.getModelText(); + + if (newValue.startsWith("-")) { + setModel(newValue.substring(1)); + } else { + setModel("-" + newValue); + } + } + + /** @return l'éditeur au quel est rattaché le handler. */ + public NumberEditor getEditor() { + return editor; + } + + protected void setModel(Number oldValue, Number newValue) { + if (editor.getBean() == null) { + return; + } + + if (log.isDebugEnabled()) { + log.debug(editor.getProperty() + " on " + editor.getBean().getClass() + " :: " + oldValue + " to " + newValue); + } + + try { + if (newValue == null && !getAcceptNull()) { + // valeur nulle sur une propriete primitive + // on ne peut pas utiliser la valeur null, mais 0 à la place + if (editor.isUseFloat()) { + getMutator().invoke(editor.getBean(), 0.0f); + } else { + getMutator().invoke(editor.getBean(), 0); + } + + } else { + getMutator().invoke(editor.getBean(), newValue); + } + String strValue; + if (newValue == null) { + strValue = ""; + } else { + strValue = newValue + ""; + if (editor.isUseFloat()) { + Float n = Float.parseFloat(strValue); + if (((float) n.intValue()) == n) { + strValue = n.intValue() + ""; + } + } + } + + editor.setModelText(strValue); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + protected void validate() { + + setPopupVisible(false); + // fire validate property (to be able to notify listeners) + editor.firePropertyChange(VALIDATE_PROPERTY, null, true); + } + + protected class PopupListener extends MouseAdapter { + + @Override + public void mousePressed(MouseEvent e) { + maybeShowPopup(e); + } + + @Override + public void mouseReleased(MouseEvent e) { + maybeShowPopup(e); + } + + protected void maybeShowPopup(MouseEvent e) { + if (!e.isPopupTrigger()) { + return; + } + if (editor.isAutoPopup()) { + if (editor.isPopupVisible()) { + if (!editor.getPopup().isVisible()) { + setPopupVisible(true); + } + // popup already visible + + } else { + // set popup auto + editor.setPopupVisible(true); + + } + } else { + if (editor.isPopupVisible()) { + setPopupVisible(true); + } + } + } + } + + protected Method getMutator() { + if (mutator == null) { + Object bean = editor.getBean(); + if (bean == null) { + throw new NullPointerException("could not find bean in " + editor); + } + String property = editor.getProperty(); + if (property == null) { + throw new NullPointerException("could not find property in " + editor); + } + + try { + PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor(bean, property); + mutator = descriptor.getWriteMethod(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + return mutator; + } + + public Boolean getAcceptNull() { + if (acceptNull == null) { + Method m = getMutator(); + if (m == null) { + // should never happens + throw new IllegalStateException("could not find the mutator"); + } + Class<?> returnType = m.getParameterTypes()[0]; + acceptNull = !returnType.isPrimitive(); + if (log.isDebugEnabled()) { + log.debug(acceptNull + " for mutator " + m.getName() + " type : " + returnType); + } + } + return acceptNull; + } +} \ No newline at end of file Added: jaxx/trunk/jaxx-runtime-swing-widget/src/main/java/jaxx/runtime/swing/editor/NumberEditorPopup.css =================================================================== --- jaxx/trunk/jaxx-runtime-swing-widget/src/main/java/jaxx/runtime/swing/editor/NumberEditorPopup.css (rev 0) +++ jaxx/trunk/jaxx-runtime-swing-widget/src/main/java/jaxx/runtime/swing/editor/NumberEditorPopup.css 2009-05-04 19:39:58 UTC (rev 1382) @@ -0,0 +1,25 @@ +JButton { + font-size: 14; + focusPainted: false; + focusable: false; +} + +JButton.digit { + foreground: blue; +} + +JButton.operator { + foreground: #009900; +} + +JButton.clear { + foreground: red; +} + +JButton:mouseover { + font-weight: bold; +} + +JButton.operator:mouseover { + font-weight: normal; +} Modified: jaxx/trunk/jaxx-runtime-swing-widget/src/main/resources/i18n/jaxx-runtime-swing-widget-en_GB.properties =================================================================== --- jaxx/trunk/jaxx-runtime-swing-widget/src/main/resources/i18n/jaxx-runtime-swing-widget-en_GB.properties 2009-05-04 16:50:00 UTC (rev 1381) +++ jaxx/trunk/jaxx-runtime-swing-widget/src/main/resources/i18n/jaxx-runtime-swing-widget-en_GB.properties 2009-05-04 19:39:58 UTC (rev 1382) @@ -0,0 +1,17 @@ +.= +0= +1= +2= +3= +4= +5= +6= +7= +8= +9= +H= +numbereditor.action.reset.tip=Reset +numbereditor.action.show.tip=Show numeric panel +numbereditor.clearAll=C +numbereditor.clearOne=CE +numbereditor.toggleSign=+/- Modified: jaxx/trunk/jaxx-runtime-swing-widget/src/main/resources/i18n/jaxx-runtime-swing-widget-fr_FR.properties =================================================================== --- jaxx/trunk/jaxx-runtime-swing-widget/src/main/resources/i18n/jaxx-runtime-swing-widget-fr_FR.properties 2009-05-04 16:50:00 UTC (rev 1381) +++ jaxx/trunk/jaxx-runtime-swing-widget/src/main/resources/i18n/jaxx-runtime-swing-widget-fr_FR.properties 2009-05-04 19:39:58 UTC (rev 1382) @@ -0,0 +1,17 @@ +.= +0= +1= +2= +3= +4= +5= +6= +7= +8= +9= +H= +numbereditor.action.reset.tip=R\u00E9initialiser +numbereditor.action.show.tip=Afficher le pav\u00E9 num\u00E9rique +numbereditor.clearAll=C +numbereditor.clearOne=CE +numbereditor.toggleSign=+/- Added: jaxx/trunk/jaxx-runtime-swing-widget/src/main/resources/icons/action-numbereditor-calculator.png =================================================================== (Binary files differ) Property changes on: jaxx/trunk/jaxx-runtime-swing-widget/src/main/resources/icons/action-numbereditor-calculator.png ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: jaxx/trunk/jaxx-runtime-swing-widget/src/main/resources/icons/action-numbereditor-reset.png =================================================================== (Binary files differ) Property changes on: jaxx/trunk/jaxx-runtime-swing-widget/src/main/resources/icons/action-numbereditor-reset.png ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: jaxx/trunk/jaxx-runtime-swing-widget/src/main/resources/icons/action-numbereditor-validate.png =================================================================== (Binary files differ) Property changes on: jaxx/trunk/jaxx-runtime-swing-widget/src/main/resources/icons/action-numbereditor-validate.png ___________________________________________________________________ Name: svn:mime-type + application/octet-stream
participants (1)
-
tchemit@users.labs.libre-entreprise.org