]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/SetKmzImageSize.java
Version 8, September 2009
[GpsPrune.git] / tim / prune / function / SetKmzImageSize.java
1 package tim.prune.function;
2
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.FlowLayout;
6 import java.awt.GridLayout;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.awt.event.KeyAdapter;
10 import java.awt.event.KeyEvent;
11 import java.awt.event.MouseAdapter;
12
13 import javax.swing.BorderFactory;
14 import javax.swing.JButton;
15 import javax.swing.JDialog;
16 import javax.swing.JLabel;
17 import javax.swing.JPanel;
18 import javax.swing.SwingConstants;
19
20 import tim.prune.App;
21 import tim.prune.Config;
22 import tim.prune.GenericFunction;
23 import tim.prune.I18nManager;
24 import tim.prune.gui.WholeNumberField;
25
26 /**
27  * Class to provide the function to set the image size for kmz output
28  */
29 public class SetKmzImageSize extends GenericFunction
30 {
31         private JDialog _dialog = null;
32         private JButton _okButton = null;
33         private WholeNumberField _widthField = null, _heightField = null;
34
35
36         /**
37          * Constructor
38          * @param inApp application object for callback
39          */
40         public SetKmzImageSize(App inApp)
41         {
42                 super(inApp);
43         }
44
45         /** Get the name key */
46         public String getNameKey() {
47                 return "function.setkmzimagesize";
48         }
49
50         /**
51          * Begin the function
52          */
53         public void begin()
54         {
55                 // Make dialog window
56                 if (_dialog == null)
57                 {
58                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
59                         _dialog.setLocationRelativeTo(_parentFrame);
60                         _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
61                         _dialog.getContentPane().add(makeDialogComponents());
62                         _dialog.pack();
63                 }
64                 // Initialise values from config
65                 _widthField.setValue(Config.getConfigInt(Config.KEY_KMZ_IMAGE_WIDTH));
66                 _heightField.setValue(Config.getConfigInt(Config.KEY_KMZ_IMAGE_HEIGHT));
67                 _dialog.setVisible(true);
68         }
69
70
71         /**
72          * Create dialog components
73          * @return Panel containing all gui elements in dialog
74          */
75         private Component makeDialogComponents()
76         {
77                 JPanel dialogPanel = new JPanel();
78                 dialogPanel.setLayout(new BorderLayout());
79
80                 // Make a central panel with the text boxes
81                 JPanel mainPanel = new JPanel();
82                 mainPanel.setLayout(new GridLayout(0, 2));
83                 mainPanel.add(makeRightLabel("dialog.saveconfig.prune.kmzimagewidth"));
84                 _widthField = new WholeNumberField(4);
85                 mainPanel.add(_widthField);
86                 mainPanel.add(makeRightLabel("dialog.saveconfig.prune.kmzimageheight"));
87                 _heightField = new WholeNumberField(4);
88                 mainPanel.add(_heightField);
89                 dialogPanel.add(mainPanel, BorderLayout.CENTER);
90
91                 // Listeners to enable/disable ok button
92                 KeyAdapter keyListener = new KeyAdapter() {
93                         /** Key released */
94                         public void keyReleased(KeyEvent arg0) {
95                                 _okButton.setEnabled(_widthField.getValue()>0 && _heightField.getValue()>0);
96                         }
97                 };
98                 MouseAdapter mouseListener = new MouseAdapter() {
99                         public void mouseReleased(java.awt.event.MouseEvent arg0) {
100                                 _okButton.setEnabled(_widthField.getValue()>0 && _heightField.getValue()>0);
101                         };
102                 };
103                 _widthField.addKeyListener(keyListener);
104                 _heightField.addKeyListener(keyListener);
105                 _widthField.addMouseListener(mouseListener);
106                 _heightField.addMouseListener(mouseListener);
107
108                 // button panel at bottom
109                 JPanel buttonPanel = new JPanel();
110                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
111                 _okButton = new JButton(I18nManager.getText("button.ok"));
112                 ActionListener okListener = new ActionListener() {
113                         public void actionPerformed(ActionEvent e)
114                         {
115                                 finish();
116                         }
117                 };
118                 _okButton.addActionListener(okListener);
119                 _okButton.setEnabled(false);
120                 buttonPanel.add(_okButton);
121                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
122                 cancelButton.addActionListener(new ActionListener() {
123                         public void actionPerformed(ActionEvent e)
124                         {
125                                 _dialog.dispose();
126                         }
127                 });
128                 buttonPanel.add(cancelButton);
129                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
130                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
131                 return dialogPanel;
132         }
133
134         /**
135          * @param inKey text key
136          * @return right-aligned label
137          */
138         private static final JLabel makeRightLabel(String inKey)
139         {
140                 JLabel label = new JLabel(I18nManager.getText(inKey) + " : ");
141                 label.setHorizontalAlignment(SwingConstants.RIGHT);
142                 return label;
143         }
144
145
146         /**
147          * Finish the dialog when OK pressed
148          */
149         private void finish()
150         {
151                 if (_widthField.getValue() > 0 && _heightField.getValue() > 0)
152                 {
153                         // Set entered values in Config
154                         Config.setConfigInt(Config.KEY_KMZ_IMAGE_WIDTH, _widthField.getValue());
155                         Config.setConfigInt(Config.KEY_KMZ_IMAGE_HEIGHT, _heightField.getValue());
156                         _dialog.dispose();
157                 }
158         }
159 }