Created
July 9, 2012 00:33
-
-
Save repomaa/3073562 to your computer and use it in GitHub Desktop.
Gui for adding and searching ingredients
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
package gui; | |
import java.awt.Component; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.io.IOException; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Vector; | |
import javax.swing.Box; | |
import javax.swing.BoxLayout; | |
import javax.swing.InputVerifier; | |
import javax.swing.JButton; | |
import javax.swing.JComboBox; | |
import javax.swing.JComponent; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JPanel; | |
import javax.swing.JScrollPane; | |
import javax.swing.JTable; | |
import javax.swing.JTextField; | |
import javax.swing.table.DefaultTableModel; | |
import javax.swing.text.JTextComponent; | |
import logic.Ingredient; | |
import logic.database.JSONException; | |
import logic.database.Manager; | |
public class Gui extends JFrame implements ActionListener { | |
private JTextField name; | |
private JTextField carbs; | |
private JTextField fat; | |
private JTextField protein; | |
private JTextField serving; | |
private JTextField alcohol; | |
private JComboBox<String> servingType; | |
private Manager manager; | |
private Integer id; | |
private final Vector<String> columnNames = new Vector<String>(); | |
MyTableModel<Object> model; | |
private JTextField search; | |
public Gui() throws IOException { | |
columnNames.addAll(Arrays.asList(new String[]{"Id","Name","Carbs","Fat","Protein","Alcohol","Serving","Serving type"})); | |
JTable table = table(); | |
JPanel panel = new JPanel(); | |
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); | |
setContentPane(panel); | |
panel.add(inputBox()); | |
panel.add(searchBox()); | |
panel.add(new JScrollPane(table)); | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
manager = new Manager(); | |
pack(); | |
} | |
private Box searchBox() { | |
Box box = Box.createHorizontalBox(); | |
box.add(new JLabel("Search: ")); | |
search = new JTextField(); | |
box.add(search); | |
JButton searchButton = new JButton("Search"); | |
search.setActionCommand("search"); | |
search.addActionListener(this); | |
box.add(searchButton); | |
return box; | |
} | |
private JTable table() { | |
model = new MyTableModel<Object>(columnNames); | |
JTable table = new JTable(model); | |
return table; | |
} | |
private Box inputBox() { | |
Box box = Box.createVerticalBox(); | |
Box nameBox = Box.createHorizontalBox(); | |
nameBox.add(new JLabel("Name: ")); | |
name = new JTextField(); | |
nameBox.add(name); | |
nameBox.add(Box.createHorizontalGlue()); | |
box.add(nameBox); | |
Box nutriBox = Box.createHorizontalBox(); | |
nutriBox.add(new JLabel("Carbs: ")); | |
carbs = new FloatField(); | |
nutriBox.add(carbs); | |
nutriBox.add(new JLabel("g ")); | |
nutriBox.add(new JLabel("Fat: ")); | |
fat = new FloatField(); | |
nutriBox.add(fat); | |
nutriBox.add(new JLabel("g ")); | |
nutriBox.add(new JLabel("Protein: ")); | |
protein = new FloatField(); | |
nutriBox.add(protein); | |
nutriBox.add(new JLabel("g ")); | |
nutriBox.add(new JLabel("Alcohol: ")); | |
alcohol = new FloatField(); | |
nutriBox.add(alcohol); | |
nutriBox.add(new JLabel("g")); | |
box.add(nutriBox); | |
Box servingBox = Box.createHorizontalBox(); | |
servingBox.add(new JLabel("Serving size: ")); | |
serving = new FloatField(); | |
servingBox.add(serving); | |
servingBox.add(new JLabel("g ")); | |
servingType = new JComboBox<String>(); | |
servingType.addItem("Pieces"); | |
servingType.addItem("Slices"); | |
servingType.addItem("Servings"); | |
servingType.addItem("Cups"); | |
servingType.addItem("Glasses"); | |
servingType.addItem("Halves"); | |
servingType.addItem("Tablespoons"); | |
servingType.addItem("Teaspoons"); | |
servingType.addItem("Bottles"); | |
servingType.addItem("Cans"); | |
servingType.setSelectedIndex(2); | |
servingBox.add(servingType); | |
servingBox.add(Box.createHorizontalGlue()); | |
JButton addUpdate = new JButton("Add/Update"); | |
servingBox.add(addUpdate); | |
addUpdate.setActionCommand("addUpdate"); | |
addUpdate.addActionListener(this); | |
box.add(servingBox); | |
return box; | |
} | |
public static void main(String[] args) { | |
Gui gui; | |
try { | |
gui = new Gui(); | |
gui.setVisible(true); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
if(e.getActionCommand().equals("addUpdate")) { | |
try { | |
Ingredient ingredient = new Ingredient(name.getText().trim(), | |
Double.parseDouble(carbs.getText()), | |
Double.parseDouble(fat.getText().replace(",", ".")), | |
Double.parseDouble(protein.getText().replace(",", ".")), | |
Double.parseDouble(alcohol.getText().replace(",", ".")), | |
Double.parseDouble(serving.getText().replace(",", ".")), | |
servingType.getSelectedIndex()); | |
if(id == null) { | |
manager.add(ingredient); | |
} else { | |
manager.update(id, ingredient); | |
id = null; | |
} | |
} catch (NumberFormatException e1) { | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} catch (IOException e1) { | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} catch (JSONException e1) { | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} | |
} | |
else if (e.getActionCommand().equals("search")) { | |
try { | |
List<Ingredient> ingredients = manager.search(search.getText()); | |
updateTable(ingredients); | |
} catch (IOException e1) { | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} catch (JSONException e1) { | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} | |
} | |
} | |
private void updateTable(List<Ingredient> ingredients) { | |
Vector<Vector<Object>> data = new Vector<Vector<Object>>(); | |
for(Ingredient ingredient : ingredients) { | |
Vector<Object> ingredientData = new Vector<Object>(); | |
ingredientData.add(ingredient.id); | |
ingredientData.add(ingredient.name); | |
ingredientData.add(ingredient.carbs); | |
ingredientData.add(ingredient.fat); | |
ingredientData.add(ingredient.protein); | |
ingredientData.add(ingredient.alcohol); | |
ingredientData.add(ingredient.serving); | |
ingredientData.add(ingredient.servingType); | |
data.add(ingredientData); | |
} | |
model.setData(data); | |
pack(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment