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;
21  
22  import static net.sf.jhylafax.JHylaFAX.i18n;
23  import javax.swing.DefaultComboBoxModel;
24  import javax.swing.JComboBox;
25  import javax.swing.JLabel;
26  import javax.swing.JPanel;
27  import javax.swing.JSpinner;
28  import javax.swing.JTextField;
29  import javax.swing.SpinnerNumberModel;
30  import net.sf.jhylafax.Settings.Notification;
31  import net.sf.jhylafax.Settings.Resolution;
32  import net.sf.jhylafax.fax.FaxJob;
33  import net.sf.jhylafax.fax.Paper;
34  import org.xnap.commons.gui.Dialogs;
35  import com.jgoodies.forms.builder.DefaultFormBuilder;
36  import com.jgoodies.forms.layout.FormLayout;
37  
38  /**
39   * A panel for editing of fax job properties.
40   * 
41   * @author Steffen Pingel
42   */
43  public class JobPanel extends JPanel implements LocaleChangeListener {
44  
45  	private static final int DEFAULT_COLUMNS = 20;
46  	
47  	private JLabel notificationLabel;
48  	private JLabel resolutionLabel;
49  	private JSpinner prioritySpinner;
50  	private JLabel priorityLabel;
51  	private JSpinner maxTriesSpinner;
52  	private JLabel maxTriesLabel;
53  	private JSpinner maxDialsSpinner;
54  	private JLabel maxDialsLabel;
55  	private JLabel paperLabel;
56  	private DefaultComboBoxModel notificationModel;
57  	private DefaultComboBoxModel resolutionModel;
58  	private DefaultComboBoxModel paperModel;
59  	private JTextField fullnameTextField;
60  	private JTextField emailTextField;
61  	private JLabel fullnameLabel;
62  	private JLabel emailLabel;
63  
64  	private SpinnerNumberModel priorityModel;
65  
66  	public JobPanel(boolean border) {
67  		FormLayout layout = new FormLayout("left:max(40dlu;pref), 3dlu, pref, pref:grow, min:grow", "");
68  		DefaultFormBuilder builder = new DefaultFormBuilder(layout, this);
69  		if (border) {
70  			builder.setDefaultDialogBorder();
71  		}
72  		builder.appendSeparator(i18n.tr("Sender"));
73  		
74  		fullnameTextField = new JTextField(DEFAULT_COLUMNS);
75  		fullnameLabel = builder.append("", fullnameTextField, 3);
76  		builder.nextLine();
77  
78  		emailTextField = new JTextField(DEFAULT_COLUMNS);
79  		emailLabel = builder.append("", emailTextField, 3);
80  		builder.nextLine();
81  		
82  		builder.appendSeparator(i18n.tr("Parameter"));
83  
84  		notificationModel = new DefaultComboBoxModel();
85  		JComboBox notificationComboBox = new JComboBox(notificationModel);
86  		notificationLabel = builder.append("", notificationComboBox, 2);
87  		builder.nextLine();
88  		
89  		resolutionModel = new DefaultComboBoxModel();
90  		JComboBox resolutionComboBox = new JComboBox(resolutionModel);
91  		resolutionLabel = builder.append("", resolutionComboBox, 2);
92  		builder.nextLine();
93  
94  		priorityModel = new SpinnerNumberModel(0, 0, 255, 1);
95  		prioritySpinner = new JSpinner(priorityModel);
96  		priorityLabel = builder.append("", prioritySpinner);
97  		builder.nextLine();
98  		
99  		maxTriesSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 255, 1));
100 		maxTriesLabel = builder.append("", maxTriesSpinner);
101 		builder.nextLine();
102 		
103 		maxDialsSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 255, 1));
104 		maxDialsLabel = builder.append("", maxDialsSpinner);
105 		builder.nextLine();
106 		
107 		paperModel = new DefaultComboBoxModel();
108 		JComboBox paperComboBox = new JComboBox(paperModel);
109 		paperLabel = builder.append("", paperComboBox, 2);
110 	}
111 	
112 	public void setSenderEditable(boolean editSender) {
113 		fullnameTextField.setEditable(editSender);
114 		emailTextField.setEditable(editSender);
115 	}
116 	
117 	public void updateLabels() {
118 		fullnameLabel.setText(i18n.tr("Name"));
119 		emailLabel.setText(i18n.tr("Email"));
120 
121 		notificationLabel.setText(i18n.tr("Notify"));
122 		resolutionLabel.setText(i18n.tr("Resolution"));
123 		priorityLabel.setText(i18n.tr("Priority"));
124 		maxTriesLabel.setText(i18n.tr("Maximum Tries"));
125 		maxDialsLabel.setText(i18n.tr("Maximum Dials"));
126 		paperLabel.setText(i18n.tr("Paper Format"));
127 		
128 		Object selectedItem = notificationModel.getSelectedItem();
129 		notificationModel.removeAllElements();
130 		for (Settings.Notification notification : Settings.Notification.values()) {
131 			notificationModel.addElement(notification);
132 		}
133 		notificationModel.setSelectedItem(selectedItem);
134 		selectedItem = resolutionModel.getSelectedItem();
135 		resolutionModel.removeAllElements();
136 		for (Settings.Resolution resolution : Settings.Resolution.values()) {
137 			resolutionModel.addElement(resolution);
138 		}
139 		resolutionModel.setSelectedItem(selectedItem);
140 		selectedItem = paperModel.getSelectedItem();
141 		paperModel.removeAllElements();
142 		for (Paper paper : Paper.values()) {
143 			paperModel.addElement(paper);
144 		}
145 		paperModel.setSelectedItem(selectedItem);
146 	}
147 
148 	public void applyTo(FaxJob job) {
149 		job.setSender(fullnameTextField.getText());
150 		job.setNotifyAdress(emailTextField.getText());
151 		
152 		job.setNotify(((Notification)notificationModel.getSelectedItem()).getCommand());
153 		job.setResolution((((Resolution)resolutionModel.getSelectedItem()).getLinesPerInch()));
154 		job.setPriority(priorityModel.getNumber().intValue());
155 		job.setMaxTries(((Integer)maxTriesSpinner.getValue()).intValue());
156 		job.setMaxDials(((Integer)maxDialsSpinner.getValue()).intValue());
157 		Paper paper = (Paper)paperModel.getSelectedItem();
158 		job.setPageWidth(paper.getWidth());
159 		job.setPageLength(paper.getHeight());
160 
161 	}
162 	
163 	public void revertFrom(FaxJob job) {
164 		fullnameTextField.setText(job.getSender());
165 		emailTextField.setText(job.getNotifyAdress());
166 
167 		if (job.getNotify() == null) {
168 			notificationModel.setSelectedItem(Settings.Notification.NEVER);
169 		}
170 		else {
171 			try {
172 				notificationModel.setSelectedItem(Settings.Notification.getEnum(job.getNotify()));
173 			} catch (IllegalArgumentException e) {
174 				Dialogs.showError(this, i18n.tr("Unknown notification type. Using default."));
175 				notificationModel.setSelectedItem(Settings.NOTIFICATION.getValue());
176 			}
177 		}
178 		try {
179 			resolutionModel.setSelectedItem(Settings.Resolution.getEnum(job.getVerticalResolution()));
180 		} catch (IllegalArgumentException e) {
181 			Dialogs.showError(this, i18n.tr("Invalid resolution. Using default."));
182 			resolutionModel.setSelectedItem(Settings.RESOLUTION.getValue());
183 		}
184 		prioritySpinner.setValue(job.getPriority());
185 		maxTriesSpinner.setValue(job.getMaxTries());
186 		maxDialsSpinner.setValue(job.getMaxDials());
187 		try {
188 			paperModel.setSelectedItem(Paper.getEnum(job.getPageWidth(), job.getPageLength()));
189 		} catch (IllegalArgumentException e) {
190 			Dialogs.showError(this, i18n.tr("Unknown paper type. Using default."));
191 			paperModel.setSelectedItem(Settings.PAPER.getValue());
192 		}
193 	}
194 	
195 	public void revertFromSettings() {
196 		fullnameTextField.setText(Settings.FULLNAME.getValue());
197 		emailTextField.setText(Settings.EMAIL.getValue());
198 
199 		notificationModel.setSelectedItem(Settings.NOTIFICATION.getValue());
200 		resolutionModel.setSelectedItem(Settings.RESOLUTION.getValue());
201 		prioritySpinner.setValue(Settings.PRIORITY.getValue());
202 		maxTriesSpinner.setValue(Settings.MAXTRIES.getValue());
203 		maxDialsSpinner.setValue(Settings.MAXDIALS.getValue());
204 		paperModel.setSelectedItem(Settings.PAPER.getValue());
205 	}
206 
207 	public void applyToSettings()
208 	{
209 		Settings.FULLNAME.setValue(fullnameTextField.getText());
210 		Settings.EMAIL.setValue(emailTextField.getText());
211 		
212 		Settings.NOTIFICATION.setValue((Notification)notificationModel.getSelectedItem());
213 		Settings.RESOLUTION.setValue((Resolution)resolutionModel.getSelectedItem());
214 		Settings.PRIORITY.setValue(priorityModel.getNumber().intValue());
215 		Settings.MAXTRIES.setValue((Integer)maxTriesSpinner.getValue());
216 		Settings.MAXDIALS.setValue((Integer)maxDialsSpinner.getValue());
217 		Settings.PAPER.setValue((Paper)paperModel.getSelectedItem());
218 	}
219 
220 	public void revertFromDefaultSettings()
221 	{
222 		fullnameTextField.setText(Settings.FULLNAME.getDefaultValue());
223 		emailTextField.setText(Settings.EMAIL.getDefaultValue());
224 
225 		notificationModel.setSelectedItem(Settings.NOTIFICATION.getDefaultValue());
226 		resolutionModel.setSelectedItem(Settings.RESOLUTION.getDefaultValue());
227 		prioritySpinner.setValue(Settings.PRIORITY.getDefaultValue());
228 		maxTriesSpinner.setValue(Settings.MAXTRIES.getDefaultValue());
229 		maxDialsSpinner.setValue(Settings.MAXDIALS.getDefaultValue());
230 		paperModel.setSelectedItem(Settings.PAPER.getDefaultValue());
231 	}
232 	
233 }