1
2
3
4
5
6
7
8
9
10
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
46
47
48
49 public class InputFolders extends JPanel {
50
51
52 protected File path;
53
54
55 protected JButton compareButton;
56
57
58 protected GlobalContext context;
59
60
61 protected JFrame window;
62
63
64 public static final int MAX_RECENT = 10;
65
66
67 public static Icon icon = new ImageIcon(InputFolders.class.getResource("/bullet.png"));
68
69
70 protected FileSelector leftPanel;
71
72
73 protected FileSelector rightPanel;
74
75
76
77
78
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
182
183
184
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
204
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
214
215 inputWindow.setVisible(true);
216
217 }
218
219
220
221
222
223
224
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
238 e.printStackTrace();
239 }
240 }
241 }
242
243
244
245
246 public static class Link extends JLabel {
247
248
249 protected Action action;
250
251
252 protected Color fg;
253
254
255
256
257 protected Link() {
258 }
259
260
261
262
263
264
265 public Link(String name, Icon icon) {
266 super(name, icon, JLabel.LEFT);
267 init();
268 }
269
270
271
272
273
274 public void setAction(Action action) {
275 this.action = action;
276 }
277
278
279
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
314
315 public static class PreviousSession extends Link {
316
317
318 protected String left;
319
320
321 protected String right;
322
323
324
325
326
327
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 }