Problems with grid column rendering in Vaadin - rendering

I am using a Grid to display books information. Those books a historical novels and they are sorted by the year in which action is occurred. My first column in the grid is this year. Book object has this year as integer where positive numbers are AD and negative are BC. I am rendering this column with following class:
public class ActionYearRenderer extends ComponentRenderer<Div, Book> {
private Integer lastYear = Integer.MIN_VALUE;
public void reset() {
this.lastYear = Integer.MIN_VALUE;
}
#Override
public Div createComponent(Book item) {
Integer actionYear = item.getActionYear();
Div result = new Div();
if(!Objects.equals(actionYear, lastYear)) {
lastYear = actionYear;
if(actionYear < -10000) {
result.setText("Prehistoric times");
} else if (actionYear >= 0) {
result.setText("year " + actionYear.toString());
} else {
result.setText("year " + Integer.toString(Math.abs(actionYear)) + " B.C.E.");
}
}
result.getStyle().set("word-wrap", "normal");
return result;
}
}
I need to display a year only once for the all books referring to a single year. It is working almost as I wanted it to do. It displays everything correctly at the beginning, but I also have ItemDetailsRenderer, which opens book annotation and review. When details are closed, action year disappears from the grid. I presume, that the item is being re-rendered and it is causing this behavior. How can I preserve the content of this cell?

Related

FastObjectListView UpdateObject() randomly reorders rows within primary sort

Data is a generic List of domain objects.
I click the "Deploy Status" column header to sort on that column.
I have a button that does nothing more than folv.UpdateObject(someObject) .
Every time I press that button, the Deploy Status column maintains its sort, but all rows within the sorted blocks are randomly reordered, as per screenshot.
I have commented out everything in the form's code beyond loading the data, the test button, and the FastObjectListView's column.Add() and .SetObjects(). There are no event handlers wired up for the FastObjectListView. I am not setting PrimarySort or SecondarySort in code; only by clicking with the mouse.
You should be able to fix this problem by either calling Sort after your button's call to UpdateObject or changing your usage of UpdateObject to RefreshObject
Reproducing the problem (C# Repro for the issue in the API)
This seems to reproduce the problem you are having. Run the code, sort the Other column ascending. Click the update button.
public class MainForm : Form
{
public MainForm()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(300, 300);
this.Name = "MainForm";
this.ResumeLayout(false);
this.PerformLayout();
var OLVa = new FastObjectListView();
OLVa.Width = 250;
OLVa.Height = 250;
OLVa.Columns.Add(new OLVColumn("ID", "ID"));
OLVa.Columns.Add(new OLVColumn("Other", "Other"));
var l1 = new lolz(1, 3);
OLVa.AddObject(l1);
OLVa.AddObject(new lolz(2,3));
this.Controls.Add(OLVa);
var btn = new Button()
{
Text = "Update",
Top = OLVa.Bottom
};
btn.Click += (s,e)=>OLVa.UpdateObject(l1);
this.Controls.Add(btn);
}
private class lolz
{
public int ID;
public int Other;
public lolz(int id, int other)
{
ID = id;
Other = other;
}
}
}
Fixing the problem
The following would fix it for the above example:
btn.Click += (s,e)=>
{
OLVa.BeginUpdate();
try
{
OLVa.UpdateObject(l1);
OLVa.Sort();
}
finally
{
OLVa.EndUpdate();
}
};

Adding quantity if the product is exist in the DataGridView

Can someone help me with my problem?
The picture below shows the same item I inputted. What I want is I don't like to show duplicate items in DataGridView. If the same product record adds, then the new will not be showed, it just add the quantity when clicking the "Save" button. And I don't know how to code it I'm just new to vb.net. Can somebody help me how to do it?? It would be a big help for me if you do, thank you so much!
Below is my code for Save button:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
initializeCon()
Dim found As Boolean = False
If (DataGridView1.Rows.Count > 0) Then
For Each row As DataGridViewRow In DataGridView1.Rows
If (Convert.ToString(row.Cells(1).Value) = dtDate.Text) And (Convert.ToString(row.Cells(2).Value) = txtProductCode.Text) AndAlso
(Convert.ToString(row.Cells(3).Value) = txtProductName.Text) Then
row.Cells(4).Value = Convert.ToString(txtQuantity.Text + Convert.ToInt16(row.Cells(4).Value))
found = True
End If
Next
If Not found Then
cmd = New SqlCommand("INSERT INTO tbl_productOrders VALUES('" & txtID.Text & "','" & dtDate.Text & "','" & txtProductCode.Text & "','" & txtProductName.Text & "'," & txtQuantity.Text & ");", con)
cmd.ExecuteNonQuery()
clrtxt()
SaveMsg()
Getdata()
End If
End If
End Sub
this is an axample to avoid duplicates while adding rows
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim exist As Boolean = False, numrow As Integer = 0, numtext As Integer
For Each itm As DataGridViewRow In DataGridView1.Rows
If itm.Cells(0).Value IsNot Nothing Then
If itm.Cells(0).Value.ToString = TextBox1.Text Then
exist = True
numrow = itm.Index
numtext = CInt(itm.Cells(1).Value)
Exit For
End If
End If
Next
If exist = False Then
DataGridView1.Rows.Add(New String() {TextBox1.Text, TextBox2.Text})
Else
DataGridView1.Rows(numrow).Cells(1).Value = CInt(TextBox2.Text) + numtext
End If
End Sub
There are two aspects to improve your program.
1. Make comparing products better
Since you need to check individual properties to find equal product, the better way is to overload == operator along with implementing IEquatable interface. You can read more here. When you do this, you can compare products with == operator: if (product1 == product2) { }. In this case three properties are being compared. If they all are the same, then two products are equal.
2. Make adding products to DataGridView easier
In order to make adding products to DataGridView easier, you need to leverage the handy binding mechanism in Windows Forms, which appeared in .NET Framework 2.0 - BindingSource. It acts like a medium between your data and controls. In order to make this class usable, you need to use another handy class - BindingList. You feed BindingList to BindingSource and assign BindingSource to DataSource property of DataGridView. After that you work only with BindingList collection (adding/removing/editing) - and all the propagation is done by BindinSource.
To summarize, here is the explanation and full code. Here you can download the project itself.
When you add product, the code first checks whether such product already exists in DataGridView (Add product button). Do note that we don't work with DataGridView directly - we work only with the collection. Thanks to enhancements, we can use LINQ here. If the product is not found, we add it to collection. If it's found, we just update the quantity.
If you need to update selected product data (Change quantity button), you only need to retrieve it from selected rows and use BoundDataItem - this is where our product is located. Then just update properties of product you need.
If you need to remove product (Delete product button), retrieve it from selected row and delete it from the collection.
Important! Do not forget to call Refresh() method on DataGridView after all taken actions in order to see the changes.
namespace WinFormsApp
{
public partial class Root : Form
{
private BindingList<Product> list = null;
private BindingSource bindingSource = null;
public Root() => InitializeComponent();
private void OnFormLoaded(object sender, EventArgs e)
{
// Our collection holding products
list = new BindingList<Product>();
// I've set up columns manually (with applied binding),
// so switch generating columns off.
dataGrid.AutoGenerateColumns = false;
// Disable adding new row
dataGrid.AllowUserToAddRows = false;
// Create our medium between grid and collection
bindingSource = new BindingSource { DataSource = list };
// Set binding
dataGrid.DataSource = bindingSource;
}
private void OnAddRecord(object sender, EventArgs e)
{
// Create new product
var new_product = new Product
{
Date = dtPicker.Value.ToShortDateString(),
Code = txtCode.Text,
Name = txtName.Text,
Quantity = npQuantity.Value
};
// No need to check individual properties here
// as == and IEquatable will do all the work.
// We can safely use LINQ here.
var p = list.FirstOrDefault(x => x == new_product);
if (p == null)
{
// Product is not found. Add it to list.
list.Add(new_product);
}
else
{
// Product is found. Update quantity.
p.Quantity += new_product.Quantity;
}
dataGrid.Refresh(); //Required to reflect changes
}
private void OnChangeQuantity(object sender, EventArgs e)
{
// Change quantity here.
var product = GetProduct();
if (product != null)
{
// Update product's quantity.
product.Quantity *= 2;
// Do not forget to refresh grid.
dataGrid.Refresh();
}
}
private void OnDeleteProduct(object sender, EventArgs e)
{
// Delete product here.
var product = GetProduct();
if (product != null)
{
// We need to delete product only from collection
list.Remove(product);
// Do not forget to refresh grid
dataGrid.Refresh();
}
}
// Retrieve product from selected row.
private Product GetProduct() =>
dataGrid.SelectedCells.Count == 0 ?
null :
dataGrid.SelectedCells[0].OwningRow.DataBoundItem as Product;
}
class Product : IEquatable<Product>
{
public string Date { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public decimal Quantity { get; set; }
// Overload == operator
public static bool operator ==(Product firstProduct, Product secondProduct)
{
// Check for null on left side.
if (Object.ReferenceEquals(firstProduct, null))
{
if (Object.ReferenceEquals(secondProduct, null))
{
// null == null = true.
return true;
}
// Only the left side is null.
return false;
}
// Equals handles case of null on right side.
return firstProduct.Equals(secondProduct);
}
// Overload != operator (required when overloading == operator)
public static bool operator !=(Product firstProduct, Product secondProduct) =>
!(firstProduct == secondProduct);
// Implementing IEquatable<T> interface
public bool Equals(Product other)
{
// If 'other' is null, return false.
if (Object.ReferenceEquals(other, null))
{
return false;
}
// Optimization for a common success case.
if (Object.ReferenceEquals(this, other))
{
return true;
}
// If run-time types are not exactly the same, return false.
if (this.GetType() != other.GetType())
{
return false;
}
// Return true if the fields match.
return
Date == other.Date &&
Code == other.Code &&
Name == other.Name;
}
public override bool Equals(object obj) => this.Equals(obj as Product);
public override int GetHashCode() =>
Date.GetHashCode() + Code.GetHashCode() + Name.GetHashCode();
// Optional. For debugging purposes.
public override string ToString() =>
$"Date: {Date}, Code: {Code}, Name: {Name}, Quantity: {Quantity}";
}
}

javafx format cells from ArrayList

I am working on a calendar application that shall retrieve date values from an ArrayList, which retrieves the string values from a SQL database. Formatting is achieved through a day cell factory.
Problem is that not only the dates from the ArrayList are formatted, by the cells of the whole month have a green background. Anyone has a hint of what I am doing wrong?
I started programming a year ago with Python and I am working with JavaFx for only a couple of months now, so my experiences with ArrayLists and cell factories are still quite limited.
Any help would be greatly appreciated. Here is what I have so far ....
ObservableList<LocalDate> dates = FXCollections.observableArrayList();
LocalDate date_1 = LocalDate.parse("2017-05-20");
LocalDate date_2 = LocalDate.parse ("2017-05-18");
dates.add(date_1);
dates.add(date_2);
Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, DateCell>() {
public DateCell call (final DatePicker datePicker ) {
return new DateCell() {
#Override
public void updateItem (LocalDate item , boolean empty) {
super.updateItem(item, empty);
for (LocalDate ldt : dates) {
this.setStyle("-fx-background-color: green");
this.setTextFill(Color.BLACK);
this.setTooltip(new Tooltip("hello"));
}
};
};
};
};
dp.setDayCellFactory(dayCellFactory);
DatePickerSkin datePickerSkin = new DatePickerSkin(dp);
Node popupContent = datePickerSkin.getPopupContent();
calendar_pane_1.getChildren().add(popupContent);
Your cell's updateItem(...) method:
public void updateItem (LocalDate item , boolean empty) {
super.updateItem(item, empty);
for (LocalDate ldt : dates) {
this.setStyle("-fx-background-color: green");
this.setTextFill(Color.BLACK);
this.setTooltip(new Tooltip("hello"));
}
}
sets the style of the cell so that it has a green background, no matter the value of the item that is passed to the method. (It even does it twice, since there are two elements in the dates list.) So clearly, every cell will have a green background.
You didn't state it in the question, but I'm guessing that you only want the items in your list to have the styled background. You need:
public void updateItem (LocalDate item , boolean empty) {
super.updateItem(item, empty);
if (item != null && dates.contains(item)) {
this.setStyle("-fx-background-color: green; -fx-text-fill: black ;");
this.setTooltip(new Tooltip("hello"));
} else {
this.setStyle("");
this.setTooltip(null);
}
}

How do I create a bar chart showing percentages bound to a list of objects?

Using the DataVisualization.Charting.Chart control, I need to create a bar chart (maybe stacked bar chart) that shows per person the number of hours booked for that person and a those hours' percentage of total hours. So far I am a bit overwhelmed by the number of collections and properties to set on this beast, so I'd appreciate some help on first getting my chart up, then I'll explore more on my own.
I need to bind the chart to a list of the following object:
Public Class DOHoursChartItem
Public Property Name As String
Public Property Hours As Double
Public Property Percent As Double
End Class
I'm not sure I need the percentage property here, in favour of somehow letting the chart control handle this and just give it the Hours value per point and a total hours value, but that's why I'm asking: how do I set up the chart I describe above?
I'm not very good in VB, so I will start posting an example in C# (then I can try to translate it if you really need).
Here are three examples of methods that you can use to bind your items to a mschart and get columns charts:
Example 1: single area and side-by-side columns
private void FillChartSingleArea()
{
// this set the datasource
this.chart1.DataSource = GetItems();
// clear all the (possible) existing series
this.chart1.Series.Clear();
// add the hours series
var hoursSeries = this.chart1.Series.Add("Hours");
hoursSeries.XValueMember = "Name";
hoursSeries.YValueMembers = "Hours";
hoursSeries.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
// add the percentages series
var percSeries = this.chart1.Series.Add("Percentages");
percSeries.XValueMember = "Name";
percSeries.YValueMembers = "Percent";
percSeries.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
}
Example 2: two charts one upon the other
private void FillChartDoubleArea()
{
// this set the datasource
this.chart1.DataSource = GetItems();
// clear all the (possible) existing series
this.chart1.Series.Clear();
// clear all the existing areas and add 2 new areas
this.chart1.ChartAreas.Clear();
this.chart1.ChartAreas.Add("Area1");
this.chart1.ChartAreas.Add("Area2");
// add the hours series
var hoursSeries = this.chart1.Series.Add("Hours");
hoursSeries.ChartArea = "Area1";
hoursSeries.XValueMember = "Name";
hoursSeries.YValueMembers = "Hours";
hoursSeries.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
// add the percentages series
var percSeries = this.chart1.Series.Add("Percentages");
hoursSeries.ChartArea = "Area2";
percSeries.XValueMember = "Name";
percSeries.YValueMembers = "Percent";
percSeries.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
}
Example 3: single area and stacked columns
private void FillStackedChartSingleArea()
{
// this set the datasource
this.chart1.DataSource = GetItems();
// clear all the (possible) existing series
this.chart1.Series.Clear();
// add the hours series
var hoursSeries = this.chart1.Series.Add("Hours");
hoursSeries.XValueMember = "Name";
hoursSeries.YValueMembers = "Hours";
hoursSeries.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedColumn;
// add the percentages series
var percSeries = this.chart1.Series.Add("Percentages");
percSeries.XValueMember = "Name";
percSeries.YValueMembers = "Percent";
percSeries.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedColumn;
}
Where GetItems method is defined as follows (for all the examples):
private List<DOHoursChartItem> GetItems()
{
var items = new List<DOHoursChartItem>()
{
new DOHoursChartItem("John", 120),
new DOHoursChartItem("Amanda", 40),
new DOHoursChartItem("David", 70),
new DOHoursChartItem("Rachel", 10),
};
// compute the percentages
var totalHours = items.Sum(x => x.Hours);
foreach (var item in items)
item.Percent = (item.Hours * 100.0) / totalHours;
return items;
}
and DOHoursChartItem as :
class DOHoursChartItem
{
public String Name { get; set; }
public double Hours { get; set; }
public double Percent { get; set; }
public DOHoursChartItem(string name, double hours)
{
this.Name = name;
this.Hours = hours;
}
}
N.B.
these are actually Column charts; by setting the ChartType to Bar (or StackedBar), you will get the same result but the bars will have an horizontal orientation.

Telerik Mail Merge - Friendly Names (using RadRichTextBox)

I think i'm missing something obvious...
I'm using the Telerik Rad controls for WPF but i assume that the Rich text box uses some similar implementation for the mail merge functionality.
I want to have some friendly names on my mail merge fields. (namely spaces in the field names)
So i have a class for instance
Public Class someclass
{
<DisplayName("This is the complex description of the field")>
Public property thisfieldnamehasacomplexdescription as string
Public property anothercomplexfield as string
}
This is the only way i know to get "Friendly" names in the dropdown that is the mail merge.
So the two fields turn up okay as :
"This is the complex description of the field"
"anothercomplexfield"
but only anothercomplexfield actually populates with data when you do the merge.
Am i going to have to template the raddropdownbutton that holds the mail merge fields?
Is there an example of this somewhere?
Also a sub question. How do i add a scroll bar on these things?
(also i know this board is not a TELERIK specific board (duh!) but this might be useful to someone in the future. So i'll copy the answer i get from Telerik into here!
http://www.telerik.com/community/forums/wpf/richtextbox/558428-radrichtextbox-mailmerge---using-displayname-to-create-a-friendly-name-with-spaces.aspx )
This is what telerik gave me:
With the default MergeFields, it is not possible to change the display name fragment of the field in order to achieve a more friendly look. This should be possible if you implement a custom MergeField by deriving from the MergeField class. Here is a sample implementation that shows how this can be done:
public class CustomMergeField : MergeField
{
private const string CustomFieldName = "CustomField";
static CustomMergeField()
{
CodeBasedFieldFactory.RegisterFieldType(CustomMergeField.CustomFieldName, () => { return new CustomMergeField(); });
}
public override string FieldTypeName
{
get
{
return CustomMergeField.CustomFieldName;
}
}
public override Field CreateInstance()
{
return new CustomMergeField();
}
protected override DocumentFragment GetDisplayNameFragment()
{
return base.CreateFragmentFromText(string.Format(Field.DisplayNameFragmentFormat, this.GetFriendlyFieldName(this.PropertyPath)));
}
private string GetFriendlyFieldName(string fieldName)
{
int lettersInEnglishAlphabet = 26;
List<char> separators = new List<char>(lettersInEnglishAlphabet);
for (int i = 0; i < lettersInEnglishAlphabet; i++)
{
separators.Add((char)('A' + i));
}
StringBuilder newFieldName = new StringBuilder();
int previousIndex = 0;
for (int i = 1; i < fieldName.Length; i++)
{
if (separators.Contains(fieldName[i]))
{
if (previousIndex > 0)
{
newFieldName.Append(" ");
}
newFieldName.Append(fieldName.Substring(previousIndex, i - previousIndex));
previousIndex = i;
}
}
newFieldName.Append(" " + fieldName.Substring(previousIndex));
return newFieldName.ToString();
}
}
Note that the fragment that is shown when the DisplayMode is Code cannot be changed.
As for your other question, you can change the content of the dropdown button to show the friendly name of the fields and to include a scrollbar in the following way:
1. First, remove the binding of the button to the InsertMergeFieldEmptyCommand from XAML and give it a name (e.g. insertMergeField).
2. Next, add the following code in code-behind:
AddMergeFieldsInDropDownContent(this.insertMergeFieldButton);
private void AddMergeFieldsInDropDownContent(RadRibbonDropDownButton radRibbonDropDownButton)
{
Grid grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(100, GridUnitType.Pixel) });
ScrollViewer scrollViewer = new ScrollViewer();
scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
StackPanel stackPanel = new StackPanel();
foreach (string fieldName in this.editor.Document.MailMergeDataSource.GetColumnNames())
{
RadRibbonButton fieldButton = new RadRibbonButton()
{
Text = this.GetFriendlyFieldName(fieldName),
Size = ButtonSize.Medium,
HorizontalAlignment = HorizontalAlignment.Stretch,
HorizontalContentAlignment = HorizontalAlignment.Left
};
fieldButton.Command = this.editor.Commands.InsertFieldCommand;
fieldButton.CommandParameter = new MergeField() { PropertyPath = fieldName };
//or
//fieldButton.CommandParameter = new CustomMergeField() { PropertyPath = fieldName };
stackPanel.Children.Add(fieldButton);
}
stackPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
scrollViewer.Content = stackPanel;
grid.Children.Add(scrollViewer);
radRibbonDropDownButton.DropDownContent = grid;
}
You can, of course optimize the code of the GetFriendlyName method and add it in a way that will be available by both classes.