1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.aequo.file;
14
15 import java.awt.BorderLayout;
16 import java.awt.Dimension;
17 import java.awt.Font;
18 import java.awt.event.ComponentEvent;
19 import java.awt.event.ComponentListener;
20
21 import javax.swing.JFrame;
22 import javax.swing.JMenu;
23 import javax.swing.JMenuBar;
24
25 import org.abstracthorizon.aequo.GlobalContext;
26 import org.abstracthorizon.aequo.action.AboutAction;
27 import org.abstracthorizon.aequo.action.BaseAction;
28 import org.abstracthorizon.aequo.action.ExitAction;
29 import org.abstracthorizon.aequo.action.PreviousSessionAction;
30 import org.abstracthorizon.aequo.gui.AbstractComparePanel;
31 import org.abstracthorizon.aequo.util.Preferences;
32 import org.abstracthorizon.aequo.util.StringUtils;
33 import org.abstracthorizon.aequo.util.prefs.RecentFileEntry;
34
35
36
37
38
39
40 public abstract class BaseFileCompareWindow extends JFrame {
41
42
43 protected GlobalContext context;
44
45 @SuppressWarnings("unchecked")
46
47 protected AbstractComparePanel comparePanel;
48
49
50 protected static ComponentListener resizeListener = new ComponentListener() {
51
52 public void componentHidden(ComponentEvent e) { }
53
54 public void componentMoved(ComponentEvent e) { }
55
56 public void componentShown(ComponentEvent e) {
57 BaseFileCompareWindow window = (BaseFileCompareWindow)e.getComponent();
58 window.calculateTitle();
59 }
60
61 public void componentResized(ComponentEvent e) {
62 BaseFileCompareWindow window = (BaseFileCompareWindow)e.getComponent();
63 window.calculateTitle();
64 }
65 };
66
67
68
69
70
71 public BaseFileCompareWindow(GlobalContext context) {
72 this.context = context;
73
74 addComponentListener(resizeListener);
75 }
76
77
78
79
80 public void initialise() {
81 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
82
83 createComparePanel();
84
85 getContentPane().add(comparePanel, BorderLayout.CENTER);
86
87 setupMenu();
88
89 setPreferredSize(new Dimension(750, 550));
90
91 context.addWindow(this, getWindowId());
92 }
93
94
95
96
97
98 protected String getWindowId() {
99 return "window.generic";
100 }
101
102 protected void setupMenu() {
103 JMenuBar menuBar = new JMenuBar();
104 setJMenuBar(menuBar);
105
106 Preferences prefs = context.getPreferences();
107
108 JMenu fileMenu = new JMenu("File");
109 menuBar.add(fileMenu);
110 fileMenu.add(new org.abstracthorizon.aequo.action.NewSessionAction());
111
112 int recentNum = RecentFileEntry.getNumberOfRecent(prefs);
113 if (recentNum > 0) {
114
115 JMenu recentMenu = new JMenu("Recent Sessions");
116 recentMenu.setIcon(BaseAction.EMPTY_ICON);
117 fileMenu.add(recentMenu);
118
119 for (int i = 1; i <= recentNum; i++) {
120 PreviousSessionAction previousSession = new PreviousSessionAction();
121 previousSession.setGlobalContext(context);
122 previousSession.setSessionNo(i);
123 recentMenu.add(previousSession);
124 }
125 }
126
127 fileMenu.addSeparator();
128 fileMenu.add(new ExitAction());
129
130 JMenu editMenu = new JMenu("Edit");
131 menuBar.add(editMenu);
132
133 JMenu viewMenu = new JMenu("View");
134 menuBar.add(viewMenu);
135
136 comparePanel.createMenu(menuBar);
137
138 JMenu helpMenu = new JMenu("Help");
139 menuBar.add(helpMenu);
140
141 helpMenu.add(new AboutAction());
142
143 }
144
145
146
147
148 protected abstract void createComparePanel();
149
150
151
152
153 protected void calculateTitle() {
154 int width = getWidth();
155 Font titleFont = getFont();
156 String title = getTitle();
157 String newTitle = StringUtils.calculateTitle(this, width, getLeftFileName(), getRightFileName(), titleFont);
158 if (!newTitle.equals(title)) {
159 setTitle(newTitle);
160 }
161 }
162
163
164 protected abstract String getLeftFileName();
165
166
167 protected abstract String getRightFileName();
168 }