/winebrewdb/src/uk/co/pori/winebrewdb/recipe/RecipePanel.java

http://winebrewdb.googlecode.com/ · Java · 148 lines · 85 code · 39 blank · 24 comment · 9 complexity · 40f9eaaa88688bed21e130d88d1edf56 MD5 · raw file

  1. package uk.co.pori.winebrewdb.recipe;
  2. import java.awt.Font;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import javax.swing.ImageIcon;
  6. import javax.swing.JButton;
  7. import javax.swing.JFileChooser;
  8. import javax.swing.JLabel;
  9. import javax.swing.JPanel;
  10. import javax.swing.JTabbedPane;
  11. import javax.swing.event.ChangeEvent;
  12. import javax.swing.event.ChangeListener;
  13. import uk.co.pori.winebrewdb.MainWindow;
  14. import net.miginfocom.swing.MigLayout;
  15. /**
  16. * This is the main recipe panel that contains all of the sub panels in a tab layout.
  17. *
  18. * @author Paul.Bellchambers
  19. *
  20. */
  21. public class RecipePanel extends JPanel {
  22. private static final long serialVersionUID = 1332318777984759664L;
  23. public static JPanel RecipePanel;
  24. private static JLabel RecipeHeader;
  25. private static JLabel RecipeSubtitle;
  26. static JTabbedPane tabbedRecipePane;
  27. private static String RecipePanelStatus = "DeInitialised";
  28. public static JButton btnPrintRecipe;
  29. /**
  30. * Initialises all the recipe panels (including getting all data) so that they are displayed on screen.
  31. */
  32. public static void initialisePanel(){
  33. RecipePanel = new JPanel();
  34. RecipePanel.setLayout(new MigLayout("", "[grow][65px:n:65px]", "[20px:n:20px][25px:n:25px][grow]"));
  35. //Header
  36. RecipeHeader = new JLabel("Recipe");
  37. RecipeHeader.setFont(new Font("Tahoma", Font.BOLD, 18));
  38. RecipePanel.add(RecipeHeader, "cell 0 0,grow");
  39. //Subtitle
  40. RecipeSubtitle = new JLabel("Browse and edit the Recipe database.");
  41. RecipeSubtitle.setFont(new Font("Tahoma", Font.ITALIC, 13));
  42. RecipePanel.add(RecipeSubtitle, "cell 0 1,growx,aligny top");
  43. //Print recipe button
  44. btnPrintRecipe = new JButton();
  45. btnPrintRecipe.setIcon(new ImageIcon(RecipePanel.class.getResource("/uk/co/pori/winebrewdb/images/print.png")));
  46. btnPrintRecipe.setToolTipText("Save currently selected recipe data to printable .pdf");
  47. btnPrintRecipe.setVisible(false);
  48. btnPrintRecipe.setEnabled(false);
  49. RecipePanel.add(btnPrintRecipe, "cell 1 0");
  50. //Tabbed Pane
  51. tabbedRecipePane = new JTabbedPane(JTabbedPane.TOP);
  52. RecipePanel.add(tabbedRecipePane, "cell 0 2 2,grow");
  53. //Recipe Search Tab
  54. RecipeSearchPanel.initialisePanel();
  55. tabbedRecipePane.addTab("Search", null, RecipeSearchPanel.tabbedRecipeSearchPanel, null);
  56. //Recipe Data Tab
  57. RecipeDataPanel.initialisePanel();
  58. tabbedRecipePane.addTab("Recipe Data", null, RecipeDataPanel.tabbedRecipeDataPanel, null);
  59. //Add New Recipe Tab
  60. RecipeAddPanel.initialisePanel();
  61. tabbedRecipePane.addTab("Add New Recipe", new ImageIcon(RecipePanel.class.getResource("/uk/co/pori/winebrewdb/images/new.png")), RecipeAddPanel.tabbedRecipeAddPanel, null);
  62. //Set some tabs disabled initially
  63. tabbedRecipePane.setEnabledAt(1, false);
  64. //Add it all to the main window
  65. MainWindow.WineBrewDBFrame.getContentPane().add(RecipePanel, "cell 0 0,grow");
  66. RecipePanel.setVisible(false);
  67. RecipePanelStatus = "Initialised";
  68. //Add print button listener
  69. btnPrintRecipe.addActionListener(new ActionListener() {
  70. public void actionPerformed(ActionEvent e) {
  71. JFileChooser c = new JFileChooser();
  72. int rVal = c.showSaveDialog(MainWindow.WineBrewDBFrame);
  73. if (rVal == JFileChooser.APPROVE_OPTION) {
  74. String pdflocation = c.getCurrentDirectory().toString() + MainWindow.OSSlash + c.getSelectedFile().getName() + ".pdf";
  75. RecipePDF.createPDF(pdflocation);
  76. }
  77. if (rVal == JFileChooser.CANCEL_OPTION) {
  78. }
  79. }
  80. });
  81. tabbedRecipePane.addChangeListener(new ChangeListener() {
  82. // This method is called whenever the selected tab changes
  83. public void stateChanged(ChangeEvent evt) {
  84. JTabbedPane pane = (JTabbedPane)evt.getSource();
  85. // Get current tab
  86. int sel = pane.getSelectedIndex();
  87. if(sel == 0 || sel == 2){
  88. btnPrintRecipe.setVisible(false);
  89. btnPrintRecipe.setEnabled(false);
  90. }else{
  91. btnPrintRecipe.setVisible(true);
  92. btnPrintRecipe.setEnabled(true);
  93. }
  94. }
  95. });
  96. }
  97. /**
  98. * De-initialises all the recipe panels so that they are no longer displayed on screen.
  99. */
  100. public static void deinitialisePanel(){
  101. if(RecipePanelStatus.equals("Initialised")) {
  102. RecipePanel.setVisible(false);
  103. tabbedRecipePane.removeAll();
  104. RecipePanel.removeAll();
  105. MainWindow.WineBrewDBFrame.getContentPane().remove(RecipePanel);
  106. RecipePanelStatus = "DeInitialised";
  107. }
  108. }
  109. }