View Javadoc

1   /*
2    *  Copyright (C) 2003-2005 SINTEF
3    *  Author:  Fredrik Vraalsen (fredrik dot vraalsen at sintef dot no)
4    *  Webpage: http://coras.sourceforge.net/
5    *
6    *  This program is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public License
8    *  as published by the Free Software Foundation; either version 2.1
9    *  of the License, or (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this program; if not, write to the Free
18   *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19   *  02111-1307 USA
20   */
21  package coras.client.ui;
22  
23  import java.awt.Frame;
24  import java.beans.PropertyChangeEvent;
25  import java.beans.PropertyChangeListener;
26  
27  import javax.swing.Icon;
28  import javax.swing.ImageIcon;
29  import javax.swing.JDialog;
30  import javax.swing.JLabel;
31  import javax.swing.JOptionPane;
32  import javax.swing.SwingUtilities;
33  
34  import coras.reuse.CategoryEnum;
35  /***
36   * @author fvr
37   *
38   * To change the template for this generated type comment go to
39   * Window - Preferences - Java - Code Generation - Code and Comments
40   */
41  public class NewPackageDialog extends JDialog {
42  	
43  	private static final ClassLoader cl = NewPackageDialog.class.getClassLoader();
44  	private static final Icon EMPTY_ICON = new ImageIcon(cl.getResource(Messages.getString("NewPackageDialog.emptyIcon"))); //$NON-NLS-1$
45  	private static final Icon ERROR_ICON = new ImageIcon(cl.getResource(Messages.getString("NewPackageDialog.errorIcon"))); //$NON-NLS-1$
46  
47  	private static final String WHITESPACE_PATTERN = "//s*"; //$NON-NLS-1$
48  
49  	private javax.swing.JPanel jContentPane = null;
50  
51  	private javax.swing.JPanel buttonPanel = null;
52  	private javax.swing.JButton okButton = null;
53  	private javax.swing.JButton cancelButton = null;
54  	protected int result = JOptionPane.CANCEL_OPTION;
55  	
56  	private PackageDetailsPanel packageDetailsPanel = null;
57  	private JLabel statusLabel = null;
58  	/***
59  	 * This is the default constructor
60  	 */
61  	public NewPackageDialog() {
62  	    this(null);
63  	}
64  	
65  	public NewPackageDialog(Frame owner) {
66  		super(owner);
67  		initialize();
68  		update();
69  	}
70  	
71  	public int getResult() {
72  		return result;
73  	}
74  	
75  	public boolean isCategoryEnabled() {
76  	    return getPackageDetailsPanel().isCategoryEnabled();
77  	}
78  	
79  	public void setCategoryEnabled(boolean enabled) {
80  	    getPackageDetailsPanel().setCategoryEnabled(enabled);
81  	}
82  
83  	public boolean isDomainEnabled() {
84  	    return getPackageDetailsPanel().isDomainEnabled();
85  	}
86  	
87  	public void setDomainEnabled(boolean enabled) {
88  	    getPackageDetailsPanel().setDomainEnabled(enabled);
89  	}
90  
91  	public String getProjectName() {
92          return getPackageDetailsPanel().getPackageName();
93      }
94  
95      public String getFullDescription() {
96          return getPackageDetailsPanel().getFullDescription();
97      }
98  
99      public CategoryEnum getCategory() {
100         return getPackageDetailsPanel().getCategory();
101     }
102     
103     public String getDomain() {
104         return getPackageDetailsPanel().getDomain();
105     }
106     
107     public String getShortDescription() {
108         return getPackageDetailsPanel().getShortDescription();
109     }
110 	
111 	/***
112 	 * This method initializes this
113 	 * 
114 	 * @return void
115 	 */
116 	private void initialize() {
117 		this.setBounds(0, 0, 400, 300);
118 		this.setContentPane(getJContentPane());
119 		this.setModal(true);
120 	}
121 	/***
122 	 * This method initializes jContentPane
123 	 * 
124 	 * @return javax.swing.JPanel
125 	 */
126 	private javax.swing.JPanel getJContentPane() {
127 		if(jContentPane == null) {
128 			statusLabel = new JLabel();
129 			statusLabel.setIcon(EMPTY_ICON);
130 			jContentPane = new javax.swing.JPanel();
131 			jContentPane.setLayout(new java.awt.BorderLayout());
132 			jContentPane.add(statusLabel, java.awt.BorderLayout.NORTH);
133 			jContentPane.add(getPackageDetailsPanel(), java.awt.BorderLayout.CENTER);
134 			jContentPane.add(getButtonPanel(), java.awt.BorderLayout.SOUTH);
135 		}
136 		return jContentPane;
137 	}
138 	/***
139 	 * This method initializes jPanel	
140 	 * 	
141 	 * @return javax.swing.JPanel	
142 	 */    
143 	private javax.swing.JPanel getButtonPanel() {
144 		if (buttonPanel == null) {
145 			buttonPanel = new javax.swing.JPanel();
146 			buttonPanel.add(getOkButton(), null);
147 			buttonPanel.add(getCancelButton(), null);
148 		}
149 		return buttonPanel;
150 	}
151 	/***
152 	 * This method initializes jButton	
153 	 * 	
154 	 * @return javax.swing.JButton	
155 	 */    
156 	private javax.swing.JButton getOkButton() {
157 		if (okButton == null) {
158 			okButton = new javax.swing.JButton();
159 			okButton.setText(Messages.getString("NewPackageDialog.okButton")); //$NON-NLS-1$
160 			okButton.setMnemonic(java.awt.event.KeyEvent.VK_O);
161 			okButton.addActionListener(new java.awt.event.ActionListener() { 
162 				public void actionPerformed(java.awt.event.ActionEvent e) {    
163 					result = JOptionPane.OK_OPTION;
164 					dispose();
165 				}
166 			});
167 		}
168 		return okButton;
169 	}
170 	/***
171 	 * This method initializes jButton1	
172 	 * 	
173 	 * @return javax.swing.JButton	
174 	 */    
175 	private javax.swing.JButton getCancelButton() {
176 		if (cancelButton == null) {
177 			cancelButton = new javax.swing.JButton();
178 			cancelButton.setText(Messages.getString("NewPackageDialog.cancelButton")); //$NON-NLS-1$
179 			cancelButton.setMnemonic(java.awt.event.KeyEvent.VK_C);
180 			cancelButton.addActionListener(new java.awt.event.ActionListener() { 
181 				public void actionPerformed(java.awt.event.ActionEvent e) {
182 					result = JOptionPane.CANCEL_OPTION;
183 					dispose();
184 				}
185 			});
186 		}
187 		return cancelButton;
188 	}
189 	
190     /***
191 	 * This method initializes packageDetailsPanel	
192 	 * 	
193 	 * @return coras.client.ui.PackageDetailsPanel	
194 	 */    
195 	private PackageDetailsPanel getPackageDetailsPanel() {
196 		if (packageDetailsPanel == null) {
197 			packageDetailsPanel = new PackageDetailsPanel();
198 			packageDetailsPanel.setEditable(true);
199 			packageDetailsPanel.setCategoryEditable(true);
200 			packageDetailsPanel.setDomainEditable(true);
201 			packageDetailsPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(5,5,5,5));
202 			packageDetailsPanel.addPropertyChangeListener(PackageDetailsPanel.PACKAGE_NAME, new PropertyChangeListener() {
203 				public void propertyChange(PropertyChangeEvent evt) {
204 					fireUpdateEvent();
205 				}
206 			});
207 		}
208 		return packageDetailsPanel;
209 	}
210 
211     
212     private void fireUpdateEvent() {
213         SwingUtilities.invokeLater(new Runnable() {
214             public void run() {
215                 update();
216             }
217         });
218     }
219 
220 	private void update() {
221     	if (getProjectName().matches(WHITESPACE_PATTERN)) {
222     		statusLabel.setText(Messages.getString("NewPackageDialog.emptyNameErrorMessage")); //$NON-NLS-1$
223     		statusLabel.setIcon(ERROR_ICON);
224     		getOkButton().setEnabled(false);
225     	} else {
226     		statusLabel.setText(null);
227     		statusLabel.setIcon(EMPTY_ICON);
228     		getOkButton().setEnabled(true);
229     	}
230 	}
231 	
232 }  //  @jve:visual-info  decl-index=0 visual-constraint="10,10"