Created
June 30, 2011 13:56
-
-
Save jrdalpra/1056274 to your computer and use it in GitHub Desktop.
Esboço de um framework Swing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- usar o que já existe | |
- apenas criar facilitadores para que não seja necessário "gastar" tanto tempo | |
- convenção sobre configuração | |
- usar anotações ao invés de setters do swing | |
- usar o máximo possível de frameworks já existentes. apenas criar uma série de faciliadores | |
@Entity | |
@Bindable | |
class Pais { | |
@Id | |
@Mask("999.999") | |
@Formatter(NumberFormatter.class) | |
private Long codigo; | |
@NotNull | |
@NotEmpty | |
@Length(min=2,max=120) | |
private String nome; | |
// getters , setter ( lombok ) | |
} | |
// Tela de cadastro de Pais | |
@Properties({ | |
@Property(name="title",value="{pais.classe}"), // buscar de um resourcebundle padrão | |
@Property(name="size",value="[300,300]"), /*@Property(name="size",value="[{largura.padrao},{altura.padrao}]"),*/ | |
@Property(name="visible",value="true") | |
}) | |
public class PaisForm extends JPanel | |
{ | |
/* | |
1o) Injetar as dependencias | |
2o) temos que que reconhecer quais são os componentes existentes | |
3o) Definir properties dos componentes declarados | |
4o) Criar binding entre os componentes | |
*/ | |
@Inject // CDI | |
private PaisService service; | |
@BindGroup({ | |
@Bindable(source="pais.codigo",mutual=true) // sabe que a property padrão é "text" | |
@Bindable(source="box.selected",property="enabled") | |
}) | |
private JTextField codigo; | |
private JTextField outroCampo; | |
@Bindable(source="box.selected",property="enabled") // está habilitado qdo o checkbox está selecionado | |
private JTextField name; | |
private JCheckBox box; | |
@Properties({@Property(name="label",value="{acao.salvar}")}) | |
@Action(method="aoClicarNoBotaoSalvar") | |
@Icon("classpath:META-INF/images/buttons/salvar.png") | |
private JButton salvar; | |
@Properties({@Property(name="label",value="{acao.cancelar}", | |
@Property(name="icon",value="{images.button.cancelar}") // 1o mudar o valor para o conteudo do resource bundle, depois carrega icone | |
private JButton cancelar; // action padrão onButtonCancelarPressed | |
private Pais pais; | |
//public PaisForm(PaisService service, PaisValidator validator){ | |
public PaisForm(PaisService service, BeanValidator validator){ | |
this.service = service; | |
this.validator = validator; | |
this.validator.view(this); | |
} | |
@PostConstruct | |
public void init(){ | |
setLayout(form(label("Código"),label("Nome")).rowGap("2px")); /* formlayout, já diz os labels e o gap entre as linhas */ | |
add(codigo,constraint(1,2,PREFFERED,PREFFERED)); // linha 1, coluna 2, largura e altura preferencial | |
add(name,constraint(2,2,FILL,PREFFERED); // linha 2 , coluna 2, largura preenche e altura preferencial | |
} | |
// todo, permitir que o usuario mude o "padrao" de metodos "action" | |
public void onButtonCancelarPressed(...){ | |
clear(this); | |
close(this); // metodo static de classe utilitaria | |
} | |
public void aoClicarNoBotaoSalvar(){ | |
try{ | |
// validação na parte cliente | |
if( validator.isValid(pais) && confirm('Deseja salvar?') ){ | |
service.salva(pais); | |
clear(this); // metodo static de classe utilitaria | |
} | |
} catch(Exception exception){ | |
show(exception); // mostrar erro usando classe utilitaria | |
} | |
} | |
public static void main(String[] args) { | |
form = ... // usar CDI | |
} | |
} | |
// Tela de Login | |
@Properties({ @Property(name = "title", value = "Login") }) | |
public class LoginForm extends JFrame { | |
private static final long serialVersionUID = -6040427781663021616L; | |
@Bindable(source = "usuario.login", mutual = true) | |
private JTextField login; | |
@BindGroup({ | |
@Bindable(source = "usuario.senha", mutual = true), | |
@Bindable(source = "${login.text != null && login.text.length > 0}", property = "enabled") }) | |
private JPasswordField senha; | |
private Usuario usuario; | |
private JLabel lbl_login; | |
private JLabel lbl_senha; | |
@Action(method="aoClicarNoBotaoOk") | |
private JButton btn_ok; | |
public void initUI() { | |
setLayout(new BorderLayout()); | |
add(new JPanel() { | |
{ | |
setLayout(form(lbl_login, lbl_senha)); | |
add(login, null); | |
add(senha, null); | |
} | |
}, BorderLayout.CENTER); | |
add(new JPanel() { | |
{ | |
setLayout(flow()); | |
add(btn_ok); | |
} | |
}, BorderLayout.SOUTH); | |
} | |
public void aoClicarNoBotaoOk(ActionEvent event){ | |
System.out.println(usuario.getLogin()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment