1   /**
2    * JHylaFax - A java client for HylaFAX.
3    *
4    * Copyright (C) 2005 by Steffen Pingel <steffenp@gmx.de>
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation; either version 2 of the License, or
9    * (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
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19   */
20  package net.sf.jhylafax.addressbook;
21  
22  import static net.sf.jhylafax.JHylaFAX.i18n;
23  
24  import java.io.File;
25  
26  import javax.swing.ButtonGroup;
27  import javax.swing.Icon;
28  import javax.swing.JComponent;
29  import javax.swing.JLabel;
30  import javax.swing.JRadioButton;
31  import javax.swing.JTextField;
32  
33  import net.sf.jhylafax.Constants;
34  import net.sf.jhylafax.LocaleChangeListener;
35  import net.wimpi.pim.contact.facades.SimpleContact;
36  
37  import org.xnap.commons.gui.Dialogs;
38  import org.xnap.commons.gui.ErrorDialog;
39  import org.xnap.commons.gui.FileChooserPanel;
40  import org.xnap.commons.gui.util.EnableListener;
41  import org.xnap.commons.gui.util.IconHelper;
42  import org.xnap.commons.gui.wizard.WizardDialog;
43  import org.xnap.commons.gui.wizard.WizardDialogListener;
44  import org.xnap.commons.gui.wizard.WizardPage;
45  import org.xnap.commons.io.FileExtensionFilter;
46  
47  import com.jgoodies.forms.builder.DefaultFormBuilder;
48  import com.jgoodies.forms.layout.FormLayout;
49  
50  /**
51   * A wizard for importing address book entries. 
52   * 
53   * @author Steffen Pingel
54   */
55  public class ImportWizard extends WizardDialog implements LocaleChangeListener {
56  
57  	private FilePage filePage;
58  	private FormatPage formatPage;
59  	private boolean firstTime = true;
60  	private AddressBook addressbook;
61  	
62  	public ImportWizard(AddressBook addressbook) {
63  		super(addressbook);
64  
65  		this.addressbook = addressbook;
66  		
67  		filePage = new FilePage();
68  		addPage(filePage, "file");
69  
70  		formatPage = new FormatPage();
71  		addPage(formatPage, "format");
72  
73  		addWizardDialogListener(new WizardDialogListener() {
74  
75  			@SuppressWarnings("deprecation")
76              public void pageChanged(WizardPage oldPage, WizardPage newPage)
77  			{
78  				if (firstTime) {
79  					if (oldPage == filePage && newPage == formatPage) {
80  						formatPage.guessFormat(filePage.fileChooserPanel.getFilename());
81  						firstTime = false;
82  					}
83  				}
84  			}
85  			
86  		});
87  		
88  		updateLabels();
89  		revert();
90  		
91  		pack();
92  	}
93  
94  	private DefaultFormBuilder createForm() {
95  		FormLayout layout = new FormLayout("left:max(20dlu;pref), 3dlu, pref, 3dlu, left:max(20dlu;pref), pref, pref:grow", "");
96  		DefaultFormBuilder builder = new DefaultFormBuilder(layout);
97  		builder.setDefaultDialogBorder();
98  		return builder;
99  	}
100 
101 	@Override
102 	public void finish()
103 	{
104 		File file = filePage.fileChooserPanel.getFile();
105 		if (file == null) {
106 			Dialogs.showError(this,	i18n.tr("No file selected"), i18n.tr("Import Error"));
107 			return;
108 		}
109 		
110 		try {
111 			SimpleContact[] contacts;
112 			if (formatPage.vCardFormatRadioButton.isSelected()) {
113 				contacts = addressbook.importVCardContacts(file);
114 			}
115 			else if (formatPage.suseFaxFormatRadioButton.isSelected()) {
116 				contacts = addressbook.importCSVContacts(file, "|");
117 			}
118 			else {
119 				String separator = formatPage.separatorTextField.getText();
120 				if (file == null) {
121 					Dialogs.showError(this,	i18n.tr("No valid separator specified"), i18n.tr("Import Error"));
122 					return;
123 				}
124 				contacts = addressbook.importCSVContacts(file, separator);
125 			}
126 			Dialogs.showInfo(this, 
127 					i18n.tr("Imported {0} contacts", contacts.length), 
128 					i18n.tr("JHylaFAX Address Book"));
129 
130 		}
131 		catch (Exception e) {
132 			ErrorDialog.showError(this, 
133 					i18n.tr("Could not import from file \"{0}\"", file.getAbsolutePath()), 
134 					i18n.tr("JHylaFAX Addressbook Error"), e);
135 			return;
136 		}
137 
138 		super.finish();
139 	}
140 	
141 	public void revert()
142 	{
143 		formatPage.revert();
144 		filePage.revert();
145 	}
146 	
147 	public void updateLabels()
148 	{
149 		setTitle(i18n.tr("JHylaFAX Address Book Import Wizard"));
150 		
151 		formatPage.updateLabels();
152 		filePage.updateLabels();
153 	}
154 
155 	private class FilePage implements WizardPage {
156 		
157 		private DefaultFormBuilder builder;
158 		FileChooserPanel fileChooserPanel;
159 		private JLabel fileChooserPanelLabel;
160 
161 		public FilePage() {
162 			builder = createForm();
163 
164 			fileChooserPanel = new FileChooserPanel(Constants.DEFAULT_COLUMNS);
165 			fileChooserPanel.getFileChooser().addChoosableFileFilter(
166 					new FileExtensionFilter(i18n.tr("vCards (*.vcf)"), ".vcf"));
167 			fileChooserPanel.getFileChooser().addChoosableFileFilter(
168 					new FileExtensionFilter(i18n.tr("CSV (*.csv)"), ".csv"));
169 			fileChooserPanel.getFileChooser().addChoosableFileFilter(
170 					new FileExtensionFilter(i18n.tr("SuSEFax (.susephone)"), ".susephone"));
171 			fileChooserPanel.getFileChooser().setFileHidingEnabled(false);
172 			fileChooserPanelLabel = builder.append("", fileChooserPanel, 5);
173 		}
174 
175 		public boolean apply() {
176 			return true;
177 		}
178 
179 		public String getDescription() {
180 			return i18n.tr("Select file to import.");
181 		}
182 
183 		public Icon getIcon() {
184 			return IconHelper.getTitleIcon("file.png");
185 		}
186 
187 		public JComponent getPanel() {
188 			return builder.getPanel();
189 		}
190 
191 		public String getTitle() {
192 			return i18n.tr("Import File");
193 		}
194 
195 		public void revert() {
196 		}
197 		
198 		public void updateLabels() {
199 			fileChooserPanelLabel.setText(i18n.tr("File:"));
200 		}
201 	}
202 	
203 	private class FormatPage implements WizardPage {
204 
205 		private DefaultFormBuilder builder;
206 		JRadioButton csvFormatRadioButton;
207 		JRadioButton vCardFormatRadioButton;
208 		JRadioButton suseFaxFormatRadioButton;
209 		private JTextField separatorTextField;
210 		private JLabel separatorLabel;
211 		private JLabel separatorDescriptionLabel;
212 
213 		public FormatPage() {
214 			builder = createForm();
215 			
216 			vCardFormatRadioButton = new JRadioButton();
217 			builder.append(vCardFormatRadioButton);
218 			builder.nextLine();
219 			
220 			csvFormatRadioButton = new JRadioButton();
221 			builder.append(csvFormatRadioButton);
222 			separatorTextField = new JTextField(1);
223 			separatorLabel = builder.append("", separatorTextField);
224 			builder.nextLine();
225 
226 			separatorDescriptionLabel = new JLabel(); 
227 			builder.append(separatorDescriptionLabel, 7);
228 			builder.nextLine();
229 			
230 			csvFormatRadioButton.addItemListener(new EnableListener(
231 					separatorLabel, separatorTextField, separatorDescriptionLabel));
232 			
233 			suseFaxFormatRadioButton = new JRadioButton();
234 			builder.append(suseFaxFormatRadioButton);
235 			builder.nextLine();
236 			
237 			ButtonGroup bg = new ButtonGroup();
238 			bg.add(vCardFormatRadioButton);
239 			bg.add(csvFormatRadioButton);
240 			bg.add(suseFaxFormatRadioButton);
241 		}			
242 
243 		public boolean apply() {
244 			
245 			return true;
246 		}
247 
248 		public String getDescription() {
249 			return i18n.tr("Select import format.");
250 		}
251 
252 		public Icon getIcon() {
253 			return null;
254 		}
255 
256 		public void guessFormat(String filename) {
257 			if (filename.endsWith("vcf")) {
258 				vCardFormatRadioButton.setSelected(true);
259 			}
260 			else if (filename.endsWith("susephone")) {
261 				suseFaxFormatRadioButton.setSelected(true);
262 			}
263 			else {
264 				csvFormatRadioButton.setSelected(true);
265 			}
266 		}
267 		
268 		public JComponent getPanel() {
269 			return builder.getPanel();
270 		}
271 
272 		public String getTitle() {
273 			return i18n.tr("Import Format");
274 		}
275 
276 		public void revert()
277 		{
278 			csvFormatRadioButton.setSelected(true);
279 			separatorTextField.setText(",");
280 		}
281 	
282 		public void updateLabels() {
283 			vCardFormatRadioButton.setText(i18n.tr("vCard"));
284 			csvFormatRadioButton.setText(i18n.tr("CSV"));
285 			separatorLabel.setText(i18n.tr("Separator:"));
286 			separatorDescriptionLabel.setText(i18n.tr("Expected Format: First Name, Last Name, Fax, Company"));
287 			suseFaxFormatRadioButton.setText(i18n.tr("SuSEFax"));
288 		}
289 		
290 	}
291 
292 }