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.Toolkit;
25  import java.awt.event.ActionEvent;
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 loadConfigAction;
55    private Action exitAction;
56    private Action loadLog4JAction;
57    private Action loadUtilLoggingAction;
58    private Action remoteLog4JAction;
59    private Action remoteUtilLoggingAction;
60    private Action saveAction;
61  
62    public FileMenu(final LogUI logUI) {
63      super("File");
64      setMnemonic(KeyEvent.VK_F);
65  
66      loadConfigAction = new AbstractAction("Load Chainsaw configuration"){
67          public void actionPerformed(ActionEvent actionEvent) {
68              logUI.showReceiverConfiguration();
69          }
70      };
71  
72      loadLog4JAction =
73        new FileLoadAction(
74          logUI, new XMLDecoder(logUI), "Open log4j XML-formatted file (.xml or .zip)...", false);
75  
76        loadLog4JAction.putValue(
77          Action.ACCELERATOR_KEY,
78          KeyStroke.getKeyStroke(KeyEvent.VK_O, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
79        loadLog4JAction.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_L));
80        loadLog4JAction.putValue(Action.SHORT_DESCRIPTION, "Loads events from a local XMLLayout-formatted file ");
81        loadLog4JAction.putValue(Action.SMALL_ICON, new ImageIcon(ChainsawIcons.FILE_OPEN));
82  
83      loadUtilLoggingAction =
84        new FileLoadAction(
85          logUI, new UtilLoggingXMLDecoder(logUI),
86          "Open util.logging XML-formatted file (.xml or .zip)...", false);
87  
88      remoteLog4JAction =
89        new FileLoadAction(
90          logUI, new XMLDecoder(logUI), "Open remote log4j XML-formatted file (.xml or .zip)...",
91          true);
92      remoteUtilLoggingAction =
93        new FileLoadAction(
94          logUI, new UtilLoggingXMLDecoder(logUI),
95          "Open remote util.logging XML-formatted file (.xml or .zip)...", true);
96  
97      saveAction = new FileSaveAction(logUI);
98  
99      JMenuItem loadChainsawConfig = new JMenuItem(loadConfigAction);
100     JMenuItem loadLog4JFile = new JMenuItem(loadLog4JAction);
101     JMenuItem loadUtilLoggingFile = new JMenuItem(loadUtilLoggingAction);
102     JMenuItem remoteLog4JFile = new JMenuItem(remoteLog4JAction);
103     JMenuItem remoteUtilLoggingFile = new JMenuItem(remoteUtilLoggingAction);
104     JMenuItem saveFile = new JMenuItem(saveAction);
105 
106     exitAction =
107       new AbstractAction() {
108           public void actionPerformed(ActionEvent e) {
109             logUI.exit();
110           }
111         };
112 
113     exitAction.putValue(
114       Action.ACCELERATOR_KEY,
115       KeyStroke.getKeyStroke(KeyEvent.VK_X, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
116     exitAction.putValue(Action.SHORT_DESCRIPTION, "Exits the Application");
117     exitAction.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_X));
118     exitAction.putValue(Action.NAME, "Exit");
119 
120     JMenuItem menuItemExit = new JMenuItem(exitAction);
121 
122     add(loadChainsawConfig);
123     add(loadLog4JFile);
124     add(loadUtilLoggingFile);
125     addSeparator();
126     add(remoteLog4JFile);
127     add(remoteUtilLoggingFile);
128     addSeparator();
129     add(saveFile);
130     addSeparator();
131 
132     final JMenu mrulog4j = new JMenu("MRU...");
133     
134   
135     
136     MRUFileList.addChangeListener(new ChangeListener() {
137         
138         public void stateChanged(ChangeEvent e) {
139             
140             buildMRUMenu(mrulog4j, logUI);
141         }
142         
143     });
144     buildMRUMenu(mrulog4j, logUI);
145     
146     add(mrulog4j);
147     if (!OSXIntegration.IS_OSX) {
148         addSeparator();
149         add(menuItemExit);
150     }
151     
152     
153   }
154 
155   private void buildMRUMenu(final JMenu mrulog4j, final LogUI logui) {
156         mrulog4j.removeAll();
157         int counter = 1;
158         if (MRUFileList.log4jMRU().getMRUList().size() > 0) {
159             for (Iterator iter = MRUFileList.log4jMRU().getMRUList().iterator(); iter
160                     .hasNext();) {
161                 final URL url = (URL) iter.next();
162                 // TODO work out the 'name', for local files it can't just be the full path
163                 final String name = url.getProtocol().startsWith("file")?url.getPath().substring(url.getPath().lastIndexOf('/')+1):url.getPath();
164                 String title = (counter++) + " - " + url.toExternalForm();
165                 JMenuItem menuItem = new JMenuItem(new AbstractAction(title) {
166 
167                     public void actionPerformed(ActionEvent e) {
168                         FileLoadAction.importURL(logui.handler,
169                                 new XMLDecoder(), name, url);
170                     }
171                 });
172                 mrulog4j.add(menuItem);
173             }
174         } else {
175             JMenuItem none = new JMenuItem("None as yet...");
176             none.setEnabled(false);
177             mrulog4j.add(none);
178         }
179     }
180   Action getLog4JFileOpenAction() {
181     return loadLog4JAction;
182   }
183 
184   Action getUtilLoggingJFileOpenAction() {
185     return loadUtilLoggingAction;
186   }
187 
188   Action getFileSaveAction() {
189     return saveAction;
190   }
191 
192   Action getExitAction() {
193     return exitAction;
194   }
195 }