update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / refactoring / migration / EditMigrationDialog.java
blob09f874f368520398f0b26318143f89578c20f7a6
2 /*
3 * Copyright 2000-2009 JetBrains s.r.o.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package com.intellij.refactoring.migration;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.ui.DialogWrapper;
21 import com.intellij.refactoring.RefactoringBundle;
22 import com.intellij.ui.DocumentAdapter;
23 import com.intellij.ui.IdeBorderFactory;
24 import com.intellij.ui.ScrollPaneFactory;
25 import com.intellij.util.ui.Table;
27 import javax.swing.*;
28 import javax.swing.event.DocumentEvent;
29 import javax.swing.event.ListSelectionEvent;
30 import javax.swing.event.ListSelectionListener;
31 import javax.swing.table.AbstractTableModel;
32 import javax.swing.table.TableModel;
33 import java.awt.*;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
37 public class EditMigrationDialog extends DialogWrapper{
38 private JTable myTable;
39 private JButton myEditButton;
40 private JButton myAddButton;
41 private JButton myRemoveButton;
42 private JButton myMoveUpButton;
43 private JButton myMoveDownButton;
44 private JTextField myNameField;
45 private JTextArea myDescriptionTextArea;
46 private final Project myProject;
47 private final MigrationMap myMigrationMap;
49 public EditMigrationDialog(Project project, MigrationMap migrationMap) {
50 super(project, true);
51 myProject = project;
52 myMigrationMap = migrationMap;
53 setHorizontalStretch(1.2f);
54 setTitle(RefactoringBundle.message("edit.migration.map.title"));
55 init();
56 validateOKButton();
59 public JComponent getPreferredFocusedComponent() {
60 return myNameField;
63 private void validateOKButton() {
64 boolean isEnabled = true;
65 if (myNameField.getText().trim().length() == 0) {
66 isEnabled = false;
67 } else if (myMigrationMap.getEntryCount() == 0) {
68 isEnabled = false;
70 setOKActionEnabled(isEnabled);
73 public String getName() {
74 return myNameField.getText();
77 public String getDescription() {
78 return myDescriptionTextArea.getText();
81 protected JComponent createNorthPanel() {
82 JPanel panel = new JPanel(new GridBagLayout());
83 GridBagConstraints gbConstraints = new GridBagConstraints();
85 gbConstraints.insets = new Insets(4, 4, 4, 4);
86 gbConstraints.fill = GridBagConstraints.VERTICAL;
87 gbConstraints.weightx = 0;
88 gbConstraints.weighty = 1;
89 gbConstraints.anchor = GridBagConstraints.EAST;
90 JLabel promptLabel = new JLabel(RefactoringBundle.message("migration.map.name.prompt"));
91 panel.add(promptLabel, gbConstraints);
93 gbConstraints.fill = GridBagConstraints.BOTH;
94 gbConstraints.weightx = 1;
95 gbConstraints.gridwidth = GridBagConstraints.REMAINDER;
96 myNameField = new JTextField(myMigrationMap.getName());
97 myNameField.getDocument().addDocumentListener(new DocumentAdapter() {
98 protected void textChanged(DocumentEvent e) {
99 validateOKButton();
102 panel.add(myNameField, gbConstraints);
104 gbConstraints.fill = GridBagConstraints.VERTICAL;
105 gbConstraints.weightx = 0;
106 gbConstraints.weighty = 1;
107 gbConstraints.gridwidth = GridBagConstraints.RELATIVE;
108 gbConstraints.anchor = GridBagConstraints.EAST;
109 JLabel descriptionPromptLabel = new JLabel(RefactoringBundle.message("migration.map.description.label"));
110 panel.add(descriptionPromptLabel, gbConstraints);
112 gbConstraints.fill = GridBagConstraints.BOTH;
113 gbConstraints.weightx = 1;
114 gbConstraints.gridwidth = GridBagConstraints.REMAINDER;
116 myDescriptionTextArea = new JTextArea(myMigrationMap.getDescription(), 3, 40);
117 myDescriptionTextArea.setLineWrap(true);
118 myDescriptionTextArea.setWrapStyleWord(true);
119 JScrollPane scrollPane = new JScrollPane(myDescriptionTextArea);
120 scrollPane.setBorder(null);
121 scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
122 scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
123 myDescriptionTextArea.setFont(myNameField.getFont());
124 myDescriptionTextArea.setBackground(myNameField.getBackground());
125 scrollPane.setBorder(myNameField.getBorder());
126 panel.add(scrollPane, gbConstraints);
128 return panel;
131 protected JComponent createCenterPanel() {
132 JPanel tablePanel = new JPanel(new BorderLayout());
133 tablePanel.setBorder(IdeBorderFactory.createBorder());
134 tablePanel.add(createTable(), BorderLayout.CENTER);
136 JPanel tableButtonsPanel = new JPanel();
137 tableButtonsPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
139 tableButtonsPanel.setLayout(new GridBagLayout());
140 GridBagConstraints gbConstraints = new GridBagConstraints();
141 gbConstraints.gridwidth = GridBagConstraints.REMAINDER;
142 gbConstraints.fill = GridBagConstraints.HORIZONTAL;
143 gbConstraints.insets = new Insets(5,0,5,0);
145 myAddButton = new JButton(RefactoringBundle.message("migration.add.button"));
146 myAddButton.addActionListener(new ActionListener() {
147 public void actionPerformed(ActionEvent e) {
148 validateOKButton();
151 tableButtonsPanel.add(myAddButton, gbConstraints);
153 myEditButton = new JButton(RefactoringBundle.message("migration.edit.button"));
154 tableButtonsPanel.add(myEditButton, gbConstraints);
155 myRemoveButton = new JButton(RefactoringBundle.message("migration.remove.button"));
156 myRemoveButton.addActionListener(new ActionListener() {
157 public void actionPerformed(ActionEvent e) {
158 validateOKButton();
161 tableButtonsPanel.add(myRemoveButton, gbConstraints);
163 myMoveUpButton = new JButton(RefactoringBundle.message("migration.move.up.button"));
164 tableButtonsPanel.add(myMoveUpButton, gbConstraints);
165 myMoveDownButton = new JButton(RefactoringBundle.message("migration.move.down.button"));
166 tableButtonsPanel.add(myMoveDownButton, gbConstraints);
168 gbConstraints.weighty = 1;
169 tableButtonsPanel.add(new JPanel(), gbConstraints);
171 tablePanel.add(tableButtonsPanel, BorderLayout.EAST);
173 myEditButton.addActionListener(
174 new ActionListener() {
175 public void actionPerformed(ActionEvent event) {
176 edit();
181 myAddButton.addActionListener(
182 new ActionListener() {
183 public void actionPerformed(ActionEvent event) {
184 addRow();
189 myRemoveButton.addActionListener(
190 new ActionListener() {
191 public void actionPerformed(ActionEvent event) {
192 removeRow();
197 myMoveUpButton.addActionListener(
198 new ActionListener() {
199 public void actionPerformed(ActionEvent event) {
200 moveUp();
205 myMoveDownButton.addActionListener(
206 new ActionListener() {
207 public void actionPerformed(ActionEvent event) {
208 moveDown();
213 myTable.getSelectionModel().addListSelectionListener(
214 new ListSelectionListener(){
215 public void valueChanged(ListSelectionEvent e){
216 enableButtons();
221 enableButtons();
222 return tablePanel;
225 private void edit() {
226 EditMigrationEntryDialog dialog = new EditMigrationEntryDialog(myProject);
227 int selected = myTable.getSelectedRow();
228 if (selected < 0)
229 return;
230 MigrationMapEntry entry = myMigrationMap.getEntryAt(selected);
231 dialog.setEntry(entry);
232 dialog.show();
233 if (!dialog.isOK()){
234 return;
236 dialog.updateEntry(entry);
237 AbstractTableModel model = (AbstractTableModel)myTable.getModel();
238 model.fireTableRowsUpdated(selected, selected);
241 private void addRow() {
242 EditMigrationEntryDialog dialog = new EditMigrationEntryDialog(myProject);
243 MigrationMapEntry entry = new MigrationMapEntry();
244 dialog.setEntry(entry);
245 dialog.show();
246 if (!dialog.isOK()){
247 return;
249 dialog.updateEntry(entry);
250 myMigrationMap.addEntry(entry);
251 AbstractTableModel model = (AbstractTableModel)myTable.getModel();
252 model.fireTableRowsInserted(myMigrationMap.getEntryCount() - 1, myMigrationMap.getEntryCount() - 1);
253 myTable.setRowSelectionInterval(myMigrationMap.getEntryCount() - 1, myMigrationMap.getEntryCount() - 1);
256 private void removeRow() {
257 int selected = myTable.getSelectedRow();
258 if (selected < 0)
259 return;
260 myMigrationMap.removeEntryAt(selected);
261 AbstractTableModel model = (AbstractTableModel)myTable.getModel();
262 model.fireTableRowsDeleted(selected, selected);
263 if (selected >= myMigrationMap.getEntryCount()){
264 selected--;
266 if (selected >= 0){
267 myTable.setRowSelectionInterval(selected, selected);
271 private void moveUp() {
272 int selected = myTable.getSelectedRow();
273 if (selected < 1)
274 return;
275 MigrationMapEntry entry = myMigrationMap.getEntryAt(selected);
276 MigrationMapEntry previousEntry = myMigrationMap.getEntryAt(selected - 1);
277 myMigrationMap.setEntryAt(previousEntry, selected);
278 myMigrationMap.setEntryAt(entry, selected - 1);
279 AbstractTableModel model = (AbstractTableModel)myTable.getModel();
280 model.fireTableRowsUpdated(selected - 1, selected);
281 myTable.setRowSelectionInterval(selected - 1, selected - 1);
284 private void moveDown() {
285 int selected = myTable.getSelectedRow();
286 if (selected >= myMigrationMap.getEntryCount() - 1)
287 return;
288 MigrationMapEntry entry = myMigrationMap.getEntryAt(selected);
289 MigrationMapEntry nextEntry = myMigrationMap.getEntryAt(selected + 1);
290 myMigrationMap.setEntryAt(nextEntry, selected);
291 myMigrationMap.setEntryAt(entry, selected + 1);
292 AbstractTableModel model = (AbstractTableModel)myTable.getModel();
293 model.fireTableRowsUpdated(selected, selected + 1);
294 myTable.setRowSelectionInterval(selected + 1, selected + 1);
297 private JScrollPane createTable() {
298 final String[] names = {
299 RefactoringBundle.message("migration.type.column.header"),
300 RefactoringBundle.message("migration.old.name.column.header"),
301 RefactoringBundle.message("migration.new.name.column.header")};
303 // Create a model of the data.
304 TableModel dataModel = new AbstractTableModel() {
305 public int getColumnCount() {
306 return 3;
309 public int getRowCount() {
310 return myMigrationMap.getEntryCount();
313 public Object getValueAt(int row, int col) {
314 MigrationMapEntry entry = myMigrationMap.getEntryAt(row);
315 if (col == 0){
316 if (entry.getType() == MigrationMapEntry.PACKAGE && entry.isRecursive()){
317 return RefactoringBundle.message("migration.package.with.subpackages");
319 else if (entry.getType() == MigrationMapEntry.PACKAGE && !entry.isRecursive()){
320 return RefactoringBundle.message("migration.package");
322 else{
323 return RefactoringBundle.message("migration.class");
327 String suffix = (entry.getType() == MigrationMapEntry.PACKAGE ? ".*" : "");
328 if (col == 1){
329 return entry.getOldName() + suffix;
331 else{
332 return entry.getNewName() + suffix;
336 public String getColumnName(int column) {
337 return names[column];
340 public Class getColumnClass(int c) {
341 return String.class;
344 public boolean isCellEditable(int row, int col) {
345 return false;
348 public void setValueAt(Object aValue, int row, int column) {
352 // Create the table
353 myTable = new Table(dataModel);
354 myTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
356 myTable.setPreferredScrollableViewportSize(new Dimension(300, myTable.getRowHeight() * 10));
358 return ScrollPaneFactory.createScrollPane(myTable);
361 private void enableButtons() {
362 int selectedIndex = myTable.getSelectedRow();
363 myEditButton.setEnabled(selectedIndex != -1);
364 myRemoveButton.setEnabled(selectedIndex != -1);
365 myMoveDownButton.setEnabled(selectedIndex != -1 && selectedIndex < myTable.getRowCount() - 1);
366 myMoveUpButton.setEnabled(selectedIndex > 0);