View Javadoc
1   package com.nilhcem.fakesmtp.gui.info;
2   
3   import com.nilhcem.fakesmtp.core.ArgsHandler;
4   import com.nilhcem.fakesmtp.core.I18n;
5   import com.nilhcem.fakesmtp.gui.DirChooser;
6   import com.nilhcem.fakesmtp.model.UIModel;
7   
8   import javax.swing.JTextField;
9   import java.awt.Color;
10  import java.awt.event.MouseEvent;
11  import java.awt.event.MouseListener;
12  import java.util.Observable;
13  import java.util.Observer;
14  
15  /**
16   * Text field in which will be written the path where emails will be automatically saved.
17   *
18   * @author Nilhcem
19   * @since 1.0
20   */
21  public final class SaveMsgField extends Observable implements Observer {
22  
23  	private final JTextField saveMsgField = new JTextField(UIModel.INSTANCE.getSavePath());
24  
25  	/**
26  	 * Creates a text field and adds a mouse listener, to display the directory chooser dialog when a user clicks on the field.
27  	 * <p>
28  	 * The text field will be disabled by default to avoid the user to type any folder directly.<br>
29  	 * Instead, he can use the directory chooser dialog to select the path he wants.
30  	 * </p>
31  	 */
32  	public SaveMsgField() {
33  		saveMsgField.setToolTipText(I18n.INSTANCE.get("savemsgfield.tooltip"));
34  
35  		// Disable edition but keep the same background color
36  		Color bg = saveMsgField.getBackground();
37  		saveMsgField.setEditable(false);
38  		saveMsgField.setBackground(bg);
39  
40  		if (!ArgsHandler.INSTANCE.memoryModeEnabled()) {
41  			// Add a MouseListener
42  			saveMsgField.addMouseListener(new MouseListener() {
43  				@Override
44  				public void mouseClicked(MouseEvent e) {
45  				}
46  
47  				@Override
48  				public void mousePressed(MouseEvent e) {
49  					openFolderSelection();
50  				}
51  
52  				@Override
53  				public void mouseReleased(MouseEvent e) {
54  				}
55  
56  				@Override
57  				public void mouseEntered(MouseEvent e) {
58  				}
59  
60  				@Override
61  				public void mouseExited(MouseEvent e) {
62  				}
63  
64  				private void openFolderSelection() {
65  					setChanged();
66  					notifyObservers();
67  				}
68  			});
69  		}
70  	}
71  
72  	/**
73  	 * Returns the JTextField object.
74  	 *
75  	 * @return the JTextField object.
76  	 */
77  	public JTextField get() {
78  		return saveMsgField;
79  	}
80  
81  	/**
82  	 * Updates the content of the JTextField with the new directory value.
83  	 * <p>
84  	 * Once a directory has been chosen by the {@link DirChooser}, the latter will
85  	 * notify this class, so that it can update its content.
86  	 * </p>
87  	 *
88  	 * @param o the observable element which will notify this class.
89  	 * @param arg optional parameters (not used).
90  	 */
91  	@Override
92  	public void update(Observable o, Object arg) {
93  		if (o instanceof DirChooser) {
94  			saveMsgField.setText(UIModel.INSTANCE.getSavePath());
95  		}
96  	}
97  }