Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

Commit

Permalink
Refactor XML elements of Formatters jmix-projects/jmix-ui#452
Browse files Browse the repository at this point in the history
  • Loading branch information
glebfox committed May 18, 2021
1 parent c1d51c9 commit 6c150df
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.haulmont.cuba.core.global.AppBeans;
import io.jmix.core.Messages;
import io.jmix.core.metamodel.datatype.FormatStringsRegistry;
import io.jmix.core.security.CurrentAuthentication;
import org.dom4j.Element;

Expand All @@ -41,6 +42,7 @@ public DateFormatter(Element element) {
public String apply(Date value) {
messages = AppBeans.get(Messages.class);
currentAuthentication = AppBeans.get(CurrentAuthentication.class);
formatStringsRegistry = AppBeans.get(FormatStringsRegistry.class);
return super.apply(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.haulmont.cuba.core.global.AppBeans;
import io.jmix.core.Messages;
import io.jmix.core.metamodel.datatype.DatatypeRegistry;
import io.jmix.core.metamodel.datatype.FormatStringsRegistry;
import io.jmix.core.security.CurrentAuthentication;
import org.dom4j.Element;

Expand All @@ -37,6 +39,8 @@ public NumberFormatter(Element element) {
public String apply(Number value) {
messages = AppBeans.get(Messages.class);
currentAuthentication = AppBeans.get(CurrentAuthentication.class);
formatStringsRegistry = AppBeans.get(FormatStringsRegistry.class);
datatypeRegistry = AppBeans.get(DatatypeRegistry.class);
return super.apply(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,14 @@ public static Formatter<?> loadFormatter(Element element, ClassManager classMana
return (Formatter<?>) constructor.newInstance(formatterElement);
} catch (Throwable e) {
throw new GuiDevelopmentException(
String.format("Unable to instantiate class %s: %s", className, e.toString()), context);
String.format("Unable to instantiate class %s: %s", className, e), context);
}
} catch (NoSuchMethodException e) {
try {
return (Formatter<?>) aClass.getDeclaredConstructor().newInstance();
} catch (Exception e1) {
throw new GuiDevelopmentException(
String.format("Unable to instantiate class %s: %s", className, e1.toString()), context);
String.format("Unable to instantiate class %s: %s", className, e1), context);
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright 2021 Haulmont.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package spec.haulmont.cuba.web.formatter

import com.haulmont.cuba.gui.components.formatters.DateFormatter
import com.haulmont.cuba.gui.components.formatters.NumberFormatter
import com.haulmont.cuba.web.formatter.screen.FormatterTestScreen
import io.jmix.core.metamodel.datatype.DatatypeRegistry
import io.jmix.core.metamodel.datatype.FormatStrings
import io.jmix.core.metamodel.datatype.FormatStringsRegistry
import io.jmix.core.security.CurrentAuthentication
import io.jmix.ui.component.Label
import org.springframework.beans.factory.annotation.Autowired
import spec.haulmont.cuba.web.UiScreenSpec

import java.text.DecimalFormat
import java.text.SimpleDateFormat

class FormatterTest extends UiScreenSpec {

@Autowired
CurrentAuthentication currentAuthentication

@Autowired
FormatStringsRegistry formatStringsRegistry

@Autowired
DatatypeRegistry datatypeRegistry

@Override
void setup() {
exportScreensPackages(['com.haulmont.cuba.web.formatter.screen'])

formatStringsRegistry.setFormatStrings(Locale.ENGLISH, new FormatStrings(
'.' as char, ',' as char,
"#,##0", "#,##0.###", "#,##0.##",
"dd/MM/yyyy", "dd/MM/yyyy HH:mm", "dd/MM/yyyy HH:mm Z", "HH:mm", "HH:mm Z",
"True", "False"));
}

def "Formatter is applied for field"(String id, Class<io.jmix.ui.component.formatter.Formatter> formatterClass) {
showTestMainScreen()

when: "Screen is loaded"

def formattersScreen = screens.create(FormatterTestScreen)
formattersScreen.show()

then: "Formatter is applied for the field"

noExceptionThrown()
((Label) formattersScreen.getWindow().getComponent(id)).getFormatter().getClass() == formatterClass

where:

id | formatterClass
"dateFormatterField" | DateFormatter
"dateTimeFormatterField" | DateFormatter
"numberFormatterField" | NumberFormatter
"defaultNumberFormatterField" | io.jmix.ui.component.formatter.NumberFormatter
}

def "DateFormatter is applied for field using the XML attribute 'class'"() {
showTestMainScreen()

when: "Screen is loaded"

def formattersScreen = screens.create(FormatterTestScreen)
formattersScreen.show()

def dateFormatterField = (Label<Date>) formattersScreen.getWindow().getComponent("dateFormatterField")

def dateFormat = new SimpleDateFormat("yyyy-MM-dd")

then: "DateFormatter is applied for field"

noExceptionThrown()
dateFormatterField.getRawValue() == dateFormat.format(dateFormatterField.getValue())
}

def "DateFormatter is applied for field using the XML attribute 'name'"() {
showTestMainScreen()

when: "Screen is loaded"

def formattersScreen = screens.create(FormatterTestScreen)
formattersScreen.show()

def dateTimeFormatterField = (Label<Date>) formattersScreen.getWindow().getComponent("dateTimeFormatterField")

def dateTimeFormat = new SimpleDateFormat(
formatStringsRegistry.getFormatStrings(currentAuthentication.getLocale()).getDateTimeFormat())

then: "DateFormatter with type attribute is applied for field"

noExceptionThrown()
dateTimeFormatterField.getRawValue() == dateTimeFormat.format(dateTimeFormatterField.getValue())
}

def "NumberFormatter is applied for field using the XML attribute 'class'"() {
showTestMainScreen()

when: "Screen is loaded"

def formattersScreen = screens.create(FormatterTestScreen)
formattersScreen.show()

def numberFormatterField = (Label<Date>) formattersScreen.getWindow().getComponent("numberFormatterField")

def decimalFormat = new DecimalFormat("#,###",
formatStringsRegistry.getFormatStrings(currentAuthentication.getLocale()).getFormatSymbols())

then: "NumberFormatter is applied for field"

noExceptionThrown()
numberFormatterField.getRawValue() == decimalFormat.format(numberFormatterField.getValue())
}

def "NumberFormatter without format attribute is applied for field using beanLocator"() {
showTestMainScreen()

when: "Screen is loaded"

def formattersScreen = screens.create(FormatterTestScreen)
formattersScreen.show()

def defaultNumberFormatterField = (Label<Date>) formattersScreen.getWindow().getComponent("defaultNumberFormatterField")

def datatype = datatypeRegistry.get(Long.class)

then: "NumberFormatter without format attribute for field"

noExceptionThrown()
defaultNumberFormatterField.getRawValue() == datatype.format(defaultNumberFormatterField.getValue(), currentAuthentication.getLocale())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2021 Haulmont.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.haulmont.cuba.web.formatter.screen;

import io.jmix.core.TimeSource;
import io.jmix.ui.component.Label;
import io.jmix.ui.component.formatter.NumberFormatter;
import io.jmix.ui.screen.Screen;
import io.jmix.ui.screen.Subscribe;
import io.jmix.ui.screen.UiController;
import io.jmix.ui.screen.UiDescriptor;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

@UiDescriptor("/spec/haulmont/cuba/web/formatter/screen/formatter-test-screen.xml")
@UiController
public class FormatterTestScreen extends Screen {

@Autowired
protected Label<Date> dateFormatterField;
@Autowired
protected Label<Date> dateTimeFormatterField;
@Autowired
protected Label<Long> numberFormatterField;
@Autowired
protected Label<Long> defaultNumberFormatterField;

@Autowired
protected TimeSource timeSource;

@Autowired
protected ObjectProvider<NumberFormatter> numberFormatterObjectProvider;

@Subscribe
protected void onInit(InitEvent event) {
defaultNumberFormatterField.setFormatter(numberFormatterObjectProvider.getObject());

dateFormatterField.setValue(timeSource.currentTimestamp());
dateTimeFormatterField.setValue(timeSource.currentTimestamp());
numberFormatterField.setValue(timeSource.currentTimeMillis());
defaultNumberFormatterField.setValue(timeSource.currentTimeMillis());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!--
~ Copyright 2021 Haulmont.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<window xmlns="http://schemas.haulmont.com/cuba/screen/window.xsd">
<layout>
<label id="dateFormatterField">
<formatter class="com.haulmont.cuba.gui.components.formatters.DateFormatter"
format="yyyy-MM-dd"/>
</label>
<label id="dateTimeFormatterField">
<formatter class="com.haulmont.cuba.gui.components.formatters.DateFormatter"
type="DATETIME"/>
</label>
<label id="numberFormatterField">
<formatter class="com.haulmont.cuba.gui.components.formatters.NumberFormatter"
format="#,###"/>
</label>
<label id="defaultNumberFormatterField"/>
</layout>
</window>

0 comments on commit 6c150df

Please sign in to comment.