1 /* DebuggingPanel.java -- Displays and sets options for debugging.
2 Copyright (C) 2010 Red Hat
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 package net.sourceforge.jnlp.controlpanel;
20 
21 import java.awt.BorderLayout;
22 import java.awt.Component;
23 import java.awt.Dimension;
24 import java.awt.FlowLayout;
25 import java.awt.GridBagConstraints;
26 import java.awt.GridBagLayout;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.awt.event.ItemEvent;
30 import java.awt.event.ItemListener;
31 
32 import javax.swing.Box;
33 import javax.swing.JButton;
34 import javax.swing.JCheckBox;
35 import javax.swing.JComboBox;
36 import javax.swing.JLabel;
37 import javax.swing.JPanel;
38 import javax.swing.JTextField;
39 import javax.swing.event.DocumentEvent;
40 import javax.swing.event.DocumentListener;
41 
42 import net.sourceforge.jnlp.config.DeploymentConfiguration;
43 import net.sourceforge.jnlp.config.PathsAndFiles;
44 import net.sourceforge.jnlp.runtime.Translator;
45 import net.sourceforge.jnlp.util.logging.LogConfig;
46 
47 /**
48  * This displays the options related to debugging.
49  *
50  * @author Andrew Su (asu@redhat.com, andrew.su@utoronto.ca)
51  *
52  */
53 public class DebuggingPanel extends NamedBorderPanel implements ItemListener {
54 
55     /** List of properties used by checkboxes in this panel */
56     public static String[] properties = {
57             DeploymentConfiguration.KEY_ENABLE_LOGGING,
58             DeploymentConfiguration.KEY_ENABLE_LOGGING_HEADERS,
59             DeploymentConfiguration.KEY_ENABLE_LOGGING_TOFILE,
60             DeploymentConfiguration.KEY_ENABLE_LEGACY_LOGBASEDFILELOG,
61             DeploymentConfiguration.KEY_ENABLE_APPLICATION_LOGGING_TOFILE,
62             DeploymentConfiguration.KEY_ENABLE_LOGGING_TOSTREAMS,
63             DeploymentConfiguration.KEY_ENABLE_LOGGING_TOSYSTEMLOG
64 
65     };
66 
67      private final DeploymentConfiguration config;
68 
69     /**
70      * Create a new instance of the debugging panel.
71      *
72      * @param config
73      *            loaded DeploymentConfiguration file.
74      */
DebuggingPanel(DeploymentConfiguration config)75     public DebuggingPanel(DeploymentConfiguration config) {
76         super(Translator.R("CPHeadDebugging"), new GridBagLayout());
77 
78         this.config = config;
79 
80         addComponents();
81     }
82 
83 
fileLoggingAct(JCheckBox source, JCheckBox... targets)84     private void fileLoggingAct(JCheckBox source, JCheckBox... targets) {
85         if (source.isSelected()) {
86             for (JCheckBox target : targets) {
87                 target.setEnabled(true);
88             }
89         } else {
90             for (JCheckBox target : targets) {
91                 target.setEnabled(false);
92             }
93         }
94     }
95 
96     /**
97      * Add components to panel.
98      */
addComponents()99     private void addComponents() {
100         GridBagConstraints c = new GridBagConstraints();
101 
102 
103         final JLabel debuggingDescription = new JLabel("<html>" + Translator.R("CPDebuggingDescription") + "<hr /><br /></html>");
104         final JLabel logsDestinationTitle = new JLabel(Translator.R("CPFilesLogsDestDir")+": ");
105         final JTextField logsDestination = new JTextField(PathsAndFiles.LOG_DIR.getFullPath(config));
106         logsDestination.getDocument().addDocumentListener(new DocumentListener() {
107 
108 
109             @Override
110             public void insertUpdate(DocumentEvent e) {
111                  save();
112             }
113 
114             @Override
115             public void removeUpdate(DocumentEvent e) {
116                  save();
117             }
118 
119             @Override
120             public void changedUpdate(DocumentEvent e) {
121                 save();
122 
123             }
124 
125             private void save() {
126                 PathsAndFiles.LOG_DIR.setValue(logsDestination.getText(), config);
127             }
128         });
129         final JButton logsDestinationReset = new JButton(Translator.R("CPFilesLogsDestDirResert"));
130         logsDestinationReset.addActionListener(new ActionListener() {
131 
132             @Override
133             public void actionPerformed(ActionEvent e) {
134                 logsDestination.setText(PathsAndFiles.LOG_DIR.getDefaultFullPath());
135             }
136         });
137 
138         final JCheckBox[] debuggingOptions = {
139                 new JCheckBox(Translator.R("DPEnableLogging")),
140                 new JCheckBox(Translator.R("DPEnableHeaders")),
141                 new JCheckBox(Translator.R("DPEnableFile")),
142                 new JCheckBox(Translator.R("DPEnableLegacyFileLog")),
143                 new JCheckBox(Translator.R("DPEnableClientAppFileLogging")),
144                 new JCheckBox(Translator.R("DPEnableStds")),
145                 new JCheckBox(Translator.R("DPEnableSyslog"))
146         };
147 
148         debuggingOptions[2].addActionListener(new ActionListener() {
149 
150             @Override
151             public void actionPerformed(ActionEvent e) {
152                 fileLoggingAct(debuggingOptions[2], debuggingOptions[3], debuggingOptions[4]);
153             }
154 
155         });
156         final String[] hints = {
157                 (Translator.R("DPEnableLoggingHint")),
158                 (Translator.R("DPEnableHeadersHint")),
159                 (Translator.R("DPEnableFileHint", LogConfig.getLogConfig().getIcedteaLogDir())),
160                 (Translator.R("DPEnableLegacyFileLogHint")),
161                 (Translator.R("DPEnableClientAppFileLoggingHint")),
162                 (Translator.R("DPEnableStdsHint")),
163                 (Translator.R("DPEnableSyslogHint"))
164         };
165 
166         final ComboItem[] javaConsoleItems = { new ComboItem(Translator.R("DPDisable"), DeploymentConfiguration.CONSOLE_DISABLE),
167                 new ComboItem(Translator.R("DPHide"), DeploymentConfiguration.CONSOLE_HIDE),
168                 new ComboItem(Translator.R("DPShow"), DeploymentConfiguration.CONSOLE_SHOW),
169                 new ComboItem(Translator.R("DPShowPluginOnly"), DeploymentConfiguration.CONSOLE_SHOW_PLUGIN),
170                 new ComboItem(Translator.R("DPShowJavawsOnly"), DeploymentConfiguration.CONSOLE_SHOW_JAVAWS) };
171 
172         JLabel consoleLabel = new JLabel(Translator.R("DPJavaConsole"));
173         JComboBox<ComboItem> consoleComboBox = new JComboBox<>();
174         consoleComboBox.setActionCommand(DeploymentConfiguration.KEY_CONSOLE_STARTUP_MODE); // The property this comboBox affects.
175 
176         JPanel consolePanel = new JPanel();
177         consolePanel.setLayout(new FlowLayout(FlowLayout.LEADING));
178         consolePanel.add(consoleLabel);
179         consolePanel.add(consoleComboBox);
180 
181         c.fill = GridBagConstraints.BOTH;
182         c.weightx = 1;
183         c.gridx = 0;
184         c.gridy = 0;
185         add(debuggingDescription, c);
186 
187         /*
188          * Add the items to the panel unless we can not get the values for them.
189          */
190         for (int i = 0; i < properties.length; i++) {
191             String s = config.getProperty(properties[i]);
192             c.gridy++;
193             if (i == 2) {
194                 JLabel space = new JLabel("<html>" + Translator.R("CPDebuggingPossibilites") + ":</html>");
195                 add(space, c);
196                 c.gridy++;
197             }
198 
199             //move  5th and 6th checkbox below  logsDestination
200             if (i == 3 || i == 4) {
201                 c.gridx += 1;
202                 if (i == 4) {
203                     c.gridy--;
204                 }
205             } else {
206                 c.gridx = 0;
207             }
208             debuggingOptions[i].setSelected(Boolean.parseBoolean(s));
209             debuggingOptions[i].setActionCommand(properties[i]);
210             debuggingOptions[i].setToolTipText(hints[i]);
211             debuggingOptions[i].addItemListener(this);
212             add(debuggingOptions[i], c);
213 
214             if (i == 2) {
215                 c.gridx++;
216                 JPanel resetTitlePanel = new JPanel(new BorderLayout(10, 0));
217                 resetTitlePanel.add(logsDestinationReset, BorderLayout.LINE_START);
218                 resetTitlePanel.add(logsDestinationTitle, BorderLayout.LINE_END);
219                 add(resetTitlePanel, c);
220                 c.gridx++;
221                 add(logsDestination, c);
222                 c.gridx -= 2;
223             }
224         }
225 
226 
227         for (int j = 0; j < javaConsoleItems.length; j++) {
228             consoleComboBox.addItem(javaConsoleItems[j]);
229             if (config.getProperty(DeploymentConfiguration.KEY_CONSOLE_STARTUP_MODE).equals(javaConsoleItems[j].getValue())) {
230                 consoleComboBox.setSelectedIndex(j);
231             }
232         }
233         c.gridy++;
234         consoleComboBox.addItemListener(this);
235         add(consolePanel, c);
236 
237         // pack the bottom so that it doesn't change size if resized.
238         Component filler = Box.createRigidArea(new Dimension(1, 1));
239         c.gridy++;
240         c.weighty = 1;
241         add(filler, c);
242         fileLoggingAct(debuggingOptions[2], debuggingOptions[3], debuggingOptions[4]);
243     }
244 
245     @Override
itemStateChanged(ItemEvent e)246     public void itemStateChanged(ItemEvent e) {
247 
248         Object o = e.getSource();
249 
250         if (o instanceof JCheckBox) {
251             JCheckBox jcb = (JCheckBox) o;
252             config.setProperty(jcb.getActionCommand(), String.valueOf(jcb.isSelected()));
253         } else if (o instanceof JComboBox) {
254             @SuppressWarnings("unchecked")
255             JComboBox<ComboItem> jcb = (JComboBox<ComboItem>) o;
256             ComboItem c = (ComboItem) e.getItem();
257             config.setProperty(jcb.getActionCommand(), c.getValue());
258         }
259 
260     }
261 }
262