How to Overload Checkbox Related Functions in Android Studios - android-checkbox

#Override
public void setChecked(boolean checked) {
CheckBox checkb = (CheckBox) findViewById(R.id.a);
if (checkb.isChecked() != checked) {
checkb.setChecked(checked) ;
}
}
#Override
public boolean isChecked() {
CheckBox checkb = (CheckBox) findViewById(R.id.a);
return checkb.isChecked() ;
}
#Override
public void toggle() {
CheckBox checkb = (CheckBox) findViewById(R.id.a);
setChecked(checkb.isChecked() ? false : true) ;
}
That's how one class is defined.
How do I modify those functions if I want to use them in other check boxes?
For example, you can be used on findViewByld(R.id.b);

You have two choices:
Override CheckBox object (reuse methods by inheritance)
Add CheckBoxHelper (reuse methods by delegation)
I suggest the second approach:
class CheckBoxHelper {
public void setChecked(CheckBox c, boolean checked) {
if (c.isChecked() != checked) {
c.setChecked(checked) ;
}
}
.. etc
}

Related

Is PropertyChanged += LinkLabel_PropertyChanged; same as protected override void OnPropertyChanged(string propertyName = null)

In a Xamarin template like this. I think there are two ways to check if a property has changed.
Adding PropertyChanged += LinkLabel_PropertyChanged;
Overriding, calling base
If I want to do something when more than one property has changed is there any difference between these two ways of calling a method?
public class LinkLabel : Label
{
public LinkLabel()
{
PropertyChanged += LinkLabel_PropertyChanged;
}
protected override void OnPropertyChanged(string propertyName = null)
{
base.OnPropertyChanged(propertyName);
// Check property name and do action here
}
private void LinkLabel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
// Check property name and do action here
}
}
For reference here is what I coded and I am wondering if that's a good solution:
public class LinkLabel : Label
{
public LinkLabel()
{
SetDynamicResource(Label.FontFamilyProperty, "Default-Regular");
SetDynamicResource(Label.FontSizeProperty, "LabelTextFontSize");
SetDynamicResource(Label.TextColorProperty, "LinkLabelColor");
VerticalOptions = LayoutOptions.CenterAndExpand;
VerticalTextAlignment = TextAlignment.Center;
}
public static readonly BindableProperty IsImportantProperty =
BindableProperty.Create(nameof(IsImportant), typeof(bool), typeof(LinkLabel), false);
public bool IsImportant
{
get { return (bool)GetValue(IsImportantProperty); }
set { SetValue(IsImportantProperty, value); }
}
protected override void OnPropertyChanged(string propertyName = null)
{
base.OnPropertyChanged(propertyName);
if (propertyName == IsEnabledProperty.PropertyName ||
propertyName == IsImportantProperty.PropertyName)
{
if (this.IsEnabled) {
if (this.IsImportant)
this.SetDynamicResource(Label.TextColorProperty, "LinkLabelImportantColor");
else
this.SetDynamicResource(Label.TextColorProperty, "LinkLabelColor");
}
else
this.SetDynamicResource(Label.TextColorProperty, "LinkLabelDisabledColor");
}
}
}
Yes, the difference is that registering for the PropertyChanged event works from outside, overriding the protected(!) OnPropertyChanged method works only from within derived classes of Label.
So you would normally only create a new derived LinkLabel class if you want to change the behavior of the label. There, you'd override the OnPropertyChanged (if you need to).
If you want to get informed about a change in your main form, you would register the event directly there. No need to create a derived class.

Is the TFS CheckIn Policy Evaluate method being invoked on a static object

