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.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   * Base class for compare windows
37   *
38   * @author Daniel Sendula
39   */
40  public abstract class BaseFileCompareWindow extends JFrame {
41  
42      /** Global context */
43      protected GlobalContext context;
44  
45      @SuppressWarnings("unchecked")
46      /** Compare panel */
47      protected AbstractComparePanel comparePanel;
48      
49      /** Resize listener to calculate window title */
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       * Constructor
69       * @param context
70       */
71      public BaseFileCompareWindow(GlobalContext context) {
72          this.context = context;
73          
74          addComponentListener(resizeListener);
75      }
76  
77      /**
78       * Creates components for this window
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       * Returns window id. Subclases should override this method.
96       * @return window id
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      * Creates compare panel
147      */
148     protected abstract void createComparePanel();
149     
150     /**
151      * Calculates window title
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     /** This method is to be overridden to return left file name for the title */
164     protected abstract String getLeftFileName();
165 
166     /** This method is to be overridden to return right file name for the title */
167     protected abstract String getRightFileName();
168 }