How to define a max limit of selected items in TwinColSelect - java

I want to define a maximum number of selected items in TwinColSelect in my vaadin ui. How can I achieve it. I want to allow the user to select only 4 planets out of available 8.My current code is
TwinColSelect table = new TwinColSelect();
table.setImmediate(true);
String planets[] = {"Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};
for (int pl=0; pl<planets.length; pl++)
table.addItem(planets[pl]);
table.addListener(new Property.ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
/*String [] arr=(String [] )event.getProperty().getValue();
if(arr.length>=4){
return;
}*/
}
});

You can implement something like this by changing select's value:
//Constant declared somewhere in the class
private static final int SELECTED_ITEMS_LIMIT = 4;
...
table.addListener(new Property.ValueChangeListener() {
#Override
public void valueChange(ValueChangeEvent event) {
Collection selectedItems = (Collection) event.getProperty().getValue();
if(selectedItems.size()>SELECTED_ITEMS_LIMIT) { //Set no more than SELECTED_ITEMS_LIMIT values
Set<String> noMoreThanLimitItems = new HashSet<String>(selectedItems.size());
int counter = 0;
for(Object item : selectedItems) {
if(counter>=SELECTED_ITEMS_LIMIT) {
break;
}
noMoreThanLimitItems.add((String) item);
counter++;
}
event.getProperty().setValue(noMoreThanLimitItems);
}
}
});
Drawbacks:
Code is server-side. So, more than four planets might appear as selected between browser requests.
Call to Property.setValue fires ValueChangeEvent. So, handling code will be executed twice every time when user tries to select more than four planets

Thanks to default locale in advance. Yesterday i made some eddits to the above code which remove the first drawback mentioned in the other answer, as well as changed the add which became decrepated.
The answer for Vaadin 7 would be as follows then
table.addValueChangeListener(new Property.ValueChangeListener() {
#Override
public void valueChange(ValueChangeEvent event) {
Collection selectedItems = (Collection) event.getProperty().getValue();
if(selectedItems.size()>SELECTED_ITEMS_LIMIT) { //Set no more than SELECTED_ITEMS_LIMIT values
Set<String> noMoreThanLimitItems = new HashSet<String>(selectedItems.size());
int counter = 0;
for(Object item : selectedItems) {
if(counter>=SELECTED_ITEMS_LIMIT) {
break;
}
noMoreThanLimitItems.add((String) item);
counter++;
}
event.getProperty().setValue(noMoreThanLimitItems);
//cast to twincol to get the needed functions
//markasdirty makes the object be repainted, so you wil not see more then 4 selected
((TwinColSelect)event.getProperty()).markasdirty();
}
}
});

Related

Setting ChoiceBox item too slow in javafx

i have a problem with setting item in ChoiceBox, so basicly i must load data from a database i do it in another thread :
final Service<ObservableList<Country>> countryService = new Service<ObservableList<Country>>() {
#Override
protected Task<ObservableList<Country>> createTask() {
return new Task<ObservableList<Country>>() {
#Override
protected ObservableList<Country> call() throws Exception {
Dao<Country, Integer> countriesDao = null;
List<Country> result = null;
try {
countriesDao = DaoManager.createDao(Connection.getNewInstance(), Country.class);
System.out.println("getting data");
result = countriesDao.queryForAll();
System.out.println("got data");
} catch (SQLException ex) {
Logger.getLogger(ListClientsController.class.getName()).log(Level.SEVERE, null, ex);
}
return FXCollections.observableArrayList(result);
}
};
}
};
countryService.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
#Override
public void handle(WorkerStateEvent event) {
// taking a lot of time here like 4-5 second and freeze
// the gui(normal because it executed in Javafx Application Thread
// but why its take so much time??
cbSearchCountry.setItems(countryService.getValue());
}
});
countryService.start();
Normally database access should take a time longer that setting a list to a ChoiceBox, but no here fetching 150 record from my database is instantaneous but settings observable list to my ChoiceBox take about 5 seconds why?
because i have too much Node in my current Scene??
Use ChoiceBox only if you have < 10 items, else use ComboBox

Filtering latest tweets

I am trying to filter the latest tweets with the words sad, okay, joyful.
When a tweet which has either word inside it, I would like it to print that tweet. But I would also like there to be a delay in the tweets which are printed. So about a 10 second delay between each tweet. For example:
If a tweet comes through : #joker im so sad today
Then I want that to print to the screen, and to the following message
System.out.println("*************************a sad tweet");
Then if a tweet comes through after this: #programmer im joyful
Then 10 seconds after the last tweet I want that tweet to come through with the following message drawn to the screen.
System.out.println("*************************a joyful tweet");
And so on.
Below, I have made some code which allows you to filter the tweets, but im unsure of how to test and print a separate message for each tweet. I tried storing this in an Arraylist and retrieving each tweet with a message, but this doesn't work. Is there a way to do this?
Im using processing 2 and twitter4j 3
Any suggestions? Solutions
void GetTweetsByKeywords()
{
List<String>mood = new ArrayList <String>();
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("XXXX");
cb.setOAuthConsumerSecret("XXX");
cb.setOAuthAccessToken("XXXX");
cb.setOAuthAccessTokenSecret("XXXX");
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
StatusListener statusListener = new StatusListener()
{
private int count = 0;
private long originalTweetId = 0;
#Override
public void onStatus(Status status)
{
System.out.println(status.getUser().getName() + " : " + status.getText());
} //en of the onStatus()
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice)
{
// should really remove deleted tweets here ...
}
public void onTrackLimitationNotice(int numberOfLimitedStatuses)
{
}
public void onScrubGeo(long userId, long upToStatusId) {
// should really remove deleted location information here ...
}
public void onStallWarning(StallWarning stallWarning) {
// should really do something about stalls here ...
}
#Override
public void onException(Exception ex)
{
ex.printStackTrace();
}
}; //end of the listener
String keywords[] = {"sad","okay","joyful"};
for(int i=0; i<keywords.length; i++)
{
FilterQuery fq = new FilterQuery();
fq.track(keywords);
twitterStream.addListener(statusListener);
twitterStream.filter(fq);
mood.add(//here i want to add the filtered tweets);
System.out.println("Heres a filter :" + mood.get(i));
if (mood.get(i).equals("sad"))
{
System.out.println("*********************************************a sad tweet");
}
else if (mood.get(i).equals("joyful"))
{
System.out.println("*********************************************a joyfull tweet");
}
else if(mood.get(i).equals("okay"))
{
System.out.println("*********************************************okay tweet");
}
}
}
The easiest is to use a separate thread and a queue. Do this:
final Queue<Status> queue = new LinkedBlockingQueue<Status>(10000);
new Thread(){
// In your status listener, post tweets to the queue
...
public void onStatus(Status status){
queue.offer(status);
}
...
// Create TwitterStream instance, add query
// and start listening
twitterStream.filter(fq);
}.start();
while(!Thread.currentThread().isInterrupted()){
Status nextTweet = queue.take();
System.out.println("Do stuff with tweet");
}

GXT-3 issue to load data for my Chart

My problem is annoying. My server side is generating 12 random numbers (double here).
My Client side received the correct data but nothing is displayed in my Chart. That worked fine with hardcoded data in the store but not with a REST call.
The transfer between my server and my client is that :
[{"key":"key0","value":0.47222548599297787},{"key":"key1","value":0.6009173797369691},{"key":"key2","value":0.13880104282435624},{"key":"key3","value":0.01804674319345545},{"key":"key4","value":0.5547733564202956},{"key":"key5","value":0.8229999661308851},{"key":"key6","value":0.8959346004391032},{"key":"key7","value":0.6848052288628435},{"key":"key8","value":0.10222856671111813},{"key":"key9","value":0.6931371931409103},{"key":"key10","value":0.2994297934549003},{"key":"key11","value":0.47566752196381334}]
Here my simple class used for my test. I am a newbie with GXT 3
public void onModuleLoad() {
final ListStore<JSOModel> store;
final ContentPanel panel = new FramedPanel();
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, "/ws/DocumentService/v1/test");
builder.setHeader("Accept", "application/json");
HttpProxy proxy = new HttpProxy(builder);
final Loader<ListLoadConfig, ListLoadResult<JSOModel>> loader = new ListLoader<ListLoadConfig, ListLoadResult<JSOModel>>(proxy, new DataReader<ListLoadResult<JSOModel>, String>() {
#Override
public ListLoadResult<JSOModel> read(Object loadConfig, String data) {
List<JSOModel> jsoModels = new ArrayList<JSOModel>();
JsArray<JSOModel> jsoModelJsArray = JSOModel.arrayFromJson(data);
if(jsoModelJsArray != null) {
for(int i = 0; i < jsoModelJsArray.length(); i++) {
jsoModels.add(jsoModelJsArray.get(i));
}
}
return new ListLoadResultBean<JSOModel>(jsoModels);
}
});
store = new ListStore<JSOModel>(new ModelKeyProvider<JSOModel>() {
#Override
public String getKey(JSOModel item) {
return item.get("key");
}
});
loader.addLoadHandler(new LoadResultListStoreBinding<ListLoadConfig, JSOModel, ListLoadResult<JSOModel>>(store) {
#Override
public void onLoad(LoadEvent<ListLoadConfig, ListLoadResult<JSOModel>> event) {
ListLoadResult<JSOModel> loaded = event.getLoadResult();
if(loaded.getData() == null) {
store.replaceAll(new ArrayList<JSOModel>());
} else {
store.replaceAll(loaded.getData());
}
}
});
Chart<JSOModel> chart = new Chart<JSOModel>();
chart.setStore(store);
chart.setShadowChart(true);
NumericAxis<JSOModel> axis = new NumericAxis<JSOModel>();
axis.setPosition(Chart.Position.LEFT);
axis.addField(new ValueProvider<JSOModel, Number>() {
#Override
public Number getValue(JSOModel JSOModel) {
return JSOModel.getNumber("value");
}
#Override
public void setValue(JSOModel JSOModel, Number number) {
}
#Override
public String getPath() {
return "key";
}
});
axis.setTitleConfig(new TextSprite("Number of hits"));
axis.setWidth(50);
axis.setMinimum(0);
axis.setMaximum(100);
chart.addAxis(axis);
PathSprite odd = new PathSprite();
odd.setOpacity(1);
odd.setFill(new Color("#dff"));
odd.setStroke(new Color("#aaa"));
odd.setStrokeWidth(0.5);
axis.setGridOddConfig(odd);
CategoryAxis<JSOModel, String> horizontalAxis = new CategoryAxis<JSOModel, String>();
horizontalAxis.setPosition(Chart.Position.BOTTOM);
horizontalAxis.setField(new ValueProvider<JSOModel, String>() {
#Override
public String getValue(JSOModel JSOModel) {
return JSOModel.get("key");
}
#Override
public void setValue(JSOModel JSOModel, String s) {
}
#Override
public String getPath() {
return "key";
}
});
horizontalAxis.setTitleConfig(new TextSprite("month of year"));
chart.addAxis(horizontalAxis);
LineSeries<JSOModel> column = new LineSeries<JSOModel>();
column.setYAxisPosition(Chart.Position.LEFT);
column.setStroke(new RGB(148,174,10));
column.setHighlighting(true);
chart.addSeries(column);
axis.addField(column.getYField());
chart.addSeries(column);
chart.setHeight(100);
chart.setWidth(100);
Button b = new Button("ha");
b.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent clickEvent) {
loader.load();
}
});
RootPanel.get().add(b);
panel.setCollapsible(true);
panel.setHeadingText("Column Chart");
panel.setPixelSize(620, 500);
panel.setBodyBorder(true);
VerticalLayoutContainer layout = new VerticalLayoutContainer();
panel.add(layout);
chart.setLayoutData(new VerticalLayoutContainer.VerticalLayoutData(1,1));
layout.add(chart);
chart.setBackground(new Color("#dff"));
RootPanel.get().add(panel);
There are two ways to wire the chart into a store. One is to simply specify that the chart is using a store via setStore, as you have done:
chart.setStore(store);
When you do this, you must also inform the chart when it must redraw everything - you must call:
chart.redrawChart();
This call must be made shortly after the load is completed - consider doing it at the end of onLoad.
Why is this required? In some cases, developers want to make many changes to the store, one at a time, and if the chart automatically updated after each change, that would spawn many slow changes to the data model, and could end up looking strange. In a case like this, you would only call redrawChart() after all changes were complete.
There is another option however - instead of calling setStore, you can call bindStore, and ask the Chart to automatically update whenever any change occurs to the chart:
chart.bindStore(store);
In your case, this is likely the correct answer.

model is getting updated but pageablelistview not reflecting the change on UI + wicket

