package ksiazka.kucharska; import java.awt.*; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.List; import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.border.EmptyBorder; import javax.swing.border.LineBorder; import javax.swing.event.DocumentListener; import javax.swing.event.ListSelectionListener; import javax.swing.filechooser.FileNameExtensionFilter; public class CB_View extends JFrame{ private static final long serialVersionUID = 2634383304749703669L; private JTextField txtSearch = new JTextField(); private JTextField txtEdTitle = new JTextField(); private JTextField txtTags = new JTextField(); private JTextArea txtDescription = new JTextArea(); private JTextArea txtIngredients = new JTextArea(); private JButton btnAddRecipe = new JButton("Nowy"); private JButton btnDelRecipe = new JButton("Usuń"); private JButton btnLoadImg = new JButton("Wczytaj"); private JButton btnCancel = new JButton("Anuluj"); private JButton btnSave = new JButton("Dodaj"); private ImageIcon imgDefaultRecipe = new ImageIcon(); private JScrollPane scrollPane_1 = new JScrollPane(); private JScrollPane scrollPane_2 = new JScrollPane(); private JSplitPane splitPane = new JSplitPane(); private DefaultListModel recipesListModel = new DefaultListModel(); private JList recipesList = new JList(recipesListModel); private JLabel lblIngredients = new JLabel("Składniki:"); private JLabel lblDescription = new JLabel("Opis wykonania:"); private JLabel imgRecipe = new JLabel(""); private JLabel lblRecipeTitle = new JLabel("Przepis"); private JLabel lblTags = new JLabel("Tagi:"); private JFileChooser fcImg = new JFileChooser(); private JScrollPane scrollPane = new JScrollPane(); public CB_View(){ this.setResizable(false); this.setTitle("Książka Kucharska v1.0"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBounds(100, 100, 720, 551); JPanel contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); this.setContentPane(contentPane); splitPane.setResizeWeight(0.35); 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); txtSearch.setBounds(5, 5, 237, 20); txtSearch.setColumns(10); scrollPane_2.setBounds(5, 30, 237, 448); scrollPane_2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); scrollPane_2.setMaximumSize(new Dimension(100, 100)); recipesList.setSize(new Dimension(100, 0)); recipesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); recipesList.setMaximumSize(new Dimension(40, 0)); scrollPane_2.setViewportView(recipesList); JPanel panel_2 = new JPanel(); splitPane.setRightComponent(panel_2); panel_2.setLayout(null); btnSave.setVisible(false); btnSave.setBounds(319, 12, 117, 25); txtEdTitle.setVisible(false); txtEdTitle.setBounds(26, 15, 281, 19); txtEdTitle.setColumns(10); lblRecipeTitle.setFont(new Font("Dialog", Font.BOLD, 18)); lblRecipeTitle.setBounds(12, 12, 442, 22); lblIngredients.setBounds(26, 61, 70, 15); imgRecipe.setHorizontalAlignment(SwingConstants.CENTER); imgRecipe.setBorder(new LineBorder(SystemColor.textHighlightText)); imgRecipe.setBounds(272, 88, 164, 126); 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); try { BufferedImage bi = ImageIO.read(getClass().getResource("/no_img_recipe.jpg")); imgDefaultRecipe = new ImageIcon(bi); imgRecipe.setIcon(imgDefaultRecipe); bi.flush(); } catch (IOException e) { e.printStackTrace(); } btnLoadImg.setVisible(false); scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); scrollPane.setBounds(26, 254, 410, 196); txtDescription = new JTextArea(); txtDescription.setEditable(false); txtDescription.setLineWrap(true); scrollPane.setViewportView(txtDescription); btnLoadImg.setBounds(304, 217, 102, 25); txtTags.setEditable(false); txtTags.setBounds(26, 480, 410, 19); txtTags.setColumns(10); lblTags.setBounds(26, 462, 70, 15); btnCancel.setVisible(false); btnCancel.setBounds(319, 46, 117, 25); lblDescription.setBounds(26, 227, 152, 15); scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); scrollPane_1.setBounds(26, 88, 228, 126); txtIngredients.setEditable(false); txtIngredients.setLineWrap(true); scrollPane_1.setViewportView(txtIngredients); scrollPane_2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); scrollPane_2.setMaximumSize(new Dimension(100, 100)); scrollPane_2.setViewportView(recipesList); panel.setLayout(null); panel.add(txtSearch); panel.add(scrollPane_2); btnAddRecipe.setBounds(5, 481, 116, 25); panel.add(btnAddRecipe); btnDelRecipe.setBounds(125, 481, 116, 25); panel.add(btnDelRecipe); panel.add(txtSearch); panel_2.add(btnSave); panel_2.add(txtEdTitle); panel_2.add(lblRecipeTitle); panel_2.add(lblIngredients); panel_2.add(lblTags); panel_2.add(imgRecipe); panel_2.add(scrollPane); panel_2.add(btnLoadImg); panel_2.add(txtTags); panel_2.add(btnCancel); panel_2.add(lblDescription); panel_2.add(scrollPane_1); } public String getRecipeTitle(){ return lblRecipeTitle.getText(); } public String getRecipeEdTitle(){ return txtEdTitle.getText(); } public String getRecipeIngredients(){ return txtIngredients.getText(); } public String getRecipeDescription(){ return txtDescription.getText(); } public String getRecipeTags(){ return txtTags.getText().toString(); } public JFileChooser getFileChooser(){ return fcImg; } public JList getRecipesList(){ return recipesList; } public JLabel getImgRecipe(){ return imgRecipe; } public ImageIcon getDefaultImgRecipe(){ return imgDefaultRecipe; } public String getSearchValue(){ return txtSearch.getText(); } public void setRecipeTitle(String name){ lblRecipeTitle.setText(name); } public void setRecipeIngredients(String ingredients){ txtIngredients.setText(ingredients); } public void setRecipeDescription(String description){ txtDescription.setText(description); } public void setRecipeTags(String tags){ txtTags.setText(tags); } public void setRecipesList(List names){ recipesListModel.clear(); for (String r : names) { recipesListModel.addElement(r); } } public void setRecipesListItem(String item){ recipesListModel.addElement(item); } public void setRecipeIcon(Icon icon){ imgRecipe.setIcon(icon); } public void resetCaretPositions(){ txtDescription.setCaretPosition(0); txtIngredients.setCaretPosition(0); } void addListener(ActionListener listenForAddButton){ btnAddRecipe.addActionListener(listenForAddButton); } void delListener(ActionListener listenForDelButton){ btnDelRecipe.addActionListener(listenForDelButton); } void loadListener(ActionListener listenForLoadButton){ btnLoadImg.addActionListener(listenForLoadButton); } void cancelListener(ActionListener listenForAddButton){ btnCancel.addActionListener(listenForAddButton); } void saveListener(ActionListener listenForAddButton){ btnSave.addActionListener(listenForAddButton); } void listSelectListener(ListSelectionListener listenForSelectionChange){ recipesList.addListSelectionListener(listenForSelectionChange); } void searchTypeListener(DocumentListener listenForSearchTyping){ txtSearch.getDocument().addDocumentListener(listenForSearchTyping); } 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); txtDescription.setText(""); txtEdTitle.setText(""); txtIngredients.setText(""); txtTags.setText(""); } }