I have the following sample TFS CheckIn Policy:
[Serializable()]
public class AuditControlsPolicy : PolicyBase
{
public List<string> list;
public AuditControlsPolicy() : base()
{
list = new List<string>() { "a", "b", "c" };
System.Windows.Forms.MessageBox.Show("in constructor");
}
public override string Description
{
get { return "my description"; }
}
public override string Type
{
get { return "my policy"; }
}
public override string TypeDescription
{
get { return "description"; }
}
public override string InstallationInstructions
{
get { return "install instructions"; }
}
public override Microsoft.TeamFoundation.VersionControl.Client.PolicyFailure[] Evaluate()
{
List<PolicyFailure> policyFailures = new List<PolicyFailure>();
if (list == null)
System.Windows.Forms.MessageBox.Show("list is null");
else
System.Windows.Forms.MessageBox.Show(String.Join(",", list.ToArray()));
return policyFailures.ToArray();
}
public override void DisplayHelp(PolicyFailure failure)
{
MessageBox.Show("No help available at this time");
}
public override void Activate(PolicyFailure failure)
{
MessageBox.Show(failure.Message);
}
protected override void OnPolicyStateChanged(PolicyFailure[] failures)
{
base.OnPolicyStateChanged(failures);
}
public override void Initialize(IPendingCheckin pendingCheckin)
{
base.Initialize(pendingCheckin);
pendingCheckin.PendingChanges.CheckedPendingChangesChanged += PendingCheckinCheckedPendingChangesChanged;
}
public override void Dispose()
{
PendingCheckin.PendingChanges.CheckedPendingChangesChanged -= PendingCheckinCheckedPendingChangesChanged;
base.Dispose();
}
private void PendingCheckinCheckedPendingChangesChanged(object sender, EventArgs e)
{
OnPolicyStateChanged(Evaluate());
}
public override bool Edit(IPolicyEditArgs policyEditArgs)
{
return true;
}
}
It is properly registered and "works" -- however, it appears that the instance member field list is not initialized when the Evaluate method is called.
When I toggle to Pending Changes view in Visual Studio with at least one pending change I get the message box of "In Constructor" multiple times. This is followed by "list is null", even though I clearly initialize the field in my instance constructor. If I declare my list as a static and initialize it in the instance constructor, then it display my list of values.
It almost seems as if Visual Studio is invoking the Evaluate method on a static object, even though it is not declared as such.
Is the Evaluate method being invoked as a static? Am I missing something about how I should be constructing my Policy Object?
Evaluate method shouldn't be invoked on a static class and the Visual Studio also will not invoke the Evaluate method on a static object. There must be something wrong in your code, try to move list = new List<string>() { "a", "b", "c" }; to public override void Initialize(IPendingCheckin pendingCheckin) and try again.
According to your description and code, guess the Evaluate() method had being invoked on the parent class PolicyBase before AuditControlsPolicy, that's why you got the list is nul.

Custom context menu on custom TreeItems

My domain model consists of multiple types of objects that need to be listed in a tree view, let's call them A and B. Each of these types needs to do a different action when selected in the tree view.
What I have done is this:
Created an interface for both of them to implement:
public interface ITVItem
{
public String toString();
public void HandleClick();
}
public class A implements ITVItem
{
#Override
public void HandleClick()
{
//Do something
}
#Override
public String toString()
{
return "Item type A";
}
}
public class B implements ITVItem
{
#Override
public void HandleClick()
{
//Do something different
}
#Override
public String toString()
{
return "Item type B";
}
}
The tree view is populated like this:
TreeItem<ITVItem> tvItem= new TreeItem<ITVItem> (new A());
rootNode.getChildren().add(target);
treeView.setRoot(rootNode);
Now, when the user clicks on an item i handle it like this:
#FXML
private void SelectionChanged(MouseEvent event)
{
Node node = event.getPickResult().getIntersectedNode();
// Accept clicks only on node cells, and not on empty spaces of the TreeView
if (node instanceof Text || (node instanceof TreeCell && ((TreeCell) node).getText() != null))
{
tVMain.getSelectionModel().getSelectedItem());
ITVItem tvItem = ((ITVItem) ((TreeItem) tVMain.getSelectionModel().getSelectedItem()).getValue());
tvItem.HandleClick();
}
}
What I need next is to have a custom context menu for each of the domain object types (A and B) to appear when the user right-clicks. How am I supposed to do this?

