View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * @author Paul Smith <psmith@apache.org>
20   *
21  */
22  package org.apache.log4j.chainsaw;
23  
24  import java.awt.event.ActionEvent;
25  import java.awt.event.InputEvent;
26  import java.awt.event.KeyEvent;
27  import java.net.URL;
28  import java.util.Iterator;
29  
30  import javax.swing.AbstractAction;
31  import javax.swing.Action;
32  import javax.swing.ImageIcon;
33  import javax.swing.JMenu;
34  import javax.swing.JMenuItem;
35  import javax.swing.KeyStroke;
36  import javax.swing.event.ChangeEvent;
37  import javax.swing.event.ChangeListener;
38  
39  import org.apache.log4j.chainsaw.icons.ChainsawIcons;
40  import org.apache.log4j.chainsaw.osx.OSXIntegration;
41  import org.apache.log4j.chainsaw.prefs.MRUFileList;
42  import org.apache.log4j.xml.UtilLoggingXMLDecoder;
43  import org.apache.log4j.xml.XMLDecoder;
44  
45  
46  /***
47   * The complete File Menu for the main GUI, containing
48   * the Load, Save, Close Welcome Tab, and Exit actions
49   *
50   * @author Paul Smith <psmith@apache.org>
51   * @author Scott Deboy <sdeboy@apache.org>
52   */
53  class FileMenu extends JMenu {
54    private Action exitAction;
55    private Action loadLog4JAction;
56    private Action loadUtilLoggingAction;
57    private Action remoteLog4JAction;
58    private Action remoteUtilLoggingAction;
59    private Action saveAction;
60  
61    public FileMenu(final LogUI logUI) {
62      super("File");
63      setMnemonic(KeyEvent.VK_F);
64  
65      loadLog4JAction =
66        new FileLoadAction(
67          logUI, new XMLDecoder(logUI), "Load Log4J File...", false);
68  
69        loadLog4JAction.putValue(
70          Action.ACCELERATOR_KEY,
71          KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
72        loadLog4JAction.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_L));
73        loadLog4JAction.putValue(Action.SHORT_DESCRIPTION, "Loads an XML event file");
74        loadLog4JAction.putValue(Action.SMALL_ICON, new ImageIcon(ChainsawIcons.FILE_OPEN));
75  
76      loadUtilLoggingAction =
77        new FileLoadAction(
78          logUI, new UtilLoggingXMLDecoder(logUI),
79          "Load Java Util File...", false);
80  
81      remoteLog4JAction =
82        new FileLoadAction(
83          logUI, new XMLDecoder(logUI), "Load Remote Log4J File...",
84          true);
85      remoteUtilLoggingAction =
86        new FileLoadAction(
87          logUI, new UtilLoggingXMLDecoder(logUI),
88          "Load Remote Java Util File...", true);
89  
90      saveAction = new FileSaveAction(logUI);
91  
92      JMenuItem loadLog4JFile = new JMenuItem(loadLog4JAction);
93      JMenuItem loadUtilLoggingFile = new JMenuItem(loadUtilLoggingAction);
94      JMenuItem remoteLog4JFile = new JMenuItem(remoteLog4JAction);
95      JMenuItem remoteUtilLoggingFile = new JMenuItem(remoteUtilLoggingAction);
96      JMenuItem saveFile = new JMenuItem(saveAction);
97  
98      exitAction =
99        new AbstractAction() {
100           public void actionPerformed(ActionEvent e) {
101             logUI.exit();
102           }
103         };
104 
105     exitAction.putValue(
106       Action.ACCELERATOR_KEY,
107       KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK));
108     exitAction.putValue(Action.SHORT_DESCRIPTION, "Exits the Application");
109     exitAction.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_X));
110     exitAction.putValue(Action.NAME, "Exit");
111 
112     JMenuItem menuItemExit = new JMenuItem(exitAction);
113 
114     add(loadLog4JFile);
115     add(loadUtilLoggingFile);
116     addSeparator();
117     add(remoteLog4JFile);
118     add(remoteUtilLoggingFile);
119     addSeparator();
120     add(saveFile);
121     addSeparator();
122 
123     final JMenu mrulog4j = new JMenu("MRU...");
124     
125   
126     
127     MRUFileList.addChangeListener(new ChangeListener() {
128         
129         public void stateChanged(ChangeEvent e) {
130             
131             buildMRUMenu(mrulog4j, logUI);
132         }
133         
134     });
135     buildMRUMenu(mrulog4j, logUI);
136     
137     add(mrulog4j);
138     if (!OSXIntegration.IS_OSX) {
139         addSeparator();
140         add(menuItemExit);
141     }
142     
143     
144   }
145 
146   private void buildMRUMenu(final JMenu mrulog4j, final LogUI logui) {
147         mrulog4j.removeAll();
148         int counter = 1;
149         if (MRUFileList.log4jMRU().getMRUList().size() > 0) {
150             for (Iterator iter = MRUFileList.log4jMRU().getMRUList().iterator(); iter
151                     .hasNext();) {
152                 final URL url = (URL) iter.next();
153                 // TODO work out the 'name', for local files it can't just be the full path
154                 final String name = url.getProtocol().startsWith("file")?url.getPath().substring(url.getPath().lastIndexOf('/')+1):url.getPath();
155                 String title = (counter++) + " - " + url.toExternalForm();
156                 JMenuItem menuItem = new JMenuItem(new AbstractAction(title) {
157 
158                     public void actionPerformed(ActionEvent e) {
159                         FileLoadAction.importURL(logui.handler,
160                                 new XMLDecoder(), name, url);
161                     }
162                 });
163                 mrulog4j.add(menuItem);
164             }
165         } else {
166             JMenuItem none = new JMenuItem("None as yet...");
167             none.setEnabled(false);
168             mrulog4j.add(none);
169         }
170     }
171   Action getLog4JFileOpenAction() {
172     return loadLog4JAction;
173   }
174 
175   Action getUtilLoggingJFileOpenAction() {
176     return loadUtilLoggingAction;
177   }
178 
179   Action getFileSaveAction() {
180     return saveAction;
181   }
182 
183   Action getExitAction() {
184     return exitAction;
185   }
186 }