📄 commentsarea.java
字号:
/* * FindBugs - Find Bugs in Java programs * Copyright (C) 2006, University of Maryland * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package edu.umd.cs.findbugs.gui2;import java.awt.BorderLayout;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.util.ArrayList;import java.util.Iterator;import java.util.LinkedList;import javax.swing.JComboBox;import javax.swing.JMenu;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.SwingUtilities;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;import javax.swing.tree.TreePath;import edu.umd.cs.findbugs.BugDesignation;import edu.umd.cs.findbugs.BugInstance;import edu.umd.cs.findbugs.BugDesignation;import edu.umd.cs.findbugs.I18N;import edu.umd.cs.findbugs.annotations.CheckForNull;/** * @author pugh */public class CommentsArea { private JTextArea userCommentsText = new JTextArea(); private Color userCommentsTextUnenabledColor; private JComboBox designationComboBox; private ArrayList<String> designationKeys; LinkedList<String> prevCommentsList = new LinkedList<String>(); final static private int prevCommentsMaxSize = 10; private JComboBox prevCommentsComboBox = new JComboBox(); private boolean dontShowAnnotationConfirmation = false; private boolean changed; final MainFrame frame; CommentsArea(MainFrame frame) { this.frame = frame; } /** * Create center panel that holds the user input combo boxes and TextArea. */ JPanel createCommentsInputPanel() { JPanel centerPanel = new JPanel(); BorderLayout centerLayout = new BorderLayout(); centerLayout.setVgap(10); centerPanel.setLayout(centerLayout); userCommentsText.getDocument().addDocumentListener( new DocumentListener() { public void insertUpdate(DocumentEvent e) { setProjectChanged(true); changed = true; } public void removeUpdate(DocumentEvent e) { setProjectChanged(true); changed = true; } public void changedUpdate(DocumentEvent e) { changed = true; } }); userCommentsTextUnenabledColor = centerPanel.getBackground(); userCommentsText.setLineWrap(true); userCommentsText .setToolTipText(edu.umd.cs.findbugs.L10N.getLocalString("tooltip.enter_comments", "Enter your comments about this bug here")); userCommentsText.setWrapStyleWord(true); userCommentsText.setEnabled(false); userCommentsText.setBackground(userCommentsTextUnenabledColor); JScrollPane commentsScrollP = new JScrollPane(userCommentsText); prevCommentsComboBox.setEnabled(false); prevCommentsComboBox .setToolTipText(edu.umd.cs.findbugs.L10N.getLocalString("tooltip.reuse_comments", "Use this to reuse a previous textual comment for this bug")); prevCommentsComboBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED && prevCommentsComboBox.getSelectedIndex() != 0) { setCurrentUserCommentsText(getCurrentPrevCommentsSelection()); prevCommentsComboBox.setSelectedIndex(0); } } }); designationComboBox = new JComboBox(); designationKeys = new ArrayList<String>(); designationComboBox.setEnabled(false); designationComboBox .setToolTipText(edu.umd.cs.findbugs.L10N.getLocalString("tooltip.select_designation", "Select a user designation for this bug")); designationComboBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (frame.userInputEnabled && e.getStateChange() == ItemEvent.SELECTED) { if (frame.currentSelectedBugLeaf == null) setDesignationNonLeaf(designationComboBox .getSelectedItem().toString()); else if (!alreadySelected()) setDesignation(designationComboBox.getSelectedItem() .toString()); } } /* * Checks to see if the designation is already selected as that. * This was created because it was found the itemStateChanged method * is called when the combo box is set when a bug is clicked. */ private boolean alreadySelected() { return designationKeys.get( designationComboBox.getSelectedIndex()).equals( frame.currentSelectedBugLeaf.getBug() .getNonnullUserDesignation() .getDesignationKey()); } }); designationKeys.add(""); designationComboBox.addItem(""); for (String s : I18N.instance().getUserDesignationKeys(true)) { designationKeys.add(s); designationComboBox.addItem(Sortables.DESIGNATION.formatValue(s)); } setUnknownDesignation(); centerPanel.add(designationComboBox, BorderLayout.NORTH); centerPanel.add(commentsScrollP, BorderLayout.CENTER); centerPanel.add(prevCommentsComboBox, BorderLayout.SOUTH); return centerPanel; } void setUnknownDesignation() { designationComboBox.setSelectedIndex(0); // WARNING: this is hard // coded in here. } /** * Sets the user comment panel to whether or not it is enabled. If isEnabled * is false will clear the user comments text pane. * * @param isEnabled */ void setUserCommentInputEnable(final boolean isEnabled) { SwingUtilities.invokeLater(new Runnable() { public void run() { setUserCommentInputEnableFromSwingThread(isEnabled); } }); } /** * Sets the user comment panel to whether or not it is enabled. If isEnabled * is false will clear the user comments text pane. * * @param isEnabled */ void setUserCommentInputEnableFromSwingThread(final boolean isEnabled) { frame.userInputEnabled = isEnabled; if (!isEnabled) {// This so if already saved doesn't make it seem project changed boolean b = frame.getProjectChanged(); userCommentsText.setText(""); // WARNING: this is hard coded in here, but needed // so when not enabled shows default setting of designation setUnknownDesignation(); userCommentsText.setBackground(userCommentsTextUnenabledColor); setProjectChanged(b); } else userCommentsText.setBackground(Color.WHITE); userCommentsText.setEnabled(isEnabled); prevCommentsComboBox.setEnabled(isEnabled); designationComboBox.setEnabled(isEnabled); } /** * Updates comments tab. Takes node passed and sets the designation and * comments. * * @param node */ void updateCommentsFromLeafInformation(final BugLeafNode node) { SwingUtilities.invokeLater(new Runnable() { public void run() { //This so if already saved doesn't make it seem project changed boolean b = frame.getProjectChanged(); BugInstance bug = node.getBug(); setCurrentUserCommentsText(bug.getAnnotationText()); designationComboBox.setSelectedIndex(designationKeys .indexOf(bug.getNonnullUserDesignation() .getDesignationKey())); setUserCommentInputEnableFromSwingThread(true); changed = false; setProjectChanged(b); } }); } void updateCommentsFromNonLeafInformation(final BugAspects theAspects) { SwingUtilities.invokeLater(new Runnable() { public void run() { //This so if already saved doesn't make it seem project changed boolean b = frame.getProjectChanged(); updateCommentsFromNonLeafInformationFromSwingThread(theAspects); setUserCommentInputEnableFromSwingThread(true); changed = false; setProjectChanged(b); } }); } /** * Saves the current comments to the BugLeafNode passed in. If the passed in * node's annotation is already equal to the current user comment then will * not do anything so setProjectedChanged is not made true. Will also add * the comment if it is new to the previous comments list. * * @param node */ private void saveCommentsToBug(BugLeafNode node) { if (node == null) return; String comments = getCurrentUserCommentsText(); if (node.getBug().getAnnotationText().equals(comments)) return; node.getBug().setAnnotationText(comments); setProjectChanged(true); changed = false; addToPrevComments(comments); } private boolean confirmAnnotation() { String[] options = { edu.umd.cs.findbugs.L10N.getLocalString("dlg.yes_btn", "Yes"), edu.umd.cs.findbugs.L10N.getLocalString("dlg.no_btn", "No"), edu.umd.cs.findbugs.L10N.getLocalString("dlg.yes_dont_ask_btn", "Yes, and don't ask me this again")}; if (dontShowAnnotationConfirmation) return true; int choice = JOptionPane .showOptionDialog( frame, edu.umd.cs.findbugs.L10N.getLocalString("dlg.changing_text_lbl", "Changing this text box will overwrite the annotations associated with all bugs in this folder and subfolders. Are you sure?"), edu.umd.cs.findbugs.L10N.getLocalString("dlg.annotation_change_ttl", "Annotation Change"), JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); switch (choice) { case 0: return true; case 1: return false; case 2: dontShowAnnotationConfirmation = true; return true; default: return true; } } private void saveCommentsToNonLeaf(BugAspects aspects) { if (aspects == null) return; if (!changed) return; String newComment = getCurrentUserCommentsText(); if (newComment.equals(getNonLeafCommentsText(aspects))) return; else if (confirmAnnotation()) { BugSet filteredSet = aspects .getMatchingBugs(BugSet.getMainBugSet()); for (BugLeafNode nextNode : filteredSet) { saveCommentsToBug(nextNode); } } changed = false; } /** * Saves comments to the current selected bug. * */ public void saveComments() { saveComments(frame.currentSelectedBugLeaf, frame.currentSelectedBugAspects); } public void saveComments(BugLeafNode theNode, BugAspects theAspects) { if (theNode != null) saveCommentsToBug(theNode); else saveCommentsToNonLeaf(theAspects); } /** * Deletes the list have already. Then loads from list. Will load from the * list until run out of room in the prevCommentsList. * * @param list */ void loadPrevCommentsList(String[] list) { int count = 0; for (String str : list) { if (str.equals(""))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -