Binding the style property of all cells in a JavaFX TableView - javafx-2

I have a JavaFX TableView where each row should have a conditional style.
The styling is dependent on whether the source item of of the table row is present in a certain list or not.
This is what I have so far:
1) The data class that holds the data of a table row together with two boolean properties (true if the data is contained in list X) and a string property that should bind to the correct style attributes.
private class WebPageData {
private WebPage page;
private BooleanProperty isReferenced = new SimpleBooleanProperty(false);
private BooleanProperty isReferencing = new SimpleBooleanProperty(false);
private StringBinding style = new When(isReferenced).then("...").otherwise(...);
}
2) A change listener on table selection change that updates each boolean property accordingly, when the table selection changes
tblResultData.getSelectionModel().getSelectedIndices().addListener(new ListChangeListener<Integer>() {
#Override
public void onChanged(ListChangeListener.Change<? extends Integer> arg0) {
if (arg0.getList().size() == 0) {
selectedPage.set(null);
} else {
// for coloring only consider the first selected row
// multi select must be doable for certain other features
WebPage selectedWebPage = tblResultData.getItems().get(arg0.getList().get(0)).page;
selectedPage.set(selectedWebPage);
// tableModel.data holds a list of data for every table row
for (WebPageData data : tableModel.data) {
boolean referenced = selectedWebPage.getReferencedWebPagesList().contains(data.page);
boolean referencing = selectedWebPage.getReferencingWebPagesList().contains(data.page);
data.isReferenced.set(referenced);
data.isReferencing.set(referencing);
}
}
}
});
Now what I want to do is to somehow bind the style property of each table cell to the style property of WebPageData - so that the change listener updates the two boolean properties, therefore the style property of WebPageData is updated and in consequence the style of the table cell changes.
I tried to bind the style during creation phase by using a custom TableCellFactory, but of course this approach fails as there is no WebPageData instance present at this time. As the TableColumn classes don't provide an opportunity to iterate over all cells (so I could bind the style after the table actually gets its data), the only option I currently see is to keep a reference to each created table cell. I don't consider this solution is good practice.
So is there any other option to bind the cell styles? If I don't bind them, I have to set the styles manually each time the table selection changes - which puts me to the "I can't iterate over cells" problem again.

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.scene.control.TreeTableRow;
public class HighlightBoundTreeTableRow extends TreeTableRow<Thing> {
private static final String CHOSEN_STYLE_CLASS = "chosenStyle";
private final ObjectProperty<Boolean> chosen = new SimpleObjectProperty<>();
private final ChangeListener propertyChangeListener = (obs, ov, nv) -> updateHighlight();
#Override
protected void updateItem(Thing item, boolean empty) {
super.updateItem(item, empty);
//cleanup
getStyleClass().remove(CHOSEN_STYLE_CLASS);
chosen.unbind(); // unbinding something that is not bound has no effect
chosen.removeListener(propertyChangeListener); // also ok to remove a listener that was never there
if (empty) {
return;
}
chosen.bind(item.chosenProperty()); //bind will also set the intial value
chosen.addListener(propertyChangeListener);
updateHighlight();
}
private void updateHighlight() {
if (chosen.get()) {
getStyleClass().add(CHOSEN_STYLE_CLASS);
} else {
getStyleClass().remove(CHOSEN_STYLE_CLASS);
}
}
}
I know this was asked forever ago but maybe it'll help someone.
I had a similar issue I wanted to solve. I know you're using a TableCell and this involves a TreeTableRow but I believe the concept is the same: You want to change a field in your data object and have that change update the styling on wherever that object is being displayed in a table.
So I extended TreeTableRow and gave that class its own property field to hold on to. Every time that row is updated I unbind that property and rebind it to the field I want to listen to. (I do the same with the listener.) Since every time updateItem() is called it could be getting a different instance of my data object.
"chosenStyle" is just a class in my style sheet that changes the background color. Using classes instead of calling setStyle() makes it easier to remove the styling.

Related

Custom selector challenges

I have a custom screen with a multiple custom selectors, which change what they select based on dropdown lists.
The solution I implemented is shown in a previous case:
Dynamically changing PXSelector in Acumatica (thanks).
My challenge is twofold:
1.) If the dropdown selection is "No Lookup", then I want the PXSelector Attribute to essentially be removed - leaving just a text entry. Not sure if this is even possible...
2.) If one of the selectors (let's say Projects) is selected, I'd like the selection of the following selector (let's say Tasks) to filter based on the Project selected.
Thanks much...
1) I think the only way to do this is to create your own attribute.
Something like that:
public class PXSelectorTextEditAttribute : PXSelectorAttribute
{
bool selectorMode;
public PXSelectorTextEditAttribute(Type type, bool selectorOn):base(type)
{
selectorMode = selectorOn;
}
public override void FieldVerifying(PXCache sender, PXFieldVerifyingEventArgs e)
{
if(selectorMode)
base.FieldVerifying(sender, e);
}
public static void SwitchSelectorMode(PXSelectorTextEditAttribute attribute, bool onOff)
{
attribute.selectorMode = onOff;
}
}
You will be able to turn on and off the 'selector' part of the attribute. With the field verifying turned off you will be able to put any value to the field just like in simple TextEdit field. However, the lookup button in the right end of the field still will be visible. I have no idea how to hide it.
2) This behavior can be implemented easily. You will need something like that(example based on cashaccount):
[PXSelector(typeof(Search<CABankTran.tranID, Where<CABankTran.cashAccountID, Equal<Current<Filter.cashAccountID>>>>))]
If you want to see all records when the cashaccount is not defined then you just modify the where clause by adding Or<Current<Filter.cashAccountID>, isNull>
Also don't forget to add AutoRefresh="true" to the PXSelector in the aspx. Without it your selector will keep the list of the records untill you press refresh inside of it.

Move records up/down in grid with "SortOrder" field and "Up"/"Down" buttons

Current customization project I'm working on has the requirement of displaying / editing a grid with a "Sort Order" for records. The "SortOrder" field is read only with up/down buttons to allow the user to re-order the items in the grid.
The "SortOrder" column in the DAC is a simple Int field.
The PXSelect statement for the grid is using a OrderBy>> to display the records.
The Grid in the ASPX is a defined with "SyncPosition= true"
I've added an Up/Down button that increments/decrements the "SortOrder" value for the current selected record.
The issue that I'm running into is that the first time "Up" or "Down" is clicked, the "SortOrder" field is updated however the rows do not move. Once I click Save to persist the update, the grid then refreshes with the right order.
I've looked through the the rest of the code but all other situations where this is used is for treeviews, not grids.
I've tried adding a View.RequestRefresh() at the end of my Action but this doesn't cause the reorder.
What would be the best way without a Persist after each move to get the Grid to update and reflect the current order from the cache values? As usual I'm assuming I'm overlooking something simple.
Any advice would be appreciated.
I had a look at the generic inquiry designer source code - it has an up/down button in the grid to reorder the fields. The views don't have an OrderBy clause:
public PXSelect<GIFilter, Where<GIFilter.designID, Equal<Current<GIDesign.designID>>>> Parameters;
OrderBy is not necessary because the LineNbr field is a key field - system automatically orders the records by the key fields.
public abstract class lineNbr : IBqlField { }
[PXDBInt(IsKey = true)]
[PXDefault]
[PXLineNbr(typeof(GIDesign))]
[PXParent(typeof(Select<GIDesign,
Where<GIDesign.designID, Equal<Current<GIFilter.designID>>>>))]
public virtual int? LineNbr { get; set; }
The code for the button looks like this:
[PXButton(ImageKey = Sprite.Main.ArrowUp, Tooltip = ActionsMessages.ttipRowUp)]
[PXUIField(DisplayName = ActionsMessages.RowUp, MapEnableRights = PXCacheRights.Update)]
protected void moveUpFilter()
{
if (this.Parameters.Current == null)
return;
GIFilter prev = PXSelect<GIFilter, Where<GIFilter.designID, Equal<Current<GIDesign.designID>>, And<GIFilter.lineNbr, Less<Current<GIFilter.lineNbr>>>>, OrderBy<Desc<GIFilter.lineNbr>>>.Select(this);
if (prev != null)
this.SwapItems(this.Parameters.Cache, prev, this.Parameters.Current);
}
[PXButton(ImageKey = Sprite.Main.ArrowDown, Tooltip = ActionsMessages.ttipRowDown)]
[PXUIField(DisplayName = ActionsMessages.RowDown, MapEnableRights = PXCacheRights.Update)]
protected void moveDownFilter()
{
if (this.Parameters.Current == null)
return;
GIFilter next = PXSelect<GIFilter, Where<GIFilter.designID, Equal<Current<GIDesign.designID>>, And<GIFilter.lineNbr, Greater<Current<GIFilter.lineNbr>>>>, OrderBy<Asc<GIFilter.lineNbr>>>.Select(this);
if (next != null)
this.SwapItems(this.Parameters.Cache, next, this.Parameters.Current);
}
The SwapItems function is shared between all the move up / move down actions:
private void SwapItems(PXCache cache, object first, object second)
{
object temp = cache.CreateCopy(first);
foreach (Type field in cache.BqlFields)
if (!cache.BqlKeys.Contains(field))
cache.SetValue(first, field.Name, cache.GetValue(second, field.Name));
foreach (Type field in cache.BqlFields)
if (!cache.BqlKeys.Contains(field))
cache.SetValue(second, field.Name, cache.GetValue(temp, field.Name));
cache.Update(first);
cache.Update(second);
}
Finally, there's a bit of JavaScript code in the ASPX code - it may or may not be what you're missing to get the feature to work correctly; i'm not exactly sure what it's doing but would encourage you to open SM208000.aspx in an editor and look for commandResult. Also check out the CallbackCommands that are defined on the grids which support up/down - it may have something to do with it.

Xpages "filter by category name" with two fields values (Restricting a View to Multiple Categories)

I want to design a view that display only projects of selected customer and services, so I created a view that customer and service columns are categorized and a view panel for this view. If I put
document1.getDocument().getItemValueString("Customer")
it works with a restrict single category. How I can do this with tow categorized columns.
Thanks in advance
Could you implement a repeat within a repeat for this?
The first repeat would capture your first category. Then inside you have a panel and a repeat and the second repeat would further filter the datasource by the second category
You cheat. Have one column that concatenates customer and project. Grab that column in your selection and split it in code.
Then use the selected value for the one column. Lets presume your original columns are customer and project. So your new column would have the formula customer+"~"+project. You then read this values to populate a SSJS object or a variable, so you can retrieve the customers (first dropdown) and the projects (second dropdown). In a dropdown you can use the format Display|Value, so a good approach is to have the value in the format customer~project.
As said you can do that in Java or JavaScript. Since I like Java's collection framework a lot, here's the Java version:
import java.util.Set;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;
import lotus.domino.Database;
import lotus.domino.NotesException;
import lotus.domino.View;
import lotus.domino.ViewEntry;
import lotus.domino.ViewEntryCollection;
public class SplitCategoryBean {
private static final String SEPARATOR = "~";
private final Map<String,Set<String>> allCategories = new TreeMap<String, Set<String>>();
public void populate(Database db, String viewName) throws NotesException {
View v = db.getView(viewName);
ViewEntryCollection vec = v.getAllEntries();
ViewEntry ve = vec.getFirstEntry();
while (ve != null) {
ViewEntry nextVE = vec.getNextEntry(ve);
this.addEntry(ve.getColumnValues().get(0).toString());
ve.recycle();
ve = nextVE;
}
vec.recycle();
v.recycle();
}
private void addEntry(String combinedCategory) {
String[] splitCategory = combinedCategory.split(SEPARATOR);
String key = splitCategory[0];
String value = splitCategory[1];
Set<String> thisCategory = (this.allCategories.containsKey(key)) ? this.allCategories.get(key): new TreeSet<String>();
thisCategory.add(value+"|"+combinedCategory);
this.allCategories.put(key, thisCategory);
}
public Set<String> getFirstCategory() {
return this.allCategories.keySet();
}
public Set<String> getSecondCategoryReadyForDropDown(String key) {
return this.allCategories.get(key);
}
}
You would configure that as a managed bean (viewScope) and in the queryOpen you call the populate method. Then you can easily bind your first selection to #{beanName.firstCategory} for the selection and e.g. #{viewScope.curCustomer} for the value. The second drop-down you use the rendered="#{viewScope.curCustomer}" so it only shows when the customer is selected. And you bind the selections to #{javascript:beanName.getSecondCategoryReadyForDropDown(viewScope.curCustomer);
Put refreshs on the change events and render the view only if you have a project selected.
Does that work for you?
Thanks guys
I created a view that the first column is categorized and its value is Customer+Service then put
document1.getDocument().getItemValueString("Customer") + document1.getDocument().getItemValueString(“Service")
in "filter by category name" of view panel it works now.

Resharper create property with backing field

How do you create property with a backing field in Resharper?
This is the first thing you would want to do with a class and I cannot find how to it.
Its so simple.
Within the body of the class, type prop and hit Tab. Supply the property's type and name. This will create an autoproperty (these days, it is typically one of these you would want to create).
Then, with the cursor on the property name, hit Alt+Enter and choose To property with backing field.
The quickest way I've found is to type your property as if it exists already:
this.MyProperty = "hello";
Then Alt-Return on the property name, and choose Create Property 'MyProperty' then hit tab to choose between auto-property, managed backing field, or default member body.
I believe this is quicker than using the prop shortcut, hitting tab, specifying the property type, hitting tab twice, specifying the name, and then pressing Alt-Return on the property name and choosing the 'to property with backing field' selection.
You could also create a Live Template so that there is slightly less work, e.g:
/// <summary>
/// Private backing field for $Property$ property
/// </summary>
private $Type$ $BackingField$;
public $Type$ $Property$
{
get
{
return this.$BackingField$;
}
set
{
this.$BackingField$ = value;
}
}
The parameter $BackingField$ can be generated automatically from $Property$ by the "first character in lower case" macro of the template editor and setting it to "Not editable".
But I believe the first method is the quickest and easiest.
If you already have a property with a getter and setter, sometimes you will find that the backing field you used is no longer available because you changed the base class for the current class or you made other changes. For example, in the LinesMax property below, the backing field Height is available from an inherited class:
public int LinesMax
{
get { return ConvertValGet(AdjustMetricEnum.Height, Height); }
set { Height = ConvertValSet(AdjustMetricEnum.Height, value); }
}
If the above code works but I decide afterwards no longer to inherit the class that exposes the Height property, the two Height variable names will turn red in the IDE to indicate that they're no longer available to the code.
At that point, I may want to create a private backing field. To easily create that, I can first modify the Height name to _height, then place the cursor on either _height name, hit Alt-Enter then choose Create field '_height'. A backing field will be created.

What is the correct way to format SPGridView values being displayed?

Problem
As we know, SharePoint saves data in database in plain text. Some fields even have concatenated strings like <id>;#<value> for user fields. Percents are saved as doubles (1.00000000000000 for 100%) and etc.
Ofcourse, I want to display data as they are displayed in lists.
What should I do?
Should I use derived SPBoundField to format values (Which I actually did and it works fine until you want to filter (probably SPBoundField won't format me values because i use ObjectDataSource not list and with reflector I saw if there are SPListItems in datasource, then it formats correctly. Not my case)
alt text http://img199.imageshack.us/img199/2797/ss20090820110331.png
Or must I loop through all the DataTable and format each row accordingly?
What are Your techniques?
Thank you.
Here is how I solved this issue.
<asp:TemplateField HeaderText="Campaign Members">
<ItemTemplate>
<%# RemoveCharacters(Eval("CampaignMembers").ToString())%>
</ItemTemplate>
</asp:TemplateField>
// Make sure declare using System.Text.RegularExpression;
protected string RemoveCharacters(object String)
{
string s1 = String.ToString();
string newString = Regex.Replace(s1, #"#[\d-];", string.Empty);
newString = Regex.Replace(newString, "#", " ");
return newString.ToString();
}
I normaly use ItemTemplates that inherit from ITemplate. With in the ItemTemplate I use the SPFieldxxxValue classes or some custom formating code. This saves looping through the DataTable and the ItemTemplates can be reused.
The ItemTemplates are attached in Column Binding
E.G
// Normal Data Binding
SPBoundField fld = new SPBoundField();
fld.HeaderText = field.DisplayName;
fld.DataField = field.InternalName;
fld.SortExpression = field.InternalName;
grid.Columns.Add(fld);
// ItemTemplate Binding
TemplateField fld = new TemplateField();
fld.HeaderText = field.DisplayName;
fld.ItemTemplate = new CustomItemTemplateClass(field.InternalName);
fld.SortExpression = field.InternalName;
grid.Columns.Add(fld);
An example of a ItemTemplate
public class CustomItemTemplateClass : ITemplate
{
private string FieldName
{ get; set; }
public CustomItemTemplateClass(string fieldName, string formatString)
{
FieldName = fieldName;
}
#region ITemplate Members
public void InstantiateIn(Control container)
{
Literal lit = new Literal();
lit.DataBinding += new EventHandler(lit_DataBinding);
container.Controls.Add(lit);
}
#endregion
void lit_DataBinding(object sender, EventArgs e)
{
Literal lit = (Literal)sender;
SPGridViewRow container = (SPGridViewRow)lit.NamingContainer;
string fieldValue = ((DataRowView)container.DataItem)[FieldName].ToString();
//Prosses Filed value here
SPFieldLookupValue lookupValue = new SPFieldLookupValue(fieldValue);
//Display new value
lit.Text = lookupValue.LookupValue;
}
}
Here are a few options. I don't know the output of all of them (would be a good blog post) but one of them should do what you want:
SPListItem.GetFormattedValue()
SPField.GetFieldValue()
SPField.GetFieldValueAsHtml()
SPField.GetFieldValueAsText()
It may also be handy to know that if you ever want to make use of the raw values then have a look at the SPField*XYZ*Value classes. For example the form <id>;#<value> you mention is represented by the class SPFieldUserValue. You can pass the raw text to its constructor and extract the ID, value, and most usefully User very easily.
I would suggest either to format the values before binding them to the spgridview. Linq and an anonymous type is preffered or to call a code behind function on the field that needs the formatting upon binding.
DataField='<%# FormatUserField(Eval("UserFieldName")) %>'
or...maybe a templated field?
After all, i did have not know any other solution to loop through DataTable rows and format them accordingly.
If your SPGridView's data source is list, try out SPBoundField.

Resources