Alert Box Class - apache-flex

I want to make a reusable Alert Box Class which will be instantiated on various screens of my Flex Project.
Can some tell me whats next in the code below, because am sort of lost regarding how to set the message and title and how to call the Class in my project?
Any help.
Thanks
package components
{
import mx.controls.Alert;
import mx.core.mx_internal;
public class myAlertBox extends Alert
{
public function AlertBoza()
{
super();
var a:Alert;
}
override public static function show():void{
}
}
}

You do not need to extend Alert since the Alert.show() function is static. But you can set it as follows inserting a constructor for a message string and a class member. With that cou can just call the class with the constructor and show the alertbox.
package components
{
import mx.controls.Alert;
import mx.core.mx_internal;
public class myAlertBox
{
private var message:String;
public function myAlertBox(message:String = "")
{
super();
this.message = message;
}
public function show():void{
Alert.show(message);
}
}
}
In another class you can call:
var box:myAlertBox = new myAlertBox("Error");
myAlertBox.show();

If you just want to show a simple alert box, just use mx.controls.Alert directly as you can specify the title and the message show then:
import mx.controls.Alert;
Alert.show("the message", "the title");

Related

BatchMessagingMessageConverter support headerMapper trusted types

I'm using spring-boot 2.7.4 and spring-cloud-dependencies 2021.0.4.
I haven't found any solution in spring documentation for add trustedTypes in BatchMessagingMessageConverter. I'm using kafka for read messages in batch-mode. If I insert a custom header (my own class) when the consumer read the header return a DefaultKafkaHeaderMapper$NonTrustedHeaderType and not my class.
I have in my configuration this key to activate batch mode:
spring.cloud.stream.bindings.nameBind-in-0.consumer.batch-mode=true
I tried in debug to add to headerMapper in BatchMessagingMessageConverter the package of my class and all works fine. There is a way to specify my package in configuration?
I followed the documentation https://docs.spring.io/spring-cloud-stream/docs/3.2.5/reference/html/spring-cloud-stream-binder-kafka.html#kafka-binder-properties, I created a bean like this:
#Bean("kafkaHeaderMapperCustom")
KafkaHeaderMapper getKafkaHeaderMapperCustom() {
var defKHM = new DefaultKafkaHeaderMapper();
defKHM.addTrustedPackages("*");
return defKHM;
}
Specified to key spring.cloud.stream.kafka.binder.headerMapperBeanName in configuration but doesn't work, I suppose that configuration is valid for not batch context?
I tried also these properties:
spring.kafka.consumer.properties.spring.json.trusted.packages
spring.json.trusted.packages
EDIT - Add example:
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.context.annotation.Bean;
import org.springframework.kafka.support.DefaultKafkaHeaderMapper;
import org.springframework.kafka.support.KafkaHeaderMapper;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
#SpringBootApplication
public class Application {
public static final String HEADER_KEY = "CUSTOM_HEADER_KEY";
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Bean
public ApplicationRunner runner(StreamBridge streamBridge) {
return args -> {
var headers = new MessageHeaders(Map.of(HEADER_KEY, new CustomHeaderClass("field1Value", LocalDate.now())));
headers.get(KafkaHeaders.BATCH_CONVERTED_HEADERS);
var message = MessageBuilder.createMessage(new ExampleBrokenHeaderEntity("randomValue", "randomName"), headers);
streamBridge.send("stackoverflow-example", message);
};
}
#Bean
public Consumer<Message<List<ExampleBrokenHeaderEntity>>> readFromKafkaBatchMode() {
return messages -> {
var brokenHeader = ((ArrayList<Map<String, Object>>) messages.getHeaders().get(KafkaHeaders.BATCH_CONVERTED_HEADERS)).get(0).get(HEADER_KEY);
System.out.println("BATCH - Class header: " + (brokenHeader != null ? brokenHeader.getClass() : null));
};
}
#Bean
public Consumer<Message<ExampleBrokenHeaderEntity>> readFromKafkaNoBatchMode() {
return messages -> {
var brokenHeader = messages.getHeaders().get(HEADER_KEY);
System.out.println("NO_BATCH - Class header: " + (brokenHeader != null ? brokenHeader.getClass() : null));
};
}
#Bean("kafkaHeaderMapperCustom")
public KafkaHeaderMapper getKafkaHeaderMapperBatchMode() {
var kafkaHeaderMapperCustom = new DefaultKafkaHeaderMapper();
kafkaHeaderMapperCustom.addTrustedPackages("*");
return kafkaHeaderMapperCustom;
}
}
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
#Data
#NoArgsConstructor
#AllArgsConstructor
public class CustomHeaderClass {
private String field1;
private LocalDate field2;
}
import lombok.AllArgsConstructor;
import lombok.Data;
#Data
#AllArgsConstructor
public final class ExampleBrokenHeaderEntity {
private String type;
private String name;
}
spring.cloud.stream.kafka.binder.brokers=x.x.x.x:xxxx
spring.cloud.function.definition=readFromKafkaNoBatchMode;readFromKafkaBatchMode
spring.cloud.stream.bindings.readFromKafkaBatchMode-in-0.destination=stackoverflow-example
spring.cloud.stream.bindings.readFromKafkaBatchMode-in-0.group=readFromKafkaBatchMode
spring.cloud.stream.bindings.readFromKafkaBatchMode-in-0.consumer.batch-mode=true
spring.cloud.stream.bindings.readFromKafkaNoBatchMode-in-0.destination=stackoverflow-example
spring.cloud.stream.bindings.readFromKafkaNoBatchMode-in-0.group=readFromKafkaNoBatchMode
spring.cloud.stream.kafka.binder.headerMapperBeanName=kafkaHeaderMapperCustom
The output of example is:
NO_BATCH - Class header: class com.example.kafka.header.types.CustomHeaderClass
BATCH - Class header: class org.springframework.kafka.support.DefaultKafkaHeaderMapper$NonTrustedHeaderType
It's a bug; the binder only sets the custom header mapper on a record converter:
private MessageConverter getMessageConverter(
final ExtendedConsumerProperties<KafkaConsumerProperties> extendedConsumerProperties) {
MessageConverter messageConverter = BindingUtils.getConsumerMessageConverter(getApplicationContext(),
extendedConsumerProperties, this.configurationProperties);
if (messageConverter instanceof MessagingMessageConverter) {
((MessagingMessageConverter) messageConverter).setHeaderMapper(getHeaderMapper(extendedConsumerProperties));
}
return messageConverter;
}
There should be similar code for when the converter is a BatchMessagingMessageConverter.
The work around is to define a custom message converter for the batch consumer:
#Bean("batchConverter")
BatchMessageConverter batchConverter(KafkaHeaderMapper kafkaHeaderMapperCustom) {
BatchMessagingMessageConverter batchConv = new BatchMessagingMessageConverter();
batchConv.setHeaderMapper(kafkaHeaderMapperCustom);
return batchConv;
}
spring.cloud.stream.kafka.bindings.readFromKafkaBatchMode-in-0.consumer.converter-bean-name=batchConverter
NO_BATCH - Class header: class com.example.demo.So74294156Application$CustomHeaderClass
BATCH - Class header: class com.example.demo.So74294156Application$CustomHeaderClass
Please open an issue against Spring Cloud Stream, referencing this question/answer.

