Cook book written in Java
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

356 lines
12 KiB

package ksiazka.kucharska;
import java.awt.BorderLayout;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.RenderingHints;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.border.LineBorder;
import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.swing.SwingConstants;
import ksiazka.kucharska.RetrieveData;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.ScrollPaneConstants;
import java.awt.Dimension;
public class FrmMain extends JFrame {
/**
*
*/
private static final long serialVersionUID = 2634383304749703669L;
private JPanel contentPane;
private JTextField txtSearch;
public static JTextField txtEdTitle;
public static JTextField txtTags;
private JButton btnSave;
private JButton btnCancel;
public static JTextArea txtDescription;
private JButton btnAddRecipe;
private JButton btnDelRecipe;
private JButton btnLoadImg;
public static JLabel lblRecipeTitle;
private static DefaultListModel recipesListModel;
public static JLabel imgRecipe;
private JFileChooser fcImg;
public static ImageIcon imgDefaultRecipe;
private JList recipesList;
private JScrollPane scrollPane_1;
public static JTextArea txtIngredients;
private JScrollPane scrollPane_2;
void DeactivateAddMenu() {
btnAddRecipe.setEnabled(true);
btnCancel.setVisible(false);
btnDelRecipe.setEnabled(true);
btnLoadImg.setVisible(false);
txtSearch.setEnabled(true);
btnSave.setVisible(false);
txtDescription.setEditable(false);
txtEdTitle.setVisible(false);
txtIngredients.setEditable(false);
txtTags.setEditable(false);
lblRecipeTitle.setVisible(true);
recipesList.setEnabled(true);
}
void ActivateAddMenu() {
btnAddRecipe.setEnabled(false);
btnCancel.setVisible(true);
btnDelRecipe.setEnabled(false);
btnLoadImg.setVisible(true);
txtSearch.setEnabled(false);
btnSave.setVisible(true);
txtDescription.setEditable(true);
txtEdTitle.setVisible(true);
txtIngredients.setEditable(true);
txtTags.setEditable(true);
lblRecipeTitle.setVisible(false);
recipesList.setEnabled(false);
imgRecipe.setIcon(imgDefaultRecipe);
//czyszczenie pól formularza
txtDescription.setText("");
txtEdTitle.setText("");
txtIngredients.setText("");
txtTags.setText("");
}
public static DefaultListModel getRecipesList()
{
return recipesListModel;
}
/**
* Create the frame.
*/
public FrmMain() {
setResizable(false);
setTitle("Książka Kucharska v1.0");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 723, 549);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JSplitPane splitPane = new JSplitPane();
splitPane.setResizeWeight(0.18);
splitPane.setRequestFocusEnabled(false);
splitPane.setPreferredSize(new Dimension(200, 27));
splitPane.setDividerSize(5);
splitPane.setEnabled(false);
contentPane.add(splitPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
panel.setMaximumSize(new Dimension(100, 32767));
splitPane.setLeftComponent(panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[] {0, 0};
gbl_panel.rowHeights = new int[] {0, 0, 0};
gbl_panel.columnWeights = new double[]{1.0, 1.0};
gbl_panel.rowWeights = new double[]{0.0, 1.0, 0.0};
panel.setLayout(gbl_panel);
txtSearch = new JTextField();
txtSearch.setText("wpisz tytuł/tagi...");
GridBagConstraints gbc_txtSearch = new GridBagConstraints();
gbc_txtSearch.gridwidth = 2;
gbc_txtSearch.insets = new Insets(5, 5, 5, 0);
gbc_txtSearch.fill = GridBagConstraints.HORIZONTAL;
gbc_txtSearch.gridx = 0;
gbc_txtSearch.gridy = 0;
panel.add(txtSearch, gbc_txtSearch);
txtSearch.setColumns(10);
recipesListModel = new DefaultListModel();
recipesList = new JList(recipesListModel);
recipesList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e){
if(recipesList.getSelectedIndex() != -1)
RetrieveData.fillComponents(recipesList.getSelectedValue().toString());
}
});
scrollPane_2 = new JScrollPane();
scrollPane_2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane_2.setMaximumSize(new Dimension(100, 100));
scrollPane_2.setViewportView(recipesList);
GridBagConstraints gbc_scrollPane_2 = new GridBagConstraints();
gbc_scrollPane_2.gridwidth = 2;
gbc_scrollPane_2.insets = new Insets(0, 0, 5, 0);
gbc_scrollPane_2.fill = GridBagConstraints.BOTH;
gbc_scrollPane_2.gridx = 0;
gbc_scrollPane_2.gridy = 1;
panel.add(scrollPane_2, gbc_scrollPane_2);
btnAddRecipe = new JButton("Nowy");
GridBagConstraints gbc_btnAddRecipe = new GridBagConstraints();
gbc_btnAddRecipe.fill = GridBagConstraints.HORIZONTAL;
gbc_btnAddRecipe.insets = new Insets(0, 5, 0, 5);
gbc_btnAddRecipe.gridx = 0;
gbc_btnAddRecipe.gridy = 2;
panel.add(btnAddRecipe, gbc_btnAddRecipe);
btnAddRecipe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ActivateAddMenu();
}
});
btnDelRecipe = new JButton("Usuń");
GridBagConstraints gbc_btnDelRecipe = new GridBagConstraints();
gbc_btnDelRecipe.fill = GridBagConstraints.HORIZONTAL;
gbc_btnDelRecipe.gridx = 1;
gbc_btnDelRecipe.gridy = 2;
panel.add(btnDelRecipe, gbc_btnDelRecipe);
btnDelRecipe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultListModel model = (DefaultListModel) recipesList.getModel();
if (recipesList.getSelectedIndex() != -1)
{
int dialogResult = JOptionPane.showConfirmDialog (null, "Czy na pewno chcesz usunąć przepis?", "Pytanie", JOptionPane.YES_NO_OPTION);
if(dialogResult == JOptionPane.YES_OPTION){
RetrieveData.removeRecipe(recipesList.getSelectedValue().toString());
model.remove(recipesList.getSelectedIndex());
if (recipesList.getModel().getSize()>0)
{
recipesList.setSelectedIndex(0);
RetrieveData.fillComponents(recipesList.getSelectedValue().toString());
}
else
{
txtDescription.setText("");
lblRecipeTitle.setText("Nazwa przepisu");
txtIngredients.setText("");
txtTags.setText("");
}
}
}
}
});
JPanel panel_2 = new JPanel();
splitPane.setRightComponent(panel_2);
panel_2.setLayout(null);
btnSave = new JButton("Dodaj");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
RetrieveData.addNewRecipe();
DefaultListModel model = (DefaultListModel) recipesList.getModel();
model.addElement(txtEdTitle.getText());
DeactivateAddMenu();
recipesList.setSelectedIndex(recipesList.getModel().getSize()-1);
RetrieveData.fillComponents(recipesList.getSelectedValue().toString());
}
});
btnSave.setVisible(false);
btnSave.setBounds(319, 12, 117, 25);
panel_2.add(btnSave);
txtEdTitle = new JTextField();
txtEdTitle.setVisible(false);
txtEdTitle.setBounds(26, 15, 281, 19);
panel_2.add(txtEdTitle);
txtEdTitle.setColumns(10);
lblRecipeTitle = new JLabel("Nazwa przepisu");
lblRecipeTitle.setFont(new Font("Dialog", Font.BOLD, 18));
lblRecipeTitle.setBounds(12, 12, 442, 22);
panel_2.add(lblRecipeTitle);
JLabel lblIngredients = new JLabel("Składniki:");
lblIngredients.setBounds(26, 61, 70, 15);
panel_2.add(lblIngredients);
imgRecipe = new JLabel("");
imgRecipe.setHorizontalAlignment(SwingConstants.CENTER);
imgRecipe.setBorder(new LineBorder(SystemColor.textHighlightText));
imgRecipe.setBounds(272, 88, 164, 126);
panel_2.add(imgRecipe);
btnLoadImg = new JButton("Wczytaj");
fcImg = new JFileChooser();
FileNameExtensionFilter ffAll = new FileNameExtensionFilter("Wszystkie grafiki", "jpg", "jpeg", "gif", "png");
fcImg.addChoosableFileFilter(ffAll);
fcImg.addChoosableFileFilter(new FileNameExtensionFilter("Pliki JPEG", "jpg", "jpeg"));
fcImg.addChoosableFileFilter(new FileNameExtensionFilter("Pliki GIF", "gif"));
fcImg.addChoosableFileFilter(new FileNameExtensionFilter("Pliki PNG", "png"));
fcImg.setAcceptAllFileFilterUsed(false);
fcImg.setFileFilter(ffAll);
imgDefaultRecipe = new ImageIcon("no_img_recipe.jpg");
imgRecipe.setIcon(imgDefaultRecipe);
btnLoadImg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//otwórz okno dialogowe
int returnVal = fcImg.showOpenDialog(FrmMain.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fcImg.getSelectedFile();
try {
BufferedImage srcImg = ImageIO.read(file); //czytaj plik
//wyznacz szerokość obrazka jako stosunek (przy maksymalizacji wysokości
int w = (int)Math.ceil(srcImg.getWidth()/(double)srcImg.getHeight()*imgRecipe.getHeight());
BufferedImage resizedImg = new BufferedImage(w, imgRecipe.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, w, imgRecipe.getHeight(), null);
g2.dispose();
ImageIcon iconLogo = new ImageIcon(resizedImg);
imgRecipe.setIcon(iconLogo);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
btnLoadImg.setVisible(false);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(26, 254, 410, 196);
panel_2.add(scrollPane);
txtDescription = new JTextArea();
txtDescription.setEditable(false);
txtDescription.setLineWrap(true);
scrollPane.setViewportView(txtDescription);
btnLoadImg.setBounds(304, 217, 102, 25);
panel_2.add(btnLoadImg);
txtTags = new JTextField();
txtTags.setEditable(false);
txtTags.setBounds(26, 480, 410, 19);
panel_2.add(txtTags);
txtTags.setColumns(10);
JLabel lblTags = new JLabel("Tagi:");
lblTags.setBounds(26, 462, 70, 15);
panel_2.add(lblTags);
btnCancel = new JButton("Anuluj");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DeactivateAddMenu();
if(recipesList.getSelectedIndex() != -1)
RetrieveData.fillComponents(recipesList.getSelectedValue().toString());
}
});
btnCancel.setVisible(false);
btnCancel.setBounds(319, 46, 117, 25);
panel_2.add(btnCancel);
JLabel lblDescription = new JLabel("Opis wykonania:");
lblDescription.setBounds(26, 227, 152, 15);
panel_2.add(lblDescription);
scrollPane_1 = new JScrollPane();
scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane_1.setBounds(26, 88, 228, 126);
panel_2.add(scrollPane_1);
txtIngredients = new JTextArea();
txtIngredients.setEditable(false);
txtIngredients.setLineWrap(true);
scrollPane_1.setViewportView(txtIngredients);
//RetrieveData.insertSampleData();
RetrieveData.fillList();
}
}