Today I will have question regarding formatting numbers in Tapestry5 grid.
I have couple of fields in Grid component that I need to format due to some rules that concerns decimal mark and thousands separator. For textfields I'm using translator defined for whole application (like in this example) but I have no idea how to get same functionality for grid (or even if it is possible at all).I would like to have same mechanism available for all grids in my whole applications.
Of course not all numeric grid field will be currencies so there should be some kind of possibility to configure them.
I know that with grid I can use ValueEncoder but I think that this won't work in that case because it encodes whole object displayed in grid, not only some defined fields.
One more thing: I know that I can define each field to get value from method and there do all functionality, but I'm looking for something more flexible - like mentioned earlier "translator" for textfields.
This can be done by the combination of a few things
A DataTypeAnalyzer which produces a String datatype given a PropertyAdapter
A DisplayBlockContribution which maps the datatype to a block (see also EditBlockContribution).
Contribute the DisplayBlockContribution to the BeanBlockSource
Note that this will not only affect the grid, it will also affect all bean based components (eg BeanDisplay).
Jumpstart example here
You can override the default grid cell rendering behaviour by defining a block property named xCell (where x is the column name). The documentation gives an example of customising rendering of the lastName column.
<t:grid source="users" row="user">
<p:lastNameCell>
<t:pagelink page="user/view" context="user.id">${user.lastname}</t:pagelink>
</p:lastNameCell>
</t:grid>
Related
I'm doing a favor for an engineer friend by making him a program that helps him with the scheduling of his factory's production. Each type of product is broken down to a set of steps (they share a lot of them, but there are a few differences).
The programming issue:
Each time a new production process is registered I display a number of checkboxes representing the before mentioned steps. He can choose which steps he needs added for this particular product. If he checks a checkbox, two (or more) textfields appear where he can add additional information (starting date, duration, comments, etc...). My problem is that this is a lot of individual components and I am unsure how to handle them. Since I will need to have access to all of them at some point (the checkboxes to see if that step is needed and all the textfields for the data) I was thinking of having them all as fields, but that doesn't feel right...
Another approach could be to make a container class that groups the textfields together with the checkbox. Something like this:
ArrayList<MyComponentGroup> group;
for (MyComponentGroup cg : group) {
if (cg.getCheckBox().isSelected()) {
//access and read the data from all the textfields in this object
}
}
What is the Java programming convention or the most commonly used method to handle this situation?
Here's what I would do when dealing with tons of components and similar requirements:
I would model the relationship between options (available through checkbox selections) and the related data to fill (requirements). This model may already be available for you.
I would attempt to use PropertyEditor instances and map them to model elements.
When the time comes to save or use the data filled by the user, I would just walk the model represented on the screen, grab the associated editors and deal with the value of those editors.
I think that the approach that I described above will give you less work and potentially and it will bring more flexibility for your friend.
You'd only pay the initial cost of getting the components relationships/dependencies in a nice model as well as registering the relevant PropertyEditors for visual editing.
One approach is to consistently give each JComponent a unique name. Use something hierarchical to fit the complex process, like "Whites.Rinsecycle.enableCB". For completeness, store this String as a clientProperty in the JComponent. Then you can use that as a key in a large Map to access all the components.
Maybe not the most "elegant" (I'd tend to go with a hierarchy of JPanels with relevant fields) but for a slightly quick and dirty, moderate sized project this is reasonable.
Is there an existing library that automatically creates a Java Swing form from a Properties (or Properties-like) object? i.e. shows 2 columns, as many rows as there are properties, properly justified Property names on the left, JTextFields for the values on the right.
I do not have access to the property names (or expected types) at compile time.
In addition, the solution must allow some value fields to be set read-only after construction.
A great solution would :
allow some property values to be specified as sensitive, requiring a JPasswordField
provide input format checking, e.g. against an object type (such as URL, Double, etc)
or by type-sensitive so that appropriate widgets (or buttons to bring up appropriate widgets) are used instead of JTextField for standard object types. e.g. JFileChooser for properties expected to be of a File type, SwingX Colour/Date selection, numerical format checking)
Getting into type-specific properties starts to sound like JavaBeans. I'd rather not go down the JavaBeans route unless there is a really easy - not a big framework - solution for this for an object that is a Javabean.
(I already know how to manually do this and could write a simple implementation myself that ignores sensitivity/type information - anyone answering along those lines will be shot down! I only want to know if such a beast already exists, preferably in a well maintained library)
UPDATE: related to Java Beans - creating an inspector window
No such thing exists. However, I wrote a rudimentary feature (and released it OSS) for https://github.com/fommil/zibaldone
We use JIDE, which not open-source.
If you don't mind that, take their Property Grid for a spin - it seem to match all your requirements.
I am writing some UI persistence methods for an application. I'm trying to expand to a JXTable with a fixed set of N columns in the table model.
How can I get and set the following information for column #k in the JXTable, where k is the column number in table model order? It's not as easy as it sounds, or at least I'm missing some tricky bit of information.
column width
column visibility
column order
TableColumnModelExt.getColumns() includes this crazy note, which I'm having trouble parsing:
java.util.List<javax.swing.table.TableColumn> getColumns(boolean includeHidden)
Returns a List of contained TableColumns. Includes or excludes invisible columns, depending on whether the includeHidden is true or false, respectively. If false, an Iterator over the List is equivalent to the Enumeration returned by getColumns().
NOTE: the order of columns in the List depends on whether or not the invisible columns are included, in the former case it's the insertion order in the latter it's the current order of the visible columns.
Also, I know how to use JTable.convertColumnIndexToView(), but it returns -1 for hidden columns, and leaves no information about which order the hidden column would go if it were made visible again. JXTable knows this information because you can restore column visibility, and it puts the column where it was before it was hidden.
If I understand you correctly, you are after the view column index a hidden column would have if made visible again. That's not directly supported.
For saving/restoring purposes, there's a class XProperties (not officially supported, but working smoothly) doing so in the context of AppFramework which might give you an idea of how to do it.
Edit
To get a TableColumn by modelIndex, get all columns, loop and compare the modelIndex. Some pseudo-code in a pseudo method getColumnFor(modelIndex):
List allColumns = table.getColumns(true);
forEach {
if (column.getModelIndex() == modelIndex) {
return column;
}
return null;
Consider using the Swing Application Framework (JSR-296)
Although this project is now dead AFAIK, I think it's a better starting point than implementing this feature from scratch. (It's Open Source.)
If you're building your application on top of the NetBeans Platform (which I highly recommend), then have a look at my blog on how to use these two frameworks together:
http://puces-blog.blogspot.com/2009/04/netbeans-platform-meets-swing.html
Note: There is also the Better Swing Application Framework
The Better Swing Application Framework is a fork of the original Swing Application Framework (appframework) reference implementation of JSR 296. Since August 2009, the original Swing Application Framework project has been on hold, and therefore this fork was created to carry on the work until the original project resumes.
I don't know much about this, but it might be even a better starting point.
Wicket has many implementations of AbstractRepeaters: ListView, DataView, GridView, Loop, PropertyListView etc.
Personally, I find it hard to determine which view would be ideal for which scenario. I usually stick to DataView but that's simply because I'm used to it. Maybe GridView would be better for scenario A, a PropertyListView for B, ....
Is anyone aware of a blog or any tutorial where the differences of the views are explained or anyone who can explain which view is best for which use case?
Wicket has a lot of additional, trivial classes, which is causing your confusion. Different components are better for different scenarios, but there are a lot of Wicket components for rare cases that don't add any real complexity.
For example, RequiredTextField is an entire class that is equivalent to:
TextField x = new TextField("text");
x.setRequired(true);
I presume this stems from an older version where setting required was more complicated, but it's still there to cause some confusion.
Many of your repeaters are similar. PropertyListView just wraps the model in a CompoundPropertyModel, making property expressions easier (see below). However, you could easily make this change yourself.
So, here is my quick summary as I have been unable to find an up-to-date blog post as you've described:
RepeatingView - very useful when you do not have a list or you are adding different types of components (and therefore different internal markup).
ListView - useful if you have a List and you're displaying the whole thing. Sadly, it does not work with other sorted collections.
DataView - useful if you are loading from a Database. Additional methods allow you to easily sort, page, and modify the data set.
PropertyListView - useful if you are simply displaying values via a property expression. Allows you to do
item.add(new Label("name"));
instead of
item.add(new Label("name", new PropertyModel<String>(item.getModel(), "name")))
Loop - useful if you want to repeat an Integer number of times instead of a set list of data. This would be equivalent to a ListView whose model object is a List<Integer> filled with integers from 0 to length
GridView - useful for taking a single list (e.g. 21 strings) and using two sets of markup (inner/outer) to display that list in groups (e.g. a 7x3 grid). It assumes, however, that your markup uses certain wicket:id's, which is not well documented. I think you would be better off with a pair of nested RepeatingView components, which accomplish the same thing.
Hope that helps!
I would like to achieve the following in a configurable manner (that is little or no code modification in order to change behavior) :
Problem Statement :
a) For each part there are a set of key value pairs that can vary
b) The key is a static string label and the value can be one of (text, single select list of values)
c) The system should present an UI to allow entry/modification of values and allow modification(in well defined ways) to the set of key-value pairs allowed for a part
d) The values must be validatable before entry into the database
Constraints :
java(1.4), struts, hibernate, oracle
Are there any open source java based frameworks that can be integrated that could go towards satisfying the problem statement?
I thought that this kind of problem would have been solved but when I research on the web I am not finding any hits - maybe my query is not being properly targeted.
thanks
What I was looking for was a big picture framework that would also solve this mini-problem along the way. Then it might have made sense for me to pick up and learn the framework.
Also frameworks are useful because the patterns are well thought out and cover gotchas that I might encounter if I were to invent my own.
Since I didn't find one, here is the database design for my problem for which I have also developed a partially working prototype for all the technically challenging questions. At the end I have listed areas I have not yet covered in my prototype (I am not providing the code here for that) but which I don't think would be all that difficult to implement.
To recap, I have a set of parts each of which is identified by a number and has varying properties associated with it in the form of a label and a value. An example of such might be the set of SNPs (single nucleotide polymprphisms) and the label and value would be characteristics of each SNP.
I want to capture those characteristics in an extensible manner.
Table1 : SNP_TEMPLATE
Field Name
TEMPLATE_SEQID
CATEGORY_LABEL
CATEGORY_VALUE
ATTRIBUTE_LABEL
ATTRIBUTE_VALUE
ATTRIBUTE_TYPE
ATTRIBUTE_TYPE_VAL_LABEL
SEQUENCE
MANDATORY
Table2 : SNP_PERSIST
ATTRIBUTE_SEQID
SNP_SEQID
CATEGORY_LABEL
ATTRIBUTE_LABEL
VALUE