1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.ninjasoft.igallery.swingui;
18
19 import java.io.*;
20 import java.util.*;
21
22 import java.awt.*;
23 import java.awt.event.*;
24 import javax.swing.*;
25 import javax.swing.event.*;
26 import javax.swing.table.*;
27
28 import org.ninjasoft.igallery.logic.*;
29 import org.ninjasoft.igallery.data.*;
30
31 import com.apple.eawt.*;
32
33 public class MainFrame extends JFrame implements StatusListener {
34 private boolean initialized = false;
35 private Actions actions = new Actions();
36 private JTextField urlText = new JTextField();
37 private JTextField usernameText = new JTextField();
38 private JPasswordField passwordText = new JPasswordField();
39 private IPhotoTableModel iPhotoTableModel = new IPhotoTableModel();
40 private GalleryTableModel galleryTableModel = new GalleryTableModel();
41 private JTable iPhotoTable = new JTable(iPhotoTableModel);
42 private JTable galleryTable = new JTable(galleryTableModel);
43 private JButton loginButton = new JButton("Login");
44 private JButton quitButton = new JButton("Quit");
45 private JButton compareButton = new JButton("Compare");
46 private JButton uploadButton = new JButton("Upload");
47 private JButton cancelButton = new JButton("Cancel");
48 private JPanel buttonSwapPanel = new JPanel();
49 private CardLayout buttonSwapLayout = new CardLayout();
50 private boolean loggedIn = false;
51 private IGalleryLogic iGalleryLogic = new IGalleryLogic(this);
52 private ApplicationProperties applicationProperties = null;
53 private JTextArea status = new JTextArea();
54 private String version = "";
55 private JCheckBox debugCheckbox = new JCheckBox("Debug");
56
57 public void initialize() {
58 if (initialized)
59 return;
60 initialized = true;
61 initializeApple();
62 initializeVersion();
63 initializeGui();
64 initializeEvents();
65 setLoggedIn(false);
66 this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
67 iGalleryLogic.loadIPhotoAlbums();
68 iPhotoTableModel.setAlbumList(iGalleryLogic.getIPhotoAlbums());
69 initializePreferences();
70 }
71
72 private void initializeGui() {
73 this.setSize(500, 400);
74 this.setTitle("iGallery " + version);
75 Dimension windowSize = this.getSize();
76 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
77 this.setLocation(screenSize.width/2 - windowSize.width/2, screenSize.height/2 - windowSize.height/2);
78 Font font = new Font("Dialog", Font.PLAIN, 9);
79 status.setFont(font);
80 status.setEditable(false);
81 status.setOpaque(false);
82 status.setRows(4);
83 Container pane = this.getContentPane();
84 pane.setLayout(new BorderLayout());
85 pane.add(PanelBuilder.createLoginPanel(urlText, usernameText, passwordText, loginButton, debugCheckbox), BorderLayout.NORTH);
86 pane.add(PanelBuilder.createLists(iPhotoTable, galleryTable), BorderLayout.CENTER);
87 buttonSwapPanel.setLayout(buttonSwapLayout);
88 buttonSwapPanel.add(uploadButton, "upload");
89 buttonSwapPanel.add(cancelButton, "cancel");
90 buttonSwapLayout.show(buttonSwapPanel, "upload");
91 pane.add(PanelBuilder.createButtons(status, compareButton, buttonSwapPanel, cancelButton, uploadButton), BorderLayout.SOUTH);
92 this.galleryTable.setDefaultRenderer(String.class, new DefaultTableCellRenderer() {
93 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
94 Component result = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
95 if (column == 1) {
96 ((DefaultTableCellRenderer) result).setHorizontalTextPosition(DefaultTableCellRenderer.RIGHT);
97 ((DefaultTableCellRenderer) result).setHorizontalAlignment(DefaultTableCellRenderer.RIGHT);
98 }else{
99 ((DefaultTableCellRenderer) result).setHorizontalTextPosition(DefaultTableCellRenderer.LEFT);
100 ((DefaultTableCellRenderer) result).setHorizontalAlignment(DefaultTableCellRenderer.LEFT);
101 }
102 return result;
103 }
104 });
105 }
106
107 private void initializeEvents() {
108 KeyListener keyListener = new KeyListener() {
109 public void keyPressed(KeyEvent e) {
110 int code = e.getKeyCode();
111 int modifier = e.getModifiers();
112 if (code == KeyEvent.VK_ENTER) {
113 toggleLoginState();
114 loginButton.requestFocus();
115 } else if ((modifier == KeyEvent.META_MASK) && (code == KeyEvent.VK_W)) {
116 dispose();
117 }
118 }
119 public void keyReleased(KeyEvent e) {}
120 public void keyTyped(KeyEvent e) {}
121 };
122 cancelButton.setEnabled(false);
123 urlText.addKeyListener(keyListener);
124 usernameText.addKeyListener(keyListener);
125 passwordText.addKeyListener(keyListener);
126 this.loginButton.setActionCommand("login");
127 this.loginButton.addActionListener(actions);
128 this.compareButton.setActionCommand("compare");
129 this.compareButton.addActionListener(actions);
130 this.uploadButton.setActionCommand("upload");
131 this.uploadButton.addActionListener(actions);
132 this.cancelButton.setActionCommand("cancel");
133 this.cancelButton.addActionListener(actions);
134 debugCheckbox.addChangeListener(new ChangeListener() {
135 public void stateChanged(ChangeEvent e) {
136 Cookiejar.DEBUG = debugCheckbox.isSelected();
137 GalleryConnection.DEBUG = debugCheckbox.isSelected();
138 }
139 });
140 }
141
142 private void initializePreferences() {
143 try{
144 applicationProperties = new ApplicationProperties();
145 }catch(java.io.IOException e){e.printStackTrace();}
146 urlText.setText(applicationProperties.getProperty("url", "http://domain.com/gallery/gallery_remote2.php"));
147 usernameText.setText(applicationProperties.getProperty("username", "admin"));
148 passwordText.setText(applicationProperties.getProperty("password", ""));
149 debugCheckbox.setSelected(applicationProperties.getProperty("debug", "false").equals("true"));
150 }
151
152 private void initializeApple() {
153 System.setProperty("com.apple.mrj.application.apple.menu.about.name", "iGallery");
154 System.setProperty("com.apple.laf.useScreenMenuBar", "true");
155 System.setProperty("apple.awt.brushMetalLook", "true");
156 Application application = Application.getApplication();
157 application.addApplicationListener(new ApplicationAdapter() {
158 public void handleQuit(ApplicationEvent event) {
159 dispose();
160 }
161 });
162 application.setEnabledAboutMenu(false);
163 application.setEnabledPreferencesMenu(false);
164 }
165
166 private void initializeVersion() {
167 InputStream in = this.getClass().getResourceAsStream("/org/ninjasoft/igallery/version.properties");
168 if (in == null)
169 return;
170 Properties versionProperties = new Properties();
171 try{
172 versionProperties.load(in);
173 in.close();
174 }catch(Exception e) {}
175 String versionTemp = versionProperties.getProperty("version", "");
176 if ((versionTemp != null) && (versionTemp.length() > 0) && !versionTemp.equals("@VERSION@"))
177 this.version = versionTemp;
178 }
179
180 private void setLoggedIn(boolean b) {
181 this.loggedIn = b;
182 urlText.setEnabled(!b);
183 usernameText.setEnabled(!b);
184 passwordText.setEnabled(!b);
185 loginButton.setText(b ? "Logout" : "Login");
186 uploadButton.setEnabled(b);
187 quitButton.setEnabled(b);
188 compareButton.setEnabled(b);
189 galleryTable.setEnabled(b);
190 iPhotoTable.setEnabled(b);
191 }
192
193 private void toggleLoginState() {
194 if (this.loggedIn) {
195 iGalleryLogic.logout();
196 setLoggedIn(false);
197 }else{
198 loginButton.setEnabled(false);
199 uploadButton.setEnabled(false);
200 compareButton.setEnabled(false);
201 if (iGalleryLogic.login(urlText.getText(), usernameText.getText(), new String(passwordText.getPassword()))) {
202 setLoggedIn(true);
203 this.galleryTableModel.setAlbumList(iGalleryLogic.getGalleryAlbums());
204 }
205 new Thread(new Runnable() {
206 public void run() {
207 setBusy(true);
208 cancelButton.setEnabled(false);
209 iGalleryLogic.getAlbumDetails();
210 setBusy(false);
211 }
212 }).start();
213 }
214 }
215
216 private void compare() {
217 if (iPhotoTable.getSelectedRow() < 0)
218 return;
219 if (galleryTable.getSelectedRow() < 0)
220 return;
221 IPhotoAlbum iPhotoAlbum = (IPhotoAlbum) iGalleryLogic.getIPhotoAlbums().get(iPhotoTable.getSelectedRow());
222 GalleryAlbum galleryAlbum = (GalleryAlbum) iGalleryLogic.getGalleryAlbums().get(galleryTable.getSelectedRow());
223 iGalleryLogic.compareAlbums(iPhotoAlbum, galleryAlbum);
224 int extraLocal = iGalleryLogic.getMissingRemote().size();
225 int extraRemote = iGalleryLogic.getMissingLocal().size();
226 String message = "";
227 if ((extraLocal == 0) && (extraRemote == 0)) {
228 message = "The albums are the same";
229 }
230 if (extraLocal > 0) {
231 message = "There " +
232 (extraLocal == 1 ? "is " : "are ") +
233 + extraLocal + " picture" +
234 (extraLocal == 1 ? "" : "s") +
235 " in the iPhoto album but not yet on the server.";
236 }
237 if (extraRemote > 0) {
238 if (message.length() > 0)
239 message = message + "\n";
240 message = message + "There " +
241 (extraLocal == 1 ? "is " : "are ") +
242 + extraRemote + " picture" +
243 (extraLocal == 1 ? "" : "s") +
244 " on the server but not in the iPhoto album.";
245 }
246 JOptionPane.showMessageDialog(this, message, "Info", JOptionPane.INFORMATION_MESSAGE);
247 }
248
249 private void upload() {
250 if (iPhotoTable.getSelectedRow() < 0)
251 return;
252 if (galleryTable.getSelectedRow() < 0)
253 return;
254 IPhotoAlbum iPhotoAlbum = (IPhotoAlbum) iGalleryLogic.getIPhotoAlbums().get(iPhotoTable.getSelectedRow());
255 GalleryAlbum galleryAlbum = (GalleryAlbum) iGalleryLogic.getGalleryAlbums().get(galleryTable.getSelectedRow());
256 iGalleryLogic.upload(galleryAlbum, iPhotoAlbum);
257 }
258
259 private void cancel() {
260 iGalleryLogic.cancel();
261 }
262
263 public void setError(String message) {
264 System.out.println(message);
265 scrollStatus(message);
266 }
267 public void setStatus(String message) {
268 int row = galleryTable.getSelectedRow();
269 galleryTableModel.tellChanged();
270 if (row > 0)
271 galleryTable.addRowSelectionInterval(row, row);
272 System.out.println(message);
273 scrollStatus(message);
274 }
275 public void setBusy(boolean b) {
276 uploadButton.setEnabled(!b);
277 compareButton.setEnabled(!b);
278 loginButton.setEnabled(!b);
279 cancelButton.setEnabled(b);
280 buttonSwapLayout.show(buttonSwapPanel, b ? "cancel" : "upload");
281 }
282 public void scrollStatus(String message) {
283 StringBuffer text = new StringBuffer(status.getText());
284 text.append("\n");
285 text.append(message);
286 if (text.length() > 512) {
287 int pos = text.indexOf("\n");
288 text.delete(0, pos);
289 }
290 status.setText(text.toString());
291 status.setCaretPosition(text.length());
292 }
293
294 public class Actions implements ActionListener {
295 public void actionPerformed(ActionEvent e) {
296 String command = e.getActionCommand();
297 command = command == null ? "" : command;
298 if (command.equals("login"))
299 toggleLoginState();
300 else if (command.equals("compare"))
301 compare();
302 else if (command.equals("upload"))
303 upload();
304 else if (command.equals("cancel"))
305 cancel();
306 }
307 }
308
309 public void dispose() {
310 applicationProperties.setProperty("url", urlText.getText());
311 applicationProperties.setProperty("username", usernameText.getText());
312 applicationProperties.setProperty("password", new String(passwordText.getPassword()));
313 applicationProperties.setProperty("debug", debugCheckbox.isSelected() ? "true" : "false");
314 try{
315 applicationProperties.save();
316 }catch(java.io.IOException e){e.printStackTrace();}
317 System.exit(0);
318 }
319
320 public void setVisible(boolean b) {
321 initialize();
322 super.setVisible(b);
323 }
324
325
326 public static void main(String[] args) {
327 new MainFrame().setVisible(true);
328 }
329 }