I have a simple scale with a range of 0 - 100. I bound that to a label via WindowsBuilder. I want the text to display the numerical value on the scale and a % following that. How would I go about doing this?
The source code was generated by Windows Builder
protected DataBindingContext initDataBindings() {
DataBindingContext bindingContext = new DataBindingContext();
//
IObservableValue observeTextLblPercentObserveWidget = WidgetProperties.text().observe(lblPercent);
IObservableValue observeSelectionScaleObserveWidget = WidgetProperties.selection().observe(scale);
bindingContext.bindValue(observeTextLblPercentObserveWidget, observeSelectionScaleObserveWidget, null, null);
//
return bindingContext;
}
I don't know how you may do it in WindowsBuilder, but you need to add org.eclipse.core.databinding.conversion.IConverter.
Actually I think you'll need two converters (from model to text that appends "%" and one for reverse convertion to parse user input) and a validator.
Related
I have this json object that has a boolean data type. I'm trying to look or learn on how i set my boolean as a response for my retrofit builder. So basically i have this "status" that indicates true or false in my json object. How do i set the that status and declare that if the status is true the text color will be green or else it will be false. I'm still a beginner with this bear with me pls.
It's because response.body().getSoftware()[i].status is a boolean type. You can't use boolean as an input parameter in setText method - the signature of this method allows you to pass String or Int(string resource id).
To reach your goal try setting text color using this variable:
if (response.body().getSoftware()[i].status) {
softwareStatus.setTextColor(Color.GREEN);
} else {
softwareStatus.setTextColor(Color.RED);
}
softwareStatus.setText(response.body().getSoftware()[i].status.toString())
//or use softwareStatus.setText("" + response.body().getSoftware()[i].status) if status is primitive boolean and not object Boolean
Also, don't forget to set some text if it's not set in your XML layout.
UPD (to have all statuses in one coloured string):
for (int i = 0; i < response.body().getSoftware(); i++) {
String textToSet = "" + response.body().getSoftware()[i].status;
Spannable spannable = new SpannableString(textToSet);
ForegroundColorSpan fcs = new ForegroundColorSpan(response.body().getSoftware()[i].status ? Color.GREEN : Color.RED);
spannable.setSpan(fcs, 0, textToSet.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
softwareStatus.append(spannable);
softwareStatus.append("\n\n");
}
it says it cannot resolve method 'settext'(boolean)
You are getting this error because you are trying to pass a boolean value parameter to setText method, such signature of setText doesn't exist, hence it is giving you this error. You can read about setText here.
If you need to display true/false in your TextView you can do like,
setText(someBoolean + "");
or if you need to show something else based on true/false then you should check value in if condition and then do whatever you need to.
It seems like you are getting this error here
softwareStatus.setText(response.body().getSoftware()[i].status);
You can try like,
softwareStatus.setText(response.body().getSoftware()[i].status + "");
I'm trying to fetch Real values from a Model computed by a Solver. However, even though I've set pp.decimal to true (both in the SMT2 file and using the Global.setParameter), that's only obeyed when printing the model itself.
When I attempt to fetch values by using model.getConstInterp over the values of model.getConstDecls, they all display fractions (making my hacky solution of using Double.parseDouble infeasible).
I was wondering if there's any convenient way to fetch the values of constant functions within the model without forcing me to write a parser (for either the model or the arithmetic expressions it's producing).
Any help would be much appreciated.
EDIT to include example:
BoolExpr[] assertions = ctx.parseSMTLIB2String(smt, null, null, null, null);
// get solver from context (modelled upon assertions)
Solver solver = ctx.mkSolver();
solver.add(assertions);
switch (solver.check()) {
case SATISFIABLE: {
// fetch our model
Model model = solver.getModel();
System.out.println(model);
for(FuncDecl constant : model.getConstDecls()) {
// get the interpretation
Expr value = model.getConstInterp(constant);
System.out.println(value.toString());
Output:
(define-fun b () Real
(- 1.0))
(define-fun w2 () Real
0.5)
(define-fun w1 () Real
0.5)
-1
1/2
1/2
I'm looking to somehow extract the results of these constant functions into Java doubles. I could simply parse the values of the Exprs' toString() if they would both abide by pp.decimal.
After light digging (beyond the scope of looking at autocomplete suggestions), I worked out that you can simply check if the Expr you have is a RatNum. From there, you can up-cast to a RatNum and use getNumerator and getDenominator() and yield a double from division that way.
Expr value = model.getConstInterp(constant);
if(value.isRatNum()) {
RatNum rational = (RatNum) value;
IntNum num = rational.getNumerator(), den = rational.getDenominator();
System.out.println("Value = " + ((double) num.getInt() / den.getInt()));
}
This makes sense now.
I am facing some issues when applying some conditional formatting to an Excel document using POI java library. I am trying to highlight duplicate text values.The below code works properly except for fact that the rule format is not set. When I open the excel document, I see that the rule has been added (Screen shot of new rule) but that no format/color has been assigned to it.
public void conditionalFormatting() throws Exception {
/* Read Workbook and Identify Color Scale Range */
sheet = wb.getSheetAt(0);
List<String> my_range = new ArrayList<>();
my_range.add("F2:F" + (rowIndex + 1));
/* Add Conditional Formatting Rule */
CTConditionalFormatting colorScale = sheet.getCTWorksheet().addNewConditionalFormatting();
colorScale.setSqref(my_range); // Attach Range to conditional formatting set
CTCfRule myCFRule = colorScale.addNewCfRule(); //create a rule
myCFRule.setType(STCfType.DUPLICATE_VALUES); // set type of rule to Colour Scale
myCFRule.setPriority(1); // rule priority = 1
}
Does anyone know how to add a format/color to the rule?
Regards, T. Lecoffre
First up - don't work with the low-level CT classes unless you know what you're doing. Ideally, don't do it even then - too much you can get wrong / miss out!
Instead, you should be using the Apache POI usermodel classes for conditional formatting. There's documentation on the POI website about them, you can start here in the JavaDocs to read about it, or look at this program in the POI examples for the full set of options available
From the formatDuplicates method in the Conditional Formats examples, you can see that the code you need is something like:
SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("COUNTIF($F$2:$F$11,F2)>1");
FontFormatting font = rule1.createFontFormatting();
font.setFontStyle(false, true);
font.setFontColorIndex(IndexedColors.BLUE.index);
CellRangeAddress[] regions = {
CellRangeAddress.valueOf("F2:F11")
};
sheetCF.addConditionalFormatting(regions, rule1);
That will highlight the duplicates in Blue
If you want to use colour scales or similar (not sure quite how that would work for duplicates?), then there's an example in the same file too
After studying the "xlsx" file structure I was able to find what I was looking for in order to use the CT classes. In order to understand the POI library I would strongly suggest to anyone to look inside the "xlsx" zip and study the xml files (more specifically - xl/worksheets/{sheet_name}.xml ). Here is the code that I came up with:
public void conditionalFormatting() throws Exception {
/* Read Workbook and Identify Color Scale Range */
sheet = wb.getSheetAt(0);
List<String> my_range = new ArrayList<>();
my_range.add("F2:F" + (rowIndex + 1));
/* Add Conditional Formatting Rule */
CTConditionalFormatting colorScale = sheet.getCTWorksheet().addNewConditionalFormatting();
colorScale.setSqref(my_range); // Attach Range to conditional formatting set
CTCfRule myCFRule = colorScale.addNewCfRule(); //create a rule
myCFRule.setType(STCfType.DUPLICATE_VALUES); // set type of rule to Colour Scale
myCFRule.setPriority(1); // rule priority = 1
/* Add a differential formatting record */
myCFRule.setDxfId(createDXFs(wb, 12))
}
/* Create a new differential formatting record */
private static int createDXFs(XSSFWorkbook wb, int size) {
CTDxfs dxfs = wb.getStylesSource().getCTStylesheet().getDxfs();
if(dxfs == null) {
dxfs=wb.getStylesSource().getCTStylesheet().addNewDxfs();
}
dxfs.setCount(dxfs.getCount() + 1); // update the dxfs count variable
CTDxf dxf=dxfs.addNewDxf();
/* Set rule font size */
CTFontSize fontSize=dxf.addNewFont().addNewSz();
fontSize.setVal(size);
/* Set rule pattern/background color */
CTFill fill = dxf.addNewFill();
CTPatternFill pattern = fill.addNewPatternFill();
CTColor color = pattern.addNewBgColor();
color.setRgb(javax.xml.bind.DatatypeConverter.parseHexBinary("FFF8696B"));
return (int) dxfs.getCount() - 1; // return the dxf index
}
Regards, T. Lecoffre
Let's say we desire to have a non-ASCII character, for example, U+2082 (subscript 2).
Normally, we can display this in a swing component, such as JFrame, as Character.toString('\u2082').
Now, my issue is that I can't determine the the exact Unicode code, since the exact code is determined by the String supplied in the parameter. The parameter will always be a polyatomic ion - e.g. PO3. What my goal is to find the "3", turn that into a subscript 3 (U+2083), but also have the algorithm/method be abstracted enough that it will apply for any polyatomic ion (not just PO3, but also PO4 as well), and have it display correctly on a JFrame. I supply my method below.
private static String processName(String original)
{
char[] or = original.toCharArray();
int returned = -1;
for(int i = 0; i < or.length; i++)
{
if(Character.isDigit(or[i]))
{
returned = Integer.parseInt(Character.toString(or[i]));
or[i] = (char) (returned + 2080);
returned = -1;
}
}
return new String(or);
}
You probably are thinking, well, the code looks clean and should display correctly. However, the part (char) (returned+2080) doesn't display the symbol - it displays a blank box. I tried to fix it by setting a compatible font (GNU Unifont), but that didn't do anything. Any ideas?
2083 is a hex value, not decimal. See the unicode page for details about this character. I think the value you want is 0x2083, or 8323
Does anyone know how to go about creating field that would perform telephone number format masking, like here (___) ___-____:
http://www.smartclient.com/smartgwt/showcase/#form_masking
A better approach would be to let the user type whatever they want: "789-555-1234" or "(789) 555-1234" or "7895551234" and then when the field loses focus decide if what they typed can be a phone number. If so you can reformat it as "(789) 555-1234". There are several related questions about how to do that sort of thing with regular expressions; just be sure your regex accepts the format you're changing the user's input to, otherwise it will be really annoying to edit.
As an example, look what happens when you type ".5" into the left margin field in Microsoft's standard page setup dialog: when you tab out it changes it to "0.5".
UPDATE: Here's sample code in GWT to illustrate. For the sake of this example, assume there's an element called "phoneContainer" to put the text box in. GWT doesn't give you the full java.util.regex package, but it gives enough to do this:
private void reformatPhone(TextBox phoneField) {
String text = phoneField.getText();
text = text.replaceAll("\\D+", "");
if (text.length() == 10) {
phoneField.setText("(" + text.substring(0, 3) + ") " + text.substring(3, 6) + "-" + text.substring(6, 10));
}
}
public void onModuleLoad() {
final TextBox phoneField = new TextBox();
RootPanel.get("phoneContainer").add(phoneField);
phoneField.addBlurHandler(new BlurHandler(){
public void onBlur(BlurEvent event) {
reformatPhone(phoneField);
}
});
}
It looks like you'd want to create your own widget that extends the GWT input box and has a default value set to the mask you want. Then you handle the onKeypress event and update the field as needed (making sure to set the cursor position to the correct location).