1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
39
40
41
42
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
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 }