Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added InitialClassMixin to be defined after attached event of MaterialWidget #308

Merged
merged 4 commits into from
May 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ protected AbstractButton() {
getElement().getStyle().setCursor(Style.Cursor.POINTER);
}

protected AbstractButton(String... initialClass) {
this();
setInitialClass(initialClass);
}

protected AbstractButton(String text, String bgColor, WavesType waves) {
this(null, text, bgColor);
setWaves(waves);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public AbstractIconButton() {
setIconPosition(IconPosition.LEFT);
}

public AbstractIconButton(String... initialClass) {
super();
setInitialClass(initialClass);
}

@Override
public MaterialIcon getIcon() {
return icon;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@
*/
package gwt.material.design.client.base;

/*
* #%L
* GwtMaterial
* %%
* Copyright (C) 2015 - 2016 GwtMaterialDesign
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/


import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.InputElement;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package gwt.material.design.client.base;

/*
* #%L
* GwtMaterial
* %%
* Copyright (C) 2015 - 2016 GwtMaterialDesign
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/



public interface HasInitialClass {

/**
* Set the initial class into Material Components
* @param initialClass
*/
void setInitialClass(String... initialClass);

/**
* Get the initial class of Material Components
* @return
*/
String[] getInitialClass();

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,7 @@
*/

import gwt.material.design.client.base.helper.StyleHelper;
import gwt.material.design.client.base.mixin.ColorsMixin;
import gwt.material.design.client.base.mixin.CssNameMixin;
import gwt.material.design.client.base.mixin.EnabledMixin;
import gwt.material.design.client.base.mixin.FlexboxMixin;
import gwt.material.design.client.base.mixin.FocusableMixin;
import gwt.material.design.client.base.mixin.FontSizeMixin;
import gwt.material.design.client.base.mixin.GridMixin;
import gwt.material.design.client.base.mixin.IdMixin;
import gwt.material.design.client.base.mixin.ScrollspyMixin;
import gwt.material.design.client.base.mixin.SeparatorMixin;
import gwt.material.design.client.base.mixin.ShadowMixin;
import gwt.material.design.client.base.mixin.ToggleStyleMixin;
import gwt.material.design.client.base.mixin.TooltipMixin;
import gwt.material.design.client.base.mixin.WavesMixin;
import gwt.material.design.client.base.mixin.*;
import gwt.material.design.client.constants.CenterOn;
import gwt.material.design.client.constants.Display;
import gwt.material.design.client.constants.Flex;
Expand All @@ -57,8 +44,10 @@

public class MaterialWidget extends ComplexPanel implements HasId, HasEnabled, HasTextAlign, HasColors, HasGrid,
HasShadow, Focusable, HasInlineStyle, HasSeparator, HasScrollspy, HasHideOn, HasShowOn, HasCenterOn,
HasCircle, HasWaves, HasDataAttributes, HasFloat, HasTooltip, HasFlexbox, HasHoverable, HasFontWeight, HasDepth {
HasCircle, HasWaves, HasDataAttributes, HasFloat, HasTooltip, HasFlexbox, HasHoverable, HasFontWeight, HasDepth, HasInitialClass {


private InitialClassMixin<MaterialWidget> initialClassMixin;
private IdMixin<MaterialWidget> idMixin;
private EnabledMixin<MaterialWidget> enabledMixin;
private CssNameMixin<MaterialWidget, TextAlign> textAlignMixin;
Expand All @@ -84,6 +73,11 @@ public class MaterialWidget extends ComplexPanel implements HasId, HasEnabled, H
public MaterialWidget() {
}

public MaterialWidget(Element element, String... initialClass) {
this(element);
setInitialClass(initialClass);
}

public MaterialWidget(Element element) {
setElement(element);
}
Expand Down Expand Up @@ -208,6 +202,11 @@ public ToggleStyleMixin<MaterialWidget> getTruncateMixin() {
return truncateMixin;
}

public InitialClassMixin<MaterialWidget> getInitialClassMixin() {
if(initialClassMixin == null) { initialClassMixin = new InitialClassMixin<>(this); }
return initialClassMixin;
}

@Override
public void setId(String id) {
getIdMixin().setId(id);
Expand Down Expand Up @@ -692,4 +691,14 @@ private native void stopTouchStartEvent(Element e) /*-{
event.stopPropagation();
});
}-*/;

@Override
public void setInitialClass(String... initialClass) {
getInitialClassMixin().setInitialClass(initialClass);
}

@Override
public String[] getInitialClass() {
return getInitialClassMixin().getInitialClass();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package gwt.material.design.client.base.mixin;

/*
* #%L
* GwtMaterial
* %%
* Copyright (C) 2015 - 2016 GwtMaterialDesign
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/


import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.Widget;
import gwt.material.design.client.base.HasInitialClass;

/**
* @author kevzlou7979
*/
public class InitialClassMixin<T extends UIObject & HasInitialClass> extends AbstractMixin<T> implements HasInitialClass {

private String[] initialClass;

public InitialClassMixin(final T uiObject) {
super(uiObject);
}


@Override
public void setInitialClass(final String... initialClass) {
this.initialClass = initialClass;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every time setInitialClass is called it adds a new attach handler without removing the existing one. Causing unexpected behavior when called multiple times. Should change this to remove the existing handler before creating a new one.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also noting that it may not happen often it needs to be a use case that is covered.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok so we will check if it is attached, if not attached then add handler into else then we will not

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that would be the solution 👍

if(!((Widget)uiObject).isAttached()){
((Widget)uiObject).addAttachHandler(new AttachEvent.Handler() {
@Override
public void onAttachOrDetach(AttachEvent event) {

for(String s : initialClass) {
if(initialClass != null && !s.isEmpty()) {
uiObject.removeStyleName(s);
}
if(event.isAttached()){
if(initialClass != null && !s.isEmpty()) {
uiObject.addStyleName(s);
}
}
}
}
});
}
}


@Override
public String[] getInitialClass() {
return initialClass;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* #L%
*/

import com.google.gwt.dom.client.Document;
import gwt.material.design.client.ui.html.Span;

import com.google.gwt.user.client.ui.HasText;
Expand All @@ -45,7 +46,7 @@ public class MaterialBadge extends Span implements HasText {
* Collection, DropDown, SideNav and any other Material components.
*/
public MaterialBadge() {
setStyleName("badge sideBarBadge");
super(Document.get().createSpanElement(), "badge", "sideBarBadge");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
public class MaterialBreadcrumb extends AbstractIconButton {

public MaterialBreadcrumb() {
setStyleName("breadcrumb");
super("breadcrumb");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ public class MaterialCard extends MaterialWidget implements HasAxis {
* Creates and empty card.
*/
public MaterialCard() {
super(Document.get().createDivElement());
setStyleName("card");
super(Document.get().createDivElement(), "card");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
public class MaterialCardAction extends MaterialWidget {

public MaterialCardAction(){
super(Document.get().createDivElement());
setStyleName("card-action");
super(Document.get().createDivElement(), "card-action");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
public class MaterialCardContent extends MaterialWidget {

public MaterialCardContent() {
super(Document.get().createDivElement());
setStyleName("card-content");
super(Document.get().createDivElement(), "card-content");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
public class MaterialCardImage extends MaterialWidget {

public MaterialCardImage(){
super(Document.get().createDivElement());
setStyleName("card-image");
super(Document.get().createDivElement(), "card-image");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
public class MaterialCardReveal extends MaterialWidget {

public MaterialCardReveal(){
super(Document.get().createDivElement());
setStyleName("card-reveal");
super(Document.get().createDivElement(), "card-reveal");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public class MaterialCardTitle extends MaterialWidget implements HasIcon, HasTex
private Span span = new Span();

public MaterialCardTitle() {
super(Document.get().createSpanElement());
setStyleName("card-title activator");
super(Document.get().createSpanElement(), "card-title" , "activator");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ public class MaterialChip extends MaterialWidget implements HasImage, HasIcon, H
* Creates an empty chip.
*/
public MaterialChip() {
super(Document.get().createDivElement());
setStyleName("chip");
super(Document.get().createDivElement(), "chip");
}

public void setText(String text){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ protected interface HasCollapsibleParent {
* Creates an empty collapsible
*/
public MaterialCollapsible() {
super(Document.get().createULElement());
setStyleName("collapsible");
super(Document.get().createULElement(), "collapsible");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public class MaterialCollapsibleBody extends MaterialWidget implements HasCollap
* Creates empty collapsible body.
*/
public MaterialCollapsibleBody() {
super(Document.get().createDivElement());
setStyleName("collapsible-body");
super(Document.get().createDivElement(), "collapsible-body");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public class MaterialCollapsibleHeader extends MaterialWidget implements HasAllM
/** Creates empty collapsible header.
*/
public MaterialCollapsibleHeader() {
super(Document.get().createDivElement());
setStyleName("collapsible-header");
super(Document.get().createDivElement(), "collapsible-header");
}

/** Adds other components as header.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ public class MaterialCollection extends MaterialWidget {
* Creates an empty collection component.
*/
public MaterialCollection() {
super(Document.get().createULElement());
setStyleName("collection");
super(Document.get().createULElement(), "collection");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public class MaterialCollectionItem extends MaterialWidget implements HasClickHa
private HandlerRegistration handlerReg;

public MaterialCollectionItem() {
super(Document.get().createLIElement());
setStyleName("collection-item");
super(Document.get().createLIElement(), "collection-item");
UiHelper.addMousePressedHandlers(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
public class MaterialCollectionSecondary extends MaterialWidget implements HasHref {

public MaterialCollectionSecondary() {
super(Document.get().createAnchorElement());
setStyleName("secondary-content");
super(Document.get().createAnchorElement(), "secondary-content");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public class MaterialColumn extends MaterialWidget implements HasWaves, HasVisib
HasAllMouseHandlers, HasDoubleClickHandlers {

public MaterialColumn() {
super(Document.get().createDivElement());
setStyleName("col");
super(Document.get().createDivElement(), "col");
}

public MaterialColumn(int small, int medium, int large) {
Expand Down
Loading