PageRenderTime 25ms CodeModel.GetById 12ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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