Webdriver PageObject Implementation using PageFactory in Java

here is what i have so far:
A working Webdriver based Java class, which logs-in to the application and goes to a Home page:
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.AssertJUnit;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class MLoginFFTest {
private WebDriver driver;
private String baseUrl;
private String fileName = "screenshot.png";
#BeforeMethod
public void setUp() throws Exception {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.http.phishy-userpass-length", 255);
profile.setAssumeUntrustedCertificateIssuer(false);
driver = new FirefoxDriver(profile);
baseUrl = "https://a.b.c.d/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void testAccountLogin() throws Exception {
driver.get(baseUrl + "web/certLogon.jsp");
driver.findElement(By.name("logonName")).clear();
AssertJUnit.assertEquals(driver.findElement(By.name("logonName"))
.getTagName(), "input");
AssertJUnit.assertEquals(driver.getTitle(), "DA Logon");
driver.findElement(By.name("logonName")).sendKeys("username");
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("password");
driver.findElement(By.name("submit")).click();
driver.findElement(By.linkText("Account")).click();
AssertJUnit.assertEquals(driver.getTitle(), "View Account");
}
#AfterMethod
public void tearDown() throws Exception {
File screenshot = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenshot, new File(fileName));
} catch (IOException e) {
e.printStackTrace();
}
driver.quit();
}
}
Now as we see there are 2 pages:
1. Login page, where i have to enter username and password, and homepage, where i would be taken, once the authentication succeeds.
Now i want to implement this as PageObjects using Pagefactory: so i have :
package com.example.pageobjects;
import static com.example.setup.SeleniumDriver.getDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
public abstract class MPage<T> {
private static final String BASE_URL = "https://a.b.c.d/";
private static final int LOAD_TIMEOUT = 30;
private static final int REFRESH_RATE = 2;
public T openPage(Class<T> clazz) {
T page = PageFactory.initElements(getDriver(), clazz);
getDriver().get(BASE_URL + getPageUrl());
ExpectedCondition pageLoadCondition = ((MPage) page).getPageLoadCondition();
waitForPageToLoad(pageLoadCondition);
return page;
}
private void waitForPageToLoad(ExpectedCondition pageLoadCondition) {
Wait wait = new FluentWait(getDriver())
.withTimeout(LOAD_TIMEOUT, TimeUnit.SECONDS)
.pollingEvery(REFRESH_RATE, TimeUnit.SECONDS);
wait.until(pageLoadCondition);
}
/**
* Provides condition when page can be considered as fully loaded.
*
* #return
*/
protected abstract ExpectedCondition getPageLoadCondition();
/**
* Provides page relative URL/
*
* #return
*/
public abstract String getPageUrl();
}
And for login Page not sure how i would implement that, as well as the Test, which would call these pages.
I hope these links will be helpful:
page objects in webdriver
page object using jBehave
I would recommend having one class responsible for before/after methods and they should be than called before and after whole scenario. Right now you would close webdriver just after logging in to your page and I guess this is not desired beahaviour. You can extract one level of abstraction simply for Pages where all the clicking happens (right now your MLoginFFTest class does logging in) for both login page and main page.
Now the other class would simply run methods from your Pages calsses like this:
#Test
public void shouldOpenMainPage(){
LoginPage loginPage = new LoginPage();
MainPage mainPage = loginPage.loginCorrectly();
mainPage.verifyOnMainPage();
mainPage.doSthElse();
verifySth(....);
}
So now your file structure could be sth like
++pages/LoginPage.class
++pages/MainPage.class
++steps/SomeTest.class
Hope this helps.

