View Javadoc

1   /*
2    * Copyright (c) 2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.aequo.gui;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Color;
17  import java.awt.Font;
18  import java.awt.event.ActionEvent;
19  import java.awt.event.MouseEvent;
20  import java.awt.event.MouseListener;
21  import java.io.File;
22  import java.io.IOException;
23  
24  import javax.swing.AbstractAction;
25  import javax.swing.Action;
26  import javax.swing.Box;
27  import javax.swing.BoxLayout;
28  import javax.swing.Icon;
29  import javax.swing.ImageIcon;
30  import javax.swing.JButton;
31  import javax.swing.JFrame;
32  import javax.swing.JLabel;
33  import javax.swing.JPanel;
34  import javax.swing.border.CompoundBorder;
35  import javax.swing.border.EmptyBorder;
36  
37  import org.abstracthorizon.aequo.GlobalContext;
38  import org.abstracthorizon.aequo.action.CompareFilesAction;
39  import org.abstracthorizon.aequo.util.Preferences;
40  import org.abstracthorizon.aequo.util.StringUtils;
41  import org.abstracthorizon.aequo.util.filters.Filter;
42  import org.abstracthorizon.aequo.util.prefs.RecentFileEntry;
43  
44  /**
45   * Main class of this project.
46   *
47   * @author Daniel Sendula
48   */
49  public class InputFolders extends JPanel {
50      
51      /** Path */
52      protected File path;
53      
54      /** Compare button */
55      protected JButton compareButton;
56      
57      /** Global context */
58      protected GlobalContext context;
59  
60      /** Window */
61      protected JFrame window;
62      
63      /** Number of maximum recent entries */
64      public static final int MAX_RECENT = 10;
65  
66      /** Icon of bullet points */
67      public static Icon icon = new ImageIcon(InputFolders.class.getResource("/bullet.png"));
68  
69      /** Left file selector panel */
70      protected FileSelector leftPanel;
71  
72      /** Right file selector panel */
73      protected FileSelector rightPanel;
74      
75      /**
76       * Constructor
77       * @param window window to be closed when input is selected (or <code>null</code>)
78       * @param context global context
79       */
80      public InputFolders(JFrame window, GlobalContext context) {
81          super(new BorderLayout());
82          this.context = context;
83          this.window = window;
84  
85          Box topPanel = new Box(BoxLayout.X_AXIS);
86          JLabel name = new JLabel("Aequo");
87          name.setFont(name.getFont().deriveFont(20.0f));
88          topPanel.add(name);
89          topPanel.add(Box.createHorizontalGlue());
90          
91          Icon logoIcon = new ImageIcon(getClass().getResource("/aequo-logo-40-notext.png"));
92          JLabel logo = new JLabel(logoIcon);
93          logo.setFont(name.getFont());
94          topPanel.setBorder(new CompoundBorder(new LineBorder(0, 0, 1, 0), new EmptyBorder(10, 20, 10, 10)));
95          topPanel.add(logo);
96          add(topPanel, BorderLayout.NORTH);
97  
98          Box bottomPanel = new Box(BoxLayout.X_AXIS);
99          bottomPanel.add(Box.createHorizontalGlue());
100         JButton exitButton = new JButton(new AbstractAction("   Exit   ") {
101             public void actionPerformed(ActionEvent e) {
102                 InputFolders.this.window.dispose();
103             }
104         });
105         bottomPanel.setBorder(new CompoundBorder(new LineBorder(1, 0, 0, 0), new EmptyBorder(10, 10, 10, 10)));
106         bottomPanel.add(exitButton);
107         add(bottomPanel, BorderLayout.SOUTH);
108         
109         JPanel middlePanel = new GradientPanel(new BorderLayout());
110         middlePanel.setBorder(new EmptyBorder(5, 20, 5, 20));
111 
112         EmptyBorder leftMargin = new EmptyBorder(5, 30, 0, 0);
113 
114         
115         JLabel question = new JLabel("What do you want to do?");
116         question.setFont(question.getFont().deriveFont(15.0f));
117         JPanel questionPanel = new JPanel(new TableFormLayout(1));
118         questionPanel.setOpaque(false);
119         questionPanel.add(question);
120         questionPanel.setBorder(new EmptyBorder(5, 0, 5, 5));
121         
122         final JPanel inputPanel = new JPanel(new TableFormLayout(3));
123         inputPanel.setOpaque(false);
124         inputPanel.setBorder(new EmptyBorder(5, 60, 5, 5));
125         inputPanel.setVisible(false);
126         
127 
128         Link newComparison = new Link("New comparison...", icon);
129         newComparison.setAction(
130             new AbstractAction("New comparison...") {
131                 public void actionPerformed(ActionEvent e) {
132                     inputPanel.setVisible(!inputPanel.isVisible());
133                 }
134             });
135         newComparison.setFont(question.getFont());
136         newComparison.setBorder(leftMargin);
137         
138         questionPanel.add(newComparison);
139         questionPanel.add(inputPanel);
140         middlePanel.add(questionPanel, BorderLayout.NORTH);
141 
142         
143         leftPanel = new FileSelector(context, inputPanel, "Left side:") {
144             public void actionPerformed(ActionEvent e) {
145                 super.actionPerformed(e);
146                 compareButton.setEnabled((leftPanel.getFile() != null) && (rightPanel.getFile() != null)); 
147             }
148         };
149 
150         rightPanel = new FileSelector(context, inputPanel, "Right side:") {
151             public void actionPerformed(ActionEvent e) {
152                 super.actionPerformed(e);
153                 compareButton.setEnabled((leftPanel.getFile() != null) && (rightPanel.getFile() != null)); 
154             }
155         };
156         
157         compareButton = new JButton("Compare");
158         compareButton.setOpaque(false);
159         compareButton.addActionListener(new CompareFilesAction(context, window) {
160             public File getLeft() {
161                 return leftPanel.getFile();
162             }
163             public File getRight() {
164                 return rightPanel.getFile();
165             }
166         });
167         
168         inputPanel.add(new JLabel(""));
169         inputPanel.add(new JLabel(""));
170         inputPanel.add(compareButton, BorderLayout.EAST);
171         compareButton.setEnabled(false);
172         add(middlePanel, BorderLayout.CENTER);
173         
174         JPanel previousSessions = new JPanel(new TableFormLayout(1));
175         previousSessions.setOpaque(false);
176         updateRecentSessions(previousSessions, question.getFont(), window);
177         middlePanel.add(previousSessions, BorderLayout.CENTER);
178     }
179     
180     /**
181      * Updates recent sessions on the panel
182      * @param panel panel to be updated
183      * @param font font to be used
184      * @param window window to be closed (or <code>null</code>)
185      */
186     protected void updateRecentSessions(JPanel panel, Font font, JFrame window) {
187         EmptyBorder leftMargin = new EmptyBorder(5, 30, 0, 0);
188         Preferences prefs = context.getPreferences();
189         int num = RecentFileEntry.getNumberOfRecent(prefs);
190         if (num > 0) {
191             for (int i = 1; i <= num; i++) {
192                 RecentFileEntry entry = RecentFileEntry.read(prefs, i);
193                 Link link = new PreviousSession(font, entry.left, entry.right);
194                 link.setAction(new CompareFilesAction(new File(entry.left), new File(entry.right), new Filter(entry.filters), context, window));
195                 link.setBorder(leftMargin);
196                 link.setFont(font);
197                 panel.add(link);
198             }
199         }
200     }
201     
202     /**
203      * Starts new session
204      * @param context global context
205      */
206     public static void startNewSession(GlobalContext context) {
207         
208         final JFrame inputWindow = new JFrame("Aequo");
209         context.addWindow(inputWindow, "window.start");
210         
211         JPanel panel = new InputFolders(inputWindow, context);
212         inputWindow.getContentPane().add(panel, BorderLayout.NORTH);
213         //inputWindow.pack();
214         
215         inputWindow.setVisible(true);
216         
217     }
218     
219     /**
220      * Adds new session to the list of recent sessions
221      * @param prefs preferences
222      * @param leftFile left file
223      * @param rightFile right file
224      * @param filters filters
225      */
226     public static void addNewSession(Preferences prefs, File leftFile, File rightFile, Filter filters) {
227         String left = leftFile.getAbsolutePath();
228         String right = rightFile.getAbsolutePath();
229         String filter = filters.toString();
230 
231         RecentFileEntry entry = new RecentFileEntry(left, right, filter);
232         
233         if (RecentFileEntry.writeNew(prefs, entry)) {
234             try {
235                 prefs.save();
236             } catch (IOException e) {
237                 // TODO ?
238                 e.printStackTrace();
239             }
240         }
241     }
242     
243     /**
244      * Link component used for recent entries, etc...
245      */
246     public static class Link extends JLabel {
247 
248         /** Action to be invoked */
249         protected Action action;
250         
251         /** Foreground to be used when hovered over */
252         protected Color fg;
253 
254         /**
255          * Constructor
256          */
257         protected Link() {
258         }
259         
260         /**
261          * Constructor
262          * @param name name of the link
263          * @param icon icon to be used
264          */
265         public Link(String name, Icon icon) {
266             super(name, icon, JLabel.LEFT);
267             init();
268         }
269         
270         /**
271          * Sets the action
272          * @param action action
273          */
274         public void setAction(Action action) {
275             this.action = action;
276         }
277         
278         /**
279          * Initialises the link
280          */
281         protected void init() {
282             fg = getForeground(); 
283             setOpaque(false);
284             
285             addMouseListener(new MouseListener() {
286 
287                 public void mouseClicked(MouseEvent e) {
288                     action.actionPerformed(new ActionEvent(this, 0, null));
289                 }
290 
291                 @SuppressWarnings("unchecked")
292                 public void mouseEntered(MouseEvent e) {
293                     setText("<html><u>" + getText() + "</u></html>");
294                     setForeground(fg.brighter());
295                 }
296 
297                 public void mouseExited(MouseEvent e) {
298                     String t = getText();
299                     if (t.startsWith("<html><u>")) {
300                         setText(t.substring(9, t.length() - 11));
301                     }
302                     setForeground(fg);
303                 }
304 
305                 public void mousePressed(MouseEvent e) { }
306                 public void mouseReleased(MouseEvent e) { }
307                 
308             });
309         }
310     }
311     
312     /**
313      * Link to be used for previous session
314      */
315     public static class PreviousSession extends Link {
316 
317         /** Left file string */
318         protected String left;
319         
320         /** Right file string */
321         protected String right;
322         
323         /**
324          * Constructor
325          * @param font font to be used
326          * @param left left file string
327          * @param right right file string
328          */
329         public PreviousSession(Font font, String left, String right) {
330             super("Compare: ", icon);
331             setText("Compare: ");
332             this.left = left;
333             this.right = right;
334         }
335 
336         public void setBounds(int x, int y, int w, int h) {
337             if ((x != getX()) || (y != getY()) || (w != getWidth()) || (h != getHeight())) {
338                 super.setBounds(x, y, w, h);
339                 String text = getText();
340                 boolean underline = false;
341                 if (text.startsWith("<html><u>")) {
342                     text = text.substring(9, text.length() - 11);
343                     underline = true;
344                 }
345                 String newText = "Compare: " + StringUtils.calculateTitle(this, w, left, right, getFont());
346                 if (!text.equals(newText)) {
347                     if (underline) {
348                         text = "<html><u>" + text + "</u></html>";
349                     }
350                     setText(newText);
351                 }
352             }
353         }
354         
355     }
356 }