diff --git a/openapi/src/com/intellij/openapi/editor/actions/ContentChooser.java b/openapi/src/com/intellij/openapi/editor/actions/ContentChooser.java new file mode 100644 index 000000000000..bfbc9dc99e55 --- /dev/null +++ b/openapi/src/com/intellij/openapi/editor/actions/ContentChooser.java @@ -0,0 +1,194 @@ +package com.intellij.openapi.editor.actions; + +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.EditorFactory; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.DialogWrapper; +import com.intellij.openapi.ui.Splitter; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.util.IconLoader; + +import javax.swing.*; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import java.awt.*; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.ArrayList; + +public abstract class ContentChooser extends DialogWrapper { + + private static final Icon textIcon = IconLoader.getIcon("/fileTypes/text.png"); + + private JList myList; + private java.util.List myAllContents; + + private JTextArea myTextArea; + private Editor myViewer; + private final boolean myUseIdeaEditor; + + private Splitter mySplitter; + private Project myProject; + + public ContentChooser(Project project, String title, boolean useIdeaEditor) { + super(project, true); + myProject = project; + myUseIdeaEditor = useIdeaEditor; + + setOKButtonText("OK"); + setTitle(title); + + init(); + } + + public JComponent getPreferredFocusedComponent() { + return myList; + } + + protected JComponent createCenterPanel() { + myList = new JList(); + myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + + rebuildListContent(); + + myList.addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent e) { + if (e.isConsumed() || e.getClickCount() != 2 || e.isPopupTrigger()) return; + close(OK_EXIT_CODE); + } + }); + + myList.setCellRenderer(new MyListCellRenderer()); + + if (myAllContents.size() > 0) { + myList.setSelectedIndex(0); + } + + myList.addKeyListener(new KeyAdapter() { + public void keyReleased(KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_DELETE) { + int selectedIndex = getSelectedIndex(); + int size = myAllContents.size(); + removeContentAt(myAllContents.get(selectedIndex)); + rebuildListContent(); + if (size == 1) { + close(CANCEL_EXIT_CODE); + return; + } + myList.setSelectedIndex(Math.min(selectedIndex, myAllContents.size() - 1)); + } + else if (e.getKeyCode() == KeyEvent.VK_ENTER) { + close(OK_EXIT_CODE); + } + } + }); + + mySplitter = new Splitter(true); + mySplitter.setFirstComponent(new JScrollPane(myList)); + mySplitter.setSecondComponent(new JPanel()); + updateViewerForSelection(); + + myList.addListSelectionListener(new ListSelectionListener() { + public void valueChanged(ListSelectionEvent e) { + updateViewerForSelection(); + } + }); + + mySplitter.setPreferredSize(new Dimension(500, 500)); + + return mySplitter; + } + + protected abstract void removeContentAt(final Data Content); + + protected String getDimensionServiceKey() { + return "#com.intellij.openapi.editor.actions.MultiplePasteAction.Chooser"; + } + + private void updateViewerForSelection() { + if (myAllContents.size() == 0) return; + String fullString = getStringRepresentationFor(myAllContents.get(getSelectedIndex())); + fullString = StringUtil.convertLineSeparators(fullString); + + if (myViewer != null) { + EditorFactory.getInstance().releaseEditor(myViewer); + } + + if (myUseIdeaEditor) { + Document doc = EditorFactory.getInstance().createDocument(fullString); + myViewer = EditorFactory.getInstance().createViewer(doc, myProject); + myViewer.getComponent().setPreferredSize(new Dimension(300, 500)); + myViewer.getSettings().setFoldingOutlineShown(false); + myViewer.getSettings().setLineNumbersShown(false); + myViewer.getSettings().setLineMarkerAreaShown(false); + mySplitter.setSecondComponent(myViewer.getComponent()); + } else { + myTextArea = new JTextArea(fullString); + myTextArea.setRows(3); + myTextArea.setWrapStyleWord(true); + myTextArea.setLineWrap(true); + myTextArea.setSelectionStart(0); + myTextArea.setSelectionEnd(myTextArea.getText().length()); + myTextArea.setEditable(false); + mySplitter.setSecondComponent(new JScrollPane(myTextArea)); + } + mySplitter.revalidate(); + } + + protected void dispose() { + super.dispose(); + if (myViewer != null) { + EditorFactory.getInstance().releaseEditor(myViewer); + myViewer = null; + } + } + + private void rebuildListContent() { + java.util.List allContents = new ArrayList(getContents()); + ArrayList contents = new ArrayList(); + ArrayList shortened = new ArrayList(); + for (int i = 0; i < allContents.size(); i++) { + Data content = allContents.get(i); + String fullString = getStringRepresentationFor(content); + if (fullString != null) { + fullString = StringUtil.convertLineSeparators(fullString, "\n"); + contents.add(content); + int lastNewLineIdx = fullString.indexOf('\n'); + shortened.add(lastNewLineIdx == -1 ? fullString : fullString.substring(0, lastNewLineIdx) + " ..."); + } + } + + myAllContents = allContents; + myList.setListData(shortened.toArray(new String[shortened.size()])); + } + + protected abstract String getStringRepresentationFor(final Data content); + + protected abstract java.util.List getContents(); + + public int getSelectedIndex() { + if (myList.getSelectedIndex() == -1) return 0; + return myList.getSelectedIndex(); + } + + public java.util.List getAllContents() { + return myAllContents; + } + + private class MyListCellRenderer extends DefaultListCellRenderer { + public Component getListCellRendererComponent( + JList list, + Object value, + int index, + boolean isSelected, + boolean cellHasFocus) { + super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); + setIcon(textIcon); + setText((String)value); + return this; + } + } +} diff --git a/openapi/src/com/intellij/openapi/vcs/VcsConfiguration.java b/openapi/src/com/intellij/openapi/vcs/VcsConfiguration.java index 5ee5cdbffd51..073d8978c825 100644 --- a/openapi/src/com/intellij/openapi/vcs/VcsConfiguration.java +++ b/openapi/src/com/intellij/openapi/vcs/VcsConfiguration.java @@ -136,4 +136,8 @@ public final class VcsConfiguration implements JDOMExternalizable, ProjectCompon public ArrayList getRecentMessages() { return new ArrayList(myLastCommitMessages); } + + public void removeMessage(final String content) { + myLastCommitMessages.remove(content); + } } diff --git a/openapi/src/com/intellij/openapi/vcs/ui/CommitMessage.java b/openapi/src/com/intellij/openapi/vcs/ui/CommitMessage.java index 94b841615781..266a7b32ed1f 100644 --- a/openapi/src/com/intellij/openapi/vcs/ui/CommitMessage.java +++ b/openapi/src/com/intellij/openapi/vcs/ui/CommitMessage.java @@ -1,77 +1,58 @@ package com.intellij.openapi.vcs.ui; -import com.intellij.ui.ColoredListCellRenderer; -import com.intellij.ui.SimpleTextAttributes; +import com.intellij.openapi.editor.actions.ContentChooser; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vcs.VcsConfiguration; import javax.swing.*; import java.awt.*; -import java.awt.event.ActionListener; import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.util.ArrayList; -import java.util.Iterator; +import java.util.List; +import java.util.Collections; public class CommitMessage extends JPanel{ private final JTextArea myCommentArea = new JTextArea(); - private final JComboBox myRecentMessages = new JComboBox(); + private final JButton myHistory = new JButton("History..."); - class RecentMessage { - private final boolean canBeInserted; - private final String myPresentation; - private final String myValue; - - public RecentMessage(final boolean canBeInserted, final String presentation, final String value) { - this.canBeInserted = canBeInserted; - myPresentation = presentation; - myValue = value; - } - - public boolean isCanBeInserted() { - return canBeInserted; - } - - public String getPresentation() { - return myPresentation; - } - - public String getValue() { - return myValue; - } - } - - public CommitMessage(ArrayList recentMessages) { + public CommitMessage(final VcsConfiguration configuration, final Project project) { super(new BorderLayout()); - add(new JScrollPane(myCommentArea), BorderLayout.CENTER); + myHistory.setMnemonic('H'); + final JScrollPane scrollPane = new JScrollPane(myCommentArea); + scrollPane.setPreferredSize(myCommentArea.getPreferredSize()); + add(scrollPane, BorderLayout.CENTER); add(new JLabel("Comment:"), BorderLayout.NORTH); + final ArrayList recentMessages = configuration.getRecentMessages(); + Collections.reverse(recentMessages); + if (!recentMessages.isEmpty()) { - add(myRecentMessages, BorderLayout.SOUTH); + final JPanel buttonPanel = new JPanel(new BorderLayout()); + buttonPanel.add(myHistory, BorderLayout.EAST); + add(buttonPanel, BorderLayout.SOUTH); - myRecentMessages.addItem(new RecentMessage(false, "", null)); - - for (Iterator iterator = recentMessages.iterator(); iterator.hasNext();) { - String s = iterator.next(); - myRecentMessages.addItem(new RecentMessage(true, createPresentation(s), s)); - } - - myRecentMessages.setRenderer(new ColoredListCellRenderer() { - protected void customizeCellRenderer( - JList list, - Object value, - int index, - boolean selected, - boolean hasFocus - ) { - if (value instanceof RecentMessage) { - append(((RecentMessage)value).getPresentation(), SimpleTextAttributes.REGULAR_ATTRIBUTES); - } - } - }); - - myRecentMessages.addActionListener(new ActionListener() { + myHistory.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - final Object selectedItem = myRecentMessages.getSelectedItem(); - if (selectedItem instanceof RecentMessage && ((RecentMessage)selectedItem).canBeInserted) { - setText(((RecentMessage)selectedItem).getValue()); + final ContentChooser contentChooser = new ContentChooser(project, "Choose Message", false){ + protected void removeContentAt(final String content) { + configuration.removeMessage(content); + } + + protected String getStringRepresentationFor(final String content) { + return content; + } + + protected List getContents() { + return recentMessages; + } + }; + contentChooser.show(); + if (contentChooser.isOK()) { + final int selectedIndex = contentChooser.getSelectedIndex(); + if (selectedIndex >= 0) { + setText(contentChooser.getAllContents().get(selectedIndex)); + } } } }); @@ -81,15 +62,6 @@ public class CommitMessage extends JPanel{ public JComponent getTextField() { return myCommentArea; } - - private String createPresentation(final String s) { - String converted = s.replaceAll("\n", " "); - if (converted.length() < 20) { - return converted; - } else { - return converted.substring(0, 20) + "..."; - } - } public void setText(final String initialMessage) { myCommentArea.setText(initialMessage); diff --git a/source/com/intellij/openapi/editor/actions/MultiplePasteAction.java b/source/com/intellij/openapi/editor/actions/MultiplePasteAction.java index 92d2c6cf586f..bc1b68dc4311 100644 --- a/source/com/intellij/openapi/editor/actions/MultiplePasteAction.java +++ b/source/com/intellij/openapi/editor/actions/MultiplePasteAction.java @@ -4,35 +4,26 @@ import com.intellij.ide.CopyPasteManagerEx; import com.intellij.ide.DataManager; import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.actionSystem.ex.DataConstantsEx; -import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.editor.EditorFactory; import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.ide.CopyPasteManager; import com.intellij.openapi.project.Project; -import com.intellij.openapi.ui.DialogWrapper; -import com.intellij.openapi.ui.Splitter; -import com.intellij.openapi.util.IconLoader; -import com.intellij.openapi.util.text.StringUtil; import javax.swing.*; -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; import javax.swing.text.DefaultEditorKit; import java.awt.*; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; -import java.awt.event.*; +import java.awt.event.ActionEvent; import java.io.IOException; -import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; /** * @author max */ public class MultiplePasteAction extends AnAction { - private static final Icon textIcon = IconLoader.getIcon("/fileTypes/text.png"); - public MultiplePasteAction() { setEnabledInModalContext(true); } @@ -45,18 +36,38 @@ public class MultiplePasteAction extends AnAction { if (!(focusedComponent instanceof JComponent)) return; - final Chooser chooser = new Chooser(project); + final ContentChooser chooser = new ContentChooser(project, "Choose Content to Paste", true){ + protected String getStringRepresentationFor(final Transferable content) { + try { + return (String)content.getTransferData(DataFlavor.stringFlavor); + } + catch (UnsupportedFlavorException e1) { + return ""; + } + catch (IOException e1) { + return ""; + } + } - if (chooser.myAllContents.length > 0) { + protected List getContents() { + return Arrays.asList(CopyPasteManager.getInstance().getAllContents()); + } + + protected void removeContentAt(final Transferable content) { + ((CopyPasteManagerEx)CopyPasteManager.getInstance()).removeContent(content); + } + }; + + if (chooser.getAllContents().size() > 0) { chooser.show(); } else { - chooser.close(Chooser.CANCEL_EXIT_CODE); + chooser.close(ContentChooser.CANCEL_EXIT_CODE); } if (chooser.isOK()) { final int selectedIndex = chooser.getSelectedIndex(); - ((CopyPasteManagerEx)CopyPasteManager.getInstance()).moveContentTopStackTop(chooser.myAllContents[selectedIndex]); + ((CopyPasteManagerEx)CopyPasteManager.getInstance()).moveContentTopStackTop(chooser.getAllContents().get(selectedIndex)); if (editor != null) { if (!editor.getDocument().isWritable()) { @@ -94,161 +105,4 @@ public class MultiplePasteAction extends AnAction { return pasteAction != null; } - private static class Chooser extends DialogWrapper { - private JList myList; - private Transferable[] myAllContents; - private Editor myViewer; - private Splitter mySplitter; - private Project myProject; - - public Chooser(Project project) { - super(project, true); - myProject = project; - - setOKButtonText("OK"); - setTitle("Choose Content to Paste"); - - init(); - } - - public JComponent getPreferredFocusedComponent() { - return myList; - } - - protected JComponent createCenterPanel() { - myList = new JList(); - myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - - rebuildListContent(); - - myList.addMouseListener(new MouseAdapter() { - public void mouseClicked(MouseEvent e) { - if (e.isConsumed() || e.getClickCount() != 2 || e.isPopupTrigger()) return; - close(OK_EXIT_CODE); - } - }); - - myList.setCellRenderer(new MyListCellRenderer()); - - if (myAllContents.length > 0) { - myList.setSelectedIndex(0); - } - - myList.addKeyListener(new KeyAdapter() { - public void keyReleased(KeyEvent e) { - if (e.getKeyCode() == KeyEvent.VK_DELETE) { - int selectedIndex = getSelectedIndex(); - int size = myAllContents.length; - ((CopyPasteManagerEx)CopyPasteManager.getInstance()).removeContent(myAllContents[selectedIndex]); - rebuildListContent(); - if (size == 1) { - close(CANCEL_EXIT_CODE); - return; - } - myList.setSelectedIndex(Math.min(selectedIndex, myAllContents.length - 1)); - } - else if (e.getKeyCode() == KeyEvent.VK_ENTER) { - close(OK_EXIT_CODE); - } - } - }); - - mySplitter = new Splitter(true); - mySplitter.setFirstComponent(new JScrollPane(myList)); - mySplitter.setSecondComponent(new JPanel()); - updateViewerForSelection(); - - myList.addListSelectionListener(new ListSelectionListener() { - public void valueChanged(ListSelectionEvent e) { - updateViewerForSelection(); - } - }); - - mySplitter.setPreferredSize(new Dimension(500, 500)); - - return mySplitter; - } - - protected String getDimensionServiceKey() { - return "#com.intellij.openapi.editor.actions.MultiplePasteAction.Chooser"; - } - - private void updateViewerForSelection() { - if (myAllContents.length == 0) return; - - try { - Transferable content = myAllContents[getSelectedIndex()]; - String fullString = (String)content.getTransferData(DataFlavor.stringFlavor); - fullString = StringUtil.convertLineSeparators(fullString); - Document doc = EditorFactory.getInstance().createDocument(fullString); - if (myViewer != null) { - EditorFactory.getInstance().releaseEditor(myViewer); - } - - myViewer = EditorFactory.getInstance().createViewer(doc, myProject); - myViewer.getComponent().setPreferredSize(new Dimension(300, 500)); - myViewer.getSettings().setFoldingOutlineShown(false); - myViewer.getSettings().setLineNumbersShown(false); - myViewer.getSettings().setLineMarkerAreaShown(false); - mySplitter.setSecondComponent(myViewer.getComponent()); - mySplitter.revalidate(); - } - catch (UnsupportedFlavorException e1) { - } - catch (IOException e1) { - } - } - - protected void dispose() { - super.dispose(); - if (myViewer != null) { - EditorFactory.getInstance().releaseEditor(myViewer); - myViewer = null; - } - } - - private void rebuildListContent() { - Transferable[] allContents = CopyPasteManager.getInstance().getAllContents(); - ArrayList contents = new ArrayList(); - ArrayList shortened = new ArrayList(); - for (int i = 0; i < allContents.length; i++) { - Transferable content = allContents[i]; - try { - String fullString = (String)content.getTransferData(DataFlavor.stringFlavor); - if (fullString != null) { - fullString = StringUtil.convertLineSeparators(fullString, "\n"); - contents.add(content); - int lastNewLineIdx = fullString.indexOf('\n'); - shortened.add(lastNewLineIdx == -1 ? fullString : fullString.substring(0, lastNewLineIdx) + " ..."); - } - } - catch (UnsupportedFlavorException e) { - } - catch (IOException e) { - } - } - - myAllContents = contents.toArray(new Transferable[contents.size()]); - myList.setListData(shortened.toArray(new String[shortened.size()])); - } - - private int getSelectedIndex() { - if (myList.getSelectedIndex() == -1) return 0; - return myList.getSelectedIndex(); - } - - private class MyListCellRenderer extends DefaultListCellRenderer { - public Component getListCellRendererComponent( - JList list, - Object value, - int index, - boolean isSelected, - boolean cellHasFocus) { - super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); - setIcon(textIcon); - setText((String)value); - return this; - } - } - } }