add file color for navigation list items which are not PsiElements but could fetch...
[fedora-idea.git] / platform / lang-impl / src / com / intellij / tools / FilterDialog.java
blobb44fe5beb9bfdcdde4afe38b14042293261fb65a
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /**
18 * @author Yura Cangea
20 package com.intellij.tools;
22 import com.intellij.execution.filters.InvalidExpressionException;
23 import com.intellij.execution.filters.RegexpFilter;
24 import com.intellij.openapi.ui.DialogWrapper;
25 import com.intellij.openapi.ui.Messages;
26 import com.intellij.ui.PopupHandler;
27 import com.intellij.CommonBundle;
29 import javax.swing.*;
30 import javax.swing.text.BadLocationException;
31 import java.awt.*;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
35 class FilterDialog extends DialogWrapper {
36 private final JTextField myRegexpField = new JTextField();
37 private final JTextField myNameField = new JTextField();
38 private final JTextField myDescriptionField = new JTextField();
40 private JPopupMenu myPopup;
42 private FilterDialog(Component component) {
43 super(component, true);
44 init();
45 setOKActionEnabled(true);
46 myRegexpField.setToolTipText(ToolsBundle.message("tools.filters.add.macro.tooltip"));
49 public static boolean editFilter(FilterInfo filterInfo, JComponent parentComponent, String title) throws InvalidExpressionException {
50 FilterDialog dialog = new FilterDialog(parentComponent);
51 dialog.setTitle(title);
52 dialog.myNameField.setText(filterInfo.getName());
53 dialog.myDescriptionField.setText(filterInfo.getDescription());
54 dialog.myRegexpField.setText(filterInfo.getRegExp());
55 dialog.show();
56 if (!dialog.isOK()) return false;
57 filterInfo.setName(dialog.myNameField.getText());
58 filterInfo.setDescription(dialog.myDescriptionField.getText());
59 filterInfo.setRegExp(dialog.myRegexpField.getText());
60 return true;
63 public JComponent getPreferredFocusedComponent() {
64 return myRegexpField;
67 protected JComponent createCenterPanel() {
68 JPanel mainPanel = new JPanel(new BorderLayout());
70 JPanel panel = new JPanel(new GridBagLayout());
72 GridBagConstraints constr;
74 constr = new GridBagConstraints();
75 constr.gridx = 0;
76 constr.gridy = 0;
77 constr.anchor = GridBagConstraints.WEST;
78 constr.weighty = 0;
79 constr.gridwidth = 1;
80 constr.insets = new Insets(5, 0, 0, 0);
81 panel.add(new JLabel(ToolsBundle.message("tools.filters.add.name.label")), constr);
83 constr.gridx = 0;
84 constr.gridy = 1;
85 constr.weightx = 1;
86 constr.gridwidth = 3;
87 constr.fill = GridBagConstraints.HORIZONTAL;
88 panel.add(myNameField, constr);
90 constr.gridx = 0;
91 constr.gridy = 2;
92 constr.weightx = 0;
93 panel.add(new JLabel(ToolsBundle.message("tools.filters.add.description.label")), constr);
95 constr.gridx = 0;
96 constr.gridy = 3;
97 constr.gridwidth = 2;
98 constr.weightx = 1;
99 panel.add(myDescriptionField, constr);
101 constr.gridy = 4;
102 constr.gridx = 0;
103 constr.gridwidth = 2;
104 constr.weightx = 0;
105 panel.add(new JLabel(ToolsBundle.message("tools.filters.add.regex.label")), constr);
107 constr.gridx = 0;
108 constr.gridy = 5;
109 constr.gridwidth = 3;
110 panel.add(myRegexpField, constr);
112 makePopup();
114 panel.setPreferredSize(new Dimension(335, 150));
116 mainPanel.add(panel, BorderLayout.NORTH);
118 return mainPanel;
121 private void makePopup() {
122 myPopup = new JPopupMenu();
123 String[] macrosName = RegexpFilter.getMacrosName();
124 JMenuItem[] items = new JMenuItem[macrosName.length];
125 for (int i = 0; i < macrosName.length; i++) {
126 items[i] = myPopup.add(macrosName[i]);
127 items[i].addActionListener(new MenuItemListener(macrosName[i]));
129 myRegexpField.addMouseListener(new PopupListener());
132 protected void doOKAction() {
133 String errorMessage = null;
134 if (noText(myNameField.getText())) {
135 errorMessage = ToolsBundle.message("tools.filters.add.name.required.error");
136 } else if (noText(myRegexpField.getText())) {
137 errorMessage = ToolsBundle.message("tools.filters.add.regex.required.error");
140 if (errorMessage != null) {
141 Messages.showMessageDialog(getContentPane(), errorMessage, CommonBundle.getErrorTitle(), Messages.getErrorIcon());
142 return;
145 try {
146 checkRegexp(myRegexpField.getText());
147 } catch (InvalidExpressionException e) {
148 Messages.showMessageDialog(getContentPane(), e.getMessage(), ToolsBundle.message("tools.filters.add.regex.invalid.title"), Messages.getErrorIcon());
149 return;
151 super.doOKAction();
154 private void checkRegexp(String regexpText) {
155 RegexpFilter.validate(regexpText);
158 private boolean noText(String text) {
159 return "".equals(text);
162 protected String getDimensionServiceKey(){
163 return "#com.intellij.tools.FilterDialog";
166 @Override
167 protected String getHelpId() {
168 return "reference.settings.ide.settings.external.tools.output.filters.add.filter";
171 private class MenuItemListener implements ActionListener {
172 private final String myMacrosName;
174 private MenuItemListener(String macrosName) {
175 myMacrosName = macrosName;
178 public void actionPerformed(ActionEvent e) {
179 int position = myRegexpField.getCaretPosition();
180 try {
181 if (myRegexpField.getText().indexOf(myMacrosName) == -1) {
182 myRegexpField.getDocument().insertString(position, myMacrosName, null);
183 myRegexpField.setCaretPosition(position + myMacrosName.length());
185 } catch (BadLocationException ex) {
187 myRegexpField.requestFocus();
191 private class PopupListener extends PopupHandler {
192 public void invokePopup(Component comp, int x, int y) {
193 myPopup.show(comp, x, y);