001: package org.acm.seguin.pmd.swingui;
002:
003: import org.acm.seguin.pmd.PMDException;
004: import org.acm.seguin.pmd.Rule;
005:
006: import javax.swing.Icon;
007: import javax.swing.JButton;
008: import javax.swing.JComboBox;
009: import javax.swing.JFileChooser;
010: import javax.swing.JLabel;
011: import javax.swing.JMenu;
012: import javax.swing.JMenuBar;
013: import javax.swing.JMenuItem;
014: import javax.swing.JPanel;
015: import javax.swing.JScrollPane;
016: import javax.swing.JSeparator;
017: import javax.swing.JTextArea;
018: import javax.swing.KeyStroke;
019: import javax.swing.UIManager;
020: import javax.swing.border.CompoundBorder;
021: import javax.swing.border.EmptyBorder;
022: import javax.swing.border.EtchedBorder;
023: import java.awt.BorderLayout;
024: import java.awt.Color;
025: import java.awt.Dimension;
026: import java.awt.Font;
027: import java.awt.FontMetrics;
028: import java.awt.GridBagConstraints;
029: import java.awt.GridBagLayout;
030: import java.awt.Insets;
031: import java.awt.event.ActionEvent;
032: import java.awt.event.ActionListener;
033: import java.awt.event.KeyEvent;
034: import java.io.File;
035:
036: /*
037: */
038:
039: /**
040: *
041: * @author Donald A. Leckie
042: * @since September 8, 2002
043: * @version $Revision: 1.1 $, $Date: 2003/07/29 20:51:59 $
044: */
045: class PreferencesEditor extends JPanel {
046: private JTextArea m_currentPathToPMD;
047: private JTextArea m_userPathToPMD;
048: private JTextArea m_sharedPathToPMD;
049: private JTextArea m_analysisResultsPath;
050: private JComboBox m_lowestPriorityForAnalysis;
051: private JMenuBar m_menuBar;
052:
053: /**
054: ********************************************************************************
055: *
056: * @pmdViewer
057: */
058: protected PreferencesEditor() throws PMDException {
059: super (new BorderLayout());
060:
061: add(createContentPanel(), BorderLayout.CENTER);
062: createMenuBar();
063: }
064:
065: /**
066: ********************************************************************************
067: *
068: * @return
069: */
070: private JScrollPane createContentPanel() throws PMDException {
071: JPanel contentPanel = new JPanel(new BorderLayout());
072: EmptyBorder emptyBorder = new EmptyBorder(100, 100, 100, 100);
073: EtchedBorder etchedBorder = new EtchedBorder(
074: EtchedBorder.LOWERED);
075: CompoundBorder compoundBorder = new CompoundBorder(
076: etchedBorder, emptyBorder);
077: contentPanel.setBorder(compoundBorder);
078: contentPanel.add(createDataPanel(), BorderLayout.NORTH);
079:
080: return ComponentFactory.createScrollPane(contentPanel);
081: }
082:
083: /**
084: ********************************************************************************
085: *
086: * @return
087: */
088: private JPanel createDataPanel() throws PMDException {
089: JPanel dataPanel;
090: int row;
091: Preferences preferences;
092: EmptyBorder emptyBorder;
093: EtchedBorder etchedBorder;
094: CompoundBorder compoundBorder;
095:
096: dataPanel = new JPanel(new GridBagLayout());
097: emptyBorder = new EmptyBorder(1, 1, 1, 1);
098: etchedBorder = new EtchedBorder(EtchedBorder.RAISED);
099: compoundBorder = new CompoundBorder(etchedBorder, emptyBorder);
100: compoundBorder = new CompoundBorder(compoundBorder,
101: etchedBorder);
102: emptyBorder = new EmptyBorder(10, 10, 10, 10);
103: compoundBorder = new CompoundBorder(compoundBorder, emptyBorder);
104: dataPanel.setBorder(compoundBorder);
105: preferences = Preferences.getPreferences();
106:
107: row = 0;
108: createLabel("Current Path to PMD Directory", dataPanel, row, 0);
109: String currentPath = preferences.getCurrentPathToPMD();
110: m_currentPathToPMD = createTextArea(currentPath, dataPanel,
111: row, 1);
112: createFileButton(dataPanel, row, 2, m_currentPathToPMD);
113:
114: row++;
115: createLabel("User Path to PMD Directory", dataPanel, row, 0);
116: String userPath = preferences.getUserPathToPMD();
117: m_userPathToPMD = createTextArea(userPath, dataPanel, row, 1);
118: createFileButton(dataPanel, row, 2, m_userPathToPMD);
119:
120: row++;
121: createLabel("Shared Path to PMD Directory", dataPanel, row, 0);
122: String sharedPath = preferences.getSharedPathToPMD();
123: m_sharedPathToPMD = createTextArea(sharedPath, dataPanel, row,
124: 1);
125: createFileButton(dataPanel, row, 2, m_sharedPathToPMD);
126:
127: row++;
128: createLabel("Analysis Results Files Path", dataPanel, row, 0);
129: String analysisResultsPath = preferences
130: .getAnalysisResultsPath();
131: m_analysisResultsPath = createTextArea(analysisResultsPath,
132: dataPanel, row, 1);
133: createFileButton(dataPanel, row, 2, m_analysisResultsPath);
134:
135: row++;
136: createLabel("Lowest Priority for Analysis", dataPanel, row, 0);
137: int priority = preferences.getLowestPriorityForAnalysis();
138: m_lowestPriorityForAnalysis = createPriorityDropDownList(
139: priority, dataPanel, row, 1);
140:
141: return dataPanel;
142: }
143:
144: /**
145: *******************************************************************************
146: *
147: * @param text
148: * @param dataPanel
149: * @param row
150: * @param column
151: */
152: private void createLabel(String text, JPanel dataPanel, int row,
153: int column) {
154: JLabel label = new JLabel(text);
155: label.setFont(UIManager.getFont("labelFont"));
156: label.setHorizontalAlignment(JLabel.RIGHT);
157: label.setForeground(UIManager.getColor("pmdBlue"));
158:
159: GridBagLayout layout;
160: GridBagConstraints constraints;
161:
162: layout = (GridBagLayout) dataPanel.getLayout();
163: constraints = layout.getConstraints(label);
164: constraints.gridx = column;
165: constraints.gridy = row;
166: constraints.gridwidth = 1;
167: constraints.gridheight = 1;
168: constraints.anchor = constraints.NORTHEAST;
169: constraints.fill = constraints.NONE;
170: constraints.insets = new Insets(2, 2, 2, 2);
171:
172: dataPanel.add(label, constraints);
173: }
174:
175: /**
176: *******************************************************************************
177: *
178: * @param text
179: * @param dataPanel
180: * @param row
181: * @param column
182: */
183: private JTextArea createTextArea(String text, JPanel dataPanel,
184: int row, int column) {
185: JTextArea textArea;
186: JScrollPane scrollPane;
187: GridBagLayout layout;
188: GridBagConstraints constraints;
189: Font font;
190: FontMetrics fontMetrics;
191: int height;
192: int width;
193: Dimension size;
194:
195: textArea = ComponentFactory.createTextArea(text);
196:
197: scrollPane = ComponentFactory.createScrollPane(textArea);
198: font = textArea.getFont();
199: fontMetrics = textArea.getFontMetrics(font);
200: width = 400;
201: height = (3 * fontMetrics.getHeight()) + 5;
202: size = new Dimension(width, height);
203: scrollPane.setSize(size);
204: scrollPane.setMinimumSize(size);
205: scrollPane.setPreferredSize(size);
206:
207: layout = (GridBagLayout) dataPanel.getLayout();
208: constraints = layout.getConstraints(scrollPane);
209: constraints.gridx = column;
210: constraints.gridy = row;
211: constraints.gridwidth = 1;
212: constraints.gridheight = 1;
213: constraints.anchor = constraints.WEST;
214: constraints.fill = constraints.BOTH;
215: constraints.insets = new Insets(2, 2, 2, 2);
216:
217: dataPanel.add(scrollPane, constraints);
218:
219: return textArea;
220: }
221:
222: /**
223: *******************************************************************************
224: *
225: * @param dataPanel
226: * @param row
227: * @param column
228: */
229: private void createFileButton(JPanel dataPanel, int row,
230: int column, JTextArea textArea) {
231: JButton button;
232: GridBagLayout layout;
233: GridBagConstraints constraints;
234: FontMetrics fontMetrics;
235: int width;
236: Dimension size;
237:
238: button = ComponentFactory.createButton("Find Directory");
239: fontMetrics = button.getFontMetrics(button.getFont());
240: width = fontMetrics.stringWidth(button.getText()) + 50;
241: size = new Dimension(width, button.getHeight());
242: //button.setSize(size);
243: button.setPreferredSize(size);
244: button.setMinimumSize(size);
245: button.setMaximumSize(size);
246: button.setBackground(UIManager.getColor("pmdBlue"));
247: button.setForeground(Color.white);
248: button
249: .addActionListener(new FileButtonActionListener(
250: textArea));
251: layout = (GridBagLayout) dataPanel.getLayout();
252: constraints = layout.getConstraints(button);
253: constraints.gridx = column;
254: constraints.gridy = row;
255: constraints.gridwidth = 1;
256: constraints.gridheight = 1;
257: constraints.anchor = constraints.WEST;
258: constraints.fill = constraints.NONE;
259: constraints.insets = new Insets(2, 2, 2, 2);
260:
261: dataPanel.add(button, constraints);
262: }
263:
264: /**
265: *******************************************************************************
266: *
267: */
268: private JComboBox createPriorityDropDownList(int priority,
269: JPanel dataPanel, int row, int column) {
270: JComboBox priorityLevel;
271: GridBagLayout layout;
272: GridBagConstraints constraints;
273:
274: priorityLevel = new JComboBox(Rule.PRIORITIES);
275: priorityLevel.setSelectedIndex(priority - 1);
276:
277: layout = (GridBagLayout) dataPanel.getLayout();
278: constraints = layout.getConstraints(priorityLevel);
279: constraints.gridx = column;
280: constraints.gridy = row;
281: constraints.gridwidth = 1;
282: constraints.gridheight = 1;
283: constraints.anchor = constraints.WEST;
284: constraints.fill = constraints.NONE;
285: constraints.insets = new Insets(2, 2, 2, 2);
286:
287: dataPanel.add(priorityLevel, constraints);
288:
289: return priorityLevel;
290: }
291:
292: /**
293: *********************************************************************************
294: *
295: */
296: private void createMenuBar() {
297: m_menuBar = new JMenuBar();
298: m_menuBar.add(new FileMenu());
299: m_menuBar.add(new HelpMenu());
300: }
301:
302: /**
303: *********************************************************************************
304: *
305: */
306: protected void setMenuBar() {
307: PMDViewer.getViewer().setJMenuBar(m_menuBar);
308: }
309:
310: /**
311: *********************************************************************************
312: *
313: */
314: public void adjustSplitPaneDividerLocation() {
315: }
316:
317: /**
318: *******************************************************************************
319: *******************************************************************************
320: *******************************************************************************
321: */
322: private class SaveActionListener implements ActionListener {
323:
324: /**
325: ********************************************************************
326: *
327: * @param event
328: */
329: public void actionPerformed(ActionEvent event) {
330: try {
331: Preferences preferences = Preferences.getPreferences();
332: preferences.setCurrentPathToPMD(m_currentPathToPMD
333: .getText());
334: preferences.setUserPathToPMD(m_userPathToPMD.getText());
335: preferences.setSharedPathToPMD(m_sharedPathToPMD
336: .getText());
337: preferences
338: .setLowestPriorityForAnalysis(m_lowestPriorityForAnalysis
339: .getSelectedIndex() + 1);
340: preferences.save();
341: } catch (PMDException pmdException) {
342: String message = pmdException.getMessage();
343: Exception exception = pmdException.getReason();
344: MessageDialog.show(PMDViewer.getViewer(), message,
345: exception);
346: }
347:
348: PreferencesEditor.this .setVisible(false);
349: }
350: }
351:
352: /**
353: *******************************************************************************
354: *******************************************************************************
355: *******************************************************************************
356: */
357: private class CancelButtonActionListener implements ActionListener {
358:
359: /**
360: ********************************************************************
361: *
362: * @param event
363: */
364: public void actionPerformed(ActionEvent event) {
365: PreferencesEditor.this .setVisible(false);
366: }
367: }
368:
369: /**
370: *******************************************************************************
371: *******************************************************************************
372: *******************************************************************************
373: */
374: private class FileButtonActionListener implements ActionListener {
375:
376: private JTextArea m_textArea;
377:
378: /**
379: **************************************************************************
380: *
381: * @param directory
382: */
383: private FileButtonActionListener(JTextArea textArea) {
384: m_textArea = textArea;
385: }
386:
387: /**
388: ********************************************************************
389: *
390: * @param event
391: */
392: public void actionPerformed(ActionEvent event) {
393: File file = new File(m_textArea.getText());
394:
395: if (file.exists() == false) {
396: file = new File(System.getProperty("user.home"));
397: } else if (file.isDirectory() == false) {
398: file = file.getParentFile();
399: }
400:
401: JFileChooser fileChooser = new JFileChooser(file);
402: fileChooser
403: .setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
404: fileChooser.setApproveButtonText("Select");
405: fileChooser.setMinimumSize(new Dimension(500, 500));
406:
407: if (fileChooser.showOpenDialog(PMDViewer.getViewer()) == JFileChooser.APPROVE_OPTION) {
408: file = fileChooser.getSelectedFile();
409:
410: m_textArea.setText(file.getPath());
411: }
412: }
413: }
414:
415: /**
416: *********************************************************************************
417: *********************************************************************************
418: *********************************************************************************
419: */
420: private class FileMenu extends JMenu {
421:
422: /**
423: ********************************************************************
424: *
425: * @param menuBar
426: */
427: private FileMenu() {
428: super ("File");
429:
430: setMnemonic('F');
431:
432: Icon icon;
433: JMenuItem menuItem;
434:
435: //
436: // Save menu item
437: //
438: icon = UIManager.getIcon("save");
439: menuItem = new JMenuItem("Save Changes", icon);
440: menuItem
441: .addActionListener((ActionListener) new SaveActionListener());
442: menuItem.setMnemonic('S');
443: menuItem.setAccelerator(KeyStroke.getKeyStroke(
444: KeyEvent.VK_S, KeyEvent.CTRL_MASK));
445: add(menuItem);
446:
447: //
448: // Separator
449: //
450: add(new JSeparator());
451:
452: //
453: // Exit menu item
454: //
455: menuItem = new JMenuItem("Exit...");
456: menuItem
457: .addActionListener((ActionListener) new ExitActionListener());
458: menuItem.setMnemonic('X');
459: add(menuItem);
460: }
461: }
462:
463: /**
464: *********************************************************************************
465: *********************************************************************************
466: *********************************************************************************
467: */
468: private class ExitActionListener implements ActionListener {
469:
470: public void actionPerformed(ActionEvent event) {
471: System.exit(0);
472: }
473: }
474: }
|