@ -10,6 +10,7 @@ import javax.swing.border.EmptyBorder;
import javax.swing.DefaultListModel ;
import javax.swing.ImageIcon ;
import javax.swing.JFileChooser ;
import javax.swing.JOptionPane ;
import javax.swing.JSplitPane ;
import javax.swing.JButton ;
import javax.swing.JTextField ;
@ -18,7 +19,6 @@ 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 ;
@ -37,6 +37,7 @@ import javax.swing.SwingConstants;
import ksiazka.kucharska.RetrieveData ;
import javax.swing.event.ListSelectionListener ;
import javax.swing.event.ListSelectionEvent ;
import javax.swing.filechooser.FileNameExtensionFilter ;
public class FrmMain extends JFrame {
@ -58,12 +59,13 @@ public class FrmMain extends JFrame {
private JButton btnLoadImg ;
public static JLabel lblRecipeTitle ;
private static DefaultListModel recipesListModel ;
private JLabel imgRecipe ;
public static JLabel imgRecipe ;
private JFileChooser fcImg ;
private ImageIcon imgDefaultRecipe ;
public static ImageIcon imgDefaultRecipe ;
private JList recipesList ;
void DeactivateAddMenu ( ) {
btnAddRecipe . setEnabled ( true ) ;
btnCancel . setVisible ( false ) ;
btnDelRecipe . setEnabled ( true ) ;
btnLoadImg . setVisible ( false ) ;
@ -78,6 +80,7 @@ public class FrmMain extends JFrame {
}
void ActivateAddMenu ( ) {
btnAddRecipe . setEnabled ( false ) ;
btnCancel . setVisible ( true ) ;
btnDelRecipe . setEnabled ( false ) ;
btnLoadImg . setVisible ( true ) ;
@ -89,13 +92,14 @@ public class FrmMain extends JFrame {
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 ( )
@ -110,7 +114,7 @@ public class FrmMain extends JFrame {
setResizable ( false ) ;
setTitle ( "Książka Kucharska v1.0" ) ;
setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE ) ;
setBounds ( 100 , 100 , 800 , 549 ) ;
setBounds ( 100 , 100 , 723 , 549 ) ;
contentPane = new JPanel ( ) ;
contentPane . setBorder ( new EmptyBorder ( 5 , 5 , 5 , 5 ) ) ;
contentPane . setLayout ( new BorderLayout ( 0 , 0 ) ) ;
@ -191,8 +195,23 @@ public class FrmMain extends JFrame {
DefaultListModel model = ( DefaultListModel ) recipesList . getModel ( ) ;
if ( recipesList . getSelectedIndex ( ) ! = - 1 )
{
RetrieveData . removeRecipe ( recipesList . getSelectedValue ( ) . toString ( ) ) ;
model . remove ( recipesList . getSelectedIndex ( ) ) ;
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 ( "" ) ;
}
}
}
}
} ) ;
@ -206,9 +225,11 @@ public class FrmMain extends JFrame {
btnSave . addActionListener ( new ActionListener ( ) {
public void actionPerformed ( ActionEvent e ) {
RetrieveData . addNewRecipe ( ) ;
DeactivateAddMenu ( ) ;
DefaultListModel model = ( DefaultListModel ) recipesList . getModel ( ) ;
model . addElement ( txtEdTitle . getText ( ) ) ;
DeactivateAddMenu ( ) ;
recipesList . setSelectedIndex ( recipesList . getModel ( ) . getSize ( ) - 1 ) ;
RetrieveData . fillComponents ( recipesList . getSelectedValue ( ) . toString ( ) ) ;
}
} ) ;
btnSave . setVisible ( false ) ;
@ -217,7 +238,7 @@ public class FrmMain extends JFrame {
txtEdTitle = new JTextField ( ) ;
txtEdTitle . setVisible ( false ) ;
txtEdTitle . setBounds ( 1 2, 15 , 295 , 19 ) ;
txtEdTitle . setBounds ( 26 , 15 , 281 , 19 ) ;
panel_2 . add ( txtEdTitle ) ;
txtEdTitle . setColumns ( 10 ) ;
@ -253,38 +274,41 @@ public class FrmMain extends JFrame {
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 ) ;
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 ( ) ;
}
}
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 ) ;
@ -305,6 +329,8 @@ public class FrmMain extends JFrame {
btnCancel . addActionListener ( new ActionListener ( ) {
public void actionPerformed ( ActionEvent e ) {
DeactivateAddMenu ( ) ;
if ( recipesList . getSelectedIndex ( ) ! = - 1 )
RetrieveData . fillComponents ( recipesList . getSelectedValue ( ) . toString ( ) ) ;
}
} ) ;
btnCancel . setVisible ( false ) ;