So I have this button on submitting it there is some functioning which will be performed and the selected checkbox needed to be removed from the list.
The piece of code below shows the usage of that button and the implementation I have done to remove the particular checkbox selection.
Button resumeDrive = new AjaxButton("resume", driveSearchForm)
/**
*
*/
private static final long serialVersionUID = -7016746377299867219L;
#Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
log.info("envoking resume");
target.addComponent(form);
try {
List<DashboardModel> list = (List<DashboardModel>) group.getDefaultModelObject();
log.info("drives data : " + list);
if (list != null) {
List<Long> drives = new ArrayList<Long>();
List<DashboardModel> drivesToRemove = new ArrayList<DashboardModel>();
for (DashboardModel drive : list) {
drives.add(drive.getExecutionUnitId());
drivesToRemove.add(drive);
log.info("drivesToRemove :" + drivesToRemove);
}
log.info("selected drive: " + drives);
if (drives.size() > 0) {
log.info("Execution Ids to resume : " + drives);
driveResumeService.resumeDrives(drives);
drivesData.removeAll(drivesToRemove);
log.info("drivesdata :" + drivesData);
warn("Execution Ids to resume : " + drives);
} else {
warn("No drives selected for resuming.");
}
} else {
info("No drives to resume.");
}
} catch (Exception e) {
warn("Failed to resume jobs. " + e.getMessage());
log.info("Failed to resume jobs", e);
}
target.addComponent(group);
}
#Override
protected void onError(AjaxRequestTarget target, Form<?> form) {
target.addComponent(form);
}
driveSearchForm.add(resumeDrive);
resumeDrive.setDefaultFormProcessing(true);
resumeDrive.add(new AjaxFormValidatingBehavior(driveSearchForm, "onClick"));
--> my model returns this driveData.
---> I'm sticking the pageableListView code also alongside.
private CheckGroup<DashboardModel> group;
group = new CheckGroup<DashboardModel>("group", new ArrayList<DashboardModel>());
driveSearchForm.add(group);
group.add(new CheckGroupSelector("allSelected"));
group.setOutputMarkupId(true);
pageableListView = new PageableListView<DashboardModel>("searchResults", driveDataModel, 10) {
private static final long serialVersionUID = 1L;
#Override
protected void populateItem(ListItem<DashboardModel> item) {
item.add(new Check("check", item.getModel()));
item.add(new Label("name", item.getModelObject().getName()));
item.add(new Label("status", item.getModelObject().getStatus().toString()));
item.add(new Label("driveUrl", item.getModelObject().getDriveURL()));
}
};
pageableListView.setRenderBodyOnly(false);
pageableListView.setReuseItems(true);
group.add(pageableListView);
group.add(new PagingNavigator("navigator", pageableListView));
So I found that my models are getting updated but the same is not happening on the UI i.e.
I want the selected checkboxes to be removed from the refreshed list.....
Please suggest......
From ListView#setReuseItems():
But if you modify the listView model object, than you must manually call listView.removeAll() in order to rebuild the ListItems.
So if reuseItems is true, then at the point where you change your model call removeAll on the listview.

Eclipse JFace's Wizards

I need a wizard which second page content depends on the first page's selection. The first page asks the user the "kind" of filter he wants to create and the second one asks the user to create one filter instance of the selected "kind".
JFace's wizards pages contents (createControl(...) method) are all created when the wizard is open and not when a given page is displayed (this allow JFace to know the wizard size I guess ??).
Because of this, I have to create my second page content BEFORE the wizard is opened BUT I can't since the second page's content depends on the first page selection.
For now the cleaner solution I found consists in creating all (seconds) pages before the wizard is open (with their content) and override the getNextPage() method in the first page's implementation.
The main drawback of that solution is that it can be be expensive when there are many second pages to create.
What do you think about that solution ? How do you manage your wizard's pages ? Is there any cleaner solution I missed ?
The approach is right if you are several other pages which are
completely different one with another
depends on the previous choices made in a previous page
Then you can add the next page dynamically (also as described here)
But if you have just a next page with a dynamic content, you should be able to create that content in the onEnterPage() method
public void createControl(Composite parent)
{
//
// create the composite to hold the widgets
//
this.composite = new Composite(parent, SWT.NONE);
//
// create the desired layout for this wizard page
//
GridLayout layout = new GridLayout();
layout.numColumns = 4;
this.composite.setLayout(layout);
// set the composite as the control for this page
setControl(this.composite);
}
void onEnterPage()
{
final MacroModel model = ((MacroWizard) getWizard()).model;
String selectedKey = model.selectedKey;
String[] attrs = (String[]) model.macroMap.get(selectedKey);
for (int i = 0; i < attrs.length; i++)
{
String attr = attrs[i];
Label label = new Label(this.composite, SWT.NONE);
label.setText(attr + ":");
new Text(this.composite, SWT.NONE);
}
pack();
}
As shown in the eclipse corner article Creating JFace Wizards:
We can change the order of the wizard pages by overwriting the getNextPage method of any wizard page.Before leaving the page, we save in the model the values chosen by the user. In our example, depending on the choice of travel the user will next see either the page with flights or the page for travelling by car.
public IWizardPage getNextPage(){
saveDataToModel();
if (planeButton.getSelection()) {
PlanePage page = ((HolidayWizard)getWizard()).planePage;
page.onEnterPage();
return page;
}
// Returns the next page depending on the selected button
if (carButton.getSelection()) {
return ((HolidayWizard)getWizard()).carPage;
}
return null;
}
We define a method to do this initialization for the PlanePage, onEnterPage() and we invoke this method when moving to the PlanePage, that is in the getNextPage() method for the first page.
If you want to start a new wizard based on your selection on the first page, you can use the JFace base class org.eclipse.jface.wizard.WizardSelectionPage.
The example below shows a list of available wizards defined by an extension point.
When you press Next, the selected wizard is started.
public class ModelSetupWizardSelectionPage extends WizardSelectionPage {
private ComboViewer providerViewer;
private IConfigurationElement selectedProvider;
public ModelSetupWizardSelectionPage(String pageName) {
super(pageName);
}
private class WizardNode implements IWizardNode {
private IWizard wizard = null;
private IConfigurationElement configurationElement;
public WizardNode(IConfigurationElement c) {
this.configurationElement = c;
}
#Override
public void dispose() {
}
#Override
public Point getExtent() {
return new Point(-1, -1);
}
#Override
public IWizard getWizard() {
if (wizard == null) {
try {
wizard = (IWizard) configurationElement
.createExecutableExtension("wizardClass");
} catch (CoreException e) {
}
}
return wizard;
}
#Override
public boolean isContentCreated() {
// TODO Auto-generated method stub
return wizard != null;
}
}
#Override
public void createControl(Composite parent) {
setTitle("Select model provider");
Composite main = new Composite(parent, SWT.NONE);
GridLayout gd = new GridLayout(2, false);
main.setLayout(gd);
new Label(main, SWT.NONE).setText("Model provider");
Combo providerList = new Combo(main, SWT.NONE);
providerViewer = new ComboViewer(providerList);
providerViewer.setLabelProvider(new LabelProvider() {
#Override
public String getText(Object element) {
if (element instanceof IConfigurationElement) {
IConfigurationElement c = (IConfigurationElement) element;
String result = c.getAttribute("name");
if (result == null || result.length() == 0) {
result = c.getAttribute("class");
}
return result;
}
return super.getText(element);
}
});
providerViewer
.addSelectionChangedListener(new ISelectionChangedListener() {
#Override
public void selectionChanged(SelectionChangedEvent event) {
ISelection selection = event.getSelection();
if (!selection.isEmpty()
&& selection instanceof IStructuredSelection) {
Object o = ((IStructuredSelection) selection)
.getFirstElement();
if (o instanceof IConfigurationElement) {
selectedProvider = (IConfigurationElement) o;
setMessage(selectedProvider.getAttribute("description"));
setSelectedNode(new WizardNode(selectedProvider));
}
}
}
});
providerViewer.setContentProvider(new ArrayContentProvider());
List<IConfigurationElement> providers = new ArrayList<IConfigurationElement>();
IExtensionRegistry registry = Platform.getExtensionRegistry();
IExtensionPoint extensionPoint = registry
.getExtensionPoint(<your extension point namespace>,<extension point name>);
if (extensionPoint != null) {
IExtension extensions[] = extensionPoint.getExtensions();
for (IExtension extension : extensions) {
IConfigurationElement configurationElements[] = extension
.getConfigurationElements();
for (IConfigurationElement c : configurationElements) {
providers.add(c);
}
}
}
providerViewer.setInput(providers);
setControl(main);
}
The corresponding wizard class looks like this:
public class ModelSetupWizard extends Wizard {
private ModelSetupWizardSelectionPage wizardSelectionPage;
public ModelSetupWizard() {
setForcePreviousAndNextButtons(true);
}
#Override
public boolean performFinish() {
// Do what you have to do to finish the wizard
return true;
}
#Override
public void addPages() {
wizardSelectionPage = new ModelSetupWizardSelectionPage("Select a wizard");
addPage(wizardSelectionPage);
}
}
Another alternative is to #Override setVisible. You can update page values or add additional widgets at that time.
I have a different solution.
If page depends on the result of page 1, create a variable and pass it into to first page, when that wizard page has the option from the user, then the last thing before the page is closed is to set the variable to the required value.
Then pass this variable to wizard, then pass it to the next wizard page. Then do a simple if statement and that way you get both choices together.
Remember that in most code there is only a small difference in the user options, so remember not to get bogged down in duplicating your code.

Categories

Resources