Can't figure out why I'm getting an "Access of undefined object" error in Flex 4.5

I'm very new to Flex 4.5 and I created a class (Project.as) with the following code in it:
package classes
{
public class Project
{
public var projectName:String;
public var description:String;
public var fileLoc:String;
public function Project()
{
// This is the constructor
}
public function SayHello() {
import mx.controls.Alert;
Alert.show('howdy!','Greeting');
}
}
}
In my main.mxml file, I have the following code:
<fx:Script>
<![CDATA[
import classes.Project;
import mx.controls.Alert;
public var aProject:Project = new Project;
aProject.SayHello();
]]>
</fx:Script>
And Flex Builder is saying this:
1120: Access of undefined property aProject.
Why is it telling me this, and how can I fix it? I don't see why it's not working.
Lots of issues here.
First, I have never seen anyone put import statements inside a method. Usually they are put between the package and class definition:
package classes
{
import mx.controls.Alert;
public class Project
{
public var projectName:String;
public var description:String;
public var fileLoc:String;
public function Project()
{
// This is the constructor
}
public function SayHello() {
Alert.show('howdy!','Greeting');
}
}
}
Second; the line of ActionSCript code that you write to call a method on your class instance should be placed inside a method; not "random". Like this:
<fx:Script>
<![CDATA[
import classes.Project;
import mx.controls.Alert;
public var aProject:Project = new Project;
protected function sayHello():void{
aProject.SayHello();
}
]]>
</fx:Script>
Some way you'll want to call that method. A commenter on the original post suggested using creationComplete, which would work. However, you should be cautious about using creationComplete for "constructor-style" code in an MXML Component. preinitialize is better, and the event will fire right after the actual constructor runs. If you need to access any MXML children, have your code in an initialize event handler which runs right after createChildren() runs.
creationComplete handlers execute right after the component finishes initializing; and people often do things in creationComplete that make the component go through it's Lifecycle again, updating the display list.

