From 11a4a9dea4547ae6d4e66da7bb34f73ab60933c4 Mon Sep 17 00:00:00 2001 From: Olesya Smirnova Date: Thu, 27 Oct 2005 17:18:35 +0400 Subject: [PATCH] exceptions --- .../common/CreateFileObjects.java | 17 +++--- .../javacvsImpl/io/StreamLogger.java | 8 +-- .../projectView/impl/nodes/BasePsiNode.java | 13 ++++- .../diff/impl/mergeTool/MergeVersion.java | 6 +- .../impl/UpToDateLineNumberProviderImpl.java | 7 +-- .../CompareWithSelectedRevisionAction.java | 55 ++++++++++++------- .../com/intellij/util/ui/ListTableModel.java | 4 +- 7 files changed, 65 insertions(+), 45 deletions(-) diff --git a/plugins/cvs2/source/com/intellij/cvsSupport2/cvsoperations/common/CreateFileObjects.java b/plugins/cvs2/source/com/intellij/cvsSupport2/cvsoperations/common/CreateFileObjects.java index 308e961ecc12..2a2c541bef8d 100644 --- a/plugins/cvs2/source/com/intellij/cvsSupport2/cvsoperations/common/CreateFileObjects.java +++ b/plugins/cvs2/source/com/intellij/cvsSupport2/cvsoperations/common/CreateFileObjects.java @@ -1,5 +1,6 @@ package com.intellij.cvsSupport2.cvsoperations.common; +import com.intellij.openapi.diagnostic.Logger; import org.netbeans.lib.cvsclient.file.AbstractFileObject; import org.netbeans.lib.cvsclient.file.DirectoryObject; import org.netbeans.lib.cvsclient.file.FileObject; @@ -7,8 +8,6 @@ import org.netbeans.lib.cvsclient.file.FileObject; import java.io.File; import java.util.*; -import com.intellij.openapi.diagnostic.Logger; - /** * author: lesya */ @@ -30,13 +29,13 @@ public class CreateFileObjects { } public Collection execute(){ - for (int i = 0; i < myFiles.length; i++) { - File file = myFiles[i]; - LOG.assertTrue(file.isDirectory() || file.isFile() || file.getParentFile().isDirectory(), file.getAbsolutePath()); - String fileAbsolutePath = file.getAbsolutePath(); - String filePath = fileAbsolutePath.equals(myRootPath) ? "/" : fileAbsolutePath.substring(myRootPath.length() + 1); - File relativeFile = new File(filePath); - myResult.add(createAbstractFileObject(relativeFile.getParentFile(), relativeFile, file.isDirectory())); + for (File file : myFiles) { + if (file.isDirectory() || ! file.isFile() || !file.getParentFile().isDirectory()) { + String fileAbsolutePath = file.getAbsolutePath(); + String filePath = fileAbsolutePath.equals(myRootPath) ? "/" : fileAbsolutePath.substring(myRootPath.length() + 1); + File relativeFile = new File(filePath); + myResult.add(createAbstractFileObject(relativeFile.getParentFile(), relativeFile, file.isDirectory())); + } } return myResult; diff --git a/plugins/cvs2/source/com/intellij/cvsSupport2/javacvsImpl/io/StreamLogger.java b/plugins/cvs2/source/com/intellij/cvsSupport2/javacvsImpl/io/StreamLogger.java index fe209888485e..db6e4be3e76f 100644 --- a/plugins/cvs2/source/com/intellij/cvsSupport2/javacvsImpl/io/StreamLogger.java +++ b/plugins/cvs2/source/com/intellij/cvsSupport2/javacvsImpl/io/StreamLogger.java @@ -10,6 +10,7 @@ import java.io.*; /** * author: lesya */ +@SuppressWarnings({"IOResourceOpenedButNotSafelyClosed"}) public class StreamLogger implements IStreamLogger { private int myCloseCount = 0; @@ -25,9 +26,6 @@ public class StreamLogger implements IStreamLogger { private static final long MAX_OUTPUT_SIZE = 1000000; @NonNls private static final String OUTPUT_PATHNAME = "cvs.output"; - public StreamLogger() { - } - private OutputStream createFileOutputStream(final File cvsOutputFile) { try { return new FileOutputStream(cvsOutputFile, true); @@ -69,14 +67,14 @@ public class StreamLogger implements IStreamLogger { public void close() throws IOException { myCloseCount++; - if (myCloseCount == 2) { + if (myCloseCount == 2 && myLogOutput != null) { myLogOutput.close(); myLogOutput = null; myCloseCount = 0; } } - public int read(byte b[], int off, int len) throws IOException { + public int read(byte[] b, int off, int len) throws IOException { if (len == 0) return 0; final int read = read(); if (read == -1) return -1; diff --git a/source/com/intellij/ide/projectView/impl/nodes/BasePsiNode.java b/source/com/intellij/ide/projectView/impl/nodes/BasePsiNode.java index 3fa7485a4e1e..c60b1768bef8 100644 --- a/source/com/intellij/ide/projectView/impl/nodes/BasePsiNode.java +++ b/source/com/intellij/ide/projectView/impl/nodes/BasePsiNode.java @@ -24,7 +24,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -@SuppressWarnings({"unchecked"}) +@SuppressWarnings({"unchecked", "CastToIncompatibleInterface", "InstanceofIncompatibleInterface"}) public abstract class BasePsiNode extends ProjectViewNode { private static final Logger LOG = Logger.getInstance("#com.intellij.ide.projectView.impl.nodes.BasePsiNode"); @@ -83,11 +83,14 @@ public abstract class BasePsiNode extends ProjectViewNode public void update(PresentationData data) { - final T value = getValue(); + T value = getValue(); if (value == null || !value.isValid()) { setValue(null); } - if (getValue() == null) return; + + value = getValue(); + + if (value == null) return; int flags = Iconable.ICON_FLAG_VISIBILITY; if (isMarkReadOnly()) { @@ -107,6 +110,7 @@ public abstract class BasePsiNode extends ProjectViewNode updateImpl(data); } + @SuppressWarnings({"InstanceofIncompatibleInterface", "CastToIncompatibleInterface"}) private boolean isDeprecated() { final T element = getValue(); if (element == null || !element.isValid()) return false; @@ -118,6 +122,9 @@ public abstract class BasePsiNode extends ProjectViewNode public boolean contains(VirtualFile file) { if (getValue() == null) return false; PsiFile containingFile = getValue().getContainingFile(); + if (containingFile == null) { + return false; + } final VirtualFile valueFile = containingFile.getVirtualFile(); if (valueFile != null) { return file.equals(valueFile); diff --git a/source/com/intellij/openapi/diff/impl/mergeTool/MergeVersion.java b/source/com/intellij/openapi/diff/impl/mergeTool/MergeVersion.java index a085d8712049..bf6b339c117d 100644 --- a/source/com/intellij/openapi/diff/impl/mergeTool/MergeVersion.java +++ b/source/com/intellij/openapi/diff/impl/mergeTool/MergeVersion.java @@ -37,9 +37,9 @@ public interface MergeVersion { private final String myOriginalText; public MergeDocumentVersion(Document document, String originalText) { - LOG.assertTrue(originalText != null); - LOG.assertTrue(document != null); - LOG.assertTrue(document.isWritable()); + LOG.assertTrue(originalText != null, "text should not be null"); + LOG.assertTrue(document != null, "document should not be null"); + LOG.assertTrue(document.isWritable(), "document should be writable"); myDocument = document; myOriginalText = originalText; } diff --git a/source/com/intellij/openapi/localVcs/impl/UpToDateLineNumberProviderImpl.java b/source/com/intellij/openapi/localVcs/impl/UpToDateLineNumberProviderImpl.java index 431ec7a9166d..68cb406bf0ea 100644 --- a/source/com/intellij/openapi/localVcs/impl/UpToDateLineNumberProviderImpl.java +++ b/source/com/intellij/openapi/localVcs/impl/UpToDateLineNumberProviderImpl.java @@ -1,13 +1,12 @@ package com.intellij.openapi.localVcs.impl; import com.intellij.openapi.editor.Document; +import com.intellij.openapi.localVcs.UpToDateLineNumberProvider; import com.intellij.openapi.project.Project; import com.intellij.openapi.vcs.ex.LineStatusTracker; import com.intellij.openapi.vcs.ex.ProjectLevelVcsManagerEx; import com.intellij.openapi.vcs.ex.Range; -import com.intellij.openapi.localVcs.UpToDateLineNumberProvider; -import java.util.Iterator; import java.util.List; /** @@ -38,8 +37,8 @@ public class UpToDateLineNumberProviderImpl implements UpToDateLineNumberProvide List ranges = tracker.getRanges(); int result = currentNumber; - for (Iterator each = ranges.iterator(); each.hasNext();) { - Range range = (Range) each.next(); + for (final Object range1 : ranges) { + Range range = (Range)range1; int startOffset = range.getOffset1(); int endOffset = range.getOffset2(); diff --git a/source/com/intellij/openapi/vcs/actions/CompareWithSelectedRevisionAction.java b/source/com/intellij/openapi/vcs/actions/CompareWithSelectedRevisionAction.java index 9ad0dd92d906..f947cf3cdbaa 100644 --- a/source/com/intellij/openapi/vcs/actions/CompareWithSelectedRevisionAction.java +++ b/source/com/intellij/openapi/vcs/actions/CompareWithSelectedRevisionAction.java @@ -2,6 +2,7 @@ package com.intellij.openapi.vcs.actions; import com.intellij.openapi.actionSystem.Presentation; import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.Messages; import com.intellij.openapi.vcs.*; import com.intellij.openapi.vcs.diff.DiffProvider; import com.intellij.openapi.vcs.history.HistoryAsTreeProvider; @@ -18,13 +19,14 @@ import com.intellij.util.TreeItem; import com.intellij.util.ui.ColumnInfo; import com.intellij.util.ui.tree.TreeUtil; import com.intellij.util.ui.treetable.ListTreeTableModelOnColumns; +import com.intellij.CommonBundle; +import org.jetbrains.annotations.Nullable; import javax.swing.*; -import javax.swing.event.ListSelectionListener; import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; import javax.swing.tree.DefaultMutableTreeNode; import java.awt.*; -import java.util.Iterator; import java.util.List; public class CompareWithSelectedRevisionAction extends AbstractVcsAction { @@ -34,7 +36,7 @@ public class CompareWithSelectedRevisionAction extends AbstractVcsAction { return object.getRevision().getBranchName(); } }; - + private static final ColumnInfo REVISION_COLUMN = new ColumnInfo(VcsBundle.message("column.name.revision.list.revision")){ public String valueOf(final TreeNodeAdapter object) { return object.getRevision().getRevisionNumber().asString(); @@ -52,7 +54,7 @@ public class CompareWithSelectedRevisionAction extends AbstractVcsAction { return object.getRevision().getAuthor(); } }; - + public void update(VcsContext e, Presentation presentation) { AbstractShowDiffAction.updateDiffAction(presentation, e); } @@ -68,7 +70,7 @@ public class CompareWithSelectedRevisionAction extends AbstractVcsAction { final List revisions = session.getRevisionList(); final HistoryAsTreeProvider treeHistoryProvider = vcsHistoryProvider.getTreeHistoryProvider(); if (treeHistoryProvider != null) { - showTreePopup(treeHistoryProvider.createTreeOn(revisions), file, project, vcs.getDiffProvider()); + showTreePopup(treeHistoryProvider.createTreeOn(revisions), file, project, vcs.getDiffProvider()); } else { showListPopup(revisions, vcs, file, project); @@ -76,14 +78,14 @@ public class CompareWithSelectedRevisionAction extends AbstractVcsAction { } catch (VcsException e1) { - e1.printStackTrace(); + Messages.showErrorDialog(VcsBundle.message("message.text.cannot.show.differences"), CommonBundle.message("title.error")); } } private void showTreePopup(final List> roots, final VirtualFile file, final Project project, final DiffProvider diffProvider) { - final TreeTableView treeTable = new TreeTableView(new ListTreeTableModelOnColumns(new TreeNodeAdapter(null, null, roots), + final TreeTableView treeTable = new TreeTableView(new ListTreeTableModelOnColumns(new TreeNodeAdapter(null, null, roots), new ColumnInfo[]{BRANCH_COLUMN, REVISION_COLUMN, DATE_COLUMN, AUTHOR_COLUMN})); Runnable runnable = new Runnable() { @@ -93,8 +95,10 @@ public class CompareWithSelectedRevisionAction extends AbstractVcsAction { return; } VcsFileRevision revision = getRevisionAt(treeTable, index); - AbstractShowDiffAction.showDiff(diffProvider, revision.getRevisionNumber(), - file, project); + if (revision != null) { + AbstractShowDiffAction.showDiff(diffProvider, revision.getRevisionNumber(), + file, project); + } } }; @@ -131,18 +135,25 @@ public class CompareWithSelectedRevisionAction extends AbstractVcsAction { int y = r.y + r.height / 2 - popupSize.height / 2; popup.show(x, y); - + } - private VcsFileRevision getRevisionAt(final TreeTableView treeTable, final int index) { - return ((TreeNodeAdapter)treeTable.getItems().get(index)).getRevision(); + + @Nullable private VcsFileRevision getRevisionAt(final TreeTableView treeTable, final int index) { + final List items = treeTable.getItems(); + if (items.size() <= index) { + return null; + } else { + return ((TreeNodeAdapter)items.get(index)).getRevision(); + } + } private JPanel createMainPanel(final TreeTableView treeTable) { JScrollPane scrollPane = new JScrollPane(treeTable); treeTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); scrollPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); - + treeTable.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); if (treeTable.getRowCount() >= 20) { @@ -151,7 +162,7 @@ public class CompareWithSelectedRevisionAction extends AbstractVcsAction { else { scrollPane.getViewport().setPreferredSize(treeTable.getPreferredSize()); } - + JPanel panel = new JPanel(new BorderLayout()); panel.add(scrollPane, BorderLayout.CENTER); final JTextArea textArea = createTextArea(); @@ -162,7 +173,11 @@ public class CompareWithSelectedRevisionAction extends AbstractVcsAction { textArea.setText(""); } else { final VcsFileRevision revision = getRevisionAt(treeTable, index); - textArea.setText(revision.getCommitMessage()); + if (revision != null) { + textArea.setText(revision.getCommitMessage()); + } else { + textArea.setText(""); + } } } }); @@ -183,8 +198,8 @@ public class CompareWithSelectedRevisionAction extends AbstractVcsAction { private void showListPopup(final List revisions, final AbstractVcs vcs, final VirtualFile file, final Project project) { final DefaultListModel model = new DefaultListModel(); - for (Iterator iterator = revisions.iterator(); iterator.hasNext();) { - model.addElement(iterator.next()); + for (final VcsFileRevision revision : revisions) { + model.addElement(revision); } final JList list = new JList(model); list.setCellRenderer(new VcsRevisionListCellRenderer()); @@ -236,6 +251,7 @@ public class CompareWithSelectedRevisionAction extends AbstractVcsAction { Dimension viewPreferredSize = new Dimension(listPreferredSize.width, Math.min(listPreferredSize.height, r.height - 20)); final Container parent = list.getParent(); if (parent instanceof JComponent) { + //noinspection RedundantCast ((JComponent)parent).setPreferredSize(viewPreferredSize); } } @@ -266,7 +282,7 @@ public class CompareWithSelectedRevisionAction extends AbstractVcsAction { final JScrollPane textScrollPane = new JScrollPane(textArea); textScrollPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.lightGray),VcsBundle.message("border.selected.revision.commit.message"))); jPanel.add(textScrollPane, BorderLayout.SOUTH); - + jPanel.setPreferredSize(new Dimension(300, jPanel.getPreferredSize().height + 10)); return jPanel; } @@ -279,8 +295,7 @@ public class CompareWithSelectedRevisionAction extends AbstractVcsAction { parent.add(this); } myRevision = revision; - for (Iterator> iterator = children.iterator(); iterator.hasNext();) { - TreeItem treeItem = iterator.next(); + for (TreeItem treeItem : children) { new TreeNodeAdapter(this, treeItem, treeItem.getChildren()); } } diff --git a/util/src/com/intellij/util/ui/ListTableModel.java b/util/src/com/intellij/util/ui/ListTableModel.java index 34ce62881db8..ad244b391f95 100644 --- a/util/src/com/intellij/util/ui/ListTableModel.java +++ b/util/src/com/intellij/util/ui/ListTableModel.java @@ -73,7 +73,9 @@ public class ListTableModel extends TableViewModel implements ItemR } public void setValueAt(Object aValue, int rowIndex, int columnIndex) { - myColumnInfos[columnIndex].setValue(myItems.get(rowIndex), aValue); + if (rowIndex < myItems.size()) { + myColumnInfos[columnIndex].setValue(myItems.get(rowIndex), aValue); + } } public List getItems() {