is it possible to expand all nodes in flex tree using timer? - java

private function openAllNodes(event:TimerEvent):void {
//Alert.show(event.target.currentCount);
var index:int =event.target.currentCount - 1;
myTree.openItems = treeData..node[index] ;
}
public function start_timer():void
{
timer.start();
}
private function closeAllNodes():void {
myTree.openItems = [];
}
public var timer:Timer = new Timer(1000,19);
public function init():void
{
timer.addEventListener(TimerEvent.TIMER, openAllNodes);
}
In my project I want to show the video like growing flex tree. So I tried flex timer based to expand node one by one. But if I open the first node then automatically close second time. If there is any other way to expand nodes one by one then let me know.

If you could post your dataProvider it will be easier to determine the best approach for your problem. Of the top of my head I think you could use the method expandItem() and pass in the the item using for example (#id==index).

Related

How can I change code based on Method Argument?

public static void calculate(List<Person> data, String categoryType) {
for(int i = 0; i < categoryData.size(); i++) {
if(data.get(i).calculateCategoryOne() == firstPlace) {
...
}
}
}
If you see data.get(i).calculateCategoryOne(), the method call is for category one. The problem is that I need to copy-paste the entire code in a if-block for each category to just change this method call data.get(i).calculateCategoryTwo(), data.get(i).calculateCategoryThree(), ... data.get(i).calculateCategoryTen(),
While I can still make the logic work in this way, I feel it is redundant and not a good programming practice. Just to change one line of code, I would have to replicate the same code ten different times which will add nearly 500 lines of code.
So, my question is: Is there a way to dynamically change my method call based on the category type string argument.
I was thinking one possible way is to pass the method call in a string and convert it to a method call itself. For example, let's assume CategoryType string argument is "calculateCategoryOne()". So, data.get(i)."calculateCategoryOne()" would be recognized by the compiler as the method call itself. Is there a way to actually implement this?
I'm open to other ideas as well to reduce redundancy.
I would think using a functional interface would be appropriate here. You want different functionality depending on the categoryType, so passing in the function you want to use, rather than a String representation of it, would accomplish this.
#FunctionalInterface
public interface Calculate {
int calculate(Person data);
}
public static void calculate(List<Person> data, Calculate calculate) {
for(int i = 0; i < categoryData.size(); i++) {
if(calculate.calculate(data.get(i)) == firstPlace) {
...
}
}
}
and the call to the method would define what the calculation would be
calculate(list, p -> {
// calculation done here
});
or if this would happen frequently, you could predefine your categories once and pass those in:
Calculate categoryOne = p -> { ... };
Calculate categoryTwo = p -> { ... };
.
.
calculate(list, categoryOne);

Calculation in Wizard

This is more of a general question. We have a lot of wizard, some of which start a long-running process and display the result after. The question is: what is the correct way to do long calculations?
Formerly most wizards did their calculations in DialogPage#setVisible, something like that:
public void setVisible(final boolean visible) {
if (visible) {
getWizard().getContainer().run(true, true, new MyCalculation());
}
super.setVisible(visible);
}
I don't think that's a good idea, since usually getWizard() gets called a lot in these methods. Moreover, usually the parent wizard gets cast to a specific implementation to get input values from or set the result to other pages. So usually it looks something like this:
public void setVisible(final boolean visible) {
if (visible) {
Input input = ((MyCalculationWizard)getWizard()).getInputPage().getInput();
MyCalculation calculation = new MyCalculation(input);
getWizard().getContainer().run(true, true, calculation);
Output output = calculation.getOutput();
((MyCalculationWizard)getWizard()).getOtherPage().setOutput(output);
}
super.setVisible(visible);
}
Just from looking at the code you know that's very bad style.
So we replaced it with something that calculates in Wizard#getNextPage():
public IWizardPage getNextPage(final IWizardPage page) {
final IWizardPage nextPage = super.getNextPage(page);
if (nextPage == this.myResultPage)
getContainer().run(true, true, new MyCalculation());
return nextPage;
}
That way, the wizard is able to fine-tune a lot better than a page would, and the wizard already knows it's pages and can handle input and output a lot better than a page ever could.
The drawback is: getNextPage() gets called a lot for updating the buttons and every time really the wizard feels like it. So while it works for small processes, it does not cut it for long-running ones.
After some more poking around I found the following to work while overriding Wizard#setContainer:
public void setContainer(final IWizardContainer wizardContainer) {
final IWizardContainer oldContainer = getContainer();
if (oldContainer instanceof WizardDialog)
((WizardDialog) oldContainer).removePageChangingListener(this);
super.setContainer(wizardContainer);
if (wizardContainer instanceof WizardDialog)
((WizardDialog) wizardContainer).addPageChangingListener(this);
}
public void handlePageChanging(final PageChangingEvent event) {
final IWizardPage currentPage = (IWizardPage) event.getCurrentPage();
final IWizardPage nextPage = (IWizardPage) event.getTargetPage();
if (currentPage == this.myInputPage && nextPage == this.myResultPage)
getContainer().run(true, true, new MyCalculation());
}
The big advantage here is that the listener only gets called if the wizard wants to jump between pages, and we are able to really fine-tune the calculation (e.g. to not be called when calling 'Previous'). We are even able to not show the next page after all (event.doit = false).
The drawback is the cast of the container to WizardDialog, because potentially it could be an entirely different implementation.
So the question stands: What is the best way to start long processes in wizards?

Using a stack to solve a maze Object Oriented (backtracking) issue

I am trying to solve a maze in an object oriented way.
My maze consists of fields, each field has an element attribute,
an element is a wall, player, helper or endpoint. A field with an empty attribute is a path.
Each field keeps track of his own neighbors in a HashMap.
When the player hits a helper, the helper should solve the maze and show the correct path.
I'm trying to find the endpoint by getting the current location of the helper and storing it's neighbors fields in a stack.
This is the code as far as helper goes.
The loop never ends, and I don't know why?
public void findRoute() {
Collection<Field> c = currentField.getHashMap().values();
Stack<Field> fieldNeighbours = new Stack<Field>();
for (Field field : c) {
fieldNeighbours.push(field);
}
while (!endpointReached) {
Field p = fieldNeighbours.pop();
if (p.getElement().getNaam().equals("endPoint")) {
System.out.println("endPoint Reached! ");
endpointReached = true;
return;
}
if (!p.getElement().getNaam().equals("Wall")) {
if (!p.getHashMap().get("north").getElement().getNaam().equals("Wall")) {
fieldNeighbours.push(p.getHashMap().get("north"));
}
if (!p.getHashMap().get("south").getElement().getNaam().equals("Wall")) {
fieldNeighbours.push(p.getHashMap().get("south"));
}
if (!p.getHashMap().get("east").getElement().getNaam().equals("Wall")) {
fieldNeighbours.push(p.getHashMap().get("east"));
}
if (!p.getHashMap().get("west").getElement().getNaam().equals("wall")) {
fieldNeighbours.push(p.getHashMap().get("west"));
}
}
}
In addition to the stack you need a set of already-visited cells to avoid searching cycles repeatedly.
I'll also point out that using hashes rather than simple references is bad design for most purposes.

Problems with asynchronous methods

I'm working with Flex 4 invoking webservices from JVM 1.6
I'm trying to make asynchronous calls to java to populate three comboboxes that will display country, states and cities, these three are related (master-slave relationship) but sometimes the calls are not met, or they are not completed, and I think this is because they are asynchronous, how can I be sure that I'm making the call to fill the next combobox when the master combobox (country) is filled?
protected function comboCountry_changeHandler(idCombo:String):void
{
selectedComboCountry= idCombo;
var countryId:String;
if(selectedComboCountry == comboCountry.id){
countryId = String(comboCountry.selectedItem.countryId);
}else if(selectedCombocountry == combocountrySuc.id){
countryId = String(comboCountrySuc.selectedItem.countryId);
}
obtainStatesResult.token = wsfacturas.obtainStates(countryId);
}
protected function obtainStatesResult_resultHandler(event:ResultEvent):void
{
var StateListVo:ArrayCollection = obtainStatesResult.token.result as ArrayCollection;
if(selectedComboCountry == "comboCountrySuc"){
StateListsSuc.removeAll();
CityListsSuc.removeAll();
for (var d:int = 0; d < StateListVo.length; d++){
var estSuc:State = StateListVo[d];
StateListsSuc.addItem(estSuc);
}
comboStateSuc.dataProvider = StateListsSuc;
}
else if(selectedCombocountry == "combocountry"){
StateListsEmp.removeAll();
CityListsEmp.removeAll();
for (var i:int = 0; i < StateListVo.length; i++){
var estEmp:State = StateListVo[i];
StateListsEmp.addItem(estEmp);
}
comboState.dataProvider = StateListsEmp;
} else {
for (var f:int = 0; f < StateListVo.length; f++){
var est:State = StateListVo[f];
StateListsSuc.addItem(est);
StateListsEmp.addItem(est);
}
comboState.dataProvider = StateListsEmp;
comboStateSuc.dataProvider = StateListsSuc;
}
}
Wouldn't it mean that you probably need to load the country and wait for change event on the country combobox to update state and so on? If you do that, you dont have to worry about asynchronicity of your requests? Otherwise, you might be able to use DataProviders and they might provide Event:Complete..
I am not sure though, I definitely am not an expert.. :)
You might want to give slightly more details in your question. But I am just playing with whatever details you have provided.
1_ First asynchronous calls never caused me any problem with dynamic data binding. Sometimes
a SOAP call can take reasonable amount of time, in that case, make sure to show a busy cursor till the web service call returns.
2_Combobox has always had problem with dynamic data binding. You can create a custom combobox that extends a combobox and override the setValue method Or Else you have to iterate over the dataProvider of the combobox and look for a match in the data field and then sets the combo box to that item.
So in summary, the problem mostly have to do with binding of the new data to the rendered combobox not asynchronous calls or any latency.

SmartGWT - Update ListGridRecord dynamically

I am using SmartGWT and I have a ListGrid populated with an array of ListGridRecords using the setData() call. I am trying to update a progress property of a single record (on a timer for testing) and have it update in the browser. I have tried various combinations of draw(), redraw(), markForRedraw() etc. to no avail.
I also tried overriding the updateRecordComponent() method in my table class, but it only gets called when the records are first created (after createRecordComponent()).
I should note that I do NOT want to accomplish this by binding to a DataSource. I just want to be able to update the attribute on the client-side.
ArrayList<SegmentSortRecord> mRecords;
mRecords.add(new SegmentSortRecord("03312010_M001_S004"));
mRecords.add(new SegmentSortRecord("03312010_M001_S005"));
mRecords.add(new SegmentSortRecord("03312010_M001_S006"));
mRecords.add(new SegmentSortRecord("03312010_M001_S007"));
SegmentSortRecord[] records = new SegmentSortRecord[mRecords.size()];
mRecords.toArray(records);
mSortProgressTable.setData(records);
.
.
.
mTestTimer = new Timer()
{
public void run()
{
mTestPercent += 5;
if (mTestPercent <= 100)
{
mSortProgressTable.getRecord(2).setAttribute(Constants.PROGRESS_COL_NAME, mTestPercent);
//mSortProgressTable.markForRedraw();
//mSortProgressTable.redraw();
}
else
{
mTestPercent = 0;
}
}
};
...
#Override
protected Canvas createRecordComponent(final ListGridRecord aRecord, Integer aColumn)
{
String fieldName = getFieldName(aColumn);
// Want to override the behavior for rendering the "progress" field
if (fieldName.equals(Constants.PROGRESS_COL_NAME))
{
Progressbar bar = new Progressbar();
bar.setBreadth(10);
bar.setLength(100);
// The JavaScript record object contains attributes that we can
// access via 'getAttribute' functions.
bar.setPercentDone(aRecord.getAttributeAsInt(Constants.PROGRESS_COL_NAME));
return bar;
}
Thanks in advance for any help.
I solved the dynamic update with:
grid.getRecord(i).setAttribute(name, value);
grid.refreshRow(i);
resp.
grid.refreshCell(i, j);
ListGrid has an updateData method, where you can pass a record. Have you tried it?
a blunt and not very high-performance way is simply setting your records array again with setData or setRecords
grid.setData(recordArr);
I use that in my app, but only because all records are updated anyway.
BTW: you could set up a clientside datasource
dataSource.setClientOnly(true);
dataSource.setTestData(...);

Categories

Resources