flex tree itemclick event, doesn't work

I'm creating a reusable flex tree component. And i would like to stick in the itemclick function. So that when a user clicks anywhere on one of the tree's Branches. the branch expands.
My problem is that I don't know how I can get the listener function to fire.
What I would like to do is create the tree completely in as3. (no mxml).
Normally I set the itemClick on tree in the mxml. but I want to do this in as3.
My component has a lot more functions in it but I have deleted them so that it becomes easier to read.
Can anyone help me out on this one? I Thought if I override the createChilderen function and add the eventlistener in there, that it would work. But no luck.
this is my code;
package
{
import mx.controls.Tree;
import mx.controls.listClasses.IListItemRenderer;
import mx.events.ItemClickEvent;
import mx.events.ListEvent;
public class MyTree extends Tree
{
public function MyTree()
{
super();
}
private function tree_itemClick(evt:ListEvent):void {
var item:Object = Tree(evt.currentTarget).selectedItem;
if (dataDescriptor.isBranch(item)) {
expandItem(item, !isItemOpen(item), true);
}
}
override protected function createChildren():void{
super.createChildren();
addEventListener(ListEvent.ITEM_CLICK, tree_itemClick, true);
}
}
}
package
{
import mx.controls.Tree;
import mx.events.ListEvent;
public class MyTree extends Tree
{
public function MyTree()
{
super();
addEventListener(ListEvent.ITEM_CLICK, itemClickHandler);
}
private function itemClickHandler(event:ListEvent):void
{
trace("Success");
}
}
}

Problems with listening for an event in child object in Actionscript

I have two classes. The first one (the starting class):
package
{
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import tetris.*;
public class TetrisGame extends Sprite
{
private var _gameWell:Well;
public function TetrisGame()
{
_gameWell = new Well();
addChild(_gameWell);
}
}
}
The second:
package tetris
{
import flash.display.Sprite;
import flash.events.KeyboardEvent;
public class Well extends Sprite
{
public function Well()
{
super();
addEventListener(KeyboardEvent.KEY_DOWN, onKeyboard);
}
private function onKeyboard(event:KeyboardEvent):void
{
//some code is here
}
}
}
But when I press any buttons on my keyboard, the child class Well doesn't have any reaction. What's the problem?
OK, I get it! =))
I should set focus on the child sprite so it can listen for keyboard events.
package
{
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import tetris.*;
public class TetrisGame extends Sprite
{
private var _gameWell:Well;
public function TetrisGame()
{
_gameWell = new Well();
addChild(_gameWell);
stage.focus = _gameWell;
}
}
}
Or as an alternative; add the event listener to the stage, so it doesn't depend on the Well having focus.
package tetris
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Well extends Sprite
{
public function Well():void
{
super();
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboard);
}
private function onKeyboard(event:KeyboardEvent):void
{
//some code is here
}
}
}

Resources