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.ImageIcon; import javax.swing.JFileChooser; import javax.swing.JSplitPane; import javax.swing.JButton; import javax.swing.JTextField; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import java.awt.Image; import java.awt.Insets; import java.awt.RenderingHints; import javax.swing.JList; import javax.swing.JTextPane; 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; public class FrmMain extends JFrame { /** * */ private static final long serialVersionUID = 2634383304749703669L; private JPanel contentPane; private JTextField txtSearch; private JTextField txtEdTitle; private JTextField txtTags; private JButton btnSave; private JButton btnCancel; private JTextPane txtIngredients; private JTextPane txtDescription; private JButton btnAddRecipe; private JButton btnSearch; private JButton btnDelRecipe; private JButton btnLoadImg; private JLabel lblRecipeTitle; private JLabel imgRecipe; private JFileChooser fcImg; private ImageIcon imgDefaultRecipe; void DeactivateAddMenu() { btnCancel.setVisible(false); btnDelRecipe.setEnabled(true); btnLoadImg.setVisible(false); btnSearch.setEnabled(true); btnSave.setVisible(false); txtDescription.setEditable(false); txtEdTitle.setVisible(false); txtIngredients.setEditable(false); txtTags.setEditable(false); lblRecipeTitle.setVisible(true); } void ActivateAddMenu() { btnCancel.setVisible(true); btnDelRecipe.setEnabled(false); btnLoadImg.setVisible(true); btnSearch.setEnabled(false); btnSave.setVisible(true); txtDescription.setEditable(true); txtEdTitle.setVisible(true); txtIngredients.setEditable(true); txtTags.setEditable(true); lblRecipeTitle.setVisible(false); //czyszczenie pól formularza txtDescription.setText(""); txtEdTitle.setText(""); txtIngredients.setText(""); txtTags.setText(""); } /** * Create the frame. */ public FrmMain() { setResizable(false); setTitle("Książka Kucharska v1.0"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 800, 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.setDividerSize(5); splitPane.setEnabled(false); splitPane.setResizeWeight(0.1); contentPane.add(splitPane, BorderLayout.CENTER); JPanel panel = new JPanel(); splitPane.setLeftComponent(panel); GridBagLayout gbl_panel = new GridBagLayout(); gbl_panel.columnWidths = new int[] {0, 30}; gbl_panel.rowHeights = new int[] {0, 0, 0}; gbl_panel.columnWeights = new double[]{1.0, 0.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.insets = new Insets(0, 0, 5, 5); gbc_txtSearch.fill = GridBagConstraints.HORIZONTAL; gbc_txtSearch.gridx = 0; gbc_txtSearch.gridy = 0; panel.add(txtSearch, gbc_txtSearch); txtSearch.setColumns(10); btnSearch = new JButton("Szukaj"); GridBagConstraints gbc_btnSearch = new GridBagConstraints(); gbc_btnSearch.anchor = GridBagConstraints.NORTH; gbc_btnSearch.fill = GridBagConstraints.HORIZONTAL; gbc_btnSearch.insets = new Insets(0, 0, 5, 0); gbc_btnSearch.gridx = 1; gbc_btnSearch.gridy = 0; panel.add(btnSearch, gbc_btnSearch); JList recipesList = new JList(); GridBagConstraints gbc_recipesList = new GridBagConstraints(); gbc_recipesList.insets = new Insets(0, 0, 5, 0); gbc_recipesList.gridwidth = 2; gbc_recipesList.fill = GridBagConstraints.BOTH; gbc_recipesList.gridx = 0; gbc_recipesList.gridy = 1; panel.add(recipesList, gbc_recipesList); JPanel panel_1 = new JPanel(); GridBagConstraints gbc_panel_1 = new GridBagConstraints(); gbc_panel_1.gridwidth = 2; gbc_panel_1.gridheight = 0; gbc_panel_1.insets = new Insets(0, 0, 0, 5); gbc_panel_1.fill = GridBagConstraints.BOTH; gbc_panel_1.gridx = 0; gbc_panel_1.gridy = 2; panel.add(panel_1, gbc_panel_1); panel_1.setLayout(new GridLayout(0, 2, 0, 0)); btnAddRecipe = new JButton("Dodaj"); btnAddRecipe.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ActivateAddMenu(); } }); panel_1.add(btnAddRecipe); btnDelRecipe = new JButton("Usuń"); panel_1.add(btnDelRecipe); JPanel panel_2 = new JPanel(); splitPane.setRightComponent(panel_2); panel_2.setLayout(null); btnSave = new JButton("Zapisz"); btnSave.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("sprawdzam ikonę..."); if (imgRecipe.getIcon() == imgDefaultRecipe.getImage()) System.out.println("Ta sama ikona"); DeactivateAddMenu(); } }); btnSave.setVisible(false); btnSave.setBounds(319, 12, 117, 25); panel_2.add(btnSave); txtEdTitle = new JTextField(); txtEdTitle.setVisible(false); txtEdTitle.setBounds(12, 15, 295, 19); panel_2.add(txtEdTitle); txtEdTitle.setColumns(10); txtIngredients = new JTextPane(); txtIngredients.setEditable(false); txtIngredients.setBounds(26, 88, 228, 126); panel_2.add(txtIngredients); txtDescription = new JTextPane(); txtDescription.setEditable(false); txtDescription.setBounds(26, 254, 410, 196); panel_2.add(txtDescription); 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); JLabel lblDescription = new JLabel("Opis wykonania:"); lblDescription.setBounds(26, 227, 152, 15); panel_2.add(lblDescription); 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(); imgDefaultRecipe = new ImageIcon("no_img_recipe.jpg"); imgRecipe.setIcon(imgDefaultRecipe); btnLoadImg.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { int returnVal = fcImg.showOpenDialog(FrmMain.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fcImg.getSelectedFile(); try { BufferedImage srcImg = ImageIO.read(file); int w = (int)Math.ceil(srcImg.getWidth()/(double)srcImg.getHeight()*imgRecipe.getHeight()); System.out.println(w); 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) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); btnLoadImg.setVisible(false); 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(); } }); btnCancel.setVisible(false); btnCancel.setBounds(319, 46, 117, 25); panel_2.add(btnCancel); } }