GWT add widget UIBundle style at runtime - css

In a GWT application i am declaring the .css styles inside the UiBinder (ui.xml)
For example:
<ui:Style>
.input {
background:green;
}
</ui:Style>
And if i declare a Widget inside the UiBinder i reference the Style i was like below:
<g:Button styleName="{Style.input}"/>
which is fine.
My problem is that i want to apply that style in a Widget that i add at run time. For example a text box:
TextBox box = new TextBox();
box.setStyleName("input");
I have tried all the possible compinatations (e.g. "input", "{Style.input}"), but without any luck. I know that GWT compiles the styles inside a UiBinder file so the Widgets end up with something like "class="GLIX78"".
Is there any way i can achieve adding a style which is declared at UiBinder in a Widget at runtime?
Thanks,

You can reference the style, which you have declared in UiBinder. But you need to take some additional steps. Look at this example:
UiBinder
<!-- (1) declare your style , remember to set the type attribute correctly - you should place your package.WidgetName.StyleName in there -->
<ui:style type='z.client.TEstWidget.MyStyle'>
.blackBG {
background-color: black;
}
.grayBG {
background-color: gray;
}
</ui:style>
<g:HTMLPanel>
<g:Label ui:field="myLabel" styleName="{style.grayBG}">test</g:Label>
<g:Button ui:field="myButton">change style</g:Button>
</g:HTMLPanel>
Widget code
public class TEstWidget extends Composite {
private static TEstUiBinder uiBinder = GWT.create(TEstUiBinder.class);
interface TEstUiBinder extends UiBinder<Widget, TEstWidget> {
}
// declare the style (2)
interface MyStyle extends CssResource {
String blackBG();
String grayBG();
}
// (3) here you make reference to the style declared in uibinder
#UiField
MyStyle style;
#UiField
Button myButton;
#UiField
Label myLabel;
public TEstWidget() {
initWidget(uiBinder.createAndBindUi(this));
}
#UiHandler("myButton")
public void onClick(ClickEvent event) {
// change the background of the label
// (4) this is how you can use your style
myLabel.removeStyleName( style.grayBG());
myLabel.addStyleName(style.blackBG());
}
}

Related

Using Hostlistner and directive to add CSS class

I have created a directive and using HostListner , want to add a CSS style on Click event on the tag.Also remove on the click again.I have the following CSS .
CSS
.strikethrough { text-decoration: line-through;}
HTML
<p optionalIngredient>ABCD</p>
Directive
constructor(private elRef: ElementRef ,private renderer: Renderer2)
{ }
#HostListener('mouseenter') onMouseEnter() {
this.renderer.addClass(this.elRef.nativeElement, 'strikethrough');
}
#HostListener('mouseleave') onMouseLeave() {
this.renderer.removeClass(this.elRef.nativeElement,strikethrough');
}
You can use a boolean to keep track of whether the style is applied.
styled = false;
#HostListener('click')
onClick(){
if (!styled) this.renderer.addClass(this.elRef.nativeElement, 'strikethrough');
else this.renderer.removeClass(this.elRef.nativeElement, 'strikethrough');
this.styled = !this.styled;
}

How to add the css properties to an image in ImageResourceCell of CellTable

I created a CellTable with image column in GWT. Image is added through `ImageResource .
private Column<DocList, ImageResource> statusColumn;
private final Resources resources = GWT.create(Resources.class);
final String STATUS_COLUMN = "Status";
statusColumn = buildStatusColumn();
private Column<DocList, ImageResource> buildStatusColumn() {
statusColumn = new Column<DocList, ImageResource>(
new ImageResourceCell()) {
#Override
public ImageResource getValue(DocList object) {
return resources.draft();
}
};
statusColumn.setDataStoreName(STATUS_COLUMN);
return statusColumn;
}
public interface Resources extends ClientBundle {
#Source("draft.png")
ImageResource draft();
}
But the problem is that I want to change some css properties of the image, for example, size or cursor="pointer" for click event, but I don't know how. In Firebug there is a default size.
How to add css to image done by ImageResource in GWT?
Column has a method called getCellStyleName().
You can override this method and return a style that reflects the image you use
#Override
public String getCellStyleName()
{
return resources.draft().getName();
}
Then you could create a style in your sheet like this:
.draft img {
cursor: pointer;
width: ...;
height: ...;
}

Creating my own Custom widget in GWT and styling it using my own Custom CSS code

I have created a custom widget, basically an Image and a text label.
What must I do so that I can now set the position of the text ( top/left/bottom of the image for example ) using nothing only CSS? I mean what methods would my class need so that the CSS styling can be applied to it?
ie Can I have a css class like so
.ImageAndLabel
{
imagePosition: top;
}
Heres my gwt class
public class ImageAndLabel extends Composite
{
private Label label = new Label();
private Image img = new Image("/images/pinkBackground.png");
protected final FlexTable contentTable;
public ImageAndLabel()
{
contentTable = new FlexTable();
initWidget(contentTable);
label.getElement().getStyle().setFontSize(40, Unit.PX);
contentTable.setWidget(0,0,img);
img.setHeight("200px");
contentTable.setWidget(0,1,label);
label.getElement().getStyle().setLeft(0, Unit.PX);
img.getElement().setId("popupImg");
label.getElement().setId("popupLabel");
}
public void setText(String text)
{
label.setText(text);
}
}
I guess what I'm after is can I create my own Custom CSS codes?
You can use flextable method for alignment.
example :
flexTable.getFlexCellFormatter().setAlignment(0, 0,
HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);

Is it possible to add checkbox in button using gxt 2.5

My Class extends gxt button and I would like to add a checkBox in the button
but there is no method for the addition of a component in button.
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
public class ButtonCheckBox extends Button {
private CheckBox checkBox;
public ButtonCheckBox() {
checkBox = new CheckBox();
checkBox.setBoxLabel("CheckBox");
setWidth(200);
}
}
How can I, if possible, add a chckbox in my button?
Thanks Eliran Malka and i done this using below code
import com.dips.pei.client.view.resources.Icons;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.widget.button.ToggleButton;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
public class CheckBoxButton extends ToggleButton {
public CheckBoxButton() {
setIcon(AbstractImagePrototype.create(Icons.INSTANCE.notcheckedIcon()));
setId("checkBoxButton");
}
#Override
protected void onClick(ComponentEvent ce) {
super.onClick(ce);
if(isPressed()){
setIcon(AbstractImagePrototype.create(Icons.INSTANCE.checkedIcon()));
}else{
setIcon(AbstractImagePrototype.create(Icons.INSTANCE.notcheckedIcon()));
}
}
}
It doesn't seem to be possible using the GXT 2.x standard API, but may be achieved using an HTML Template.
Assuming the purpose of the checkbox is to serve as an indicator for the button state, i suggest you use a ToggleButton instead and modify its appearance. You can assign a CSS classname to it and apply differing background images according to its varying states:
.my-toggle-button .x-btn-mc {
background-image: url('images/toggle-button/checkbox-off.png');
}
.my-toggle-button.x-btn-pressed .x-btn-mc {
background-image: url('images/toggle-button/checkbox-on.png');
}
References (on the GXT 2.2.5 API)
Template
ToggleButton

asp:TextBox - override Text property

How do I override the TextBox property "Text" (getter) ?
According to MSDN the property is virtual so you should be able to override like so:
class MyTextBox : TextBox {
public override string Text
{
get { return /* return what you wish*/; }
set { /**/ }
}
}
You can also override one of the Render methods to have full control of generated HTML. Though perhaps you had something else in mind?

Resources