Commits

Francisco Souza committed 5894742

Adição dos arquivos do projeto.

  • Participants

Comments (0)

Files changed (15)

+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+	<classpathentry kind="lib" path="lib/json-lib-2.3-jdk15.jar"/>
+	<classpathentry kind="lib" path="lib/commons-lang-2.4.jar"/>
+	<classpathentry kind="lib" path="lib/ezmorph-1.0.6.jar"/>
+	<classpathentry kind="lib" path="lib/commons-logging-1.1.1.jar"/>
+	<classpathentry kind="lib" path="lib/commons-collections-3.2.1.jar"/>
+	<classpathentry kind="lib" path="lib/commons-beanutils-1.8.2.jar"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>AcessaIBGE</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>

File .settings/org.eclipse.jdt.core.prefs

+#Sat Feb 06 10:52:02 BRST 2010
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6

File bin/net/franciscosouza/blog/ibge/bean/Cidade.class

Binary file added.

File bin/net/franciscosouza/blog/ibge/bean/Estado.class

Binary file added.

File bin/net/franciscosouza/blog/ibge/graphic/JanelaCombos.class

Binary file added.

File lib/commons-beanutils-1.8.2.jar

Binary file added.

File lib/commons-collections-3.2.1.jar

Binary file added.

File lib/commons-lang-2.4.jar

Binary file added.

File lib/commons-logging-1.1.1.jar

Binary file added.

File lib/ezmorph-1.0.6.jar

Binary file added.

File lib/json-lib-2.3-jdk15.jar

Binary file added.

File src/net/franciscosouza/blog/ibge/bean/Cidade.java

+package net.franciscosouza.blog.ibge.bean;
+
+public class Cidade {
+
+    private String nome;
+    
+    private Estado estado;
+
+    public String getNome() {
+        return nome;
+    }
+
+    public void setNome(String nome) {
+        this.nome = nome;
+    }
+
+    public Estado getEstado() {
+        return estado;
+    }
+
+    public void setEstado(Estado estado) {
+        this.estado = estado;
+    }
+
+    @Override
+    public String toString() {
+        return this.nome;
+    }
+    
+}

File src/net/franciscosouza/blog/ibge/bean/Estado.java

+package net.franciscosouza.blog.ibge.bean;
+
+public class Estado {
+
+    private String sigla;
+    
+    private String nome;
+
+    public String getSigla() {
+        return sigla;
+    }
+
+    public void setSigla(String sigla) {
+        this.sigla = sigla;
+    }
+
+    public String getNome() {
+        return nome;
+    }
+
+    public void setNome(String nome) {
+        this.nome = nome;
+    }
+
+    @Override
+    public String toString() {
+        return this.sigla;
+    }
+    
+}

File src/net/franciscosouza/blog/ibge/graphic/JanelaCombos.java

+package net.franciscosouza.blog.ibge.graphic;
+
+import java.awt.Dimension;
+import java.awt.GraphicsConfiguration;
+import java.awt.GridLayout;
+import java.awt.HeadlessException;
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Vector;
+
+import javax.swing.DefaultComboBoxModel;
+import javax.swing.JComboBox;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+
+import net.franciscosouza.blog.ibge.bean.Cidade;
+import net.franciscosouza.blog.ibge.bean.Estado;
+import net.sf.json.JSONArray;
+import net.sf.json.JSONObject;
+
+public class JanelaCombos extends JFrame implements ItemListener {
+
+    private static final long serialVersionUID = 1L;
+
+    private JComboBox comboCidades, comboEstados;
+
+    public JanelaCombos() throws HeadlessException, IOException {
+        super();
+        this.montarJanela();
+    }
+
+    public JanelaCombos(GraphicsConfiguration arg0) throws IOException {
+        super(arg0);
+        this.montarJanela();
+    }
+
+    public JanelaCombos(String arg0, GraphicsConfiguration arg1) throws IOException {
+        super(arg0, arg1);
+        this.montarJanela();
+    }
+
+    public JanelaCombos(String arg0) throws HeadlessException, IOException {
+        super(arg0);
+        this.montarJanela();
+    }
+
+    private void montarJanela() throws IOException {
+        this.setSize(new Dimension(500, 100));
+        this.getContentPane().setLayout(new GridLayout(2, 2));
+        JLabel labelEstados = new JLabel("Selecione o estado:");
+        this.getContentPane().add(labelEstados);
+        this.preencherComboEstados();
+        this.getContentPane().add(this.comboEstados);
+
+        JLabel labelCidades = new JLabel("Selecione a cidade:");
+        this.getContentPane().add(labelCidades);
+        this.comboCidades = new JComboBox(new DefaultComboBoxModel());
+        this.getContentPane().add(this.comboCidades);
+    }
+
+private void preencherComboCidades() throws IOException {
+    Estado selecionado = (Estado) this.comboEstados.getSelectedItem();
+    URL url = new URL("http://localhost:8000/api/cidades/" + selecionado.getSigla());
+    URLConnection urlConnection = url.openConnection();
+    BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
+    String linhas = "";
+    String linha = null;
+    while ((linha = reader.readLine()) != null) {
+        linhas += linha;
+    }
+    reader.close();
+    JSONArray array = JSONArray.fromObject(linhas);
+    Object[] objCidades = array.toArray();
+    Map<String, Class> classMap = new HashMap<String, Class>();
+    classMap.put("estado", Estado.class);
+    DefaultComboBoxModel model = (DefaultComboBoxModel) this.comboCidades.getModel();
+    model.removeAllElements();
+    for (Object objCidade : objCidades) {
+        JSONObject object = JSONObject.fromObject(objCidade);
+        Cidade cidade = (Cidade) JSONObject.toBean(object, Cidade.class, classMap);
+        model.addElement(cidade);
+    }
+}
+
+private void preencherComboEstados() throws IOException {
+    URL url = new URL("http://localhost:8000/api/estados");
+    URLConnection urlConnection = url.openConnection();
+    BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
+    String linhas = "";
+    String linha = null;
+    while ((linha = reader.readLine()) != null) {
+        linhas += linha;
+    }
+    reader.close();
+    JSONArray array = JSONArray.fromObject(linhas);
+    Object[] objEstados = array.toArray();
+    DefaultComboBoxModel model = new DefaultComboBoxModel();
+    model.addElement("Selecione o estado");
+    for (Object objEstado : objEstados) {
+        JSONObject object = JSONObject.fromObject(objEstado);
+        Estado estado = (Estado) JSONObject.toBean(object, Estado.class);
+        model.addElement(estado);
+    }
+    this.comboEstados = new JComboBox(model);
+    this.comboEstados.addItemListener(this);
+}
+
+    public static void main(String[] args) {
+        try {
+            JanelaCombos janelaCombos = new JanelaCombos("Acessando API");
+            janelaCombos.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+            janelaCombos.setVisible(true);
+        } catch (HeadlessException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    @Override
+    public void itemStateChanged(ItemEvent e) {
+        if (e.getSource() == this.comboEstados) {
+            try {
+                if (this.comboEstados.getSelectedIndex() > 0) {
+                    this.preencherComboCidades();
+                } else {
+                    ((DefaultComboBoxModel)this.comboCidades.getModel()).removeAllElements();
+                }
+            } catch (IOException e1) {
+                e1.printStackTrace();
+            }
+        }
+    }
+
+}