001: package org.acm.seguin.ide.common.options;
002:
003: import org.acm.seguin.pmd.RuleSetNotFoundException;
004: import org.acm.seguin.pmd.Rule;
005:
006: import javax.swing.BorderFactory;
007: import javax.swing.JCheckBox;
008: import javax.swing.JLabel;
009: import javax.swing.JList;
010: import javax.swing.JPanel;
011: import javax.swing.JLabel;
012: import javax.swing.JScrollPane;
013: import javax.swing.JTextArea;
014: import javax.swing.JTextField;
015: import javax.swing.ListCellRenderer;
016: import javax.swing.ListSelectionModel;
017: import javax.swing.UIManager;
018: import javax.swing.border.EmptyBorder;
019: import java.awt.BorderLayout;
020: import java.awt.Component;
021: import java.awt.FlowLayout;
022: import java.awt.GridLayout;
023: import java.awt.event.MouseAdapter;
024: import java.awt.event.MouseEvent;
025:
026: /**
027: * Description of the Class
028: *
029: * @author Mike Atkinson
030: */
031: public class PMDOptionPane extends JSHelpOptionPane {
032: JTextField txtMinTileSize;
033: JTextField txtCustomRules;
034: private SelectedPanel pnlCustomRules_sp;
035: private SelectedPanel directoryPopupBox_sp;
036: private SelectedPanel txtMinTileSize_sp;
037:
038: private SelectedRules rules;
039: private JTextArea exampleTextArea = new JTextArea(10, 80);
040: private JCheckBox directoryPopupBox;
041:
042: /**
043: * Constructor for the PMDOptionPane object
044: *
045: * @param project Description of Parameter
046: */
047: public PMDOptionPane(String project) {
048: super ("javastyle.pmd", "pmd", project);
049: try {
050: rules = new SelectedRules(((project == null) ? "default"
051: : project), this );
052: } catch (RuleSetNotFoundException rsne) {
053: rsne.printStackTrace();
054: }
055: }
056:
057: /** Description of the Method */
058: public void _init() {
059: removeAll();
060:
061: addComponent(new JLabel(
062: getIdeProperty("javastyle.pmd.more.info")));
063:
064: JPanel rulesPanel = new JPanel(new BorderLayout());
065: rulesPanel
066: .setBorder(BorderFactory
067: .createTitledBorder(getIdeProperty("javastyle.pmd.rules")));
068:
069: JList list = new CheckboxList(rules.getAllBoxes());
070: list
071: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
072: rulesPanel.add(new JScrollPane(list), BorderLayout.NORTH);
073:
074: //Custom Rule Panel Defination.
075: txtCustomRules = new JTextField(props.getString(
076: "pmd.customRulesPath", ""), 30);
077:
078: directoryPopupBox = new JCheckBox("Ask for directory?", props
079: .getBoolean("pmd.ui.directorypopup", false));
080:
081: JLabel lblMinTileSize = new JLabel("Minimum Tile Size :");
082: txtMinTileSize = new JTextField(props.getString(
083: "pmd.cpd.defMinTileSize", "100"), 5);
084:
085: addComponent(null, null, rulesPanel);
086:
087: JPanel textPanel = new JPanel();
088: exampleTextArea.setEditable(false);
089: exampleTextArea.setLineWrap(true);
090: exampleTextArea.setWrapStyleWord(true);
091: textPanel
092: .setBorder(BorderFactory
093: .createTitledBorder(getIdeProperty("javastyle.pmd.example")));
094: textPanel.add(new JScrollPane(exampleTextArea));
095: addComponent(null, null, textPanel);
096:
097: addComponent(new JLabel(""));
098: addComponent(new JLabel(
099: getIdeJavaStyleOption("pmd.customRulesPath")));
100: pnlCustomRules_sp = addComponent("pmd.path", null,
101: txtCustomRules);
102: directoryPopupBox_sp = addComponent("pmd.ask.for.directory",
103: "ui.directorypopup", new JCheckBox());
104: addComponent(new JLabel(
105: getIdeJavaStyleOption("cpd.defMinTileSize")));
106: txtMinTileSize_sp = addComponent("pmd.min.tile.size", null,
107: txtMinTileSize);
108:
109: addHelpArea();
110: }
111:
112: /** Description of the Method */
113: public void _save() {
114: rules.save();
115: pnlCustomRules_sp.save();
116: directoryPopupBox_sp.save();
117: txtMinTileSize_sp.save();
118: }
119:
120: /**
121: * Description of the Class
122: *
123: * @author Chris Seguin
124: */
125: public class CheckboxList extends JList {
126:
127: /**
128: * Constructor for the CheckboxList object
129: *
130: * @param args Description of Parameter
131: * @paramargs Description of Parameter
132: * @paramargs Description of Parameter
133: * @paramargs Description of Parameter
134: */
135: public CheckboxList(Object[] args) {
136: super (args);
137: setCellRenderer(new CheckboxListCellRenderer());
138: addMouseListener(new MyMouseAdapter());
139: }
140:
141: /**
142: * Description of the Class
143: *
144: * @author Chris Seguin
145: */
146: public class CheckboxListCellRenderer implements
147: ListCellRenderer {
148: /**
149: * Gets the ListCellRendererComponent attribute of the
150: * CheckboxListCellRenderer object
151: *
152: * @param list Description of Parameter
153: * @param value Description of Parameter
154: * @param index Description of Parameter
155: * @param isSelected Description of Parameter
156: * @param cellHasFocus Description of Parameter
157: * @return The ListCellRendererComponent
158: * value
159: */
160: public Component getListCellRendererComponent(JList list,
161: Object value, int index, boolean isSelected,
162: boolean cellHasFocus) {
163: JCheckBox box = (JCheckBox) value;
164:
165: box.setEnabled(isEnabled());
166: box.setFont(getFont());
167: box.setFocusPainted(false);
168: box.setBorderPainted(true);
169: box.setBorder(isSelected ? UIManager
170: .getBorder("List.focusCellHighlightBorder")
171: : new EmptyBorder(1, 1, 1, 1));
172: return box;
173: }
174: }
175:
176: /**
177: * Description of the Class
178: *
179: * @author Chris Seguin
180: */
181: private class MyMouseAdapter extends MouseAdapter {
182: String previousExample = "";
183: private java.awt.event.MouseMotionAdapter mmma = new java.awt.event.MouseMotionAdapter() {
184: public void mouseMoved(MouseEvent e) {
185: handleMouseEvent(e);
186: }
187: };
188:
189: /**
190: * Description of the Method
191: *
192: * @param e Description of Parameter
193: */
194: public void Exited(MouseEvent e) {
195: removeMouseMotionListener(mmma);
196: }
197:
198: /**
199: * Description of the Method
200: *
201: * @param e Description of Parameter
202: */
203: public void mouseEntered(MouseEvent e) {
204: handleMouseEvent(e);
205: addMouseMotionListener(mmma);
206: }
207:
208: /**
209: * Description of the Method
210: *
211: * @param e Description of Parameter
212: */
213: public void mousePressed(MouseEvent e) {
214: int index = locationToIndex(e.getPoint());
215:
216: if (index != -1) {
217: JCheckBox box = (JCheckBox) getModel()
218: .getElementAt(index);
219:
220: box.setSelected(!box.isSelected());
221: repaint();
222: }
223: }
224:
225: private void handleMouseEvent(MouseEvent e) {
226: int index = locationToIndex(e.getPoint());
227: if (index != -1) {
228: JCheckBox box = (JCheckBox) getModel()
229: .getElementAt(index);
230: Rule rule = rules.getRule(box);
231: String example = rule.getExample().trim();
232: if (example.startsWith("\r")) {
233: example = example.substring(1);
234: }
235: if (example.startsWith("\n")) {
236: example = example.substring(1);
237: }
238: if (previousExample == null
239: || !previousExample.equals(example)) {
240: previousExample = example;
241: exampleTextArea.setText(example);
242: exampleTextArea.setCaretPosition(0);
243: String description = rule.getDescription()
244: .trim();
245: char prev = ' ';
246: StringBuffer sb = new StringBuffer();
247: for (int i = 0; i < description.length(); i++) {
248: char c = description.charAt(i);
249: if (c == '\n') {
250: c = ' ';
251: }
252: if (c == '\r') {
253: c = ' ';
254: }
255: if (!(c == ' ' && c == prev)) {
256: prev = c;
257: sb.append(c);
258: }
259: }
260: setHelpText(sb.toString());
261: }
262: }
263: }
264: }
265:
266: }
267: }
|