]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/PhotoPopupFunction.java
Version 18.1, September 2015
[GpsPrune.git] / tim / prune / function / PhotoPopupFunction.java
1 package tim.prune.function;
2
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.Dimension;
6 import java.awt.FlowLayout;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.awt.event.KeyEvent;
10 import java.awt.event.KeyListener;
11 import java.awt.event.WindowAdapter;
12 import java.awt.event.WindowEvent;
13
14 import javax.swing.JButton;
15 import javax.swing.JDialog;
16 import javax.swing.JFrame;
17 import javax.swing.JLabel;
18 import javax.swing.JPanel;
19 import tim.prune.App;
20 import tim.prune.DataSubscriber;
21 import tim.prune.GenericFunction;
22 import tim.prune.I18nManager;
23 import tim.prune.UpdateMessageBroker;
24 import tim.prune.data.Photo;
25 import tim.prune.gui.PhotoThumbnail;
26
27 /**
28  * Class to show a popup window for a photo
29  */
30 public class PhotoPopupFunction extends GenericFunction implements DataSubscriber
31 {
32         /** popup window */
33         private JFrame _frame = null; // would be a JDialog but that doesn't allow max button
34         /** label for filename */
35         private JLabel _label = null;
36         /** Photo thumbnail */
37         private PhotoThumbnail _photoThumb = null;
38
39         /**
40          * Constructor
41          * @param inApp app object
42          */
43         public PhotoPopupFunction(App inApp)
44         {
45                 super(inApp);
46         }
47
48         /**
49          * Get the name key
50          */
51         public String getNameKey() {
52                 return "function.photopopup";
53         }
54
55         /**
56          * Show the screen
57          */
58         public void begin()
59         {
60                 if (_frame == null)
61                 {
62                         _frame = new JFrame(I18nManager.getText(getNameKey()));
63                         _frame.setIconImage(_parentFrame.getIconImage());
64                         _frame.getContentPane().add(makeContents());
65                         _frame.pack();
66                         _frame.setLocationRelativeTo(_parentFrame);
67                         _frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
68                         _frame.addWindowListener(new WindowAdapter() {
69                                 public void windowClosed(WindowEvent e) {
70                                         UpdateMessageBroker.removeSubscriber(PhotoPopupFunction.this);
71                                         super.windowClosed(e);
72                                 }
73                         });
74                 }
75                 initFrame();
76                 final Photo photo = _app.getTrackInfo().getCurrentPhoto();
77                 if (photo.getWidth() <= 0 || photo.getHeight() <= 0)
78                 {
79                         _frame.setVisible(false);
80                         _app.showErrorMessageNoLookup(getNameKey(), I18nManager.getText("error.showphoto.failed")
81                          + " : " + photo.getName());
82                 }
83                 else
84                 {
85                         _frame.setVisible(true);
86                         // Add listener to Broker
87                         UpdateMessageBroker.addSubscriber(this);
88                 }
89         }
90
91         /**
92          * Initialise the frame to show the current photo
93          */
94         private void initFrame()
95         {
96                 Photo photo = _app.getTrackInfo().getCurrentPhoto();
97                 if (photo == null)
98                 {
99                         _frame.setTitle("GpsPrune - " + I18nManager.getText("details.nophoto"));
100                         _label.setText(I18nManager.getText("details.nophoto"));
101                 }
102                 else
103                 {
104                         _frame.setTitle(photo.getName());
105                         _label.setText("'" + photo.getName() + "' ("
106                                 + photo.getWidth() + " x " + photo.getHeight() + ")");
107                 }
108                 _photoThumb.setPhoto(photo);
109         }
110
111         /**
112          * @return the contents of the window as a Component
113          */
114         private Component makeContents()
115         {
116                 JPanel mainPanel = new JPanel();
117                 mainPanel.setLayout(new BorderLayout());
118                 _label = new JLabel("Photo popup");
119                 mainPanel.add(_label, BorderLayout.NORTH);
120                 _photoThumb = new PhotoThumbnail(false); // specify not in details panel
121                 _photoThumb.setPreferredSize(new Dimension(300, 300));
122                 mainPanel.add(_photoThumb, BorderLayout.CENTER);
123                 // Close button at bottom
124                 JPanel okPanel = new JPanel();
125                 okPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
126                 JButton okButton = new JButton(I18nManager.getText("button.ok"));
127                 okButton.addActionListener(new ActionListener()
128                 {
129                         public void actionPerformed(ActionEvent e) {
130                                 _frame.dispose();
131                         }
132                 });
133                 okButton.addKeyListener(new KeyListener() {
134                         public void keyPressed(KeyEvent e) {
135                                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {_frame.dispose();}
136                         }
137                         public void keyTyped(KeyEvent e) {}
138                         public void keyReleased(KeyEvent e) {}
139                 });
140                 okPanel.add(okButton);
141                 mainPanel.add(okPanel, BorderLayout.SOUTH);
142                 return mainPanel;
143         }
144
145         public void dataUpdated(byte inUpdateType)
146         {
147                 // Update photo if selection changes
148                 if ((inUpdateType & DataSubscriber.SELECTION_CHANGED) > 0)
149                 {
150                         initFrame();
151                 }
152         }
153
154         public void actionCompleted(String inMessage) {}
155 }