View Javadoc
1 package net.sourceforge.pmd.swingui; 2 3 import net.sourceforge.pmd.PMDException; 4 import net.sourceforge.pmd.Rule; 5 6 import javax.swing.Icon; 7 import javax.swing.JButton; 8 import javax.swing.JComboBox; 9 import javax.swing.JFileChooser; 10 import javax.swing.JLabel; 11 import javax.swing.JMenu; 12 import javax.swing.JMenuBar; 13 import javax.swing.JMenuItem; 14 import javax.swing.JPanel; 15 import javax.swing.JScrollPane; 16 import javax.swing.JSeparator; 17 import javax.swing.JTextArea; 18 import javax.swing.KeyStroke; 19 import javax.swing.UIManager; 20 import javax.swing.border.CompoundBorder; 21 import javax.swing.border.EmptyBorder; 22 import javax.swing.border.EtchedBorder; 23 import java.awt.BorderLayout; 24 import java.awt.Color; 25 import java.awt.Dimension; 26 import java.awt.Font; 27 import java.awt.FontMetrics; 28 import java.awt.GridBagConstraints; 29 import java.awt.GridBagLayout; 30 import java.awt.Insets; 31 import java.awt.event.ActionEvent; 32 import java.awt.event.ActionListener; 33 import java.awt.event.KeyEvent; 34 import java.io.File; 35 36 /* 37 */ 38 39 /*** 40 * 41 * @author Donald A. Leckie 42 * @since September 8, 2002 43 * @version $Revision: 1.20 $, $Date: 2003/05/28 18:08:34 $ 44 */ 45 class PreferencesEditor extends JPanel { 46 private JTextArea m_currentPathToPMD; 47 private JTextArea m_userPathToPMD; 48 private JTextArea m_sharedPathToPMD; 49 private JTextArea m_analysisResultsPath; 50 private JComboBox m_lowestPriorityForAnalysis; 51 private JMenuBar m_menuBar; 52 53 /*** 54 ******************************************************************************** 55 * 56 * @pmdViewer 57 */ 58 protected PreferencesEditor() throws PMDException { 59 super(new BorderLayout()); 60 61 add(createContentPanel(), BorderLayout.CENTER); 62 createMenuBar(); 63 } 64 65 /*** 66 ******************************************************************************** 67 * 68 * @return 69 */ 70 private JScrollPane createContentPanel() throws PMDException { 71 JPanel contentPanel = new JPanel(new BorderLayout()); 72 EmptyBorder emptyBorder = new EmptyBorder(100, 100, 100, 100); 73 EtchedBorder etchedBorder = new EtchedBorder(EtchedBorder.LOWERED); 74 CompoundBorder compoundBorder = new CompoundBorder(etchedBorder, emptyBorder); 75 contentPanel.setBorder(compoundBorder); 76 contentPanel.add(createDataPanel(), BorderLayout.NORTH); 77 78 return ComponentFactory.createScrollPane(contentPanel); 79 } 80 81 /*** 82 ******************************************************************************** 83 * 84 * @return 85 */ 86 private JPanel createDataPanel() throws PMDException { 87 JPanel dataPanel; 88 int row; 89 Preferences preferences; 90 EmptyBorder emptyBorder; 91 EtchedBorder etchedBorder; 92 CompoundBorder compoundBorder; 93 94 dataPanel = new JPanel(new GridBagLayout()); 95 emptyBorder = new EmptyBorder(1, 1, 1, 1); 96 etchedBorder = new EtchedBorder(EtchedBorder.RAISED); 97 compoundBorder = new CompoundBorder(etchedBorder, emptyBorder); 98 compoundBorder = new CompoundBorder(compoundBorder, etchedBorder); 99 emptyBorder = new EmptyBorder(10, 10, 10, 10); 100 compoundBorder = new CompoundBorder(compoundBorder, emptyBorder); 101 dataPanel.setBorder(compoundBorder); 102 preferences = Preferences.getPreferences(); 103 104 row = 0; 105 createLabel("Current Path to PMD Directory", dataPanel, row, 0); 106 String currentPath = preferences.getCurrentPathToPMD(); 107 m_currentPathToPMD = createTextArea(currentPath, dataPanel, row, 1); 108 createFileButton(dataPanel, row, 2, m_currentPathToPMD); 109 110 row++; 111 createLabel("User Path to PMD Directory", dataPanel, row, 0); 112 String userPath = preferences.getUserPathToPMD(); 113 m_userPathToPMD = createTextArea(userPath, dataPanel, row, 1); 114 createFileButton(dataPanel, row, 2, m_userPathToPMD); 115 116 row++; 117 createLabel("Shared Path to PMD Directory", dataPanel, row, 0); 118 String sharedPath = preferences.getSharedPathToPMD(); 119 m_sharedPathToPMD = createTextArea(sharedPath, dataPanel, row, 1); 120 createFileButton(dataPanel, row, 2, m_sharedPathToPMD); 121 122 row++; 123 createLabel("Analysis Results Files Path", dataPanel, row, 0); 124 String analysisResultsPath = preferences.getAnalysisResultsPath(); 125 m_analysisResultsPath = createTextArea(analysisResultsPath, dataPanel, row, 1); 126 createFileButton(dataPanel, row, 2, m_analysisResultsPath); 127 128 row++; 129 createLabel("Lowest Priority for Analysis", dataPanel, row, 0); 130 int priority = preferences.getLowestPriorityForAnalysis(); 131 m_lowestPriorityForAnalysis = createPriorityDropDownList(priority, dataPanel, row, 1); 132 133 return dataPanel; 134 } 135 136 /*** 137 ******************************************************************************* 138 * 139 * @param text 140 * @param dataPanel 141 * @param row 142 * @param column 143 */ 144 private void createLabel(String text, JPanel dataPanel, int row, int column) { 145 JLabel label = new JLabel(text); 146 label.setFont(UIManager.getFont("labelFont")); 147 label.setHorizontalAlignment(JLabel.RIGHT); 148 label.setForeground(UIManager.getColor("pmdBlue")); 149 150 GridBagLayout layout; 151 GridBagConstraints constraints; 152 153 layout = (GridBagLayout) dataPanel.getLayout(); 154 constraints = layout.getConstraints(label); 155 constraints.gridx = column; 156 constraints.gridy = row; 157 constraints.gridwidth = 1; 158 constraints.gridheight = 1; 159 constraints.anchor = constraints.NORTHEAST; 160 constraints.fill = constraints.NONE; 161 constraints.insets = new Insets(2, 2, 2, 2); 162 163 dataPanel.add(label, constraints); 164 } 165 166 /*** 167 ******************************************************************************* 168 * 169 * @param text 170 * @param dataPanel 171 * @param row 172 * @param column 173 */ 174 private JTextArea createTextArea(String text, JPanel dataPanel, int row, int column) { 175 JTextArea textArea; 176 JScrollPane scrollPane; 177 GridBagLayout layout; 178 GridBagConstraints constraints; 179 Font font; 180 FontMetrics fontMetrics; 181 int height; 182 int width; 183 Dimension size; 184 185 textArea = ComponentFactory.createTextArea(text); 186 187 scrollPane = ComponentFactory.createScrollPane(textArea); 188 font = textArea.getFont(); 189 fontMetrics = textArea.getFontMetrics(font); 190 width = 400; 191 height = (3 * fontMetrics.getHeight()) + 5; 192 size = new Dimension(width, height); 193 scrollPane.setSize(size); 194 scrollPane.setMinimumSize(size); 195 scrollPane.setPreferredSize(size); 196 197 layout = (GridBagLayout) dataPanel.getLayout(); 198 constraints = layout.getConstraints(scrollPane); 199 constraints.gridx = column; 200 constraints.gridy = row; 201 constraints.gridwidth = 1; 202 constraints.gridheight = 1; 203 constraints.anchor = constraints.WEST; 204 constraints.fill = constraints.BOTH; 205 constraints.insets = new Insets(2, 2, 2, 2); 206 207 dataPanel.add(scrollPane, constraints); 208 209 return textArea; 210 } 211 212 /*** 213 ******************************************************************************* 214 * 215 * @param dataPanel 216 * @param row 217 * @param column 218 */ 219 private void createFileButton(JPanel dataPanel, int row, int column, JTextArea textArea) { 220 JButton button; 221 GridBagLayout layout; 222 GridBagConstraints constraints; 223 FontMetrics fontMetrics; 224 int width; 225 Dimension size; 226 227 button = ComponentFactory.createButton("Find Directory"); 228 fontMetrics = button.getFontMetrics(button.getFont()); 229 width = fontMetrics.stringWidth(button.getText()) + 50; 230 size = new Dimension(width, button.getHeight()); 231 //button.setSize(size); 232 button.setPreferredSize(size); 233 button.setMinimumSize(size); 234 button.setMaximumSize(size); 235 button.setBackground(UIManager.getColor("pmdBlue")); 236 button.setForeground(Color.white); 237 button.addActionListener(new FileButtonActionListener(textArea)); 238 layout = (GridBagLayout) dataPanel.getLayout(); 239 constraints = layout.getConstraints(button); 240 constraints.gridx = column; 241 constraints.gridy = row; 242 constraints.gridwidth = 1; 243 constraints.gridheight = 1; 244 constraints.anchor = constraints.WEST; 245 constraints.fill = constraints.NONE; 246 constraints.insets = new Insets(2, 2, 2, 2); 247 248 dataPanel.add(button, constraints); 249 } 250 251 /*** 252 ******************************************************************************* 253 * 254 */ 255 private JComboBox createPriorityDropDownList(int priority, JPanel dataPanel, int row, int column) { 256 JComboBox priorityLevel; 257 GridBagLayout layout; 258 GridBagConstraints constraints; 259 260 priorityLevel = new JComboBox(Rule.PRIORITIES); 261 priorityLevel.setSelectedIndex(priority - 1); 262 263 layout = (GridBagLayout) dataPanel.getLayout(); 264 constraints = layout.getConstraints(priorityLevel); 265 constraints.gridx = column; 266 constraints.gridy = row; 267 constraints.gridwidth = 1; 268 constraints.gridheight = 1; 269 constraints.anchor = constraints.WEST; 270 constraints.fill = constraints.NONE; 271 constraints.insets = new Insets(2, 2, 2, 2); 272 273 dataPanel.add(priorityLevel, constraints); 274 275 return priorityLevel; 276 } 277 278 /*** 279 ********************************************************************************* 280 * 281 */ 282 private void createMenuBar() { 283 m_menuBar = new JMenuBar(); 284 m_menuBar.add(new FileMenu()); 285 m_menuBar.add(new HelpMenu()); 286 } 287 288 /*** 289 ********************************************************************************* 290 * 291 */ 292 protected void setMenuBar() { 293 PMDViewer.getViewer().setJMenuBar(m_menuBar); 294 } 295 296 /*** 297 ********************************************************************************* 298 * 299 */ 300 public void adjustSplitPaneDividerLocation() { 301 } 302 303 /*** 304 ******************************************************************************* 305 ******************************************************************************* 306 ******************************************************************************* 307 */ 308 private class SaveActionListener implements ActionListener { 309 310 /*** 311 ******************************************************************** 312 * 313 * @param event 314 */ 315 public void actionPerformed(ActionEvent event) { 316 try { 317 Preferences preferences = Preferences.getPreferences(); 318 preferences.setCurrentPathToPMD(m_currentPathToPMD.getText()); 319 preferences.setUserPathToPMD(m_userPathToPMD.getText()); 320 preferences.setSharedPathToPMD(m_sharedPathToPMD.getText()); 321 preferences.setLowestPriorityForAnalysis(m_lowestPriorityForAnalysis.getSelectedIndex() + 1); 322 preferences.save(); 323 } catch (PMDException pmdException) { 324 String message = pmdException.getMessage(); 325 Exception exception = pmdException.getReason(); 326 MessageDialog.show(PMDViewer.getViewer(), message, exception); 327 } 328 329 PreferencesEditor.this.setVisible(false); 330 } 331 } 332 333 /*** 334 ******************************************************************************* 335 ******************************************************************************* 336 ******************************************************************************* 337 */ 338 private class CancelButtonActionListener implements ActionListener { 339 340 /*** 341 ******************************************************************** 342 * 343 * @param event 344 */ 345 public void actionPerformed(ActionEvent event) { 346 PreferencesEditor.this.setVisible(false); 347 } 348 } 349 350 /*** 351 ******************************************************************************* 352 ******************************************************************************* 353 ******************************************************************************* 354 */ 355 private class FileButtonActionListener implements ActionListener { 356 357 private JTextArea m_textArea; 358 359 /*** 360 ************************************************************************** 361 * 362 * @param directory 363 */ 364 private FileButtonActionListener(JTextArea textArea) { 365 m_textArea = textArea; 366 } 367 368 /*** 369 ******************************************************************** 370 * 371 * @param event 372 */ 373 public void actionPerformed(ActionEvent event) { 374 File file = new File(m_textArea.getText()); 375 376 if (file.exists() == false) { 377 file = new File(System.getProperty("user.home")); 378 } else if (file.isDirectory() == false) { 379 file = file.getParentFile(); 380 } 381 382 JFileChooser fileChooser = new JFileChooser(file); 383 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 384 fileChooser.setApproveButtonText("Select"); 385 fileChooser.setMinimumSize(new Dimension(500, 500)); 386 387 if (fileChooser.showOpenDialog(PMDViewer.getViewer()) == JFileChooser.APPROVE_OPTION) { 388 file = fileChooser.getSelectedFile(); 389 390 m_textArea.setText(file.getPath()); 391 } 392 } 393 } 394 395 /*** 396 ********************************************************************************* 397 ********************************************************************************* 398 ********************************************************************************* 399 */ 400 private class FileMenu extends JMenu { 401 402 /*** 403 ******************************************************************** 404 * 405 * @param menuBar 406 */ 407 private FileMenu() { 408 super("File"); 409 410 setMnemonic('F'); 411 412 Icon icon; 413 JMenuItem menuItem; 414 415 // 416 // Save menu item 417 // 418 icon = UIManager.getIcon("save"); 419 menuItem = new JMenuItem("Save Changes", icon); 420 menuItem.addActionListener((ActionListener) new SaveActionListener()); 421 menuItem.setMnemonic('S'); 422 menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK)); 423 add(menuItem); 424 425 // 426 // Separator 427 // 428 add(new JSeparator()); 429 430 // 431 // Exit menu item 432 // 433 menuItem = new JMenuItem("Exit..."); 434 menuItem.addActionListener((ActionListener) new ExitActionListener()); 435 menuItem.setMnemonic('X'); 436 add(menuItem); 437 } 438 } 439 440 /*** 441 ********************************************************************************* 442 ********************************************************************************* 443 ********************************************************************************* 444 */ 445 private class ExitActionListener implements ActionListener { 446 447 public void actionPerformed(ActionEvent event) { 448 System.exit(0); 449 } 450 } 451 }

This page was automatically generated by Maven