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.
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
;
30 import javax
.swing
.text
.BadLocationException
;
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);
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());
56 if (!dialog
.isOK()) return false;
57 filterInfo
.setName(dialog
.myNameField
.getText());
58 filterInfo
.setDescription(dialog
.myDescriptionField
.getText());
59 filterInfo
.setRegExp(dialog
.myRegexpField
.getText());
63 public JComponent
getPreferredFocusedComponent() {
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();
77 constr
.anchor
= GridBagConstraints
.WEST
;
80 constr
.insets
= new Insets(5, 0, 0, 0);
81 panel
.add(new JLabel(ToolsBundle
.message("tools.filters.add.name.label")), constr
);
87 constr
.fill
= GridBagConstraints
.HORIZONTAL
;
88 panel
.add(myNameField
, constr
);
93 panel
.add(new JLabel(ToolsBundle
.message("tools.filters.add.description.label")), constr
);
99 panel
.add(myDescriptionField
, constr
);
103 constr
.gridwidth
= 2;
105 panel
.add(new JLabel(ToolsBundle
.message("tools.filters.add.regex.label")), constr
);
109 constr
.gridwidth
= 3;
110 panel
.add(myRegexpField
, constr
);
114 panel
.setPreferredSize(new Dimension(335, 150));
116 mainPanel
.add(panel
, BorderLayout
.NORTH
);
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());
146 checkRegexp(myRegexpField
.getText());
147 } catch (InvalidExpressionException e
) {
148 Messages
.showMessageDialog(getContentPane(), e
.getMessage(), ToolsBundle
.message("tools.filters.add.regex.invalid.title"), Messages
.getErrorIcon());
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";
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();
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
);