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 org.apache.log4j.chainsaw.icons.ChainsawIcons;
25  import org.apache.log4j.chainsaw.osx.OSXIntegration;
26  import org.apache.log4j.chainsaw.prefs.MRUFileList;
27  import org.apache.log4j.xml.UtilLoggingXMLDecoder;
28  import org.apache.log4j.xml.XMLDecoder;
29  
30  import javax.swing.*;
31  import java.awt.*;
32  import java.awt.event.ActionEvent;
33  import java.awt.event.KeyEvent;
34  import java.net.URL;
35  
36  
37  /**
38   * The complete File Menu for the main GUI, containing
39   * the Load, Save, Close Welcome Tab, and Exit actions
40   *
41   * @author Paul Smith <psmith@apache.org>
42   * @author Scott Deboy <sdeboy@apache.org>
43   */
44  class FileMenu extends JMenu {
45      private Action loadConfigAction;
46      private Action exitAction;
47      private Action loadLog4JAction;
48      private Action loadUtilLoggingAction;
49      private Action remoteLog4JAction;
50      private Action remoteUtilLoggingAction;
51      private Action saveAction;
52  
53      public FileMenu(final LogUI logUI) {
54          super("File");
55          setMnemonic(KeyEvent.VK_F);
56  
57          loadConfigAction = new AbstractAction("Load Chainsaw configuration") {
58              public void actionPerformed(ActionEvent actionEvent) {
59                  logUI.showReceiverConfiguration();
60              }
61          };
62  
63          loadLog4JAction =
64              new FileLoadAction(
65                  logUI, new XMLDecoder(logUI), "Open log4j XML-formatted file (.xml or .zip)...", false);
66  
67          loadLog4JAction.putValue(
68              Action.ACCELERATOR_KEY,
69              KeyStroke.getKeyStroke(KeyEvent.VK_O, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
70          loadLog4JAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_L);
71          loadLog4JAction.putValue(Action.SHORT_DESCRIPTION, "Loads events from a local XMLLayout-formatted file ");
72          loadLog4JAction.putValue(Action.SMALL_ICON, new ImageIcon(ChainsawIcons.FILE_OPEN));
73  
74          loadUtilLoggingAction =
75              new FileLoadAction(
76                  logUI, new UtilLoggingXMLDecoder(logUI),
77                  "Open util.logging XML-formatted file (.xml or .zip)...", false);
78  
79          remoteLog4JAction =
80              new FileLoadAction(
81                  logUI, new XMLDecoder(logUI), "Open remote log4j XML-formatted file (.xml or .zip)...",
82                  true);
83          remoteUtilLoggingAction =
84              new FileLoadAction(
85                  logUI, new UtilLoggingXMLDecoder(logUI),
86                  "Open remote util.logging XML-formatted file (.xml or .zip)...", true);
87  
88          saveAction = new FileSaveAction(logUI);
89  
90          JMenuItem loadChainsawConfig = new JMenuItem(loadConfigAction);
91          JMenuItem loadLog4JFile = new JMenuItem(loadLog4JAction);
92          JMenuItem loadUtilLoggingFile = new JMenuItem(loadUtilLoggingAction);
93          JMenuItem remoteLog4JFile = new JMenuItem(remoteLog4JAction);
94          JMenuItem remoteUtilLoggingFile = new JMenuItem(remoteUtilLoggingAction);
95          JMenuItem saveFile = new JMenuItem(saveAction);
96  
97          exitAction =
98              new AbstractAction() {
99                  public void actionPerformed(ActionEvent e) {
100                     logUI.exit();
101                 }
102             };
103 
104         exitAction.putValue(
105             Action.ACCELERATOR_KEY,
106             KeyStroke.getKeyStroke(KeyEvent.VK_X, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
107         exitAction.putValue(Action.SHORT_DESCRIPTION, "Exits the Application");
108         exitAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_X);
109         exitAction.putValue(Action.NAME, "Exit");
110 
111         JMenuItem menuItemExit = new JMenuItem(exitAction);
112 
113         add(loadChainsawConfig);
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         MRUFileList.addChangeListener(e -> buildMRUMenu(mrulog4j, logUI));
127         buildMRUMenu(mrulog4j, logUI);
128 
129         add(mrulog4j);
130         if (!OSXIntegration.IS_OSX) {
131             addSeparator();
132             add(menuItemExit);
133         }
134 
135 
136     }
137 
138     private void buildMRUMenu(final JMenu mrulog4j, final LogUI logui) {
139         mrulog4j.removeAll();
140         int counter = 1;
141         if (MRUFileList.log4jMRU().getMRUList().size() > 0) {
142             for (Object o : MRUFileList.log4jMRU().getMRUList()) {
143                 final URL url = (URL) o;
144                 // TODO work out the 'name', for local files it can't just be the full path
145                 final String name = url.getProtocol().startsWith("file") ? url.getPath().substring(url.getPath().lastIndexOf('/') + 1) : url.getPath();
146                 String title = (counter++) + " - " + url.toExternalForm();
147                 JMenuItem menuItem = new JMenuItem(new AbstractAction(title) {
148 
149                     public void actionPerformed(ActionEvent e) {
150                         FileLoadAction.importURL(logui.handler,
151                             new XMLDecoder(), name, url);
152                     }
153                 });
154                 mrulog4j.add(menuItem);
155             }
156         } else {
157             JMenuItem none = new JMenuItem("None as yet...");
158             none.setEnabled(false);
159             mrulog4j.add(none);
160         }
161     }
162 
163     Action getLog4JFileOpenAction() {
164         return loadLog4JAction;
165     }
166 
167     Action getUtilLoggingJFileOpenAction() {
168         return loadUtilLoggingAction;
169     }
170 
171     Action getFileSaveAction() {
172         return saveAction;
173     }
174 
175     Action getExitAction() {
176         return exitAction;
177     }
178 }