custom condition method attributes in C#

I'm wondering if I can do something like this in c#:
public CustomerManagerScreen()
{
[TestAttirubute("CustomerManagerScreen_Load")]
private void CustomerManagerScreen_Load(object sender, EventArgs e)
{
CustomerLoad();
}
}
as you can see, method name is a parameter of TestAttribute, what I want to achieve is CustomerManagerScreen_Load will be discarded depending on the result of the TestAttirubute
this is the attribute class...
public class TestAttirubute: System.Attribute
{
private string _MethodName = string.Empty;
public TestAttirubute(string MethodName)
{
this._MethodName = MethodName;
}
public bool hasPermission()
{
return (SessionManager.CurrentUser.UserRole.Role.Rights.Where(a => a.Resource.Code == this._MethodName).Count() != 0) ? true: false;
}
}
Not like that, but with the ConditionalAttribute you can.
One restriction however is that your methods must return void.

gwt widget - mutually-exclusive toggle button

I want a hybrid of a ToggleButton and RadioButton.
I want the "mutually-exclusive" part of RadioButton, and the gui look and behavior of ToggleButton(up and down states).
Does one already exist?
I've adapted kirushik's solution and created a simple "ToggleButtonPanel" widget that takes an arbitrary number of ToggleButtons (and possibly any other widgets you'd like to add) and a panel of your choosing (defaults to VerticalPanel) and makes the buttons mutually exclusive.
What's nice about this is that the panel itself fires ClickEvents when the buttons are clicked. This way, you can add a single ClickHandler to the ToggleGroupPanel and then determine which button was clicked using event.getSource()
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
public class ToggleButtonPanel extends Composite implements HasWidgets, HasClickHandlers{
public ToggleButtonPanel() {
this(new VerticalPanel());
}
public ToggleButtonPanel(Panel panel){
this.panel = panel;
initWidget(panel);
}
#Override
public void add(Widget w) {
if(w instanceof ToggleButton){
ToggleButton button = (ToggleButton) w;
button.addClickHandler(handler);
}
panel.add(w);
}
#Override
public void clear() {
panel.clear();
}
#Override
public Iterator<Widget> iterator() {
return panel.iterator();
}
#Override
public boolean remove(Widget w) {
return panel.remove(w);
}
#Override
public void setWidth(String width) {
panel.setWidth(width);
};
#Override
public void setHeight(String height) {
panel.setHeight(height);
}
private final Panel panel;
private ClickHandler handler = new ClickHandler(){
#Override
public void onClick(ClickEvent event) {
Iterator<Widget> itr = panel.iterator();
while(itr.hasNext()){
Widget w = itr.next();
if(w instanceof ToggleButton){
ToggleButton button = (ToggleButton) w;
button.setDown(false);
if(event.getSource().equals(button)) {
button.setDown(true);
}
}
}
for(ClickHandler h : handlers){
h.onClick(event);
}
}
};
private List<ClickHandler> handlers = new ArrayList<ClickHandler>();
#Override
public HandlerRegistration addClickHandler(final ClickHandler handler) {
handlers.add(handler);
return new HandlerRegistration() {
#Override
public void removeHandler() {
handlers.remove(handler);
}
};
}
}
Here is my pure-gwt variant:
class ThreeStateMachine extends FlowPanel{
// This is the main part - it will unset all the buttons in parent widget
// and then set only clicked one.
// One mutual handler works faster and is generally better for code reuse
private final ClickHandler toggleToThis = new ClickHandler() {
#Override
public void onClick(ClickEvent clickEvent) {
for(Widget b: ThreeStateMachine.this.getChildren()){
((ToggleButton)b).setDown(false);
}
((ToggleButton)clickEvent.getSource()).setDown(true);
}
};
private ThreeStateMachine() { // Create out widget and populat it with buttons
super();
ToggleButton b = new ToggleButton("one");
b.setDown(true);
b.addClickHandler(toggleToThis);
this.add(b);
b = new ToggleButton("two");
b.addClickHandler(toggleToThis);
this.add(b);
b = new ToggleButton("three");
b.addClickHandler(toggleToThis);
this.add(b);
}
}
Surely, one'll need css styles for gwt-ToggleButton with variants (-up-hovering etc.)
I have something that is both not in an extension library, and not dependent on a panel like the other answers. Define this class which manages the buttons. We're adding a new click listener to the buttons, which is in addition to whatever click handler you attached in the "GUI Content" class. I can't copy and paste this in, so hopefully it's syntatically correct.
public class MutuallyExclusiveToggleButtonCollection {
List<ToggleButton> m_toggleButtons = new ArrayList<ToggleButton>();
public void add(ToggleButton button) {
m_toggleButtons.add(button);
button.addClickListener(new ExclusiveButtonClickHandler());
}
private class ExclusiveButtonClickHandler impelments ClickHandler {
public void onClick(ClickEvent event) {
for(ToggleButton button : m_toggleButtons) {
boolean isSource = event.getSource().equals(button);
button.setIsDown(isSource);
}
}
}
Came across the same need, heres another solution that does away with the separate handler and works nicely in UIBinder with a declaration like:
<my:RadioToggleButton buttonGroup="btnGroup" text="Button 1" />
Here's the extended class:
import java.util.HashMap;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.user.client.ui.ToggleButton;
public class RadioToggleButton extends ToggleButton
{
private static HashMap<String,ButtonGroup> buttonGroups = new HashMap<>();
private ButtonGroup buttonGroup;
public #UiConstructor RadioToggleButton( String buttonGroupName )
{
buttonGroup = buttonGroups.get( buttonGroupName );
if( buttonGroup == null ){
buttonGroups.put( buttonGroupName, buttonGroup = new ButtonGroup() );
}
buttonGroup.addButton( this );
}
#Override
public void setDown( boolean isDown )
{
if( isDown ){
RadioToggleButton btn = buttonGroup.pressedBtn;
if( btn != null ){
btn.setDown( false );
}
buttonGroup.pressedBtn = this;
}
super.setDown( isDown );
}
private class ButtonGroup implements ClickHandler
{
RadioToggleButton pressedBtn = null;
public void addButton( ToggleButton button )
{
button.addClickHandler( this );
}
#Override
public void onClick( ClickEvent event )
{
Object obj = event.getSource();
if( pressedBtn != null ){
pressedBtn.setDown( false );
}
pressedBtn = (RadioToggleButton)obj;
pressedBtn.setDown( true );
}
}
}
gwt-ext toggleButtons
"This example illustrates Toggle Buttons. When clicked, such Buttons toggle their 'pressed' state.
The Bold, Italic and Underline toggle Buttons operate independently with respect to their toggle state while the text alignment icon Buttons belong to the same toggle group and so when one of them is click, the previously pressed Button returns to its normal state."
Register an additional ClickHandler on all the ToggleButtons.
For example, ToggleButtons home, tree, summary, detail.
public class Abc extends Composite implements ClickHandler {
ToggleButton home, tree, summary, detail
public Abc() {
// all your UiBinder initializations... blah, blah....
home.addClickHandler(this);
tree.addClickHandler(this);
summary.addClickHandler(this);
detail.addClickHandler(this);
}
#Override
public void onClick(ClickEvent p_event) {
Object v_source = p_event.getSource();
home.setDown(home==v_source);
tree.setDown(tree==v_source);
summary.setDown(summary==v_source);
detail.setDown(detail==v_source);
}
}
Of course, you just need to add all the other boilerplate code and register additional ClickHandlers for each ToggleButton.

Resources